mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 09:55:43 +00:00
adding latest
This commit is contained in:
@@ -2,6 +2,10 @@ Functionality:
|
|||||||
☐ Handle case where there are no scopes in the config and command is invoked.
|
☐ Handle case where there are no scopes in the config and command is invoked.
|
||||||
☐ Have an `--example` flag to `new` that prints an example given in the `config.yml`
|
☐ Have an `--example` flag to `new` that prints an example given in the `config.yml`
|
||||||
☐ Should be a `tembo new --list` to list all possible names.
|
☐ Should be a `tembo new --list` to list all possible names.
|
||||||
|
☐ When template not found, raise a Tembo error
|
||||||
|
currently raises jinja2.exceptions.TemplateNotFound on line 62, in _load_template
|
||||||
|
☐ Add update notification?
|
||||||
|
check pypi for latest version and compare to current
|
||||||
✔ `TEMBO_CONFIG` should follow same pattern as other env vars and be a python string when read in @done(21-10-24 05:31)
|
✔ `TEMBO_CONFIG` should follow same pattern as other env vars and be a python string when read in @done(21-10-24 05:31)
|
||||||
|
|
||||||
VSCode:
|
VSCode:
|
||||||
|
|||||||
@@ -62,5 +62,6 @@ 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", "robs presentation"])
|
# new(["meeting", "robs presentation"])
|
||||||
|
|||||||
@@ -69,9 +69,9 @@ class ScopedPageCreator(PageCreator):
|
|||||||
self.page_path = ""
|
self.page_path = ""
|
||||||
self.filename = ""
|
self.filename = ""
|
||||||
self.extension = ""
|
self.extension = ""
|
||||||
self.path_date_tokens: Tuple[int, int] = (0, 0)
|
# TODO: rename these to input tokens + more sensible
|
||||||
self.template_date_tokens: Tuple[int, int] = (0, 0)
|
self.path_input_tokens: Tuple[int, int] = (0, 0)
|
||||||
|
self.template_input_tokens: Tuple[int, int] = (0, 0)
|
||||||
|
|
||||||
def create_page(
|
def create_page(
|
||||||
self,
|
self,
|
||||||
@@ -92,77 +92,84 @@ class ScopedPageCreator(PageCreator):
|
|||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
# substitute tokens in the filepath
|
# substitute tokens in the filepath
|
||||||
try:
|
path = pathlib.Path(
|
||||||
path = pathlib.Path(
|
self._substitute_tokens(str(path), user_input, name, "path")
|
||||||
self._substitute_tokens(str(path), user_input, name, "path")
|
)
|
||||||
)
|
if sum(self.path_input_tokens) > 0:
|
||||||
except MismatchedTokenError as mismatched_path_error:
|
_highest_input_token_in_path = max(self.path_input_tokens)
|
||||||
self.path_error = mismatched_path_error
|
else:
|
||||||
|
_highest_input_token_in_path = 0
|
||||||
|
|
||||||
# get the template file
|
# get the template file
|
||||||
if template_filename is not None:
|
if template_filename is not None:
|
||||||
# load the template file contents and substitute tokens
|
# load the template file contents and substitute tokens
|
||||||
template_contents = self._load_template(self.base_path, template_filename)
|
template_contents = self._load_template(self.base_path, template_filename)
|
||||||
try:
|
template_contents = self._substitute_tokens(
|
||||||
template_contents = self._substitute_tokens(
|
template_contents, user_input, name, "template"
|
||||||
template_contents, user_input, name, "template"
|
)
|
||||||
)
|
if sum(self.template_input_tokens) > 0:
|
||||||
except MismatchedTokenError as mismatched_template_error:
|
_highest_input_token_in_template = max(self.template_input_tokens)
|
||||||
self.template_error = mismatched_template_error
|
else:
|
||||||
|
_highest_input_token_in_template = 0
|
||||||
else:
|
else:
|
||||||
template_contents = ""
|
template_contents = ""
|
||||||
if self.path_error is not None or self.template_error is not None:
|
|
||||||
# self.__mismatched_token_error(path_error, template_error)
|
self.__check_input_token_mismatch(
|
||||||
if self.path_error.args[0] > self.template_error.args[0]:
|
_highest_input_token_in_path, _highest_input_token_in_template
|
||||||
print("path_token_count > template_token_count")
|
)
|
||||||
elif self.template_error.args[0] > self.path_error.args[0]:
|
|
||||||
print("template_token_count > path_token_count")
|
|
||||||
return ScopedPage(path, template_contents)
|
return ScopedPage(path, template_contents)
|
||||||
|
|
||||||
# @staticmethod
|
def __check_input_token_mismatch(
|
||||||
# def __mismatched_token_error(
|
self, _highest_input_token_in_path: int, _highest_input_token_in_template: int
|
||||||
# path_error: MismatchedTokenError | None = None,
|
) -> None:
|
||||||
# template_error: MismatchedTokenError | None = None,
|
_highest_input_token_count = max(
|
||||||
# ) -> None:
|
_highest_input_token_in_path, _highest_input_token_in_template
|
||||||
# if isinstance(path_error, MismatchedTokenError):
|
)
|
||||||
# path_token_count = path_error.args[0]
|
if _highest_input_token_in_path < _highest_input_token_count:
|
||||||
# # logger.critical(
|
logger.critical(
|
||||||
# # "Your config specifies %s input tokens, you gave %s " "- exiting",
|
"Your config/template specifies %s input tokens, you gave %s "
|
||||||
# # path_error.args[0],
|
"- exiting",
|
||||||
# # path_error.args[1],
|
_highest_input_token_count,
|
||||||
# # )
|
self.path_input_tokens[0],
|
||||||
# # raise SystemExit(1)
|
)
|
||||||
# if isinstance(template_error, MismatchedTokenError):
|
raise SystemExit(1)
|
||||||
# template_token_count = template_error.args[0]
|
if _highest_input_token_in_path > _highest_input_token_count:
|
||||||
# # logger.critical(
|
logger.warning(
|
||||||
# # "Your template specifies %s input tokens, you gave %s " "- exiting",
|
"Your config/template specifies %s input tokens, you gave %s",
|
||||||
# # template_error.args[0],
|
_highest_input_token_count,
|
||||||
# # template_error.args[1],
|
self.path_input_tokens[0],
|
||||||
# # )
|
)
|
||||||
# # raise SystemExit(1)
|
if _highest_input_token_in_template < _highest_input_token_count:
|
||||||
# if path_token_count > template_token_count:
|
logger.critical(
|
||||||
# print("path_token_count > template_token_count")
|
"Your config/template specifies %s input tokens, you gave %s "
|
||||||
# elif template_token_count > path_token_count:
|
"- exiting",
|
||||||
# print("template_token_count > path_token_count")
|
_highest_input_token_count,
|
||||||
# raise SystemExit(1)
|
self.template_input_tokens[0],
|
||||||
|
)
|
||||||
|
raise SystemExit(1)
|
||||||
|
if _highest_input_token_in_template > _highest_input_token_count:
|
||||||
|
logger.warning(
|
||||||
|
"Your config/template specifies %s input tokens, you gave %s",
|
||||||
|
_highest_input_token_count,
|
||||||
|
self.template_input_tokens[0],
|
||||||
|
)
|
||||||
|
|
||||||
# TODO: change the annotation to include the error
|
|
||||||
def _substitute_tokens(
|
def _substitute_tokens(
|
||||||
self,
|
self,
|
||||||
tokenified_string: str,
|
tokenified_string: str,
|
||||||
user_input: Tuple[str, ...] | Tuple[()],
|
user_input: Tuple[str, ...] | Tuple[()],
|
||||||
name: str,
|
name: str,
|
||||||
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."""
|
||||||
try:
|
|
||||||
tokenified_string = self.__substitute_input_tokens(
|
tokenified_string = self.__substitute_input_tokens(
|
||||||
tokenified_string, user_input
|
tokenified_string, user_input, input_token_type
|
||||||
)
|
)
|
||||||
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)
|
||||||
except MismatchedTokenError as mismatched_token_error:
|
|
||||||
raise mismatched_token_error
|
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -175,28 +182,35 @@ class ScopedPageCreator(PageCreator):
|
|||||||
|
|
||||||
# @staticmethod
|
# @staticmethod
|
||||||
def __substitute_input_tokens(
|
def __substitute_input_tokens(
|
||||||
self, tokenified_string: str, user_input: Tuple[str, ...] | Tuple[()]
|
self,
|
||||||
|
tokenified_string: str,
|
||||||
|
user_input: Tuple[str, ...] | Tuple[()],
|
||||||
|
input_token_type: Literal["path", "template"],
|
||||||
) -> 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 there is no user input
|
||||||
if len(user_input) == 0:
|
if len(user_input) == 0:
|
||||||
# if there's no user input, but the regex matches, raise error
|
|
||||||
if len(input_extraction) > 0:
|
if len(input_extraction) > 0:
|
||||||
raise MismatchedTokenError(len(input_extraction), 0)
|
# if the regex matches, save the number of input tokens found
|
||||||
|
if input_token_type == "path": # noqa: bandit 105
|
||||||
|
# TODO: change this to a dict instead of tuple
|
||||||
|
self.path_input_tokens = (len(input_extraction), 0)
|
||||||
|
if input_token_type == "template": # noqa: bandit 105
|
||||||
|
self.template_input_tokens = (len(input_extraction), 0)
|
||||||
# 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 (
|
# if there is user input
|
||||||
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
|
|
||||||
raise MismatchedTokenError(len(input_extraction), len(user_input))
|
|
||||||
# if the length of both the input matches and the number of tokens match then
|
|
||||||
# substitute each token with the user's input
|
|
||||||
if len(user_input) > 0:
|
if len(user_input) > 0:
|
||||||
|
# save the number of input tokens, and the number of user inputs
|
||||||
|
if input_token_type == "path": # noqa: bandit 105
|
||||||
|
self.path_input_tokens = (len(input_extraction), len(user_input))
|
||||||
|
elif input_token_type == "template": # noqa: bandit 105
|
||||||
|
self.template_input_tokens = (len(input_extraction), len(user_input))
|
||||||
|
|
||||||
|
# sbustitute the input token for the user's input
|
||||||
for extracted_input, input_value in zip(input_extraction, user_input):
|
for extracted_input, input_value in zip(input_extraction, user_input):
|
||||||
tokenified_string = tokenified_string.replace(
|
tokenified_string = tokenified_string.replace(
|
||||||
extracted_input, input_value
|
extracted_input, input_value
|
||||||
@@ -205,7 +219,9 @@ class ScopedPageCreator(PageCreator):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __substitute_date_tokens(tokenified_string: str) -> str:
|
def __substitute_date_tokens(tokenified_string: str) -> str:
|
||||||
# find any {d:%d-%M-%Y} tokens
|
"""Find any {d:%d-%M-%Y} tokens."""
|
||||||
|
|
||||||
|
# extract the full token string
|
||||||
date_extraction_token = re.findall(r"(\{d\:[^}]*\})", tokenified_string)
|
date_extraction_token = re.findall(r"(\{d\:[^}]*\})", tokenified_string)
|
||||||
for extracted_token in date_extraction_token:
|
for extracted_token in date_extraction_token:
|
||||||
# extract the inner %d-%M-%Y only
|
# extract the inner %d-%M-%Y only
|
||||||
|
|||||||
Reference in New Issue
Block a user