From 1fccf89d608b4bc1180f22404377879cc1493fde Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Fri, 29 Oct 2021 00:15:54 +0100 Subject: [PATCH] adding example and template_file as optional in config --- TODO.todo | 6 ++++- tembo/cli.py | 47 +++++++++++++++++++++----------------- tembo/journal/pages.py | 51 +++++++++++++++++++++++++----------------- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/TODO.todo b/TODO.todo index 831fb56..773c760 100644 --- a/TODO.todo +++ b/TODO.todo @@ -24,7 +24,8 @@ Documentation: Functionality: ✔ Move any `tembo.CONFIG` calls out of `pages.py` and ensure these are passed in from the cli. @done(21-10-28 19:44) ✔ Make `config scope` a dict in `cli.py`. @done(21-10-28 19:44) - ☐ Add the `--example` output to the miscounted token message so the user knows the correct command to use. + ✔ Make example optional @done(21-10-29 00:15) + ✔ Add the `--example` output to the miscounted token message so the user knows the correct command to use. @done(21-10-29 00:15) ✔ Page options dataclass @done(21-10-28 20:09) ☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages? ☐ Use the python runner Duty @@ -39,6 +40,9 @@ VSCode: ☐ Document build error: PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.8.11 ☐ Freeze a click app: + ☐ If python 3.9 can be used with Pyinstaller, rewrite the code to use the latest Python features + dict.update -> |= + walrus := Tests: ☐ Write tests! @2d diff --git a/tembo/cli.py b/tembo/cli.py index 3a9b071..f1685e9 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -62,32 +62,38 @@ def new(scope, inputs, dry_run, example): raise SystemExit(1) from type_error # get the scope information from the tembo config.yml - try: - config_scope = { - option: str(user_scope[option]) - for option in [ - "name", - "example", - "path", - "filename", - "extension", - "template_filename", - ] - for user_scope in tembo.CONFIG.scopes - 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 - + config_scope = {} + for option in [ + "name", + "example", + "path", + "filename", + "extension", + "template_filename", + ]: + try: + config_scope.update( + { + option: str(user_scope[option]) + for user_scope in tembo.CONFIG.scopes + if user_scope["name"] == scope + } + ) + except KeyError as key_error: + if key_error.args[0] in ["example", "template_filename"]: + config_scope.update({key_error.args[0]: None}) + continue + 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", config_scope["name"], config_scope["example"] - if isinstance(config_scope[0][1], str) + if isinstance(config_scope["example"], str) else "No example in config.yml", ) raise SystemExit(0) @@ -99,6 +105,7 @@ def new(scope, inputs, dry_run, example): filename=config_scope["filename"], extension=config_scope["extension"], name=config_scope["name"], + example=config_scope["example"], user_input=inputs, template_filename=config_scope["template_filename"], template_path=tembo.CONFIG.template_path, diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index 20bb3e6..5774942 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -20,9 +20,10 @@ class PageCreatorOptions: filename: str extension: str name: str + example: str | None user_input: Collection[str] - template_filename: str | None = None - template_path: str | None = None + template_filename: str | None + template_path: str | None class PageCreator: @@ -108,7 +109,7 @@ class ScopedPageCreator(PageCreator): self._all_input_tokens = self._get_input_tokens( options.template_filename, options.template_path ) - self._verify_input_tokens(options.user_input) + self._verify_input_tokens(options.user_input, options.example) # get the path of the scoped page path = self._convert_to_path( @@ -148,13 +149,23 @@ class ScopedPageCreator(PageCreator): all_input_tokens.extend(re.findall(r"(\{input\d*\})", tokenified_string)) return sorted(all_input_tokens) - def _verify_input_tokens(self, user_input: Collection[str]) -> None: + def _verify_input_tokens( + self, user_input: Collection[str], example: str | None + ) -> None: if len(self._all_input_tokens) != len(user_input): - tembo.logger.critical( - "Your tembo.config/template specifies %s input tokens, you gave %s", - len(self._all_input_tokens), - len(user_input), - ) + if example is not None: + tembo.logger.critical( + "Your tembo.config/template specifies %s input tokens, you gave %s. Example command: %s", + len(self._all_input_tokens), + len(user_input), + example, + ) + else: + tembo.logger.critical( + "Your tembo.config/template specifies %s input tokens, you gave %s.", + len(self._all_input_tokens), + len(user_input), + ) raise SystemExit(1) def _get_template_contents( @@ -332,17 +343,17 @@ if __name__ == "__main__": # "scratchpad.md.tpl", # ) # ) - test_page_with_template = c.create_page( - "~/tembo", - "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}", - "file-{input0}-{name}", - ".md", - "meeting", - ("last",), - "scratchpad.md.tpl", - ) - print(test_page_with_template) - test_page_with_template.save_to_disk(False) + # test_page_with_template = c.create_page( + # "~/tembo", + # "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}", + # "file-{input0}-{name}", + # ".md", + # "meeting", + # ("last",), + # "scratchpad.md.tpl", + # ) + # print(test_page_with_template) + # test_page_with_template.save_to_disk(False) # print( # c.create_page( # "~/tembo",