From 74a49732bdddd820e410d476af54513d49b51dd5 Mon Sep 17 00:00:00 2001 From: Skye Date: Sat, 2 Dec 2023 19:02:58 -0500 Subject: [PATCH] Strengthen types in `Args` --- format.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/format.py b/format.py index 1ea917f..51914f4 100755 --- a/format.py +++ b/format.py @@ -149,7 +149,7 @@ def get_country_name( parser = argparse.ArgumentParser( prog="format", description="format postcards with latex" ) -parser.add_argument("template", help="template to use", nargs="?", default="2card") +parser.add_argument("template", help="template to use", nargs="?", type=Path, default="2card") parser.add_argument( "-o", "--origin", help="origin country code", default="us", type=iso_code ) @@ -179,7 +179,7 @@ parser.add_argument( "-a", "--address-file", default="addresses.csv", - type=str, + type=Path, help="CSV file containing addresses", ) @@ -187,7 +187,7 @@ parser.add_argument( "-i", "--content-path", default="content", - type=str, + type=Path, help="Directory containing content files", ) @@ -206,13 +206,13 @@ parser.add_argument("-w", "--watch", action="store_true", help="Watch input file class Args(argparse.Namespace): - template: str + template: Path origin: IsoCode language: IsoCode count: int skip: int - address_file: str - content_path: str + address_file: Path + content_path: Path no_content: bool dont_compile: bool @@ -287,6 +287,11 @@ if args.origin == "us": serial += 1 imb.write_current_serial(serial) +def serialize_paths(obj: object): + if isinstance(obj, Path): + return str(obj) + raise TypeError("Type not Serializable") + with (root_dir() / "options.json").open("w") as options: json.dump( fp=options, @@ -294,6 +299,7 @@ with (root_dir() / "options.json").open("w") as options: "args": args.__dict__, "cards": cards, }, + default=serialize_paths ) if args.dont_compile: