From 1df6e3f2eade49b4589f1736afd07f0e963dc257 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Mon, 25 Oct 2021 06:04:32 +0100 Subject: [PATCH] adding latest --- TODO.todo | 5 ++--- tembo/cli.py | 13 ++++++++++--- tembo/journal/pages.py | 17 +++++++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/TODO.todo b/TODO.todo index 0952f16..2e2e531 100644 --- a/TODO.todo +++ b/TODO.todo @@ -1,14 +1,13 @@ Priority: - ☐ Go through code TODOs + ✔ Go through code TODOs @done(21-10-25 05:52) ☐ Check code order and make sure things are where they should be ☐ Document the python/logging/typing in Trilium ☐ Write the tests Functionality: - ☐ a + ☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages Bug: - ☐ a Tests: ☐ Write tests! @2d diff --git a/tembo/cli.py b/tembo/cli.py index 1cc3de7..f37f6f8 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -16,7 +16,7 @@ def run(): @click.command(options_metavar="") def list_all(): - r"""List all names for "tembo new ".""" + """List all names for 'tembo new '.""" _all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes] tembo.logger.info( "%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes) @@ -24,7 +24,6 @@ def list_all(): raise SystemExit(0) -# TODO: organise this so all flags are at the top and in order @click.command(options_metavar="") @click.argument("scope", metavar="") @click.argument( @@ -44,14 +43,17 @@ def new(scope, inputs, dry_run, example): Example: tembo new meeting my_presentation """ + # get the name from the tembo config.yml try: _name_found = scope in [ user_scope["name"] for user_scope in tembo.CONFIG.scopes ] except TypeError as type_error: + # raise error if no scopes are defined tembo.logger.critical("No scopes found in config.yml - exiting") raise SystemExit(1) from type_error + # get the scope information from the tembo config.yml try: config_scope = [ ( @@ -66,9 +68,11 @@ def new(scope, inputs, dry_run, example): if user_scope["name"] == scope ] except KeyError as key_error: + # raise error if any non optional keys are missing tembo.logger.critical("Key %s not found in config.yml - exiting", key_error) raise SystemExit(1) from key_error + # print the example to the user if example: tembo.logger.info( "Example for 'tembo new %s': %s", @@ -79,6 +83,7 @@ def new(scope, inputs, dry_run, example): ) raise SystemExit(0) + # if the name is in the config.yml, create the scoped page if _name_found: scoped_page = pages.ScopedPageCreator().create_page( base_path=str(tembo.CONFIG.base_path), @@ -92,8 +97,11 @@ def new(scope, inputs, dry_run, example): scoped_page.save_to_disk(dry_run=dry_run) raise SystemExit(0) if not _name_found and len(tembo.CONFIG.scopes) > 0: + # if the name is missing in the config.yml, raise error tembo.logger.warning("Command %s not found in config.yml - exiting", scope) raise SystemExit(0) + + # raise error if no config.yml found tembo.logger.critical( "No config.yml found in %s - exiting", tembo.CONFIG.config_path ) @@ -105,7 +113,6 @@ run.add_command(list_all) if __name__ == "__main__": - # BUG: fix this bug where input tokens are mismatched # new(["meeting", "robs presentation", "meeting on gcp"]) new(["meeting", "a", "b", "c", "d", "--example"]) # new(["meeting", "robs presentation"]) diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index eeb42bc..db0620f 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -6,6 +6,7 @@ import re from typing import Tuple import jinja2 +from jinja2.exceptions import TemplateNotFound import pendulum import tembo @@ -62,7 +63,14 @@ class PageCreator: file_loader = jinja2.FileSystemLoader(template_path) env = jinja2.Environment(loader=file_loader, autoescape=True) # load the template contents - loaded_template = env.get_template(template_filename) + try: + loaded_template = env.get_template(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), + ) + raise SystemExit(1) from template_not_found return loaded_template.render() @@ -90,7 +98,7 @@ class ScopedPageCreator(PageCreator): self.extension = extension # verify the user input length matches the number of input tokens in the - # tembo.config/templates + # tembo config/templates self._all_input_tokens = self._get_input_tokens(template_filename) self._verify_input_tokens(user_input) @@ -146,10 +154,6 @@ class ScopedPageCreator(PageCreator): name: str, ) -> str: """For a tokened string, substitute input, name and date tokens.""" - # TODO: fn to get tokens from file and template - # tokenified_string = self.__substitute_input_tokens( - # tokenified_string, user_input, input_token_type - # ) tokenified_string = self.__substitute_input_tokens( tokenified_string, user_input ) @@ -163,6 +167,7 @@ class ScopedPageCreator(PageCreator): user_input: Tuple[str, ...] | Tuple[()], ) -> str: for input_value, extracted_token in zip(user_input, self._all_input_tokens): + # REVIEW: test this for spaces in the filename/input token tokenified_string = tokenified_string.replace( extracted_token, input_value.replace(" ", "_") )