From 2d91c9d62d2284f19265c957e62d20d7bca3d9ef Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sun, 24 Oct 2021 22:34:58 +0100 Subject: [PATCH] adding latest --- TODO.todo | 40 +++++++++++++++---------- tembo/journal/pages.py | 68 ++++++++++++++++-------------------------- 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/TODO.todo b/TODO.todo index c956e6e..cbe844e 100644 --- a/TODO.todo +++ b/TODO.todo @@ -8,30 +8,40 @@ Functionality: check pypi for latest version and compare to current ✔ `TEMBO_CONFIG` should follow same pattern as other env vars and be a python string when read in @done(21-10-24 05:31) +Bug: + ☐ tokens + Say we have input0 and input3 in file path + and we have input1 and input2 in template + it only recognises this as 2 inputs total, not four. + passing in tembo new meeting a b will work and input0=input1, input3=input2 + VSCode: ☐ Look at Logging: - ☐ How to raise + debug an exception? - ☐ Document how to raise a logger.critical instead of exception - in a try, except you can just do logger.critical(exec_info=1) to print the stack -Documentation: - ☐ Document usage of Panaetius in a module - Using the logger, initialising with the config path etc - ✘ Uses Pendulum tokens: https://pendulum.eustace.io/docs/#tokens @cancelled(21-10-24 05:32) - ☐ Uses `strftime` tokens: - ☐ Document latest typing. + Documentation: + ☐ Document usage of Panaetius in a module + Using the logger, initialising with the config path etc + ✘ Uses Pendulum tokens: https://pendulum.eustace.io/docs/#tokens @cancelled(21-10-24 05:32) + ☐ Uses `strftime` tokens: + ☐ Document latest typing. ☐ Using from `__future__` with `|` ☐ `using Tuple[str, ...]` ☐ `Sequence` vs `Collection` - ☐ Document how to do docstrings in python. Don't document `__init__` do it in class. - Should update the default gist to hide the `__init__` messages - ☐ Document using jinja2 briefly and link to Tembo (link to ) - Tembo: - ☐ Document creating new Tembo config + ☐ Document how to do docstrings in python. Don't document `__init__` do it in class. + Should update the default gist to hide the `__init__` messages + ☐ Document using jinja2 briefly and link to Tembo (link to ) + + Logging: + ☐ How to raise + debug an exception? + ☐ Document how to raise a logger.critical instead of exception + in a try, except you can just do logger.critical(exec_info=1) to print the stack + + Tembo: + ☐ Document creating new Tembo config ☐ ~/tembo needs creating ☐ ~/tembo/.config ☐ ~/tembo/.templates ☐ ~/tembo/logs - ☐ Document how to overwrite these with ENV vars + ☐ Document how to overwrite these with ENV vars diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index 4398af0..f27afd1 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -9,7 +9,6 @@ import jinja2 import pendulum from tembo import logger, CONFIG -from tembo.exceptions import MismatchedTokenError class PageCreator: @@ -70,8 +69,8 @@ class ScopedPageCreator(PageCreator): self.filename = "" self.extension = "" # TODO: rename these to input tokens + more sensible - self.path_input_tokens: Tuple[int, int] = (0, 0) - self.template_input_tokens: Tuple[int, int] = (0, 0) + self.path_input_token_counts = {"config": 0, "user": 0} + self.template_input_token_counts = {"config": 0, "user": 0} def create_page( self, @@ -97,10 +96,6 @@ class ScopedPageCreator(PageCreator): path = pathlib.Path( self._substitute_tokens(str(path), user_input, name, "path") ) - if sum(self.path_input_tokens) > 0: - _highest_input_token_in_path = max(self.path_input_tokens) - else: - _highest_input_token_in_path = 0 # get the template file if template_filename is not None: @@ -109,51 +104,34 @@ class ScopedPageCreator(PageCreator): template_contents = self._substitute_tokens( template_contents, user_input, name, "template" ) - if sum(self.template_input_tokens) > 0: - _highest_input_token_in_template = max(self.template_input_tokens) - else: - _highest_input_token_in_template = 0 else: template_contents = "" - self.__check_input_token_mismatch( - _highest_input_token_in_path, _highest_input_token_in_template - ) + self.__check_input_token_mismatch() return ScopedPage(path, template_contents) - def __check_input_token_mismatch( - self, _highest_input_token_in_path: int, _highest_input_token_in_template: int - ) -> None: - _highest_input_token_count = max( - _highest_input_token_in_path, _highest_input_token_in_template + def __check_input_token_mismatch(self) -> None: + _max_config_input_token_count = max( + self.path_input_token_counts["config"], + self.template_input_token_counts["config"], ) - if _highest_input_token_in_path < _highest_input_token_count: + _max_user_input_token_count = max( + self.path_input_token_counts["user"], + self.template_input_token_counts["user"], + ) + if _max_user_input_token_count < _max_config_input_token_count: logger.critical( "Your config/template specifies %s input tokens, you gave %s " "- exiting", - _highest_input_token_count, - self.path_input_tokens[0], + _max_config_input_token_count, + _max_user_input_token_count, ) raise SystemExit(1) - if _highest_input_token_in_path > _highest_input_token_count: + if _max_user_input_token_count > _max_config_input_token_count: logger.warning( "Your config/template specifies %s input tokens, you gave %s", - _highest_input_token_count, - self.path_input_tokens[0], - ) - if _highest_input_token_in_template < _highest_input_token_count: - logger.critical( - "Your config/template specifies %s input tokens, you gave %s " - "- exiting", - _highest_input_token_count, - self.template_input_tokens[0], - ) - raise SystemExit(1) - if _highest_input_token_in_template > _highest_input_token_count: - logger.warning( - "Your config/template specifies %s input tokens, you gave %s", - _highest_input_token_count, - self.template_input_tokens[0], + _max_config_input_token_count, + _max_user_input_token_count, ) def _substitute_tokens( @@ -196,9 +174,11 @@ class ScopedPageCreator(PageCreator): # if the regex matches, save the number of input tokens found if input_token_type == "path": # noqa: bandit 105 # TODO: change this to a dict instead of tuple - self.path_input_tokens = (len(input_extraction), 0) + self.path_input_token_counts["config"] = len(input_extraction) + self.path_input_token_counts["user"] = 0 if input_token_type == "template": # noqa: bandit 105 - self.template_input_tokens = (len(input_extraction), 0) + self.template_input_token_counts["config"] = len(input_extraction) + self.template_input_token_counts["user"] = 0 # if there aren't any tokens in the string, return the string return tokenified_string @@ -206,9 +186,11 @@ class ScopedPageCreator(PageCreator): if len(user_input) > 0: # save the number of input tokens, and the number of user inputs if input_token_type == "path": # noqa: bandit 105 - self.path_input_tokens = (len(input_extraction), len(user_input)) + self.path_input_token_counts["config"] = len(input_extraction) + self.path_input_token_counts["user"] = len(user_input) elif input_token_type == "template": # noqa: bandit 105 - self.template_input_tokens = (len(input_extraction), len(user_input)) + self.template_input_token_counts["config"] = len(input_extraction) + self.template_input_token_counts["user"] = len(user_input) # sbustitute the input token for the user's input for extracted_input, input_value in zip(input_extraction, user_input):