forked from jcomo/tracking-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.py
52 lines (41 loc) · 1.96 KB
/
codegen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
os.environ["CODE_GENERATING"] = "true"
from tracking_numbers.helpers.spec import iter_courier_specs # noqa:E402
from tracking_numbers.helpers.spec import iter_definitions # noqa:E402
import_statements = [
"import re",
"",
"from tracking_numbers.checksum_validator import Mod10",
"from tracking_numbers.checksum_validator import Mod7",
"from tracking_numbers.checksum_validator import S10",
"from tracking_numbers.checksum_validator import SumProductWithWeightsAndModulo",
"from tracking_numbers.definition import AdditionalValidation",
"from tracking_numbers.definition import TrackingNumberDefinition",
"from tracking_numbers.serial_number import DefaultSerialNumberParser",
"from tracking_numbers.serial_number import PrependIf",
"from tracking_numbers.serial_number import UPSSerialNumberParser",
"from tracking_numbers.types import Courier",
"from tracking_numbers.types import Product",
"from tracking_numbers.value_matcher import ExactValueMatcher",
"from tracking_numbers.value_matcher import RegexValueMatcher",
]
def main():
"""Generates the python code for all the tracking number definitions.
This is used by the public API of the library in the call to get_tracking_number().
The reason we use codegen here is so that we don't have to ship the JSON files around,
which have a lot of additional metadata, test cases, etc. and are slower to parse at
startup.
"""
with open("tracking_numbers/_generated.py", "w") as wf:
wf.write("# DO NOT EDIT - Generated by codegen.py\n")
wf.write("\n")
for import_stmt in import_statements:
wf.write(f"{import_stmt}\n")
wf.write("\n\n")
wf.write("DEFINITIONS = [\n")
for courier_spec in iter_courier_specs():
for definition, _ in iter_definitions(courier_spec):
wf.write(f" {repr(definition)},\n")
wf.write("]\n")
if __name__ == "__main__":
main()