-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-currencies.py
47 lines (33 loc) · 1 KB
/
generate-currencies.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
import pathlib
from collections.abc import Iterator
from moneyed.classes import CURRENCIES
currencies_file = pathlib.Path("src/immoney/currencies.py")
imports_template = """\
from __future__ import annotations
from typing import Final
from typing import final
from . import Currency
from .registry import CurrencyCollector
from .registry import CurrencyRegistry
__currencies: Final = CurrencyCollector[Currency]()
"""
currency_template = """
@final
class {code}Type(Currency):
code = "{code}"
subunit = {subunit}
{code}: Final = {code}Type()
__currencies.add({code})
"""
registry_template = """\
registry: Final[CurrencyRegistry[Currency]] = __currencies.finalize()
del __currencies
"""
def generate_code() -> Iterator[str]:
yield imports_template
for currency in CURRENCIES.values():
yield currency_template.format(code=currency.code, subunit=currency.sub_unit)
yield registry_template
with currencies_file.open("w") as file:
for chunk in generate_code():
file.write(chunk)