Fix cache dir being passed relatively to typst and add typing to args
This commit is contained in:
parent
3ddd3dec8e
commit
1f04a61598
101
format.py
101
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 viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<clipPath id="circle">
|
||||
<circle cx="256" cy="256" r="256" />
|
||||
</clipPath>
|
||||
<image width="512" height="512" clip-path="url(#circle)"
|
||||
xlink:href="data:;base64,{base64.b64encode(avatar_raster).decode("utf-8")}" />
|
||||
</svg>"""
|
||||
svg_text = f"""<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<clipPath id="circle">
|
||||
<circle cx="256" cy="256" r="256" />
|
||||
</clipPath>
|
||||
<image width="512" height="512" clip-path="url(#circle)"
|
||||
xlink:href="data:;base64,{base64.b64encode(avatar_raster).decode("utf-8")}" />
|
||||
</svg>"""
|
||||
|
||||
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,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue