mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 07:55:45 +00:00
adding latest
This commit is contained in:
@@ -46,3 +46,5 @@ Logging:
|
|||||||
☐ ~/tembo/.templates
|
☐ ~/tembo/.templates
|
||||||
☐ ~/tembo/logs
|
☐ ~/tembo/logs
|
||||||
☐ Document how to overwrite these with ENV vars
|
☐ Document how to overwrite these with ENV vars
|
||||||
|
☐ have a git repo with all the above already configured and walk user through
|
||||||
|
clone the repo, delete .git, git init, configure and add git origin
|
||||||
|
|||||||
@@ -3,7 +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, Literal
|
from typing import Tuple
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import pendulum
|
import pendulum
|
||||||
@@ -69,9 +69,6 @@ class ScopedPageCreator(PageCreator):
|
|||||||
self.filename = ""
|
self.filename = ""
|
||||||
self.extension = ""
|
self.extension = ""
|
||||||
self._all_input_tokens: list[str] = []
|
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}
|
|
||||||
|
|
||||||
def create_page(
|
def create_page(
|
||||||
self,
|
self,
|
||||||
@@ -88,10 +85,10 @@ 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
|
# verify the user input length matches the number of input tokens in the
|
||||||
# config/templates
|
# config/templates
|
||||||
self._all_input_tokens = self.__get_input_tokens(template_filename)
|
self._all_input_tokens = self._get_input_tokens(template_filename)
|
||||||
self.__verify_input_tokens(user_input)
|
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(
|
||||||
@@ -100,7 +97,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
|
|
||||||
# substitute tokens in the filepath
|
# substitute tokens in the filepath
|
||||||
path = pathlib.Path(
|
path = pathlib.Path(
|
||||||
self._substitute_tokens(str(path), user_input, name, "path")
|
self._substitute_tokens(str(path), user_input, name)
|
||||||
)
|
)
|
||||||
|
|
||||||
# get the template file
|
# get the template file
|
||||||
@@ -108,15 +105,14 @@ class ScopedPageCreator(PageCreator):
|
|||||||
# 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)
|
||||||
template_contents = self._substitute_tokens(
|
template_contents = self._substitute_tokens(
|
||||||
template_contents, user_input, name, "template"
|
template_contents, user_input, name
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
template_contents = ""
|
template_contents = ""
|
||||||
|
|
||||||
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]:
|
def _get_input_tokens(self, template_filename: str | None) -> list[str]:
|
||||||
path = str(
|
path = str(
|
||||||
pathlib.Path(
|
pathlib.Path(
|
||||||
self.base_path, self.page_path, self.filename, self.extension
|
self.base_path, self.page_path, self.filename, self.extension
|
||||||
@@ -132,7 +128,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
all_input_tokens.extend(re.findall(r"(\{input\d*\})", tokenified_string))
|
all_input_tokens.extend(re.findall(r"(\{input\d*\})", tokenified_string))
|
||||||
return sorted(all_input_tokens)
|
return sorted(all_input_tokens)
|
||||||
|
|
||||||
def __verify_input_tokens(self, user_input: Tuple[str, ...] | Tuple[()]) -> None:
|
def _verify_input_tokens(self, user_input: Tuple[str, ...] | Tuple[()]) -> None:
|
||||||
if len(self._all_input_tokens) != len(user_input):
|
if len(self._all_input_tokens) != len(user_input):
|
||||||
logger.critical(
|
logger.critical(
|
||||||
"Your config/template specifies %s input tokens, you gave %s",
|
"Your config/template specifies %s input tokens, you gave %s",
|
||||||
@@ -141,7 +137,25 @@ class ScopedPageCreator(PageCreator):
|
|||||||
)
|
)
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
def __substitute_input_tokens__(
|
def _substitute_tokens(
|
||||||
|
self,
|
||||||
|
tokenified_string: str,
|
||||||
|
user_input: Tuple[str, ...] | Tuple[()],
|
||||||
|
name: str,
|
||||||
|
) -> str:
|
||||||
|
"""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, 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)
|
||||||
|
return tokenified_string
|
||||||
|
|
||||||
|
def __substitute_input_tokens(
|
||||||
self,
|
self,
|
||||||
tokenified_string: str,
|
tokenified_string: str,
|
||||||
user_input: Tuple[str, ...] | Tuple[()],
|
user_input: Tuple[str, ...] | Tuple[()],
|
||||||
@@ -150,49 +164,6 @@ class ScopedPageCreator(PageCreator):
|
|||||||
tokenified_string = tokenified_string.replace(extracted_token, input_value)
|
tokenified_string = tokenified_string.replace(extracted_token, input_value)
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
|
|
||||||
def __check_input_token_mismatch(self) -> None:
|
|
||||||
_max_config_input_token_count = max(
|
|
||||||
self.path_input_token_counts["config"],
|
|
||||||
self.template_input_token_counts["config"],
|
|
||||||
)
|
|
||||||
_max_user_input_token_count = max(
|
|
||||||
self.path_input_token_counts["user"],
|
|
||||||
self.template_input_token_counts["user"],
|
|
||||||
)
|
|
||||||
if _max_user_input_token_count < _max_config_input_token_count:
|
|
||||||
logger.critical(
|
|
||||||
"Your config/template specifies %s input tokens, you gave %s "
|
|
||||||
"- exiting",
|
|
||||||
_max_config_input_token_count,
|
|
||||||
_max_user_input_token_count,
|
|
||||||
)
|
|
||||||
raise SystemExit(1)
|
|
||||||
if _max_user_input_token_count > _max_config_input_token_count:
|
|
||||||
logger.warning(
|
|
||||||
"Your config/template specifies %s input tokens, you gave %s",
|
|
||||||
_max_config_input_token_count,
|
|
||||||
_max_user_input_token_count,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _substitute_tokens(
|
|
||||||
self,
|
|
||||||
tokenified_string: str,
|
|
||||||
user_input: Tuple[str, ...] | Tuple[()],
|
|
||||||
name: str,
|
|
||||||
input_token_type: Literal["path", "template"],
|
|
||||||
) -> str:
|
|
||||||
"""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, 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)
|
|
||||||
return tokenified_string
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __substitute_name_tokens(tokenified_string: str, name: str) -> str:
|
def __substitute_name_tokens(tokenified_string: str, name: str) -> str:
|
||||||
# find any {name} tokens and substitute for the name value
|
# find any {name} tokens and substitute for the name value
|
||||||
@@ -201,47 +172,6 @@ class ScopedPageCreator(PageCreator):
|
|||||||
tokenified_string = tokenified_string.replace(extracted_input, name)
|
tokenified_string = tokenified_string.replace(extracted_input, name)
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
|
|
||||||
# @staticmethod
|
|
||||||
def __substitute_input_tokens(
|
|
||||||
self,
|
|
||||||
tokenified_string: str,
|
|
||||||
user_input: Tuple[str, ...] | Tuple[()],
|
|
||||||
input_token_type: Literal["path", "template"],
|
|
||||||
) -> str:
|
|
||||||
"""Find `{inputN}` tokens in string."""
|
|
||||||
|
|
||||||
input_extraction = re.findall(r"(\{input\d*\})", tokenified_string)
|
|
||||||
# if there is no user input
|
|
||||||
if len(user_input) == 0:
|
|
||||||
if 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_token_counts["config"] = len(input_extraction)
|
|
||||||
self.path_input_token_counts["user"] = 0
|
|
||||||
if input_token_type == "template": # noqa: bandit 105
|
|
||||||
self.template_input_token_counts["config"] = len(input_extraction)
|
|
||||||
self.template_input_token_counts["user"] = 0
|
|
||||||
# if there aren't any tokens in the string, return the string
|
|
||||||
return tokenified_string
|
|
||||||
|
|
||||||
# if there is user input
|
|
||||||
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_token_counts["config"] = len(input_extraction)
|
|
||||||
self.path_input_token_counts["user"] = len(user_input)
|
|
||||||
elif input_token_type == "template": # noqa: bandit 105
|
|
||||||
self.template_input_token_counts["config"] = len(input_extraction)
|
|
||||||
self.template_input_token_counts["user"] = len(user_input)
|
|
||||||
|
|
||||||
# sbustitute the input token for the user's input
|
|
||||||
for extracted_input, input_value in zip(input_extraction, user_input):
|
|
||||||
tokenified_string = tokenified_string.replace(
|
|
||||||
extracted_input, input_value
|
|
||||||
)
|
|
||||||
return tokenified_string
|
|
||||||
|
|
||||||
@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."""
|
||||||
|
|||||||
Reference in New Issue
Block a user