From adc19a08871964a23a27a20c384439776a52891f Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 11:59:45 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Extended `create-mcc-mnc-table.py` to support writing the resulting CPP code directly to the target file: - refactoring: renamed `print_cpp_mcc_mnc_table` to `print_cpp_mcc_mnc_create_table` to better represent its purpose - printing the body of `MccMncTable::createTable()`; - refactoring: grouped all the `print_cpp_*` functions to the single `print_cpp` function; - new feature: printing the code directly to the target file without using stdout; printing to stdout is used by default for compatibility reasons. --- scripts/create-mcc-mnc-table.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'scripts') 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 -- cgit v1.2.3