Files
mytoolkit/mytoolkit/templates_registry.py
T
Zhengshou Lai 587eac8837 feat: add journal submission kits + paper scaffold; restructure package
- init journal <elsevier|ieee|springer>: self-contained, make-buildable kits
  (Springer sn-jnl.cls + bst bundled; reuses project references.bib)
- init paper scaffold: ctexart + Fandol fonts, bib at root, latexdiff highlighted
- move templates/ under mytoolkit/, add info/references, relocate protocols
- migrate mytoolkit skill into submodule; update README/CLAUDE docs
2026-06-28 23:38:47 +08:00

67 lines
1.7 KiB
Python

"""Templates registry for md→pdf / md→docx conversion."""
import importlib.resources as resources
import json
import os
from pathlib import Path
import click
_HOME = Path(os.environ.get("MYTOOLKIT_HOME", Path.home() / ".mytoolkit"))
CONFIG_PATH = _HOME / "templates.json"
def _read() -> dict:
if not CONFIG_PATH.exists():
return {}
with open(CONFIG_PATH, encoding="utf-8") as f:
return json.load(f)
def _write(data: dict) -> None:
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
f.write("\n")
def _package_root() -> Path:
"""Return the templates directory bundled with the package."""
return Path(str(resources.files("mytoolkit") / "templates"))
def set_root(path: Path) -> Path:
"""Register an external templates root directory (override bundled templates)."""
resolved = Path(path).expanduser().resolve()
_write({"dir": str(resolved)})
return resolved
def get_root() -> Path:
"""Return the active templates root.
Uses the externally registered path if it exists and is valid; otherwise
falls back to the templates directory bundled with the package.
"""
data = _read()
raw = data.get("dir")
if raw:
root = Path(raw)
if root.is_dir():
return root
return _package_root()
def get_md_to_pdf_root() -> Path:
sub = get_root() / "md-to-pdf"
if not sub.is_dir():
raise click.UsageError(f"Subdirectory missing: {sub}")
return sub
def get_md_to_docx_root() -> Path:
sub = get_root() / "md-to-docx"
if not sub.is_dir():
raise click.UsageError(f"Subdirectory missing: {sub}")
return sub