adding latest

This commit is contained in:
2021-11-01 01:34:41 +00:00
parent 526dd733b5
commit bfc34e9414
4 changed files with 131 additions and 101 deletions

View File

@@ -19,20 +19,20 @@ required:
## tests to write
- user input is None
- page using/not using input tokens
- user input does not match number of input tokens
- no user input
- mismatched user input
- with/without example
- page using/not using date tokens
- page using/not using name tokens
- dry run
- path/page filenames can contain spaces and they are converted
- dry run
## tests done
- path/page filenames can contain spaces and they are converted
- user input is None
- page using/not using input tokens
- page using/not using date tokens
- page using/not using name tokens
- page with/without a template
- the given base path does not exist
- the given template file does not exist

View File

@@ -55,29 +55,24 @@ class PageCreator:
def create_page(self, options: PageCreatorOptions) -> Page:
raise NotImplementedError
def _convert_base_path_to_path(self) -> pathlib.Path:
# check if Tembo base path exists
def _check_base_path_exists(self) -> None:
if not pathlib.Path(self.options.base_path).expanduser().exists():
raise exceptions.BasePathDoesNotExistError(
f"Tembo base path of {self.options.base_path} does not exist."
)
def _convert_base_path_to_path(self) -> pathlib.Path:
path_to_file = (
pathlib.Path(self.options.base_path).expanduser()
/ pathlib.Path(self.options.page_path.replace(" ", "_")).expanduser()
/ self.options.filename.replace(" ", "_")
)
try:
# check for existing `.` in the extension
extension = (
self.options.extension[1:]
if self.options.extension[0] == "."
else self.options.extension
)
except IndexError:
# 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
return path_to_file
# check for existing `.` in the extension
extension = (
self.options.extension[1:]
if self.options.extension[0] == "."
else self.options.extension
)
# return path with a file
return path_to_file.with_suffix(f".{extension}")
@@ -126,6 +121,7 @@ class ScopedPageCreator(PageCreator):
def create_page(self, options: PageCreatorOptions) -> Page:
self._options = options
self._check_base_path_exists()
self._all_input_tokens = self._get_input_tokens()
self._verify_input_tokens()
@@ -145,8 +141,9 @@ class ScopedPageCreator(PageCreator):
self.options.base_path,
self.options.page_path,
self.options.filename,
self.options.extension,
).expanduser()
)
.expanduser()
.with_suffix(f".{self.options.extension}")
)
template_contents = self._load_template()
# get the input tokens from both the path and the template
@@ -295,78 +292,3 @@ class ScopedPage(Page):
scoped_page.write(self.page_content)
# TODO: pass this back somehow
tembo.logger.info("Saved %s to disk", self.path)
if __name__ == "__main__":
c = ScopedPageCreator()
# # raises error
# # print(c._substitute_tokens("scratchpad/{input0}-{d:DD-MM-YYYY}-{d:dddd}-{d:A}-file.md", None))
# print(
# c._substitute_tokens(
# "scratchpad/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}-file-{input0}.md", ("last",)
# )
# )
# print(
# c.create_page(
# "~/tembo",
# "{name}",
# "{input0}-{input1}-file",
# "md",
# "scratchpad",
# ("first", "second"),
# )
# )
# print(
# c.create_page(
# "~/tembo",
# "{name}/{d:MMMM-YY}",
# "{input0}-{d:DD-MM-YYYY}-{d:dddd}-{d:A}-file",
# "md",
# "scratchpad",
# ("first",),
# )
# )
# print(
# c.create_page(
# "~/tembo",
# "{name}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
# "file",
# "md",
# "scratchpad",
# None,
# )
# )
# print(
# c.create_page(
# "~/tembo",
# "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
# "file-{input0}-{name}",
# ".md",
# "meeting",
# ("last",),
# "scratchpad.md.tpl",
# )
# )
# test_page_with_template = c.create_page(
# "~/tembo",
# "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
# "file-{input0}-{name}",
# ".md",
# "meeting",
# ("last",),
# "scratchpad.md.tpl",
# )
# print(test_page_with_template)
# test_page_with_template.save_to_disk(False)
# print(
# c.create_page(
# "~/tembo",
# "{name}/{d:A}/{d:DD-MM-YYYY}-{d:dddd}-{d:A}",
# "file-{input0}-{name}",
# ".md",
# "meeting",
# ("last",),
# "scratchpad_templates/scratchpad.md.tpl",
# )
# )

View File

@@ -43,10 +43,10 @@ def test_create_page_template_file_does_not_exist(template_path, tmpdir):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="",
filename="",
extension="",
name="",
page_path="some_path",
filename="some_filename",
extension="some_extension",
name="some_name",
user_input=None,
example=None,
template_filename="template.md.tpl",
@@ -241,3 +241,110 @@ def test_create_tokened_page_tokens_in_filename(
# assert
assert scoped_page_file.exists()
assert caplog.records[0].message == f"Saved {scoped_page_file} to disk"
def test_create_tokened_page_input_tokens_preserve_order(datadir, caplog):
# arrange
tokened_filename = "input_token_fourth_input_first_input"
options = PageCreatorOptions(
base_path=str(datadir),
page_path="some_path",
filename="input_token_{input3}_{input0}",
extension="md",
name="some_name",
user_input=("first_input", "second_input", "third_input", "fourth_input"),
example=None,
template_filename="some_template_input_tokens_preserve_order.md.tpl",
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"
with scoped_page_file.open(mode="r", encoding="utf-8") as scoped_page_contents:
assert scoped_page_contents.readline() == "third_input second_input"
def test_create_page_spaces_in_path(tmpdir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some path with a space",
filename="some filename with a space",
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.replace(" ", "_")
/ options.filename.replace(" ", "_")
).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"
def test_create_page_dot_in_extension(tmpdir, caplog):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="some_filename",
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[1:]}")
# 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"
def test_create_page_str_representation(tmpdir):
# arrange
options = PageCreatorOptions(
base_path=str(tmpdir),
page_path="some_path",
filename="some_filename",
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)
# assert
assert str(scoped_page) == f"ScopedPage({scoped_page_file})"