From ea02c1bd5a34d5655c0e836447606ceeeef3ebcb Mon Sep 17 00:00:00 2001 From: "Soren I. Bjornstad" Date: Fri, 10 Sep 2021 11:18:03 -0500 Subject: [PATCH] fix set_tiddler_values to work with missing files and meta files --- tzk/builders.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tzk/builders.py b/tzk/builders.py index d23cc4b..583e670 100644 --- a/tzk/builders.py +++ b/tzk/builders.py @@ -444,11 +444,20 @@ def set_tiddler_values(mappings: Dict[str, str]) -> None: for tiddler, new_text in mappings.items(): tiddler_path = (Path(build_state['public_wiki_folder']) / "tiddlers" / tiddler) - with tiddler_path.open("r") as f: - tiddler_lines = f.readlines() - first_blank_line_index = next(idx - for idx, value in enumerate(tiddler_lines) - if not value.strip()) + try: + with tiddler_path.open("r") as f: + tiddler_lines = f.readlines() + except FileNotFoundError: + stop(f"File {tiddler_path} not found. " + f"(Did you forget to end the name with '.tid'?)") + + if not str(tiddler_path).endswith('.tid'): + # will be a separate meta file, so the whole thing is the text field + first_blank_line_index = 0 + else: + first_blank_line_index = next(idx + for idx, value in enumerate(tiddler_lines) + if not value.strip()) with tiddler_path.open("w") as f: f.writelines(tiddler_lines[0:first_blank_line_index+1]) f.write(new_text)