diff --git a/pyproject.toml b/pyproject.toml index c3e2c86..c2626b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,4 +24,4 @@ requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] -"tembo" = "tembo.cli:run" +"tembo" = "tembo.cli.cli:run" diff --git a/tembo/__init__.py b/tembo/__init__.py index c688a8d..4cd6b98 100644 --- a/tembo/__init__.py +++ b/tembo/__init__.py @@ -1,30 +1,3 @@ -import os - -import panaetius -from panaetius.exceptions import LoggingDirectoryDoesNotExistException +from .journal.pages import ScopedPageCreator, PageCreatorOptions __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) diff --git a/tembo/cli/__init__.py b/tembo/cli/__init__.py new file mode 100644 index 0000000..c688a8d --- /dev/null +++ b/tembo/cli/__init__.py @@ -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) diff --git a/tembo/journal/cli/cli.py b/tembo/cli/cli.py similarity index 85% rename from tembo/journal/cli/cli.py rename to tembo/cli/cli.py index 52561d1..8a59c5d 100644 --- a/tembo/journal/cli/cli.py +++ b/tembo/cli/cli.py @@ -5,7 +5,7 @@ from typing import Collection import click -import tembo +import tembo.cli from tembo.journal import pages from tembo import exceptions @@ -30,8 +30,8 @@ def run(): @click.command(options_metavar="", name="list") def list_all(): """List all scopes defined in the config.yml.""" - _all_scopes = [user_scope["name"] for user_scope in tembo.CONFIG.scopes] - tembo.logger.info( + _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) ) 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: - _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: 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 cli_message(f"Command {scope} not found in config.yml.") raise SystemExit(0) # raise error if no config.yml found - if pathlib.Path(tembo.CONFIG.config_path).exists(): - tembo.logger.critical( - "Config.yml found in %s is empty - exiting", tembo.CONFIG.config_path + if pathlib.Path(tembo.cli.CONFIG.config_path).exists(): + tembo.cli.logger.critical( + "Config.yml found in %s is empty - exiting", tembo.cli.CONFIG.config_path ) else: - tembo.logger.critical( - "No config.yml found in %s - exiting", tembo.CONFIG.config_path + tembo.cli.logger.critical( + "No config.yml found in %s - exiting", tembo.cli.CONFIG.config_path ) raise SystemExit(1) @@ -118,7 +118,7 @@ def _cli_get_config_scope(scope: str) -> dict: config_scope.update( { 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 } ) @@ -126,7 +126,7 @@ def _cli_get_config_scope(scope: str) -> dict: if key_error.args[0] in ["example", "template_filename"]: config_scope.update({key_error.args[0]: None}) continue - tembo.logger.critical( + tembo.cli.logger.critical( "Key %s not found in config. yml - exiting", 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: if example: - tembo.logger.info( + tembo.cli.logger.info( "Example for 'tembo new %s': %s", config_scope["name"], 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: page_creator_options = pages.PageCreatorOptions( - base_path=tembo.CONFIG.base_path, + base_path=tembo.cli.CONFIG.base_path, page_path=config_scope["path"], filename=config_scope["filename"], extension=config_scope["extension"], @@ -155,26 +155,26 @@ def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> page example=config_scope["example"], user_input=inputs, template_filename=config_scope["template_filename"], - template_path=tembo.CONFIG.template_path, + template_path=tembo.cli.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) + tembo.cli.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]) + tembo.cli.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( + tembo.cli.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( + tembo.cli.logger.critical( "Your tembo config.yml/template specifies %s input tokens, you gave %s", mismatched_token_error.expected, 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 - def cli_message(message: str) -> None: click.echo(f"[TEMBO] {message} 🐘") @@ -193,7 +192,7 @@ run.add_command(list_all) if __name__ == "__main__": # new(["meeting", "robs presentation", "meeting on gcp"]) - new(["meeting", "a", "b", "c", "d"]) + new(["meeting", "a", "b", "c", "d"]) # noqa # new(["meeting", "robs presentation"]) # pyinstaller diff --git a/tembo/journal/cli/new.py b/tembo/journal/cli/new.py deleted file mode 100644 index e69de29..0000000 diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index 536b124..417f969 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -278,4 +278,4 @@ class ScopedPage(Page): with scoped_page_file.open("w", encoding="utf-8") as scoped_page: scoped_page.write(self.page_content) # 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) diff --git a/tests/test_journal/test_pages.py b/tests/test_journal/test_pages.py index 7cf101a..7164fcc 100644 --- a/tests/test_journal/test_pages.py +++ b/tests/test_journal/test_pages.py @@ -3,7 +3,7 @@ import pathlib import pytest -from tembo.journal.pages import PageCreatorOptions, ScopedPageCreator +from tembo import PageCreatorOptions, ScopedPageCreator from tembo import exceptions