adding latest

This commit is contained in:
2021-10-25 03:46:11 +01:00
parent f7859defb3
commit d5879f711f
2 changed files with 49 additions and 6 deletions

View File

@@ -46,7 +46,6 @@ def new(scope, inputs, dry_run):
template_filename=str(user_scope["template_filename"]) template_filename=str(user_scope["template_filename"])
) )
scoped_page.save_to_disk(dry_run=dry_run) scoped_page.save_to_disk(dry_run=dry_run)
tembo.logger.info("Saved %s to disk", scoped_page.path)
raise SystemExit(0) raise SystemExit(0)
if not _name_found and len(tembo.CONFIG.scopes) > 0: if not _name_found and len(tembo.CONFIG.scopes) > 0:
tembo.logger.warning("Command %s not found in config.yml - exiting", scope) tembo.logger.warning("Command %s not found in config.yml - exiting", scope)
@@ -63,5 +62,5 @@ run.add_command(new)
if __name__ == "__main__": if __name__ == "__main__":
# BUG: fix this bug where input tokens are mismatched # BUG: fix this bug where input tokens are mismatched
# new(["meeting", "robs presentation", "meeting on gcp"]) # new(["meeting", "robs presentation", "meeting on gcp"])
new(["meeting", "a", "b", "c"]) new(["meeting", "a", "b", "c", "d"])
# new(["meeting", "robs presentation"]) # new(["meeting", "robs presentation"])

View File

@@ -68,6 +68,7 @@ class ScopedPageCreator(PageCreator):
self.page_path = "" self.page_path = ""
self.filename = "" self.filename = ""
self.extension = "" self.extension = ""
self._all_input_tokens: list[str] = []
# TODO: rename these to input tokens + more sensible # TODO: rename these to input tokens + more sensible
self.path_input_token_counts = {"config": 0, "user": 0} self.path_input_token_counts = {"config": 0, "user": 0}
self.template_input_token_counts = {"config": 0, "user": 0} self.template_input_token_counts = {"config": 0, "user": 0}
@@ -87,6 +88,11 @@ class ScopedPageCreator(PageCreator):
self.filename = filename self.filename = filename
self.extension = extension self.extension = extension
# verify the user input matches the number of input tokens in the
# config/templates
self._all_input_tokens = self.__get_input_tokens(template_filename)
self.__verify_input_tokens(user_input)
# get the path of the scoped page # get the path of the scoped page
path = self._convert_to_path( path = self._convert_to_path(
self.base_path, self.page_path, self.filename, self.extension self.base_path, self.page_path, self.filename, self.extension
@@ -110,6 +116,40 @@ class ScopedPageCreator(PageCreator):
self.__check_input_token_mismatch() self.__check_input_token_mismatch()
return ScopedPage(path, template_contents) return ScopedPage(path, template_contents)
def __get_input_tokens(self, template_filename: str | None) -> list[str]:
path = str(
pathlib.Path(
self.base_path, self.page_path, self.filename, self.extension
).expanduser()
)
if template_filename is not None:
template_contents = self._load_template(self.base_path, template_filename)
else:
template_contents = ""
# get the input tokens from both the path and the template
all_input_tokens = []
for tokenified_string in (path, template_contents):
all_input_tokens.extend(re.findall(r"(\{input\d*\})", tokenified_string))
return sorted(all_input_tokens)
def __verify_input_tokens(self, user_input: Tuple[str, ...] | Tuple[()]) -> None:
if len(self._all_input_tokens) != len(user_input):
logger.critical(
"Your config/template specifies %s input tokens, you gave %s",
len(self._all_input_tokens),
len(user_input),
)
raise SystemExit(1)
def __substitute_input_tokens__(
self,
tokenified_string: str,
user_input: Tuple[str, ...] | Tuple[()],
) -> str:
for input_value, extracted_token in zip(user_input, self._all_input_tokens):
tokenified_string = tokenified_string.replace(extracted_token, input_value)
return tokenified_string
def __check_input_token_mismatch(self) -> None: def __check_input_token_mismatch(self) -> None:
_max_config_input_token_count = max( _max_config_input_token_count = max(
self.path_input_token_counts["config"], self.path_input_token_counts["config"],
@@ -142,9 +182,12 @@ class ScopedPageCreator(PageCreator):
input_token_type: Literal["path", "template"], input_token_type: Literal["path", "template"],
) -> str: ) -> str:
"""For a tokened string, substitute input, name and date tokens.""" """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 = self.__substitute_input_tokens(
tokenified_string, user_input, input_token_type # tokenified_string, user_input, input_token_type
# )
tokenified_string = self.__substitute_input_tokens__(
tokenified_string, user_input
) )
tokenified_string = self.__substitute_name_tokens(tokenified_string, name) tokenified_string = self.__substitute_name_tokens(tokenified_string, name)
tokenified_string = self.__substitute_date_tokens(tokenified_string) tokenified_string = self.__substitute_date_tokens(tokenified_string)
@@ -247,9 +290,10 @@ class ScopedPage(Page):
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)
logger.info("Saved %s to disk", self.path)
else: else:
logger.info("%s already exists - skipping.", self.path) logger.info("%s already exists - skipping.", self.path)
raise SystemExit(0) raise SystemExit(0)
if __name__ == "__main__": if __name__ == "__main__":