diff --git a/format.py b/format.py index 95f6dca..1ea917f 100755 --- a/format.py +++ b/format.py @@ -5,7 +5,6 @@ import csv import hashlib import json import os -import pathlib import string import typing import urllib.parse @@ -14,25 +13,44 @@ import requests import imb from pathlib import Path -from typing import TypedDict +from typing import NewType, TypedDict # A lot of stuff needs to be in the same directory, just chdir -os.chdir(os.path.dirname(os.path.realpath(__file__))) +# os.chdir(os.path.dirname(os.path.realpath(__file__))) + + +_cache: Path | None = None def cache_dir() -> Path: - cache = Path("cache") - cache.mkdir(exist_ok=True) - return cache + global _cache + if _cache is None: + _cache = root_dir() / "cache" + _cache.mkdir(exist_ok=True) + return _cache -def iso_code(s: str) -> str: +_root: Path | None = None + + +def root_dir() -> Path: + global _root + if _root is None: + _root = Path(os.path.realpath(__file__)).parent + _root.mkdir(exist_ok=True) + return _root + + +IsoCode = NewType("IsoCode", str) + + +def iso_code(s: str) -> IsoCode: if len(s) != 2: raise ValueError("must be 2 characters long") s = s.lower() if not (s[0] in string.ascii_lowercase and s[1] in string.ascii_lowercase): raise ValueError("must be ascii letters") - return s + return IsoCode(s) def get_discord_avatar( @@ -95,23 +113,22 @@ def get_orig_avatar( def get_avatar(url: str, secrets: dict[str, str]) -> str | None: basename = hashlib.sha256(url.encode("utf-8")).hexdigest() file_path = cache_dir() / f"{basename}.svg" - if file_path.exists(): - return str(file_path) - avatar_raster = get_orig_avatar(url, basename, secrets) - if avatar_raster is None: - return None + if not file_path.exists(): + avatar_raster = get_orig_avatar(url, basename, secrets) + if avatar_raster is None: + return None - svg_text = f""" - - - - - """ + svg_text = f""" + + + + + """ - with open(file_path, "w") as svgfile: - svgfile.write(svg_text) - return str(file_path) + with open(file_path, "w") as svgfile: + svgfile.write(svg_text) + return str(file_path.relative_to(root_dir())) def get_country_name( @@ -187,7 +204,20 @@ parser.add_argument( parser.add_argument("-w", "--watch", action="store_true", help="Watch input files") -args = parser.parse_args() + +class Args(argparse.Namespace): + template: str + origin: IsoCode + language: IsoCode + count: int + skip: int + address_file: str + content_path: str + no_content: bool + dont_compile: bool + + +args = parser.parse_args(args=None, namespace=Args()) cldr_root = ET.parse( f"{os.getenv('CLDR_ROOT')}/share/unicode/cldr/common/main/{args.language}.xml" @@ -239,11 +269,10 @@ for row in rows: # Typst can't access files outside the project root, except through a symlink # Create one in cache to use here -if not os.path.exists("cache"): - os.mkdir("cache") -p = pathlib.Path("cache/content") +p = cache_dir() / "content" p.unlink(missing_ok=True) -p.symlink_to(args.content_path) +content_full_path = Path(os.getcwd()).joinpath(args.content_path) +p.symlink_to(content_full_path) cards = cards * args.count @@ -258,7 +287,7 @@ if args.origin == "us": serial += 1 imb.write_current_serial(serial) -with open("options.json", "w") as options: +with (root_dir() / "options.json").open("w") as options: json.dump( fp=options, obj={ @@ -273,14 +302,22 @@ if args.dont_compile: font_paths = os.getenv("TYPST_FONT_PATHS") assert font_paths is not None -os.execlp( - "typst", +typst_args = [ "typst", "watch" if args.watch else "compile", + "--root", + root_dir(), "--font-path", - args.content_path, + cache_dir() / "content", "--font-path", font_paths, args.template, "output.pdf", +] + +# print(*typst_args) + +os.execlp( + "typst", + *typst_args, )