chore: remove duplicate abstract PageCreator from pages submodule

This commit is contained in:
2022-01-24 21:27:32 +00:00
parent 86ffac8d67
commit 86be376d03

View File

@@ -7,9 +7,7 @@ import re
from dataclasses import dataclass
from typing import Collection, Optional
import jinja2
import pendulum
from jinja2.exceptions import TemplateNotFound
import tembo.utils
from tembo import exceptions
@@ -48,137 +46,6 @@ class PageCreatorOptions:
template_filename: Optional[str] = None
class PageCreator:
"""
A PageCreator factory base class.
This factory should implement methods to create [Page][tembo.journal.pages.Page] objects.
!!! abstract
This factory is an abstract base class and should be implemented for each
[Page][tembo.journal.pages.Page] type.
The methods
- `_check_base_path_exists()`
- `_convert_base_path_to_path()`
- `_load_template()`
are not abstract and are shared between all [Page][tembo.journal.pages.Page] types.
"""
@abstractmethod
def __init__(self, options: PageCreatorOptions) -> None:
"""
When implemented this should initialise the `PageCreator` factory.
Args:
options (PageCreatorOptions): An instance of
[PageCreatorOptions][tembo.journal.pages.PageCreatorOptions]
!!! abstract
This method is abstract and should be implemented for each
[Page][tembo.journal.pages.Page] type.
"""
raise NotImplementedError
@property
@abstractmethod
def options(self) -> PageCreatorOptions:
"""
When implemented this should return the `PageCreatorOptions` on the class.
Returns:
PageCreatorOptions: the instance of
[PageCreatorOptions][tembo.journal.pages.PageCreatorOptions] set on the class.
!!! abstract
This method is abstract and should be implemented for each
[Page][tembo.journal.pages.Page] type.
"""
raise NotImplementedError
@abstractmethod
def create_page(self) -> Page:
"""
When implemented this should create a `Page` object.
Returns:
Page: an implemented instance of [Page][tembo.journal.pages.Page] such as
[ScopedPage][tembo.journal.pages.ScopedPage].
!!! abstract
This method is abstract and should be implemented for each
[Page][tembo.journal.pages.Page] type.
"""
raise NotImplementedError
def _check_base_path_exists(self) -> None:
"""
Check that the base path exists.
Raises:
exceptions.BasePathDoesNotExistError: raised if the base path does not exist.
"""
if not pathlib.Path(self.options.base_path).expanduser().exists():
raise exceptions.BasePathDoesNotExistError(
f"Tembo base path of {self.options.base_path} does not exist."
)
def _convert_base_path_to_path(self) -> pathlib.Path:
"""
Convert the `base_path` from a `str` to a `pathlib.Path` object.
Returns:
pathlib.Path: the `base_path` as a `pathlib.Path` object.
"""
path_to_file = (
pathlib.Path(self.options.base_path).expanduser()
/ pathlib.Path(self.options.page_path.replace(" ", "_")).expanduser()
/ self.options.filename.replace(" ", "_")
)
# check for existing `.` in the extension
extension = (
self.options.extension[1:]
if self.options.extension[0] == "."
else self.options.extension
)
# return path with a file
return path_to_file.with_suffix(f".{extension}")
def _load_template(self) -> str:
"""
Load the template file.
Raises:
exceptions.TemplateFileNotFoundError: raised if the template file is specified but
not found.
Returns:
str: the contents of the template file.
"""
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:
converted_template_path = (
pathlib.Path(self.options.base_path).expanduser() / ".templates"
)
file_loader = jinja2.FileSystemLoader(converted_template_path)
env = jinja2.Environment(loader=file_loader, autoescape=True)
try:
loaded_template = env.get_template(self.options.template_filename)
except TemplateNotFound as template_not_found:
_template_file = f"{converted_template_path}/{template_not_found.args[0]}"
raise exceptions.TemplateFileNotFoundError(
f"Template file {_template_file} does not exist."
) from template_not_found
return loaded_template.render()
class ScopedPageCreator(PageCreator):
"""
Factory to create a scoped page.