mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 10:16:02 +00:00
adding latest tests + refactored cli
This commit is contained in:
@@ -32,9 +32,8 @@ def run():
|
||||
def list_all():
|
||||
"""List all scopes defined in the config.yml."""
|
||||
_all_scopes = [user_scope["name"] for user_scope in tembo.cli.CONFIG.scopes]
|
||||
tembo.cli.logger.info(
|
||||
"%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes)
|
||||
)
|
||||
_all_scopes_joined = "', '".join(_all_scopes)
|
||||
cli_message(f"{len(_all_scopes)} names found in config.yml: '{_all_scopes_joined}'")
|
||||
raise SystemExit(0)
|
||||
|
||||
|
||||
@@ -70,7 +69,7 @@ def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
|
||||
exceptions.MissingConfigYML,
|
||||
) as tembo_exception:
|
||||
cli_message(tembo_exception.args[0])
|
||||
raise SystemExit(0) from tembo_exception
|
||||
raise SystemExit(1) from tembo_exception
|
||||
|
||||
# get the scope configuration from the config.yml
|
||||
try:
|
||||
@@ -83,7 +82,7 @@ def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
|
||||
_new_show_example(example, config_scope)
|
||||
|
||||
# if the name is in the config.yml, create the scoped page
|
||||
scoped_page = _new_create_scoped_page(config_scope, inputs)
|
||||
scoped_page = new_create_scoped_page(config_scope, inputs)
|
||||
|
||||
if dry_run:
|
||||
cli_message(f"{scoped_page.path} will be created")
|
||||
@@ -99,6 +98,39 @@ def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
|
||||
raise SystemExit(0) from scoped_page_already_exists
|
||||
|
||||
|
||||
def new_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page:
|
||||
page_creator_options = pages.PageCreatorOptions(
|
||||
base_path=tembo.cli.CONFIG.base_path,
|
||||
template_path=tembo.cli.CONFIG.template_path,
|
||||
page_path=config_scope["path"],
|
||||
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"],
|
||||
)
|
||||
try:
|
||||
return pages.ScopedPageCreator(page_creator_options).create_page()
|
||||
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
|
||||
cli_message(base_path_does_not_exist_error.args[0])
|
||||
raise SystemExit(1) from base_path_does_not_exist_error
|
||||
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
|
||||
cli_message(template_file_not_found_error.args[0])
|
||||
raise SystemExit(1) from template_file_not_found_error
|
||||
except exceptions.MismatchedTokenError as mismatched_token_error:
|
||||
if config_scope["example"] is not None:
|
||||
cli_message(
|
||||
f'Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}. Example: {config_scope["example"]}'
|
||||
)
|
||||
raise SystemExit(1) from mismatched_token_error
|
||||
cli_message(
|
||||
f"Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}"
|
||||
)
|
||||
|
||||
raise SystemExit(1) from mismatched_token_error
|
||||
|
||||
|
||||
def _new_verify_name_exists(scope: str) -> None:
|
||||
_name_found = scope in [
|
||||
user_scope["name"] for user_scope in tembo.cli.CONFIG.scopes
|
||||
@@ -107,7 +139,7 @@ def _new_verify_name_exists(scope: str) -> None:
|
||||
return
|
||||
if len(tembo.cli.CONFIG.scopes) > 0:
|
||||
# if the name is missing in the config.yml, raise error
|
||||
raise exceptions.ScopeNotFound(f"Command {scope} not found in config.yml")
|
||||
raise exceptions.ScopeNotFound(f"Scope {scope} not found in config.yml")
|
||||
# raise error if no config.yml found
|
||||
if pathlib.Path(tembo.cli.CONFIG.config_path).exists():
|
||||
raise exceptions.EmptyConfigYML(
|
||||
@@ -158,51 +190,9 @@ def _new_show_example(example: bool, config_scope: dict) -> None:
|
||||
raise SystemExit(0)
|
||||
|
||||
|
||||
def _new_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page:
|
||||
page_creator_options = pages.PageCreatorOptions(
|
||||
base_path=tembo.cli.CONFIG.base_path,
|
||||
template_path=tembo.cli.CONFIG.template_path,
|
||||
page_path=config_scope["path"],
|
||||
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"],
|
||||
)
|
||||
try:
|
||||
return pages.ScopedPageCreator(page_creator_options).create_page()
|
||||
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
|
||||
cli_message(base_path_does_not_exist_error.args[0])
|
||||
raise SystemExit(1) from base_path_does_not_exist_error
|
||||
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
|
||||
cli_message(template_file_not_found_error.args[0])
|
||||
raise SystemExit(1) from template_file_not_found_error
|
||||
except exceptions.MismatchedTokenError as mismatched_token_error:
|
||||
if config_scope["example"] is not None:
|
||||
cli_message(
|
||||
f'Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}. Example: {config_scope["example"]}'
|
||||
)
|
||||
raise SystemExit(1) from mismatched_token_error
|
||||
cli_message(
|
||||
f'Your tembo config.yml/template specifies {mismatched_token_error.expected} input tokens, you gave {mismatched_token_error.given}'
|
||||
)
|
||||
|
||||
raise SystemExit(1) from mismatched_token_error
|
||||
|
||||
|
||||
def cli_message(message: str) -> None:
|
||||
click.echo(f"[TEMBO] {message} 🐘")
|
||||
|
||||
|
||||
run.add_command(new)
|
||||
run.add_command(list_all)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
new(["meeting", "a", "b", "c", "d"]) # noqa
|
||||
|
||||
# pyinstaller
|
||||
# if getattr(sys, "frozen", False):
|
||||
# run(sys.argv[1:])
|
||||
# run(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user