diff --git a/tembo/cli.py b/tembo/cli.py index 1978be1..e5810e6 100644 --- a/tembo/cli.py +++ b/tembo/cli.py @@ -46,7 +46,6 @@ def new(scope, inputs, dry_run): template_filename=str(user_scope["template_filename"]) ) scoped_page.save_to_disk(dry_run=dry_run) - tembo.logger.info("Saved %s to disk", scoped_page.path) raise SystemExit(0) if not _name_found and len(tembo.CONFIG.scopes) > 0: tembo.logger.warning("Command %s not found in config.yml - exiting", scope) @@ -63,5 +62,5 @@ run.add_command(new) if __name__ == "__main__": # BUG: fix this bug where input tokens are mismatched # new(["meeting", "robs presentation", "meeting on gcp"]) - new(["meeting", "a", "b", "c"]) + new(["meeting", "a", "b", "c", "d"]) # new(["meeting", "robs presentation"]) diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index f27afd1..98a8379 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -68,6 +68,7 @@ class ScopedPageCreator(PageCreator): self.page_path = "" self.filename = "" self.extension = "" + self._all_input_tokens: list[str] = [] # TODO: rename these to input tokens + more sensible self.path_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.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 path = self._convert_to_path( self.base_path, self.page_path, self.filename, self.extension @@ -110,6 +116,40 @@ class ScopedPageCreator(PageCreator): self.__check_input_token_mismatch() 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: _max_config_input_token_count = max( self.path_input_token_counts["config"], @@ -142,9 +182,12 @@ class ScopedPageCreator(PageCreator): input_token_type: Literal["path", "template"], ) -> str: """For a tokened string, substitute input, name and date tokens.""" - - tokenified_string = self.__substitute_input_tokens( - tokenified_string, user_input, input_token_type + # TODO: fn to get tokens from file and template + # tokenified_string = self.__substitute_input_tokens( + # 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_date_tokens(tokenified_string) @@ -247,9 +290,10 @@ class ScopedPage(Page): if not scoped_note_file.exists(): with scoped_note_file.open("w", encoding="utf-8") as scoped_page: scoped_page.write(self.page_content) + logger.info("Saved %s to disk", self.path) else: logger.info("%s already exists - skipping.", self.path) - raise SystemExit(0) + raise SystemExit(0) if __name__ == "__main__":