adding latest to CLI

This commit is contained in:
2021-11-01 20:36:16 +00:00
parent 4e20fdc2d1
commit 779e99434f
7 changed files with 54 additions and 52 deletions

View File

@@ -24,4 +24,4 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts] [tool.poetry.scripts]
"tembo" = "tembo.cli:run" "tembo" = "tembo.cli.cli:run"

View File

@@ -1,30 +1,3 @@
import os from .journal.pages import ScopedPageCreator, PageCreatorOptions
import panaetius
from panaetius.exceptions import LoggingDirectoryDoesNotExistException
__version__ = "0.1.0" __version__ = "0.1.0"
if (config_path := os.environ.get("TEMBO_CONFIG")) is not None:
CONFIG = panaetius.Config("tembo", config_path)
else:
CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True)
panaetius.set_config(CONFIG, "base_path", "~/tembo")
panaetius.set_config(CONFIG, "template_path", "~/tembo/.templates")
panaetius.set_config(CONFIG, "scopes", {})
panaetius.set_config(CONFIG, "logging.level", "DEBUG")
panaetius.set_config(CONFIG, "logging.path")
try:
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
except LoggingDirectoryDoesNotExistException:
_LOGGING_PATH = CONFIG.logging_path
CONFIG.logging_path = ""
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
logger.warning("Logging directory %s does not exist", _LOGGING_PATH)

30
tembo/cli/__init__.py Normal file
View File

@@ -0,0 +1,30 @@
import os
import panaetius
from panaetius.exceptions import LoggingDirectoryDoesNotExistException
__version__ = "0.1.0"
if (config_path := os.environ.get("TEMBO_CONFIG")) is not None:
CONFIG = panaetius.Config("tembo", config_path)
else:
CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True)
panaetius.set_config(CONFIG, "base_path", "~/tembo")
panaetius.set_config(CONFIG, "template_path", "~/tembo/.templates")
panaetius.set_config(CONFIG, "scopes", {})
panaetius.set_config(CONFIG, "logging.level", "DEBUG")
panaetius.set_config(CONFIG, "logging.path")
try:
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
except LoggingDirectoryDoesNotExistException:
_LOGGING_PATH = CONFIG.logging_path
CONFIG.logging_path = ""
logger = panaetius.set_logger(
CONFIG, panaetius.SimpleLogger(logging_level=CONFIG.logging_level)
)
logger.warning("Logging directory %s does not exist", _LOGGING_PATH)

View File

