summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/create-mcc-mnc-table.py41
1 files changed, 26 insertions, 15 deletions
diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py
index fdc9833..c2aee14 100644
--- a/scripts/create-mcc-mnc-table.py
+++ b/scripts/create-mcc-mnc-table.py
@@ -16,7 +16,7 @@ import datetime
import csv
import argparse
-from typing import Iterable, Generator
+from typing import Iterable, Generator, Optional, TextIO
from collections import namedtuple
########################################################################################################################
@@ -117,15 +117,16 @@ def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description='Generate MCC/MNC table file from Website or CSV')
parser.add_argument('-w', '--website', action='store_true')
parser.add_argument('-c', '--csv', type=str)
+ parser.add_argument('-t', '--target', type=str, default='-')
return parser
-def print_cpp_preamble() -> None:
- print(PREAMBLE_TEMPLATE.format(today=datetime.date.today()))
+def print_cpp_preamble(*, target: Optional[TextIO] = None) -> None:
+ print(PREAMBLE_TEMPLATE.format(today=datetime.date.today()), file=target)
-def print_cpp_general_code() -> None:
- print(GENERAL_CODE)
+def print_cpp_general_code(*, target: Optional[TextIO] = None) -> None:
+ print(GENERAL_CODE, file=target)
def format_mcc_mnc_line(el: MccMncElement) -> str:
@@ -150,15 +151,21 @@ def format_mcc_mnc_line(el: MccMncElement) -> str:
)
-def print_cpp_mcc_mnc_table(data: Iterable[MccMncElement]) -> None:
- print("void MccMncTable::createTable() {")
- print(" std::string sData;")
+def print_cpp_mcc_mnc_create_table(source: Iterable[MccMncElement], *, target: Optional[TextIO] = None) -> None:
+ print("void MccMncTable::createTable() {", file=target)
+ print(" std::string sData;", file=target)
- for el in data:
- print(format_mcc_mnc_line(el))
+ for el in source:
+ print(format_mcc_mnc_line(el), file=target)
- print("}")
- print("")
+ print("}", file=target)
+ print("", file=target)
+
+
+def print_cpp(source: Iterable[MccMncElement], *, target: Optional[TextIO] = None) -> None:
+ print_cpp_preamble(target=target)
+ print_cpp_general_code(target=target)
+ print_cpp_mcc_mnc_create_table(source, target=target)
def mcc_to_mcc_int(src: str) -> str:
@@ -258,9 +265,13 @@ def main() -> int:
else:
source = mcc_mnc_from_csv(args.csv)
- print_cpp_preamble()
- print_cpp_general_code()
- print_cpp_mcc_mnc_table(source)
+ if args.target == '-':
+ # Print to stdout
+ print_cpp(source, target=None)
+ else:
+ # Print to file
+ with open(args.target, 'w') as f:
+ print_cpp(source, target=f)
return 0