diff options
author | Serhii Kostiuk <serhii.o.kostiuk@globallogic.com> | 2021-05-24 10:55:59 +0300 |
---|---|---|
committer | Serhii Kostiuk <serhii.o.kostiuk@globallogic.com> | 2021-05-24 17:26:55 +0300 |
commit | f0931133aa7960d56767578ed5d8c24f66920a60 (patch) | |
tree | cd7ad97bef66047f61abf5377ed126210454471c /scripts | |
parent | efc1a9e07b7acb7750aad4029a0a7a3c34a127fe (diff) | |
download | libmts-io-f0931133aa7960d56767578ed5d8c24f66920a60.tar.gz libmts-io-f0931133aa7960d56767578ed5d8c24f66920a60.tar.bz2 libmts-io-f0931133aa7960d56767578ed5d8c24f66920a60.zip |
[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.
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/create-mcc-mnc-table.py | 69 |
1 files changed, 58 insertions, 11 deletions
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) |