mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 12:45:44 +00:00
adding latest
This commit is contained in:
@@ -13,14 +13,29 @@ from jinja2.exceptions import TemplateNotFound
|
||||
import tembo
|
||||
|
||||
|
||||
# TODO: flesh this out with details for the optional args
|
||||
@dataclass
|
||||
class PageCreatorOptions:
|
||||
"""Options dataclass to create a Page.
|
||||
|
||||
Attributes:
|
||||
base_path (str):
|
||||
page_path (str):
|
||||
filename (str):
|
||||
extension (str):
|
||||
name (str):
|
||||
user_input (Collection[str] | None, optional):
|
||||
example (str | None, optional):
|
||||
template_filename (str | None, optional):
|
||||
template_path (str | None, optional):
|
||||
"""
|
||||
|
||||
base_path: str
|
||||
page_path: str
|
||||
filename: str
|
||||
extension: str
|
||||
name: str
|
||||
user_input: Collection[str]
|
||||
user_input: Collection[str] | None = None
|
||||
example: str | None = None
|
||||
template_filename: str | None = None
|
||||
template_path: str | None = None
|
||||
@@ -92,51 +107,48 @@ class ScopedPageCreator(PageCreator):
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.base_path = ""
|
||||
self.page_path = ""
|
||||
self.filename = ""
|
||||
self.extension = ""
|
||||
# self.base_path = ""
|
||||
# self.page_path = ""
|
||||
# self.filename = ""
|
||||
# self.extension = ""
|
||||
self._all_input_tokens: list[str] = []
|
||||
self.options: PageCreatorOptions
|
||||
|
||||
def create_page(self, options: PageCreatorOptions) -> Page:
|
||||
self.base_path = options.base_path
|
||||
self.page_path = options.page_path
|
||||
self.filename = options.filename
|
||||
self.extension = options.extension
|
||||
self.options = options
|
||||
|
||||
# verify the user input length matches the number of input tokens in the
|
||||
# tembo config/templates
|
||||
self._all_input_tokens = self._get_input_tokens(
|
||||
options.template_filename, options.template_path
|
||||
)
|
||||
self._verify_input_tokens(options.user_input, options.example)
|
||||
|
||||
# get the path of the scoped page
|
||||
path = self._convert_to_path(
|
||||
self.base_path, self.page_path, self.filename, self.extension
|
||||
self.options.base_path,
|
||||
self.options.page_path,
|
||||
self.options.filename,
|
||||
self.options.extension,
|
||||
)
|
||||
|
||||
# substitute tokens in the filepath
|
||||
path = pathlib.Path(
|
||||
self._substitute_tokens(str(path), options.user_input, options.name)
|
||||
)
|
||||
|
||||
# get the template file
|
||||
template_contents = self._get_template_contents(
|
||||
options.template_filename, options.template_path
|
||||
)
|
||||
# substitute tokens in template_contents
|
||||
if options.template_filename is not None:
|
||||
template_contents = self._substitute_tokens(
|
||||
template_contents, options.user_input, options.name
|
||||
)
|
||||
|
||||
return ScopedPage(path, template_contents)
|
||||
|
||||
def _get_template_contents(
|
||||
self, template_filename: str | None, template_path: str | None
|
||||
) -> str:
|
||||
return (
|
||||
self._load_template(self.base_path, template_filename, template_path)
|
||||
self._load_template(
|
||||
self.options.base_path, template_filename, template_path
|
||||
)
|
||||
if template_filename is not None
|
||||
else ""
|
||||
)
|
||||
@@ -146,7 +158,10 @@ class ScopedPageCreator(PageCreator):
|
||||
) -> list[str]:
|
||||
path = str(
|
||||
pathlib.Path(
|
||||
self.base_path, self.page_path, self.filename, self.extension
|
||||
self.options.base_path,
|
||||
self.options.page_path,
|
||||
self.options.filename,
|
||||
self.options.extension,
|
||||
).expanduser()
|
||||
)
|
||||
template_contents = self._get_template_contents(
|
||||
@@ -159,8 +174,23 @@ class ScopedPageCreator(PageCreator):
|
||||
return sorted(all_input_tokens)
|
||||
|
||||
def _verify_input_tokens(
|
||||
self, user_input: Collection[str], example: str | None
|
||||
self, user_input: Collection[str] | None, example: str | None
|
||||
) -> None:
|
||||
if len(self._all_input_tokens) > 0 and user_input is None:
|
||||
if example is not None:
|
||||
tembo.logger.critical(
|
||||
"Your tembo.config/template specifies %s input tokens, you gave 0. Example command: %s",
|
||||
len(self._all_input_tokens),
|
||||
example,
|
||||
)
|
||||
else:
|
||||
tembo.logger.critical(
|
||||
"Your tembo.config/template specifies %s input tokens, you gave 0.",
|
||||
len(self._all_input_tokens),
|
||||
)
|
||||
raise SystemExit(1)
|
||||
if user_input is None:
|
||||
return
|
||||
if len(self._all_input_tokens) != len(user_input):
|
||||
if example is not None:
|
||||
tembo.logger.critical(
|
||||
@@ -176,11 +206,12 @@ class ScopedPageCreator(PageCreator):
|
||||
len(user_input),
|
||||
)
|
||||
raise SystemExit(1)
|
||||
return
|
||||
|
||||
def _substitute_tokens(
|
||||
self,
|
||||
tokenified_string: str,
|
||||
user_input: Collection[str],
|
||||
user_input: Collection[str] | None,
|
||||
name: str,
|
||||
) -> str:
|
||||
"""For a tokened string, substitute input, name and date tokens."""
|
||||
@@ -194,13 +225,13 @@ class ScopedPageCreator(PageCreator):
|
||||
def __substitute_input_tokens(
|
||||
self,
|
||||
tokenified_string: str,
|
||||
user_input: Collection[str],
|
||||
user_input: Collection[str] | None,
|
||||
) -> str:
|
||||
for input_value, extracted_token in zip(user_input, self._all_input_tokens):
|
||||
# REVIEW: test this for spaces in the filename/input token
|
||||
tokenified_string = tokenified_string.replace(
|
||||
extracted_token, input_value.replace(" ", "_")
|
||||
)
|
||||
if user_input is not None:
|
||||
for input_value, extracted_token in zip(user_input, self._all_input_tokens):
|
||||
tokenified_string = tokenified_string.replace(
|
||||
extracted_token, input_value.replace(" ", "_")
|
||||
)
|
||||
return tokenified_string
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user