adding latest

This commit is contained in:
2021-11-03 13:46:58 +00:00
parent 779e99434f
commit 1cc68834f0
9 changed files with 156 additions and 50 deletions

View File

@@ -6,7 +6,7 @@ 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)
CONFIG = panaetius.Config("tembo", config_path, skip_header_init=True)
else:
CONFIG = panaetius.Config("tembo", "~/tembo/.config", skip_header_init=True)

View File

@@ -7,6 +7,7 @@ import click
import tembo.cli
from tembo.journal import pages
from tembo.utils import Success
from tembo import exceptions
@@ -61,57 +62,71 @@ def new(scope: str, inputs: Collection[str], dry_run: bool, example: bool):
"""
# check that the name exists in the config.yml
_cli_verify_name_exists(scope)
try:
_new_verify_name_exists(scope)
except (
exceptions.ScopeNotFound,
exceptions.EmptyConfigYML,
exceptions.MissingConfigYML,
) as tembo_exception:
cli_message(tembo_exception.args[0])
raise SystemExit(0) from tembo_exception
# get the scope configuration from the config.yml
config_scope = _cli_get_config_scope(scope)
try:
config_scope = _new_get_config_scope(scope)
except exceptions.MandatoryKeyNotFound as mandatory_key_not_found:
cli_message(mandatory_key_not_found.args[0])
raise SystemExit(1) from mandatory_key_not_found
# if --example flag, return the example to the user
_cli_show_example(example, config_scope)
_new_show_example(example, config_scope)
# if the name is in the config.yml, create the scoped page
scoped_page = _cli_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")
raise SystemExit(0)
try:
scoped_page.save_to_disk()
result = scoped_page.save_to_disk()
if isinstance(result, Success):
cli_message(f"Saved {result.message} to disk")
raise SystemExit(0)
except exceptions.ScopedPageAlreadyExists as scoped_page_already_exists:
cli_message(f"File {scoped_page_already_exists}")
raise SystemExit(0) from scoped_page_already_exists
def _cli_verify_name_exists(scope: str) -> None:
_name_found = scope in [user_scope["name"] for user_scope in tembo.cli.CONFIG.scopes]
def _new_verify_name_exists(scope: str) -> None:
_name_found = scope in [
user_scope["name"] for user_scope in tembo.cli.CONFIG.scopes
]
if _name_found:
return
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 exceptions.ScopeNotFound(f"Command {scope} not found in config.yml")
# raise error if no config.yml found
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
raise exceptions.EmptyConfigYML(
f"Config.yml found in {tembo.cli.CONFIG.config_path} is empty"
)
else:
tembo.cli.logger.critical(
"No config.yml found in %s - exiting", tembo.cli.CONFIG.config_path
)
raise SystemExit(1)
raise exceptions.MissingConfigYML(
f"No config.yml found in {tembo.cli.CONFIG.config_path}"
)
def _cli_get_config_scope(scope: str) -> dict:
def _new_get_config_scope(scope: str) -> dict:
config_scope = {}
optional_keys = ["example", "template_filename"]
for option in [
"name",
"example",
"path",
"filename",
"extension",
"example",
"template_filename",
]:
try:
@@ -123,17 +138,19 @@ def _cli_get_config_scope(scope: str) -> dict:
}
)
except KeyError as key_error:
if key_error.args[0] in ["example", "template_filename"]:
if key_error.args[0] in optional_keys:
config_scope.update({key_error.args[0]: None})
continue
tembo.cli.logger.critical(
"Key %s not found in config. yml - exiting", key_error
)
raise SystemExit(1) from key_error
raise exceptions.MandatoryKeyNotFound(
f"Key {key_error} not found in config.yml"
)
return config_scope
def _cli_show_example(example: bool, config_scope: dict) -> None:
def _new_show_example(example: bool, config_scope: dict) -> None:
if example:
tembo.cli.logger.info(
"Example for 'tembo new %s': %s",
@@ -145,9 +162,10 @@ def _cli_show_example(example: bool, config_scope: dict) -> None:
raise SystemExit(0)
def _cli_create_scoped_page(config_scope: dict, inputs: Collection[str]) -> pages.Page:
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"],
@@ -155,7 +173,6 @@ 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.cli.CONFIG.template_path,
)
try:
return pages.ScopedPageCreator(page_creator_options).create_page()
@@ -191,9 +208,7 @@ run.add_command(list_all)
if __name__ == "__main__":
# new(["meeting", "robs presentation", "meeting on gcp"])
new(["meeting", "a", "b", "c", "d"]) # noqa
# new(["meeting", "robs presentation"])
# pyinstaller
# if getattr(sys, "frozen", False):

View File

@@ -18,3 +18,19 @@ class TemplateFileNotFoundError(Exception):
class ScopedPageAlreadyExists(Exception):
"""Raised if the scoped page file already exists."""
class MissingConfigYML(Exception):
"""Raised if the config.yml file is missing."""
class EmptyConfigYML(Exception):
"""Raised if the config.yml file is empty."""
class ScopeNotFound(Exception):
"""Raised if the scope does not exist in the config.yml."""
class MandatoryKeyNotFound(Exception):
"""Raised if a mandatory key is not found in the config.yml."""

View File

@@ -12,6 +12,7 @@ from jinja2.exceptions import TemplateNotFound
import tembo
from tembo import exceptions
import tembo.utils
# TODO: flesh this out with details for the optional args
@@ -222,7 +223,7 @@ class Page(metaclass=ABCMeta):
raise NotImplementedError
@abstractmethod
def save_to_disk(self) -> None:
def save_to_disk(self) -> tembo.utils.Success:
raise NotImplementedError
@@ -245,13 +246,13 @@ class ScopedPage(Page):
self.page_content = page_content
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) -> tembo.utils.Success:
"""Save the scoped page to disk and write the `page_content`.
If the page already exists a message will be logged to stdout and no file
@@ -274,8 +275,6 @@ class ScopedPage(Page):
scoped_page_file.parents[0].mkdir(parents=True, exist_ok=True)
if scoped_page_file.exists():
raise exceptions.ScopedPageAlreadyExists(f"{self.path} already exists")
with scoped_page_file.open("w", encoding="utf-8") as scoped_page:
scoped_page.write(self.page_content)
# TODO: pass this back somehow
tembo.cli.cli.logger.info("Saved %s to disk", self.path)
return tembo.utils.Success(str(self.path))

12
tembo/utils/__init__.py Normal file
View File

@@ -0,0 +1,12 @@
from dataclasses import dataclass
@dataclass
class Success:
"""Success message.
Attributes:
message (str): A success message.
"""
message: str