diff options
| -rwxr-xr-x | fanfictioner | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/fanfictioner b/fanfictioner index 49ec9b7..a3715c4 100755 --- a/fanfictioner +++ b/fanfictioner @@ -35,7 +35,57 @@ SD_DIR = Path("/data/LLM-models/SD") CONF = Path.home() / ".config/book-reader.conf" +def load_conf(path=CONF): + conf = {} + for line in path.read_text().splitlines(): + if "=" in line and not line.lstrip().startswith("#"): + k, v = line.split("=", 1) + conf[k.strip()] = shlex.split(v)[0] if v.strip() else "" + return conf + + +def page_path(lib, pj, n): + return Path(lib) / pj["series"] / pj["book"] / f"{n:03d}.png" + + +def todo(pj, wd, lib): + """What is left to do, derived from disk: (characters without a ref, pages not in the library).""" + refs = [c for c in pj["characters"] if not (wd / "refs" / f"{c['name']}.png").exists()] + pages = [p for p in pj["pages"] if not page_path(lib, pj, p["n"]).exists()] + return refs, pages + + +def keep(src, dst): + """Copy src to dst atomically, so a half-written file never counts as done.""" + dst.parent.mkdir(parents=True, exist_ok=True) + tmp = dst.with_name(dst.name + ".tmp") + shutil.copyfile(src, tmp) + os.replace(tmp, dst) + return dst + + def selftest(): + with tempfile.TemporaryDirectory() as t: + t = Path(t) + (t / "c.conf").write_text('# comment\nLIBRARY="/data/my lib"\nPORT=8642\n') + conf = load_conf(t / "c.conf") + assert conf == {"LIBRARY": "/data/my lib", "PORT": "8642"}, conf + + pj = {"series": "S", "book": "B", + "characters": [{"name": "Mara"}, {"name": "Jon"}], + "pages": [{"n": 1}, {"n": 2}]} + lib = t / "lib" + assert page_path(lib, pj, 7) == lib / "S" / "B" / "007.png" + (t / "refs").mkdir() + (t / "refs" / "Mara.png").touch() + page_path(lib, pj, 1).parent.mkdir(parents=True) + page_path(lib, pj, 1).touch() + refs, pages = todo(pj, t, lib) + assert [c["name"] for c in refs] == ["Jon"], refs + assert [p["n"] for p in pages] == [2], pages + + keep(t / "refs" / "Mara.png", t / "deep" / "x.png") + assert (t / "deep" / "x.png").exists() print("selftest ok") |
