mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 09:55:43 +00:00
adding latest
This commit is contained in:
@@ -1 +1,5 @@
|
|||||||
"""Tembo exceptions."""
|
"""Tembo exceptions."""
|
||||||
|
|
||||||
|
|
||||||
|
class MismatchedTokenError(Exception):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import jinja2
|
|||||||
import pendulum
|
import pendulum
|
||||||
|
|
||||||
from tembo import logger, CONFIG
|
from tembo import logger, CONFIG
|
||||||
|
from tembo.exceptions import MismatchedTokenError
|
||||||
|
|
||||||
|
|
||||||
class PageCreator:
|
class PageCreator:
|
||||||
@@ -83,23 +84,66 @@ class ScopedPageCreator(PageCreator):
|
|||||||
self.page_path = page_path
|
self.page_path = page_path
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.extension = extension
|
self.extension = extension
|
||||||
|
path_error: MismatchedTokenError | None = None
|
||||||
|
template_error: MismatchedTokenError | None = None
|
||||||
|
|
||||||
# get the path of the scoped page
|
# get the path of the scoped page
|
||||||
path = self._convert_to_path(
|
path = self._convert_to_path(
|
||||||
self.base_path, self.page_path, self.filename, self.extension
|
self.base_path, self.page_path, self.filename, self.extension
|
||||||
)
|
)
|
||||||
# substitute tokens in the filepath
|
# substitute tokens in the filepath
|
||||||
path = pathlib.Path(self._substitute_tokens(str(path), user_input, name))
|
try:
|
||||||
|
path = pathlib.Path(self._substitute_tokens(str(path), user_input, name))
|
||||||
|
except MismatchedTokenError as mismatched_path_error:
|
||||||
|
path_error = mismatched_path_error
|
||||||
# get the template file
|
# get the template file
|
||||||
if template_filename is not None:
|
if template_filename is not None:
|
||||||
# load the template file contents and substitute tokens
|
# load the template file contents and substitute tokens
|
||||||
template_contents = self._load_template(self.base_path, template_filename)
|
template_contents = self._load_template(self.base_path, template_filename)
|
||||||
template_contents = self._substitute_tokens(
|
try:
|
||||||
template_contents, user_input, name
|
template_contents = self._substitute_tokens(
|
||||||
)
|
template_contents, user_input, name
|
||||||
|
)
|
||||||
|
except MismatchedTokenError as mismatched_template_error:
|
||||||
|
template_error = mismatched_template_error
|
||||||
else:
|
else:
|
||||||
template_contents = ""
|
template_contents = ""
|
||||||
|
if path_error is not None or template_error is not None:
|
||||||
|
# self.__mismatched_token_error(path_error, template_error)
|
||||||
|
if path_error.args[0] > template_error.args[0]:
|
||||||
|
print("path_token_count > template_token_count")
|
||||||
|
elif template_error.args[0] > path_error.args[0]:
|
||||||
|
print("template_token_count > path_token_count")
|
||||||
return ScopedPage(path, template_contents)
|
return ScopedPage(path, template_contents)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __mismatched_token_error(
|
||||||
|
path_error: MismatchedTokenError | None = None,
|
||||||
|
template_error: MismatchedTokenError | None = None,
|
||||||
|
) -> None:
|
||||||
|
if isinstance(path_error, MismatchedTokenError):
|
||||||
|
path_token_count = path_error.args[0]
|
||||||
|
# logger.critical(
|
||||||
|
# "Your config specifies %s input tokens, you gave %s " "- exiting",
|
||||||
|
# path_error.args[0],
|
||||||
|
# path_error.args[1],
|
||||||
|
# )
|
||||||
|
# raise SystemExit(1)
|
||||||
|
if isinstance(template_error, MismatchedTokenError):
|
||||||
|
template_token_count = template_error.args[0]
|
||||||
|
# logger.critical(
|
||||||
|
# "Your template specifies %s input tokens, you gave %s " "- exiting",
|
||||||
|
# template_error.args[0],
|
||||||
|
# template_error.args[1],
|
||||||
|
# )
|
||||||
|
# raise SystemExit(1)
|
||||||
|
if path_token_count > template_token_count:
|
||||||
|
print("path_token_count > template_token_count")
|
||||||
|
elif template_token_count > path_token_count:
|
||||||
|
print("template_token_count > path_token_count")
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
# TODO: change the annotation to include the error
|
||||||
def _substitute_tokens(
|
def _substitute_tokens(
|
||||||
self,
|
self,
|
||||||
tokenified_string: str,
|
tokenified_string: str,
|
||||||
@@ -107,11 +151,14 @@ class ScopedPageCreator(PageCreator):
|
|||||||
name: str,
|
name: str,
|
||||||
) -> str:
|
) -> str:
|
||||||
# for a tokened string, substitute input, name and date tokens
|
# for a tokened string, substitute input, name and date tokens
|
||||||
tokenified_string = self.__substitute_input_tokens(
|
try:
|
||||||
tokenified_string, user_input
|
tokenified_string = self.__substitute_input_tokens(
|
||||||
)
|
tokenified_string, user_input
|
||||||
tokenified_string = self.__substitute_name_tokens(tokenified_string, name)
|
)
|
||||||
tokenified_string = self.__substitute_date_tokens(tokenified_string)
|
tokenified_string = self.__substitute_name_tokens(tokenified_string, name)
|
||||||
|
tokenified_string = self.__substitute_date_tokens(tokenified_string)
|
||||||
|
except MismatchedTokenError as mismatched_token_error:
|
||||||
|
raise mismatched_token_error
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -131,12 +178,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
if len(user_input) == 0:
|
if len(user_input) == 0:
|
||||||
# if there's no user input, but the regex matches, raise error
|
# if there's no user input, but the regex matches, raise error
|
||||||
if len(input_extraction) > 0:
|
if len(input_extraction) > 0:
|
||||||
logger.critical(
|
raise MismatchedTokenError(len(input_extraction), 0)
|
||||||
"Your config/template specifies %s input tokens, you gave 0 "
|
|
||||||
"- exiting",
|
|
||||||
len(input_extraction),
|
|
||||||
)
|
|
||||||
raise SystemExit(1)
|
|
||||||
# if there aren't any tokens in the string, return the string
|
# if there aren't any tokens in the string, return the string
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
# if there is user input, check the number of tokens match the number passed in
|
# if there is user input, check the number of tokens match the number passed in
|
||||||
@@ -147,12 +189,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
):
|
):
|
||||||
# if there are input matches and they don't equal the number of input
|
# if there are input matches and they don't equal the number of input
|
||||||
# tokens, raise error
|
# tokens, raise error
|
||||||
logger.critical(
|
raise MismatchedTokenError(len(input_extraction), len(user_input))
|
||||||
"Your config/template specifies %s input tokens, you gave %s - exiting",
|
|
||||||
len(input_extraction),
|
|
||||||
len(user_input),
|
|
||||||
)
|
|
||||||
raise SystemExit(1)
|
|
||||||
# if the length of both the input matches and the number of tokens match then
|
# if the length of both the input matches and the number of tokens match then
|
||||||
# substitute each token with the user's input
|
# substitute each token with the user's input
|
||||||
if len(user_input) > 0:
|
if len(user_input) > 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user