mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 11:35:44 +00:00
adding --dry-run
This commit is contained in:
@@ -21,7 +21,8 @@ def run():
|
|||||||
nargs=-1,
|
nargs=-1,
|
||||||
metavar="<inputs>",
|
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.
|
Create a new note.
|
||||||
|
|
||||||
@@ -42,8 +43,8 @@ def new(scope, inputs):
|
|||||||
user_input=inputs,
|
user_input=inputs,
|
||||||
template_filename=str(user_scope["template_filename"]),
|
template_filename=str(user_scope["template_filename"]),
|
||||||
)
|
)
|
||||||
scoped_page.save_to_disk()
|
scoped_page.save_to_disk(dry_run=dry_run)
|
||||||
tembo.logger.info("Saved %s to disk", scoped_page)
|
tembo.logger.info("Saved %s to disk", scoped_page.path)
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
tembo.logger.critical(
|
tembo.logger.critical(
|
||||||
"No config.yml found in %s - exiting", tembo.CONFIG.config_path
|
"No config.yml found in %s - exiting", tembo.CONFIG.config_path
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ class PageCreator:
|
|||||||
name: str,
|
name: str,
|
||||||
user_input: tuple[str, ...] | None,
|
user_input: tuple[str, ...] | None,
|
||||||
template_filename: str | None = None,
|
template_filename: str | None = None,
|
||||||
|
dry_run: bool = False
|
||||||
) -> Page:
|
) -> Page:
|
||||||
pass
|
raise NotImplementedError
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _convert_to_path(
|
def _convert_to_path(
|
||||||
@@ -30,7 +31,7 @@ class PageCreator:
|
|||||||
) -> pathlib.Path:
|
) -> pathlib.Path:
|
||||||
# check if Tembo base path exists
|
# check if Tembo base path exists
|
||||||
if not pathlib.Path(base_path).expanduser().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)
|
raise SystemExit(1)
|
||||||
path_to_file = (
|
path_to_file = (
|
||||||
pathlib.Path(base_path).expanduser()
|
pathlib.Path(base_path).expanduser()
|
||||||
@@ -52,7 +53,7 @@ class PageCreator:
|
|||||||
template_path = self._convert_to_path("", CONFIG.template_path, "", "")
|
template_path = self._convert_to_path("", CONFIG.template_path, "", "")
|
||||||
else:
|
else:
|
||||||
# default template_path is base_path / templates
|
# 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
|
# load the template folder
|
||||||
file_loader = jinja2.FileSystemLoader(template_path)
|
file_loader = jinja2.FileSystemLoader(template_path)
|
||||||
env = jinja2.Environment(loader=file_loader, autoescape=True)
|
env = jinja2.Environment(loader=file_loader, autoescape=True)
|
||||||
@@ -113,7 +114,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def __substitute_name_tokens(tokenified_string: str, name: str) -> str:
|
def __substitute_name_tokens(tokenified_string: str, name: str) -> str:
|
||||||
# find any {name} tokens and substitute for the name value
|
# 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:
|
for extracted_input in name_extraction:
|
||||||
tokenified_string = tokenified_string.replace(extracted_input, name)
|
tokenified_string = tokenified_string.replace(extracted_input, name)
|
||||||
return tokenified_string
|
return tokenified_string
|
||||||
@@ -135,7 +136,7 @@ class ScopedPageCreator(PageCreator):
|
|||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
# if there aren't any tokens in the string, return the string
|
# if there aren't any tokens in the string, return the string
|
||||||
return tokenified_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 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
|
# if there are input matches and they don't equal the number of input
|
||||||
# tokens, raise error
|
# tokens, raise error
|
||||||
@@ -169,34 +170,36 @@ class ScopedPageCreator(PageCreator):
|
|||||||
|
|
||||||
class Page(metaclass=ABCMeta):
|
class Page(metaclass=ABCMeta):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __init__(self, path: pathlib.Path, page_content: str) -> None:
|
def __init__(self, path: pathlib.Path, page_content: str, dry_run: bool) -> None:
|
||||||
pass
|
raise NotImplementedError
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def save_to_disk(self) -> None:
|
def save_to_disk(self, dry_run: bool) -> None:
|
||||||
pass
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class ScopedPage(Page):
|
class ScopedPage(Page):
|
||||||
"""A Page that uses substitute tokens."""
|
"""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.path = path
|
||||||
self.page_content = page_content
|
self.page_content = page_content
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return f"ScopedPage({self.path})"
|
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)
|
scoped_note_file = pathlib.Path(self.path)
|
||||||
# create the parent directories
|
# create the parent directories
|
||||||
scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True)
|
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():
|
if not scoped_note_file.exists():
|
||||||
with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
|
with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
|
||||||
scoped_page.write(self.page_content)
|
scoped_page.write(self.page_content)
|
||||||
else:
|
else:
|
||||||
logger.info("%s already exists - skipping.", str(self))
|
logger.info("%s already exists - skipping.", self.path)
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user