adding --dry-run

This commit is contained in:
2021-10-24 01:21:54 +01:00
parent 65f6a7658b
commit 118a85e6fc
2 changed files with 21 additions and 17 deletions

View File

@@ -21,7 +21,8 @@ def run():
nargs=-1,
metavar="<inputs>",
)
def new(scope, inputs):
@click.option("--dry-run", is_flag=True, default=False)
def new(scope, inputs, dry_run):
"""
Create a new note.
@@ -42,8 +43,8 @@ def new(scope, inputs):
user_input=inputs,
template_filename=str(user_scope["template_filename"]),
)
scoped_page.save_to_disk()
tembo.logger.info("Saved %s to disk", scoped_page)
scoped_page.save_to_disk(dry_run=dry_run)
tembo.logger.info("Saved %s to disk", scoped_page.path)
raise SystemExit(0)
tembo.logger.critical(
"No config.yml found in %s - exiting", tembo.CONFIG.config_path

View File

@@ -21,8 +21,9 @@ class PageCreator:
name: str,
user_input: tuple[str, ...] | None,
template_filename: str | None = None,
dry_run: bool = False
) -> Page:
pass
raise NotImplementedError
@staticmethod
def _convert_to_path(
@@ -30,7 +31,7 @@ class PageCreator:
) -> pathlib.Path:
# check if Tembo base path exists
if not pathlib.Path(base_path).expanduser().exists():
logger.critical("base path of %s does not exist - exiting", base_path)
logger.critical("Tembo base path of %s does not exist - exiting", base_path)
raise SystemExit(1)
path_to_file = (
pathlib.Path(base_path).expanduser()
@@ -52,7 +53,7 @@ class PageCreator:
template_path = self._convert_to_path("", CONFIG.template_path, "", "")
else:
# default template_path is base_path / templates
template_path = self._convert_to_path(base_path, "templates", "", "")
template_path = self._convert_to_path(base_path, ".templates", "", "")
# load the template folder
file_loader = jinja2.FileSystemLoader(template_path)
env = jinja2.Environment(loader=file_loader, autoescape=True)
@@ -113,7 +114,7 @@ class ScopedPageCreator(PageCreator):
@staticmethod
def __substitute_name_tokens(tokenified_string: str, name: str) -> str:
# find any {name} tokens and substitute for the name value
name_extraction = re.findall(r"(\{name\d*\})", tokenified_string)
name_extraction = re.findall(r"(\{name\})", tokenified_string)
for extracted_input in name_extraction:
tokenified_string = tokenified_string.replace(extracted_input, name)
return tokenified_string
@@ -135,7 +136,7 @@ class ScopedPageCreator(PageCreator):
raise SystemExit(1)
# if there aren't any tokens in the string, return the string
return tokenified_string
# if there is user input, check the number of tokens match what's passed in
# if there is user input, check the number of tokens match the number passed in
if len(input_extraction) > 0 and len(input_extraction) != len(user_input):
# if there are input matches and they don't equal the number of input
# tokens, raise error
@@ -169,34 +170,36 @@ class ScopedPageCreator(PageCreator):
class Page(metaclass=ABCMeta):
@abstractmethod
def __init__(self, path: pathlib.Path, page_content: str) -> None:
pass
def __init__(self, path: pathlib.Path, page_content: str, dry_run: bool) -> None:
raise NotImplementedError
@abstractmethod
def save_to_disk(self) -> None:
pass
def save_to_disk(self, dry_run: bool) -> None:
raise NotImplementedError
class ScopedPage(Page):
"""A Page that uses substitute tokens."""
def __init__(self, path: pathlib.Path, page_content: str):
def __init__(self, path: pathlib.Path, page_content: str) -> None:
self.path = path
self.page_content = page_content
def __str__(self):
def __str__(self) -> str:
return f"ScopedPage({self.path})"
def save_to_disk(self) -> None:
def save_to_disk(self, dry_run: bool = False) -> None:
scoped_note_file = pathlib.Path(self.path)
# create the parent directories
scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True)
if dry_run:
logger.info("%s will be created", self.path)
raise SystemExit(0)
if not scoped_note_file.exists():
with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
scoped_page.write(self.page_content)
else:
logger.info("%s already exists - skipping.", str(self))
logger.info("%s already exists - skipping.", self.path)
raise SystemExit(0)