mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 17:15:44 +00:00
adding latest tests
This commit is contained in:
@@ -3,3 +3,7 @@
|
||||
|
||||
class MismatchedTokenError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BasePathDoesNotExistError(Exception):
|
||||
pass
|
||||
|
||||
@@ -45,11 +45,16 @@ class PageCreator:
|
||||
def __init__(self, options: PageCreatorOptions) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def options(self) -> PageCreatorOptions:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def create_page(self, options: PageCreatorOptions) -> Page:
|
||||
raise NotImplementedError
|
||||
|
||||
def _convert_to_path(self) -> pathlib.Path:
|
||||
def _convert_base_path_to_path(self) -> pathlib.Path:
|
||||
# check if Tembo base path exists
|
||||
if not pathlib.Path(self.options.base_path).expanduser().exists():
|
||||
tembo.logger.critical(
|
||||
@@ -74,27 +79,24 @@ class PageCreator:
|
||||
# return path with a file
|
||||
return path_to_file.with_suffix(f".{extension}")
|
||||
|
||||
def _load_template(
|
||||
self, base_path: str, template_filename: str, template_path: str | None
|
||||
) -> str:
|
||||
# check for overriden template_path
|
||||
if template_path is not None:
|
||||
converted_template_path = pathlib.Path(template_path).expanduser()
|
||||
def _load_template(self) -> str:
|
||||
if self.options.template_filename is None:
|
||||
return ""
|
||||
if self.options.template_path is not None:
|
||||
converted_template_path = pathlib.Path(
|
||||
self.options.template_path
|
||||
).expanduser()
|
||||
else:
|
||||
# default template_path is base_path / .templates
|
||||
converted_template_path = self._convert_to_path(
|
||||
base_path, ".templates", "", ""
|
||||
)
|
||||
# load the template folder
|
||||
converted_template_path = pathlib.Path()
|
||||
|
||||
file_loader = jinja2.FileSystemLoader(converted_template_path)
|
||||
env = jinja2.Environment(loader=file_loader, autoescape=True)
|
||||
# load the template contents
|
||||
try:
|
||||
loaded_template = env.get_template(template_filename)
|
||||
loaded_template = env.get_template(self.options.template_filename)
|
||||
except TemplateNotFound as template_not_found:
|
||||
tembo.logger.critical(
|
||||
"Template file %s not found - exiting",
|
||||
str(template_path) + "/" + str(template_not_found.message),
|
||||
str(self.options.template_path) + "/" + str(template_not_found.message),
|
||||
)
|
||||
raise SystemExit(1) from template_not_found
|
||||
return loaded_template.render()
|
||||
@@ -112,39 +114,27 @@ class ScopedPageCreator(PageCreator):
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._all_input_tokens: list[str] = []
|
||||
self.options: PageCreatorOptions
|
||||
self._options: PageCreatorOptions
|
||||
|
||||
@property
|
||||
def options(self) -> PageCreatorOptions:
|
||||
return self._options
|
||||
|
||||
def create_page(self, options: PageCreatorOptions) -> Page:
|
||||
self.options = options
|
||||
self._options = options
|
||||
|
||||
self._all_input_tokens = self._get_input_tokens()
|
||||
self._verify_input_tokens()
|
||||
|
||||
path = self._convert_to_path(
|
||||
self.options.base_path,
|
||||
self.options.page_path,
|
||||
self.options.filename,
|
||||
self.options.extension,
|
||||
)
|
||||
path = self._convert_base_path_to_path()
|
||||
path = pathlib.Path(self._substitute_tokens(str(path)))
|
||||
|
||||
template_contents = self._get_template_contents()
|
||||
template_contents = self._load_template()
|
||||
if options.template_filename is not None:
|
||||
template_contents = self._substitute_tokens(template_contents)
|
||||
|
||||
return ScopedPage(path, template_contents)
|
||||
|
||||
def _get_template_contents(self) -> str:
|
||||
return (
|
||||
self._load_template(
|
||||
self.options.base_path,
|
||||
self.options.template_filename,
|
||||
self.options.template_path,
|
||||
)
|
||||
if self.options.template_filename is not None
|
||||
else ""
|
||||
)
|
||||
|
||||
def _get_input_tokens(self) -> list[str]:
|
||||
path = str(
|
||||
pathlib.Path(
|
||||
@@ -154,7 +144,7 @@ class ScopedPageCreator(PageCreator):
|
||||
self.options.extension,
|
||||
).expanduser()
|
||||
)
|
||||
template_contents = self._get_template_contents()
|
||||
template_contents = self._load_template()
|
||||
# get the input tokens from both the path and the template
|
||||
all_input_tokens = []
|
||||
for tokenified_string in (path, template_contents):
|
||||
|
||||
Reference in New Issue
Block a user