updating typing

This commit is contained in:
2021-10-24 06:01:17 +01:00
parent 67c2a02352
commit 70129585a8

View File

@@ -3,6 +3,7 @@ from __future__ import annotations
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import pathlib import pathlib
import re import re
from typing import Tuple
import jinja2 import jinja2
import pendulum import pendulum
@@ -19,9 +20,8 @@ class PageCreator:
filename: str, filename: str,
extension: str, extension: str,
name: str, name: str,
user_input: tuple[str, ...] | None, user_input: Tuple[str, ...] | Tuple[()],
template_filename: str | None = None, template_filename: str | None = None,
dry_run: bool = False
) -> Page: ) -> Page:
raise NotImplementedError raise NotImplementedError
@@ -76,7 +76,7 @@ class ScopedPageCreator(PageCreator):
filename: str, filename: str,
extension: str, extension: str,
name: str, name: str,
user_input: tuple[str, ...] | None, user_input: Tuple[str, ...] | Tuple[()],
template_filename: str | None = None, template_filename: str | None = None,
) -> Page: ) -> Page:
self.base_path = base_path self.base_path = base_path
@@ -101,7 +101,10 @@ class ScopedPageCreator(PageCreator):
return ScopedPage(path, template_contents) return ScopedPage(path, template_contents)
def _substitute_tokens( 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: ) -> str:
# for a tokened string, substitute input, name and date tokens # for a tokened string, substitute input, name and date tokens
tokenified_string = self.__substitute_input_tokens( tokenified_string = self.__substitute_input_tokens(
@@ -121,11 +124,11 @@ class ScopedPageCreator(PageCreator):
@staticmethod @staticmethod
def __substitute_input_tokens( def __substitute_input_tokens(
tokenified_string: str, user_input: tuple[str, ...] | None tokenified_string: str, user_input: Tuple[str, ...] | Tuple[()]
) -> str: ) -> str:
# find {inputN} tokens in string # find {inputN} tokens in string
input_extraction = re.findall(r"(\{input\d*\})", tokenified_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 there's no user input, but the regex matches, raise error
if len(input_extraction) > 0: if len(input_extraction) > 0:
logger.critical( logger.critical(
@@ -137,7 +140,11 @@ class ScopedPageCreator(PageCreator):
# if there aren't any tokens in the string, return the string # if there aren't any tokens in the string, return the string
return tokenified_string return tokenified_string
# if there is user input, check the number of tokens match the number passed in # 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 # if there are input matches and they don't equal the number of input
# tokens, raise error # tokens, raise error
logger.critical( logger.critical(
@@ -148,8 +155,11 @@ class ScopedPageCreator(PageCreator):
raise SystemExit(1) raise SystemExit(1)
# if the length of both the input matches and the number of tokens match then # if the length of both the input matches and the number of tokens match then
# substitute each token with the user's input # substitute each token with the user's input
for extracted_input, input_value in zip(input_extraction, user_input): if len(user_input) > 0:
tokenified_string = tokenified_string.replace(extracted_input, input_value) for extracted_input, input_value in zip(input_extraction, user_input):
tokenified_string = tokenified_string.replace(
extracted_input, input_value
)
return tokenified_string return tokenified_string
@staticmethod @staticmethod
@@ -170,7 +180,7 @@ class ScopedPageCreator(PageCreator):
class Page(metaclass=ABCMeta): class Page(metaclass=ABCMeta):
@abstractmethod @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 raise NotImplementedError
@abstractmethod @abstractmethod
@@ -189,12 +199,12 @@ class ScopedPage(Page):
return f"ScopedPage({self.path})" return f"ScopedPage({self.path})"
def save_to_disk(self, dry_run: bool = False) -> None: 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: if dry_run:
logger.info("%s will be created", self.path) logger.info("%s will be created", self.path)
raise SystemExit(0) 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(): if not scoped_note_file.exists():
with scoped_note_file.open("w", encoding="utf-8") as scoped_page: with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
scoped_page.write(self.page_content) scoped_page.write(self.page_content)
@@ -264,7 +274,7 @@ if __name__ == "__main__":
"scratchpad.md.tpl", "scratchpad.md.tpl",
) )
print(test_page_with_template) print(test_page_with_template)
test_page_with_template.save_to_disk() test_page_with_template.save_to_disk(False)
# print( # print(
# c.create_page( # c.create_page(
# "~/tembo", # "~/tembo",