From f0931133aa7960d56767578ed5d8c24f66920a60 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 10:55:59 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Extended `create-mcc-mnc-table.py` for CSV support: - refactoring: renamed the 'network' variable to 'carrier' as it's named 'carrier' in the CPP code; - new feature: added support of command-line arguments to select the source between the website and CSV; the website is used by default for compatibility reasons. --- scripts/create-mcc-mnc-table.py | 69 ++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 11 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index 1f26fe4..fdc9833 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -13,6 +13,8 @@ import re import urllib.request import urllib.parse import datetime +import csv +import argparse from typing import Iterable, Generator from collections import namedtuple @@ -105,12 +107,18 @@ Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc MccMncElement = namedtuple( 'MccMncData', - field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'network') + field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'carrier') ) ######################################################################################################################## +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) + return parser + def print_cpp_preamble() -> None: print(PREAMBLE_TEMPLATE.format(today=datetime.date.today())) @@ -122,23 +130,23 @@ def print_cpp_general_code() -> None: def format_mcc_mnc_line(el: MccMncElement) -> str: if el.mnc.upper() != "N/A": - return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{network}";'.format( + return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{carrier}";'.format( mcc_int=el.mcc_int, mnc_int=el.mnc_int, iso=el.iso, country=el.country, country_code=el.country_code, - network=el.network + carrier=el.carrier ) else: # TODO: The Country and Country Code values were swapped in the original implementation due to a bug. # Left as is for compatibility reasons. - return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({network})'.format( + return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({carrier})'.format( mcc=el.mcc, iso=el.iso, country=el.country, country_code=el.country_code, - network=el.network + carrier=el.carrier ) @@ -205,21 +213,60 @@ def mcc_mnc_from_website(url: str) -> Generator[MccMncElement, None, None]: iso=iso, country=country, country_code=country_code, - network=carrier + carrier=carrier + ) + + +def mcc_mnc_from_csv(path: str) -> Generator[MccMncElement, None, None]: + with open(path) as f: + csv_reader = csv.DictReader(f) + for row in csv_reader: + mcc = row['MCC'] + mnc = row['MNC'] + iso = row['ISO'] + country = row['Country'] + country_code = row['Country Code'] + carrier = row['Carrier'] + + mcc_int = mcc_to_mcc_int(mcc) + mnc_int = mnc_to_mnc_int(mnc) + + yield MccMncElement( + mcc=mcc, + mcc_int=mcc_int, + mnc=mnc, + mnc_int=mnc_int, + iso=iso, + country=country, + country_code=country_code, + carrier=carrier ) ######################################################################################################################## -def main(): +def main() -> int: + parser = init_argparse() + args = parser.parse_args() + + if (args.csv is not None) and args.website: + parser.error('Only one source can be used at a time.') + return 1 + + if args.csv is None: + source = mcc_mnc_from_website('http://mcc-mnc.com/') + else: + source = mcc_mnc_from_csv(args.csv) + print_cpp_preamble() print_cpp_general_code() - print_cpp_mcc_mnc_table( - mcc_mnc_from_website('http://mcc-mnc.com/') - ) + print_cpp_mcc_mnc_table(source) + + return 0 ######################################################################################################################## if __name__ == "__main__": - main() + ret = main() + exit(ret) -- cgit v1.2.3