Strengthen types in Args

This commit is contained in:
Skye 2023-12-02 19:02:58 -05:00
parent 1f04a61598
commit 74a49732bd

View file

@ -149,7 +149,7 @@ def get_country_name(
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="format", description="format postcards with latex" 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( parser.add_argument(
"-o", "--origin", help="origin country code", default="us", type=iso_code "-o", "--origin", help="origin country code", default="us", type=iso_code
) )
@ -179,7 +179,7 @@ parser.add_argument(
"-a", "-a",
"--address-file", "--address-file",
default="addresses.csv", default="addresses.csv",
type=str, type=Path,
help="CSV file containing addresses", help="CSV file containing addresses",
) )
@ -187,7 +187,7 @@ parser.add_argument(
"-i", "-i",
"--content-path", "--content-path",
default="content", default="content",
type=str, type=Path,
help="Directory containing content files", 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): class Args(argparse.Namespace):
template: str template: Path
origin: IsoCode origin: IsoCode
language: IsoCode language: IsoCode
count: int count: int
skip: int skip: int
address_file: str address_file: Path
content_path: str content_path: Path
no_content: bool no_content: bool
dont_compile: bool dont_compile: bool
@ -287,6 +287,11 @@ if args.origin == "us":
serial += 1 serial += 1
imb.write_current_serial(serial) 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: with (root_dir() / "options.json").open("w") as options:
json.dump( json.dump(
fp=options, fp=options,
@ -294,6 +299,7 @@ with (root_dir() / "options.json").open("w") as options:
"args": args.__dict__, "args": args.__dict__,
"cards": cards, "cards": cards,
}, },
default=serialize_paths
) )
if args.dont_compile: if args.dont_compile: