From 0c03f327469465009d10399a877e48766cf3ee45 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sun, 24 Oct 2021 07:06:40 +0100 Subject: [PATCH] adding latest --- tembo/exceptions.py | 4 +++ tembo/journal/pages.py | 79 +++++++++++++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/tembo/exceptions.py b/tembo/exceptions.py index c8c6ad5..a71ea99 100644 --- a/tembo/exceptions.py +++ b/tembo/exceptions.py @@ -1 +1,5 @@ """Tembo exceptions.""" + + +class MismatchedTokenError(Exception): + pass diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index ba4a6e9..ebf8bbd 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -9,6 +9,7 @@ import jinja2 import pendulum from tembo import logger, CONFIG +from tembo.exceptions import MismatchedTokenError class PageCreator: @@ -83,23 +84,66 @@ class ScopedPageCreator(PageCreator): self.page_path = page_path self.filename = filename self.extension = extension + path_error: MismatchedTokenError | None = None + template_error: MismatchedTokenError | None = None + # get the path of the scoped page path = self._convert_to_path( self.base_path, self.page_path, self.filename, self.extension ) # 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 if template_filename is not None: # load the template file contents and substitute tokens template_contents = self._load_template(self.base_path, template_filename) - template_contents = self._substitute_tokens( - template_contents, user_input, name - ) + try: + template_contents = self._substitute_tokens( + template_contents, user_input, name + ) + except MismatchedTokenError as mismatched_template_error: + template_error = mismatched_template_error else: 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) + @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( self, tokenified_string: str, @@ -107,11 +151,14 @@ class ScopedPageCreator(PageCreator): name: str, ) -> str: # for a tokened string, substitute input, name and date tokens - 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) + try: + 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) + except MismatchedTokenError as mismatched_token_error: + raise mismatched_token_error return tokenified_string @staticmethod @@ -131,12 +178,7 @@ class ScopedPageCreator(PageCreator): if len(user_input) == 0: # if there's no user input, but the regex matches, raise error if len(input_extraction) > 0: - logger.critical( - "Your config/template specifies %s input tokens, you gave 0 " - "- exiting", - len(input_extraction), - ) - raise SystemExit(1) + raise MismatchedTokenError(len(input_extraction), 0) # if there aren't any tokens in the string, return the string return tokenified_string # 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 # tokens, raise error - logger.critical( - "Your config/template specifies %s input tokens, you gave %s - exiting", - len(input_extraction), - len(user_input), - ) - raise SystemExit(1) + raise MismatchedTokenError(len(input_extraction), len(user_input)) # if the length of both the input matches and the number of tokens match then # substitute each token with the user's input if len(user_input) > 0: