adding latest

This commit is contained in:
2021-11-01 00:41:01 +00:00
parent 413f783475
commit 526dd733b5
12 changed files with 270 additions and 44 deletions

View File

@@ -39,12 +39,16 @@ Documentation:
☐ Document using datadir with a module rather than a shared one. Link to tembo as an example. ☐ Document using datadir with a module rather than a shared one. Link to tembo as an example.
☐ Can prospector ignore tests dir? document this in the gist if so ☐ Can prospector ignore tests dir? document this in the gist if so
☐ Redo the documentation on a CLI, reorganise and inocropoate all the new tembo layouts
Functionality: Functionality:
☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages? ☐ Replace loggers with `click.echo` for command outputs. Keep logging messages for actual logging messages?
Define a format: [TEMBO:$datetime] $message 🐘 - document this in general python for CLI
☐ Refactor the tembo new command so the cli is split out into manageable methods ☐ Refactor the tembo new command so the cli is split out into manageable methods
☐ Use the complicated CLI example so the tembo new has its own module to define functions in ☐ Use the complicated CLI example so the tembo new has its own module to define functions in
☐ Replace all logger errors with exceptions, move logger messages to the cli. ☐ Replace all logger errors with exceptions, move logger messages to the cli.
☐ How to pass a successful save notification back to the CLI? Return a bool? Or is there some other way?
☐ Replace pendulum with datetime
✔ Make options a property on the class, add to abstract @done(21-10-30 19:31) ✔ Make options a property on the class, add to abstract @done(21-10-30 19:31)
☐ Use the python runner Duty ☐ Use the python runner Duty
<https://github.com/pawamoy/duty> <https://github.com/pawamoy/duty>
@@ -53,6 +57,10 @@ Functionality:
☐ Build docs ☐ Build docs
☐ Document using Duty ☐ Document using Duty
Logging:
☐ Make all internal tembo logs be debug
☐ User can enable them with the config
VSCode: VSCode:
PyInstaller: PyInstaller:
☐ Document build error: <https://github.com/pyenv/pyenv/issues/1095> ☐ Document build error: <https://github.com/pyenv/pyenv/issues/1095>

View File

@@ -1,22 +1,25 @@
# testing notes # testing notes
## options
optional: optional:
- user_input - user_input
- example - example
- template_filename - template_filename
- template_path - template_path
required: required:
- base_path - base_path
- page_path - page_path
- filename - filename
- extension - extension
- name - name
## tests to write
- page with/without a template
- user input is None - user input is None
- the given base path does not exist
- page using/not using input tokens - page using/not using input tokens
- user input does not match number of input tokens - user input does not match number of input tokens
- no user input - no user input
@@ -24,34 +27,13 @@ required:
- with/without example - with/without example
- page using/not using date tokens - page using/not using date tokens
- page using/not using name tokens - page using/not using name tokens
- dry run
- path/page filenames can contain spaces and they are converted - path/page filenames can contain spaces and they are converted
## tests done
- page with/without a template
@dataclass - the given base path does not exist
class PageCreatorOptions: - the given template file does not exist
"""Options dataclass to create a Page. - page already exists
Attributes:
base_path (str):
page_path (str):
filename (str):
extension (str):
name (str):
user_input (Collection[str] | None, optional):
example (str | None, optional):
template_filename (str | None, optional):
template_path (str | None, optional):
"""
base_path: str
page_path: str
filename: str
extension: str
name: str
user_input: Collection[str] | None = None
example: str | None = None
template_filename: str | None = None
template_path: str | None = None

View File

