From 4e6efa326ed9d4a77f432a439295843fa49a9059 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sat, 23 Oct 2021 21:12:04 +0100 Subject: [PATCH] changing default config location default config is ~/tembo/.config --- tembo/__init__.py | 6 +++--- tembo/cli.py | 12 +++++++++--- tembo/journal/pages.py | 29 ++++++++++++++--------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/tembo/__init__.py b/tembo/__init__.py index 5e73856..a9fc698 100644 --- a/tembo/__init__.py +++ b/tembo/__init__.py @@ -2,14 +2,14 @@ import os import panaetius -if config_path := os.environ.get("TEMBO_CONFIG") is not None: +if (config_path := os.environ.get("TEMBO_CONFIG")) is not None: CONFIG = panaetius.Config("tembo", config_path) else: - CONFIG = panaetius.Config("tembo") + CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True) panaetius.set_config(CONFIG, "base_path", "~/tembo") -panaetius.set_config(CONFIG, "template_path", "~/tembo/templates") +panaetius.set_config(CONFIG, "template_path", "~/tembo/.templates") panaetius.set_config(CONFIG, "scopes", {}) panaetius.set_config(CONFIG, "logging.level", "DEBUG") panaetius.set_config(CONFIG, "logging.path") diff --git a/tembo/cli.py b/tembo/cli.py index 88cb88e..4275486 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -12,8 +12,6 @@ def run(): """ Tembo - an organiser for work notes. """ - print(tembo.CONFIG.base_path) - # print(tembo.CONFIG.scopes) @click.command(options_metavar="") @@ -33,7 +31,6 @@ def new(scope, inputs): Example: tembo new meeting my_presentation """ - for user_scope in tembo.CONFIG.scopes: if user_scope["name"] == scope: scoped_page = pages.ScopedPageCreator().create_page( @@ -47,6 +44,15 @@ def new(scope, inputs): ) scoped_page.save_to_disk() tembo.logger.info("Saved %s to disk", scoped_page) + raise SystemExit(0) + tembo.logger.critical( + "No config.yml found in %s - exiting", tembo.CONFIG.config_path + ) + raise SystemExit(1) run.add_command(new) + + +if __name__ == "__main__": + new(["scratchpad"], ()) diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index 74c85e5..cc346c1 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -19,7 +19,7 @@ class PageCreator: filename: str, extension: str, name: str, - user_input: tuple[str, ...], + user_input: tuple[str, ...] | None, template_filename: str | None = None, ) -> Page: pass @@ -53,7 +53,6 @@ class PageCreator: else: # default template_path is base_path / templates template_path = self._convert_to_path(base_path, "templates", "", "") - print(template_path, template_filename) # load the template folder file_loader = jinja2.FileSystemLoader(template_path) env = jinja2.Environment(loader=file_loader, autoescape=True) @@ -96,7 +95,6 @@ class ScopedPageCreator(PageCreator): template_contents = self._substitute_tokens( template_contents, user_input, name ) - # print(template_contents) else: template_contents = "" return ScopedPage(path, template_contents) @@ -138,34 +136,34 @@ class ScopedPageCreator(PageCreator): # 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 what's passed in - if len(input_extraction) != len(user_input) and len(input_extraction) > 0: + if len(input_extraction) > 0 and len(input_extraction) != len(user_input): # if there are input matches and they don't equal the number of input # tokens, raise error logger.critical( - "Your config specifies %s input tokens, you gave %s - exiting", + "Your config/template specifies %s input tokens, you gave %s - exiting", len(input_extraction), len(user_input), ) raise SystemExit(1) - # if the length of user input matches and number of tokens match, or there are - # no input matches, then substitute each token with the user's input + # if the length of both the input matches and the number of tokens match then + # substitute each token with the user's input for extracted_input, input_value in zip(input_extraction, user_input): tokenified_string = tokenified_string.replace(extracted_input, input_value) return tokenified_string @staticmethod def __substitute_date_tokens(tokenified_string: str) -> str: - # find any {d:DD-MM-YYYY} tokens + # find any {d:%d-%M-%Y} tokens date_extraction_token = re.findall(r"(\{d\:[^}]*\})", tokenified_string) for extracted_token in date_extraction_token: - # extract the inner DD-MM-YYYY only + # extract the inner %d-%M-%Y only strftime_value = re.match(r"\{d\:([^\}]*)\}", extracted_token) - if strftime_value: + if strftime_value is not None: strftime_value = strftime_value.group(1) - # replace {d:DD-MM-YYYY} with todays date formatted as DD-MM-YYYY - tokenified_string = tokenified_string.replace( - extracted_token, pendulum.now().format(strftime_value) - ) + if isinstance(strftime_value, str): + tokenified_string = tokenified_string.replace( + extracted_token, pendulum.now().strftime(strftime_value) + ) return tokenified_string @@ -198,7 +196,8 @@ class ScopedPage(Page): with scoped_note_file.open("w", encoding="utf-8") as scoped_page: scoped_page.write(self.page_content) else: - logger.info("The file %s already exists - skipping.", str(scoped_note_file)) + logger.info("%s already exists - skipping.", str(self)) + raise SystemExit(0) if __name__ == "__main__":