adding latest to CLI

This commit is contained in:
2021-11-01 20:21:30 +00:00
parent b114fa94bd
commit 4e20fdc2d1
3 changed files with 72 additions and 53 deletions

View File

@@ -1,4 +1,7 @@
from __future__ import annotations
import pathlib import pathlib
from typing import Collection
import click import click
@@ -26,7 +29,7 @@ def run():
@click.command(options_metavar="<options>", name="list") @click.command(options_metavar="<options>", name="list")
def list_all(): def list_all():
"""List all scopes defined in the config.yml""" """List all scopes defined in the config.yml."""
_all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes] _all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes]
tembo.logger.info( tembo.logger.info(
"%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes) "%s names found in config.yml: '%s'", len(_all_scopes), "', '".join(_all_scopes)
@@ -43,7 +46,7 @@ def list_all():
) )
@click.option("--dry-run", is_flag=True, default=False) @click.option("--dry-run", is_flag=True, default=False)
@click.option("--example", is_flag=True, default=False) @click.option("--example", is_flag=True, default=False)
def new(scope, inputs, dry_run, example): def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
r""" r"""
Create a new page. Create a new page.
@@ -57,61 +60,20 @@ def new(scope, inputs, dry_run, example):
Example: tembo new meeting my_presentation Example: tembo new meeting my_presentation
""" """
# get the name from the tembo config.yml # check that the name exists in the config.yml
_cli_verify_name_exists(scope) _cli_verify_name_exists(scope)
# get the scope information from the tembo config.yml # get the scope configuration from the config.yml
config_scope = _cli_get_config_scope(scope) config_scope = _cli_get_config_scope(scope)
# print the example to the user # if --example flag, return the example to the user
if example: _cli_show_example(example, config_scope)
tembo.logger.info(
"Example for 'tembo new %s': %s",
config_scope["name"],
config_scope["example"]
if isinstance(config_scope["example"], str)
else "No example in config.yml",
)
raise SystemExit(0)
# if the name is in the config.yml, create the scoped page # if the name is in the config.yml, create the scoped page
page_creator_options = pages.PageCreatorOptions( scoped_page = _cli_create_scoped_page(config_scope, inputs)
base_path=tembo.CONFIG.base_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"],
template_path=tembo.CONFIG.template_path,
)
try:
scoped_page = pages.ScopedPageCreator(page_creator_options).create_page()
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
tembo.logger.critical(base_path_does_not_exist_error)
raise SystemExit(1) from base_path_does_not_exist_error
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
tembo.logger.critical(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:
tembo.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s",
mismatched_token_error.expected,
mismatched_token_error.given,
config_scope["example"],
)
raise SystemExit(1) from mismatched_token_error
tembo.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s",
mismatched_token_error.expected,
mismatched_token_error.given,
)
raise SystemExit(1) from mismatched_token_error
if dry_run: if dry_run:
click.echo(cli_message(f"{scoped_page.path} will be created")) cli_message(f"{scoped_page.path} will be created")
raise SystemExit(0) raise SystemExit(0)
try: try:
@@ -123,9 +85,7 @@ def new(scope, inputs, dry_run, example):
def _cli_verify_name_exists(scope: str) -> None: def _cli_verify_name_exists(scope: str) -> None:
_name_found = scope in [ _name_found = scope in [user_scope["name"] for user_scope in tembo.CONFIG.scopes]
user_scope["name"] for user_scope in tembo.CONFIG.scopes
]
if _name_found: if _name_found:
return return
if len(tembo.CONFIG.scopes) > 0: if len(tembo.CONFIG.scopes) > 0:
@@ -173,6 +133,56 @@ def _cli_get_config_scope(scope: str) -> dict:
return config_scope return config_scope
def _cli_show_example(example: bool, config_scope: dict) -> None:
if example:
tembo.logger.info(
"Example for 'tembo new %s': %s",
config_scope["name"],
config_scope["example"]
if isinstance(config_scope["example"], str)
else "No example in config.yml",
)
raise SystemExit(0)
def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page:
page_creator_options = pages.PageCreatorOptions(
base_path=tembo.CONFIG.base_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"],
template_path=tembo.CONFIG.template_path,
)
try:
return pages.ScopedPageCreator(page_creator_options).create_page()
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
tembo.logger.critical(base_path_does_not_exist_error)
raise SystemExit(1) from base_path_does_not_exist_error
except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
tembo.logger.critical(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:
tembo.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s",
mismatched_token_error.expected,
mismatched_token_error.given,
config_scope["example"],
)
raise SystemExit(1) from mismatched_token_error
tembo.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s",
mismatched_token_error.expected,
mismatched_token_error.given,
)
raise SystemExit(1) from mismatched_token_error
def cli_message(message: str) -> None: def cli_message(message: str) -> None:
click.echo(f"[TEMBO] {message} 🐘") click.echo(f"[TEMBO] {message} 🐘")

0
tembo/journal/cli/new.py Normal file
View File

View File

@@ -216,6 +216,11 @@ class Page(metaclass=ABCMeta):
def __init__(self, path: pathlib.Path, page_content: str) -> None: def __init__(self, path: pathlib.Path, page_content: str) -> None:
raise NotImplementedError raise NotImplementedError
@property
@abstractmethod
def path(self) -> pathlib.Path:
raise NotImplementedError
@abstractmethod @abstractmethod
def save_to_disk(self) -> None: def save_to_disk(self) -> None:
raise NotImplementedError raise NotImplementedError
@@ -236,12 +241,16 @@ class ScopedPage(Page):
path (pathlib.Path): a `pathlib.Path` object of the page's filepath. path (pathlib.Path): a `pathlib.Path` object of the page's filepath.
page_content (str): the content of the page from the template. page_content (str): the content of the page from the template.
""" """
self.path = path self._path = path
self.page_content = page_content self.page_content = page_content
def __str__(self) -> str: def __str__(self) -> str:
return f"ScopedPage({self.path})" return f"ScopedPage({self.path})"
@property
def path(self) -> pathlib.Path:
return self._path
def save_to_disk(self) -> None: def save_to_disk(self) -> None:
"""Save the scoped page to disk and write the `page_content`. """Save the scoped page to disk and write the `page_content`.