@@ -122,8 +122,12 @@ def new(scope, inputs, dry_run, example):
tembo.logger.critical(template_file_not_found_error.args[0]) tembo.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
scoped_page.save_to_disk(dry_run=dry_run) try:
raise SystemExit(0) scoped_page.save_to_disk(dry_run=dry_run)
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
if not _name_found and len(tembo.CONFIG.scopes) > 0: if not _name_found and len(tembo.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
tembo.logger.warning("Command %s not found in config.yml - exiting", scope) tembo.logger.warning("Command %s not found in config.yml - exiting", scope)
@@ -136,6 +140,10 @@ def new(scope, inputs, dry_run, example):
raise SystemExit(1) raise SystemExit(1)
def cli_message(message: str) -> None:
click.echo(f"[TEMBO] {message} 🐘")
run.add_command(new) run.add_command(new)
run.add_command(list_all) run.add_command(list_all)

View File

@@ -11,3 +11,7 @@ class BasePathDoesNotExistError(Exception):
class TemplateFileNotFoundError(Exception): class TemplateFileNotFoundError(Exception):
"""Raised if the template file does not exist.""" """Raised if the template file does not exist."""
class ScopedPageAlreadyExists(Exception):
"""Raised if the scoped page file already exists."""

View File

@@ -74,7 +74,8 @@ class PageCreator:
else self.options.extension else self.options.extension
) )
except IndexError: except IndexError:
# REVIEW: can this be removed now this is not called anywhere else? # REVIEW: try putting a . in the config yaml and see what error is raised
# this is no longer generic it just gets the full path to the file.
# IndexError means the path is not a file, just a path # IndexError means the path is not a file, just a path
return path_to_file return path_to_file
# return path with a file # return path with a file
@@ -88,7 +89,9 @@ class PageCreator:
self.options.template_path self.options.template_path
).expanduser() ).expanduser()
else: else:
converted_template_path = pathlib.Path() converted_template_path = (
pathlib.Path(self.options.base_path).expanduser() / ".templates"
)
file_loader = jinja2.FileSystemLoader(converted_template_path) file_loader = jinja2.FileSystemLoader(converted_template_path)
env = jinja2.Environment(loader=file_loader, autoescape=True) env = jinja2.Environment(loader=file_loader, autoescape=True)
@@ -277,19 +280,21 @@ class ScopedPage(Page):
SystemExit: Exit code 0 if dry run is `True`, page is successfully saved SystemExit: Exit code 0 if dry run is `True`, page is successfully saved
or if page already exists. or if page already exists.
""" """
# TODO: move this functionality to the CLI so the page is created and the message
# returned to the user from the CLI.
if dry_run: if dry_run:
tembo.logger.info("%s will be created", self.path) tembo.logger.info("%s will be created", self.path)
raise SystemExit(0) raise SystemExit(0)
# create the parent directories # create the parent directories
scoped_note_file = pathlib.Path(self.path) scoped_note_file = pathlib.Path(self.path)
scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True) scoped_note_file.parents[0].mkdir(parents=True, exist_ok=True)
if not scoped_note_file.exists(): if scoped_note_file.exists():
with scoped_note_file.open("w", encoding="utf-8") as scoped_page: raise exceptions.ScopedPageAlreadyExists(f"{self.path} already exists")
scoped_page.write(self.page_content)
tembo.logger.info("Saved %s to disk", self.path) with scoped_note_file.open("w", encoding="utf-8") as scoped_page:
else: scoped_page.write(self.page_content)
tembo.logger.info("%s already exists - skipping.", self.path) # TODO: pass this back somehow
raise SystemExit(0) tembo.logger.info("Saved %s to disk", self.path)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -1,7 +1,13 @@
from datetime import date
import pathlib
import pytest import pytest
from tembo.journal.pages import PageCreatorOptions, ScopedPageCreator from tembo.journal.pages import PageCreatorOptions, ScopedPageCreator
from tembo.exceptions import BasePathDoesNotExistError from tembo import exceptions
DATE_TODAY = date.today().strftime("%d-%m-%Y")
def test_create_page_base_path_does_not_exist(tmpdir): def test_create_page_base_path_does_not_exist(tmpdir):
@@ -20,11 +26,218 @@ def test_create_page_base_path_does_not_exist(tmpdir):
) )
# act # act
with pytest.raises(BasePathDoesNotExistError) as base_path_does_not_exist_error: with pytest.raises(
scoped_page_creator = ScopedPageCreator().create_page(options) exceptions.BasePathDoesNotExistError
) as base_path_does_not_exist_error:
scoped_page = ScopedPageCreator().create_page(options)
# assert # assert
assert ( assert (
str(base_path_does_not_exist_error.value) str(base_path_does_not_exist_error.value)
== f"Tembo base path of {base_path} does not exist." == f"Tembo base path of {base_path} does not exist."
) )
@pytest.mark.parametrize("template_path", [(None), ("/nonexistent/path")])
def test_create_page_template_file_does_not_exist(template_path, tmpdir):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="",
filename="",
extension="",
name="",
user_input=None,
example=None,
template_filename="template.md.tpl",
template_path=template_path,
)
# act
with pytest.raises(
exceptions.TemplateFileNotFoundError
) as template_file_not_found_error:
scoped_page = ScopedPageCreator().create_page(options)
# assert
if template_path is None:
assert str(template_file_not_found_error.value) == (
f"Template file {options.base_path}/.templates/{options.template_filename} does not exist."
)
else:
assert str(template_file_not_found_error.value) == (
f"Template file {template_path}/{options.template_filename} does not exist."
)
def test_create_page_already_exists(datadir):
# arrange
options = PageCreatorOptions(
base_path=str(datadir),
page_path="does_exist",
filename="some_note",
extension="md",
name="some_name",
user_input=None,
example=None,
template_filename=None,
template_path=None,
)
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / options.filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator().create_page(options)
with pytest.raises(exceptions.ScopedPageAlreadyExists) as page_already_exists:
scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert str(page_already_exists.value) == f"{scoped_page_file} already exists"
with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readlines() == ["this file already exists\n"]
def test_create_page_without_template(tmpdir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="some_filename",
extension="some_extension",
name="some_name",
user_input=None,
example=None,
template_filename=None,
template_path=None,
)
# TODO: copy this pattern creation into the other tests
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / options.filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator().create_page(options)
scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert caplog.records[0].message == f"Saved {scoped_page_file} to disk"
with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readlines() == []
def test_create_page_with_template(datadir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(datadir),
page_path="some_path",
filename="some_note",
extension="md",
name="some_name",
user_input=None,
example=None,
template_filename="some_template_no_tokens.md.tpl",
template_path=None,
)
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / options.filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator().create_page(options)
scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert caplog.records[0].message == f"Saved {scoped_page_file} to disk"
with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readlines() == [
"scoped page file\n",
"\n",
"no tokens",
]
@pytest.mark.parametrize(
"user_input,template_filename,page_contents",
[
(None, "some_template_date_tokens.md.tpl", f"some date token: {DATE_TODAY}"),
(
("first_input", "second_input"),
"some_template_input_tokens.md.tpl",
"some input tokens second_input first_input",
),
(None, "some_template_name_tokens.md.tpl", "some name token some_name"),
],
)
def test_create_tokened_page_tokens_in_template(
datadir, caplog, user_input, template_filename, page_contents
):
# arrange
options = PageCreatorOptions(
base_path=str(datadir),
page_path="some_path",
filename="some_note",
extension="md",
name="some_name",
user_input=user_input,
example=None,
template_filename=template_filename,
template_path=None,
)
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / options.filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator().create_page(options)
scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert caplog.records[0].message == f"Saved {scoped_page_file} to disk"
with scoped_page_file.open("r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readline() == page_contents
@pytest.mark.parametrize(
"user_input,filename,tokened_filename",
[
(None, "date_token_{d:%d-%m-%Y}", f"date_token_{DATE_TODAY}"),
(None, "name_token_{name}", "name_token_some_name"),
(
("first_input", "second input"),
"input_token_{input1}_{input0}",
"input_token_second_input_first_input",
),
],
)
def test_create_tokened_page_tokens_in_filename(
datadir, caplog, user_input, filename, tokened_filename
):
# arrange
options = PageCreatorOptions(
base_path=str(datadir),
page_path="some_path",
filename=filename,
extension="md",
name="some_name",
user_input=user_input,
example=None,
template_filename=None,
template_path=None,
)
scoped_page_file = (
pathlib.Path(options.base_path) / options.page_path / tokened_filename
).with_suffix(f".{options.extension}")
# act
scoped_page = ScopedPageCreator().create_page(options)
scoped_page.save_to_disk()
# assert
assert scoped_page_file.exists()
assert caplog.records[0].message == f"Saved {scoped_page_file} to disk"

View File

@@ -1 +0,0 @@
template contents

View File

@@ -0,0 +1 @@
some date token: {d:%d-%m-%Y}

View File

@@ -0,0 +1 @@
some input tokens {input1} {input0}

View File

@@ -0,0 +1 @@
some name token {name}

View File

@@ -0,0 +1,3 @@
scoped page file
no tokens

View File

@@ -0,0 +1 @@
this file already exists