@@ -5,7 +5,7 @@ from typing import Collection
import click import click
import tembo import tembo.cli
from tembo.journal import pages from tembo.journal import pages
from tembo import exceptions from tembo import exceptions
@@ -30,8 +30,8 @@ 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.cli.CONFIG.scopes]
tembo.logger.info( tembo.cli.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)
) )
raise SystemExit(0) raise SystemExit(0)
@@ -85,21 +85,21 @@ def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
def _cli_verify_name_exists(scope: str) -> None: def _cli_verify_name_exists(scope: str) -> None:
_name_found = scope in [user_scope["name"] for user_scope in tembo.CONFIG.scopes] _name_found = scope in [user_scope["name"] for user_scope in tembo.cli.CONFIG.scopes]
if _name_found: if _name_found:
return return
if len(tembo.CONFIG.scopes) > 0: if len(tembo.cli.CONFIG.scopes) > 0:
# if the name is missing in the config.yml, raise error # if the name is missing in the config.yml, raise error
cli_message(f"Command {scope} not found in config.yml.") cli_message(f"Command {scope} not found in config.yml.")
raise SystemExit(0) raise SystemExit(0)
# raise error if no config.yml found # raise error if no config.yml found
if pathlib.Path(tembo.CONFIG.config_path).exists(): if pathlib.Path(tembo.cli.CONFIG.config_path).exists():
tembo.logger.critical( tembo.cli.logger.critical(
"Config.yml found in %s is empty - exiting", tembo.CONFIG.config_path "Config.yml found in %s is empty - exiting", tembo.cli.CONFIG.config_path
) )
else: else:
tembo.logger.critical( tembo.cli.logger.critical(
"No config.yml found in %s - exiting", tembo.CONFIG.config_path "No config.yml found in %s - exiting", tembo.cli.CONFIG.config_path
) )
raise SystemExit(1) raise SystemExit(1)
@@ -118,7 +118,7 @@ def _cli_get_config_scope(scope: str) -> dict:
config_scope.update( config_scope.update(
{ {
option: str(user_scope[option]) option: str(user_scope[option])
for user_scope in tembo.CONFIG.scopes for user_scope in tembo.cli.CONFIG.scopes
if user_scope["name"] == scope if user_scope["name"] == scope
} }
) )
@@ -126,7 +126,7 @@ def _cli_get_config_scope(scope: str) -> dict:
if key_error.args[0] in ["example", "template_filename"]: if key_error.args[0] in ["example", "template_filename"]:
config_scope.update({key_error.args[0]: None}) config_scope.update({key_error.args[0]: None})
continue continue
tembo.logger.critical( tembo.cli.logger.critical(
"Key %s not found in config. yml - exiting", key_error "Key %s not found in config. yml - exiting", key_error
) )
raise SystemExit(1) from key_error raise SystemExit(1) from key_error
@@ -135,7 +135,7 @@ def _cli_get_config_scope(scope: str) -> dict:
def _cli_show_example(example: bool, config_scope: dict) -> None: def _cli_show_example(example: bool, config_scope: dict) -> None:
if example: if example:
tembo.logger.info( tembo.cli.logger.info(
"Example for 'tembo new %s': %s", "Example for 'tembo new %s': %s",
config_scope["name"], config_scope["name"],
config_scope["example"] config_scope["example"]
@@ -147,7 +147,7 @@ def _cli_show_example(example: bool, config_scope: dict) -> None:
def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page: def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page:
page_creator_options = pages.PageCreatorOptions( page_creator_options = pages.PageCreatorOptions(
base_path=tembo.CONFIG.base_path, base_path=tembo.cli.CONFIG.base_path,
page_path=config_scope["path"], page_path=config_scope["path"],
filename=config_scope["filename"], filename=config_scope["filename"],
extension=config_scope["extension"], extension=config_scope["extension"],
@@ -155,26 +155,26 @@ def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> page
example=config_scope["example"], example=config_scope["example"],
user_input=inputs, user_input=inputs,
template_filename=config_scope["template_filename"], template_filename=config_scope["template_filename"],
template_path=tembo.CONFIG.template_path, template_path=tembo.cli.CONFIG.template_path,
) )
try: try:
return pages.ScopedPageCreator(page_creator_options).create_page() return pages.ScopedPageCreator(page_creator_options).create_page()
except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error: except exceptions.BasePathDoesNotExistError as base_path_does_not_exist_error:
tembo.logger.critical(base_path_does_not_exist_error) tembo.cli.logger.critical(base_path_does_not_exist_error)
raise SystemExit(1) from 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: except exceptions.TemplateFileNotFoundError as template_file_not_found_error:
tembo.logger.critical(template_file_not_found_error.args[0]) tembo.cli.logger.critical(template_file_not_found_error.args[0])
raise SystemExit(1) from template_file_not_found_error raise SystemExit(1) from template_file_not_found_error
except exceptions.MismatchedTokenError as mismatched_token_error: except exceptions.MismatchedTokenError as mismatched_token_error:
if config_scope["example"] is not None: if config_scope["example"] is not None:
tembo.logger.critical( tembo.cli.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s", "Your tembo config.yml/template specifies %s input tokens, you gave %s. Example: %s",
mismatched_token_error.expected, mismatched_token_error.expected,
mismatched_token_error.given, mismatched_token_error.given,
config_scope["example"], config_scope["example"],
) )
raise SystemExit(1) from mismatched_token_error raise SystemExit(1) from mismatched_token_error
tembo.logger.critical( tembo.cli.logger.critical(
"Your tembo config.yml/template specifies %s input tokens, you gave %s", "Your tembo config.yml/template specifies %s input tokens, you gave %s",
mismatched_token_error.expected, mismatched_token_error.expected,
mismatched_token_error.given, mismatched_token_error.given,
@@ -182,7 +182,6 @@ def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> page
raise SystemExit(1) from mismatched_token_error 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} 🐘")
@@ -193,7 +192,7 @@ run.add_command(list_all)
if __name__ == "__main__": if __name__ == "__main__":
# new(["meeting", "robs presentation", "meeting on gcp"]) # new(["meeting", "robs presentation", "meeting on gcp"])
new(["meeting", "a", "b", "c", "d"]) new(["meeting", "a", "b", "c", "d"]) # noqa
# new(["meeting", "robs presentation"]) # new(["meeting", "robs presentation"])
# pyinstaller # pyinstaller

View File

@@ -278,4 +278,4 @@ class ScopedPage(Page):
with scoped_page_file.open("w", encoding="utf-8") as scoped_page: with scoped_page_file.open("w", encoding="utf-8") as scoped_page:
scoped_page.write(self.page_content) scoped_page.write(self.page_content)
# TODO: pass this back somehow # TODO: pass this back somehow
tembo.logger.info("Saved %s to disk", self.path) tembo.cli.cli.logger.info("Saved %s to disk", self.path)

View File

@@ -3,7 +3,7 @@ import pathlib
import pytest import pytest
from tembo.journal.pages import PageCreatorOptions, ScopedPageCreator from tembo import PageCreatorOptions, ScopedPageCreator
from tembo import exceptions from tembo import exceptions