adding example and template_file as optional in config

This commit is contained in:
2021-10-29 00:15:54 +01:00
parent 4077ec1760
commit 1fccf89d60
3 changed files with 63 additions and 41 deletions

View File

@@ -24,7 +24,8 @@ Documentation:
Functionality:
✔ Move any `tembo.CONFIG` calls out of `pages.py` and ensure these are passed in from the cli. @done(21-10-28 19:44)
✔ Make `config scope` a dict in `cli.py`. @done(21-10-28 19:44)
☐ Add the `--example` output to the miscounted token message so the user knows the correct command to use.
✔ Make example optional @done(21-10-29 00:15)
✔ Add the `--example` output to the miscounted token message so the user knows the correct command to use. @done(21-10-29 00:15)
✔ Page options dataclass @done(21-10-28 20:09)
☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages?
☐ Use the python runner Duty
@@ -39,6 +40,9 @@ VSCode:
☐ Document build error: <https://github.com/pyenv/pyenv/issues/1095>
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.8.11
☐ Freeze a click app: <https://stackoverflow.com/questions/45090083/freeze-a-program-created-with-pythons-click-pacage>
☐ If python 3.9 can be used with Pyinstaller, rewrite the code to use the latest Python features
dict.update -> |=
walrus :=
Tests:
☐ Write tests! @2d

View File

@@ -62,9 +62,7 @@ def new(scope, inputs, dry_run, example):
raise SystemExit(1) from type_error
# get the scope information from the tembo config.yml
try:
config_scope = {
option: str(user_scope[option])
config_scope = {}
for option in [
"name",
"example",
@@ -72,22 +70,30 @@ def new(scope, inputs, dry_run, example):
"filename",
"extension",
"template_filename",
]
]:
try:
config_scope.update(
{
option: str(user_scope[option])
for user_scope in tembo.CONFIG.scopes
if user_scope["name"] == scope
}
)
except KeyError as key_error:
# raise error if any non optional keys are missing
tembo.logger.critical("Key %s not found in config.yml - exiting", key_error)
if key_error.args[0] in ["example", "template_filename"]:
config_scope.update({key_error.args[0]: None})
continue
tembo.logger.critical(
"Key %s not found in config. yml - exiting", key_error
)
raise SystemExit(1) from key_error
# print the example to the user
if example:
tembo.logger.info(
"Example for 'tembo new %s': %s",
config_scope["name"],
config_scope["example"]
if isinstance(config_scope[0][1], str)
if isinstance(config_scope["example"], str)
else "No example in config.yml",
)
raise SystemExit(0)
@@ -99,6 +105,7 @@ def new(scope, inputs, dry_run, example):
filename=config_scope["filename"],
extension=config_scope["extension"],
name=config_scope["name"],
example=config_scope["example"],
user_input=inputs,
template_filename=config_scope["template_filename"],
template_path=tembo.CONFIG.template_path,

View File

@@ -20,9 +20,10 @@ class PageCreatorOptions:
filename: str
extension: str
name: str
example: str | None
user_input: Collection[str]
template_filename: str | None = None
template_path: str | None = None
template_filename: str | None
template_path: str | None
class PageCreator:
@@ -108,7 +109,7 @@ class ScopedPageCreator(PageCreator):
self._all_input_tokens = self._get_input_tokens(
options.template_filename, options.template_path
)
self._verify_input_tokens(options.user_input)
self._verify_input_tokens(options.user_input, options.example)
# get the path of the scoped page
path = self._convert_to_path(
@@ -148,10 +149,20 @@ class ScopedPageCreator(PageCreator):
all_input_tokens.extend(re.findall(r"(\{input\d*\})", tokenified_string))
return sorted(all_input_tokens)
def _verify_input_tokens(self, user_input: Collection[str]) -> None:
def _verify_input_tokens(
self, user_input: Collection[str], example: str | None
) -> None:
if len(self._all_input_tokens) != len(user_input):
if example is not None:
tembo.logger.critical(
"Your tembo.config/template specifies %s input tokens, you gave %s",
"Your tembo.config/template specifies %s input tokens, you gave %s. Example command: %s",
len(self._all_input_tokens),
len(user_input),
example,
)
else:
tembo.logger.critical(
"Your tembo.config/template specifies %s input tokens, you gave %s.",
len(self._all_input_tokens),
len(user_input),
)
@@ -332,17 +343,17 @@ if __name__ == "__main__":
# "scratchpad.md.tpl",
# )
# )
test_page_with_template = c.create_page(
"~/tembo",
"{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
"file-{input0}-{name}",
".md",
"meeting",
("last",),
"scratchpad.md.tpl",
)
print(test_page_with_template)
test_page_with_template.save_to_disk(False)
# test_page_with_template = c.create_page(
# "~/tembo",
# "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
# "file-{input0}-{name}",
# ".md",
# "meeting",
# ("last",),
# "scratchpad.md.tpl",
# )
# print(test_page_with_template)
# test_page_with_template.save_to_disk(False)
# print(
# c.create_page(
# "~/tembo",