From fa849130585bd56b94528bcc043c4bf05d3d2f40 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 12:26:52 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Restored the original table of MCC/MNC values that was used to populate the `MTS_IO_MccMncTable.cpp` file. This step is required to extend the format of MCC/MNC table with additional fields and re-generate the file based on new data instead of changing all the lines by hand. Also added a script that restores the MCC/MNC table from a CPP file. --- scripts/restore-mcc-mnc-from-cpp.py | 115 ++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 scripts/restore-mcc-mnc-from-cpp.py (limited to 'scripts') diff --git a/scripts/restore-mcc-mnc-from-cpp.py b/scripts/restore-mcc-mnc-from-cpp.py new file mode 100644 index 0000000..041be70 --- /dev/null +++ b/scripts/restore-mcc-mnc-from-cpp.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 + +""" +Restores the original data that was used to populate the 'MTS_IO_MccMncTable.cpp' file. +Stores the result in a .csv file. +""" + +######################################################################################################################## + +import re +import csv +import argparse + +from typing import Dict, Iterable, Generator + + +######################################################################################################################## + +def init_argparse() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description='Generate MCC/MNC table file from Website or CSV') + parser.add_argument('source', type=str) + parser.add_argument('target', type=str) + return parser + + +def mcc_int_to_mcc(src: str) -> str: + hash_ = int(src) + return "{:03x}".format(hash_) + + +def mnc_int_to_mnc(src: str) -> str: + hash_ = int(src) + hash_str = "{:03x}".format(hash_) + + if hash_ & 0xf != 0xf: + return hash_str + else: + return hash_str[:-1] + + +def parse_code_line(line: str) -> Dict[str, str]: + left, right = line.split(' = ') + mcc_int, mnc_int = re.findall(r'\[(\d+)\]', left) + info = re.findall(r'"(.+)"', right)[0] + iso, country, country_code, carrier = info.split(',') + + mcc = mcc_int_to_mcc(mcc_int) + mnc = mnc_int_to_mnc(mnc_int) + + return { + 'MCC': mcc, + 'MNC': mnc, + 'ISO': iso, + 'Country': country, + 'Country Code': country_code, + 'Carrier': carrier + } + + +def parse_comment_line(line: str) -> Dict[str, str]: + match = re.search( + r'MCC\((.*)\) MNC\((.*)\) ISO\((.*)\) Country Code\((.*)\) Country\((.*)\) Carrier\((.*)\)', + line + ) + + # NOTE: The original file contains values of the "Code" and "Country" fields swapped. + mcc, mnc, iso, country, country_code, carrier = match.groups() + + return { + 'MCC': mcc, + 'MNC': mnc, + 'ISO': iso, + 'Country': country, + 'Country Code': country_code, + 'Carrier': carrier + } + + +def mcc_mnc_from_cpp(src: str) -> Generator[Dict[str, str], None, None]: + with open(src) as f: + for line in f: + if line.startswith(" //MCC"): + yield parse_comment_line(line) + + elif line.startswith(" m_mTable["): + yield parse_code_line(line) + + +def write_mcc_mnc_to_scv(data: Iterable[Dict[str, str]], target: str) -> None: + with open(target, 'w') as f: + fieldnames = ['MCC', 'MNC', 'ISO', 'Country', 'Country Code', 'Carrier'] + writer = csv.DictWriter(f, fieldnames, delimiter=',', quotechar='"', lineterminator='\n', quoting=csv.QUOTE_ALL) + writer.writeheader() + + for d in data: + writer.writerow(d) + + +######################################################################################################################## + +def main() -> int: + parser = init_argparse() + args = parser.parse_args() + + data = mcc_mnc_from_cpp(args.source) + write_mcc_mnc_to_scv(data, args.target) + + return 0 + + +######################################################################################################################## + +if __name__ == "__main__": + ret = main() + exit(ret) -- cgit v1.2.3