From 70129585a86a80aa1cb334b6269579356a484ba3 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Sun, 24 Oct 2021 06:01:17 +0100 Subject: [PATCH] updating typing --- tembo/journal/pages.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index b32019b..ba4a6e9 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -3,6 +3,7 @@ from __future__ import annotations from abc import ABCMeta, abstractmethod import pathlib import re +from typing import Tuple import jinja2 import pendulum @@ -19,9 +20,8 @@ class PageCreator: filename: str, extension: str, name: str, - user_input: tuple[str, ...] | None, + user_input: Tuple[str, ...] | Tuple[()], template_filename: str | None = None, - dry_run: bool = False ) -> Page: raise NotImplementedError @@ -76,7 +76,7 @@ class ScopedPageCreator(PageCreator): filename: str, extension: str, name: str, - user_input: tuple[str, ...] | None, + user_input: Tuple[str, ...] | Tuple[()], template_filename: str | None = None, ) -> Page: self.base_path = base_path @@ -101,7 +101,10 @@ class ScopedPageCreator(PageCreator): return ScopedPage(path, template_contents) def _substitute_tokens( - self, tokenified_string: str, user_input: tuple[str, ...] | None, name: str + self, + tokenified_string: str, + user_input: Tuple[str, ...] | Tuple[()], + name: str, ) -> str: # for a tokened string, substitute input, name and date tokens tokenified_string = self.__substitute_input_tokens( @@ -121,11 +124,11 @@ class ScopedPageCreator(PageCreator): @staticmethod def __substitute_input_tokens( - tokenified_string: str, user_input: tuple[str, ...] | None + tokenified_string: str, user_input: Tuple[str, ...] | Tuple[()] ) -> str: # find {inputN} tokens in string input_extraction = re.findall(r"(\{input\d*\})", tokenified_string) - if user_input is None: + if len(user_input) == 0: # if there's no user input, but the regex matches, raise error if len(input_extraction) > 0: logger.critical( @@ -137,7 +140,11 @@ 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 the number passed in - if len(input_extraction) > 0 and len(input_extraction) != len(user_input): + if ( + len(input_extraction) > 0 + and len(input_extraction) != len(user_input) + and len(user_input) > 0 + ): # if there are input matches and they don't equal the number of input # tokens, raise error logger.critical( @@ -148,8 +155,11 @@ class ScopedPageCreator(PageCreator): raise SystemExit(1) # 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) + if len(user_input) > 0: + for extracted_input, input_value in zip(input_extraction, user_input): + tokenified_string = tokenified_string.replace( + extracted_input, input_value + ) return tokenified_string @staticmethod @@ -170,7 +180,7 @@ class ScopedPageCreator(PageCreator): class Page(metaclass=ABCMeta): @abstractmethod - def __init__(self, path: pathlib.Path, page_content: str, dry_run: bool) -> None: + def __init__(self, path: pathlib.Path, page_content: str) -> None: raise NotImplementedError @abstractmethod @@ -189,12 +199,12 @@ class ScopedPage(Page): return f"ScopedPage({self.path})" def save_to_disk(self, dry_run: bool = False) -> None: - scoped_note_file = pathlib.Path(self.path) - # create the parent directories - scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True) if dry_run: logger.info("%s will be created", self.path) raise SystemExit(0) + # create the parent directories + scoped_note_file = pathlib.Path(self.path) + scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True) if not scoped_note_file.exists(): with scoped_note_file.open("w", encoding="utf-8") as scoped_page: scoped_page.write(self.page_content) @@ -264,7 +274,7 @@ if __name__ == "__main__": "scratchpad.md.tpl", ) print(test_page_with_template) - test_page_with_template.save_to_disk() + test_page_with_template.save_to_disk(False) # print( # c.create_page( # "~/tembo",