Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
prnt(__doc__, file=sys.stderr)
sys.exit(1)
elif opt in ("-v", "--var"):
varprefix = arg
elif opt in ("-f", "--flat"):
per_locale = False
elif opt in ("-s", "--sep"):
separator = arg
elif opt in ("-c", "--chunks"):
chunks = int(arg)
else:
prnt("Unknown option %s" % opt, file=sys.stderr)
prnt(__doc__, file=sys.stderr)
sys.exit(1)
if len(args) != 3:
prnt(__doc__, file=sys.stderr)
sys.exit(1)
if per_locale:
prefixdata = load_locale_prefixdata(args[0], separator=separator)
else:
prefixdata = {}
load_locale_prefixdata_file(prefixdata, args[0], separator=separator)
output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale, chunks)
chunk_longest = output_prefixdata_chunk(
chunk_data, chunk_file, module_prefix, per_locale)
if chunk_longest > longest_prefix:
longest_prefix = chunk_longest
with open(outfilename, "w") as outfile:
if per_locale:
prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
else:
prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
prnt(COPYRIGHT_NOTICE, file=outfile)
prnt("%s_DATA = {}" % varprefix, file=outfile)
for chunk_num in range(total_chunks):
prnt("from .data%d import data" % chunk_num, file=outfile)
prnt("%s_DATA.update(data)" % varprefix, file=outfile)
prnt("del data", file=outfile)
prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix), file=outfile)
def output_prefixdata_chunk(prefixdata, outfilename, module_prefix, per_locale):
with open(outfilename, "w") as outfile:
longest_prefix = 0
if per_locale:
prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
else:
prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
prnt(COPYRIGHT_NOTICE, file=outfile)
prnt("data = {", file=outfile)
for prefix in sorted(prefixdata.keys()):
if len(prefix) > longest_prefix:
longest_prefix = len(prefix)
if per_locale:
prnt(" '%s':%s," % (prefix, _stable_dict_repr(prefixdata[prefix])), file=outfile)
else:
prnt(" '%s':%s," % (prefix, _tuple_repr(prefixdata[prefix])), file=outfile)
prnt("}", file=outfile)
return longest_prefix
# Build up a map from country code (int) to list of region codes (ISO 3166-1 alpha 2)
country_code_to_region_code = {}
for country_id in sorted(self.territory.keys()):
terrobj = self.territory[country_id]
if terrobj.o.country_code is not None:
country_code = int(terrobj.o.country_code)
if country_code not in country_code_to_region_code:
country_code_to_region_code[country_code] = []
if terrobj.o.main_country_for_code:
country_code_to_region_code[country_code].insert(0, terrobj.o.id)
else:
country_code_to_region_code[country_code].append(terrobj.o.id)
# Emit the mapping from country code to region code if nonempty.
if len(country_code_to_region_code.keys()) > 0:
prnt(_COUNTRY_CODE_TO_REGION_CODE_PROLOG, file=outfile)
prnt("_COUNTRY_CODE_TO_REGION_CODE = {", file=outfile)
for country_code in sorted(country_code_to_region_code.keys()):
country_ids = country_code_to_region_code[country_code]
prnt(' %d: ("%s",),' % (country_code, '", "'.join(country_ids)), file=outfile)
prnt("}", file=outfile)
def emit_metadata_for_region_py(self, region, region_filename, module_prefix):
"""Emit Python code generating the metadata for the given region"""
terrobj = self.territory[region]
with open(region_filename, "w") as outfile:
prnt(_REGION_METADATA_PROLOG % {'region': terrobj.identifier(), 'module': module_prefix}, file=outfile)
prnt("PHONE_METADATA_%s = %s" % (terrobj.identifier(), terrobj), file=outfile)
with open(outfilename, "w") as outfile:
longest_prefix = 0
if per_locale:
prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
else:
prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
prnt(COPYRIGHT_NOTICE, file=outfile)
prnt("data = {", file=outfile)
for prefix in sorted(prefixdata.keys()):
if len(prefix) > longest_prefix:
longest_prefix = len(prefix)
if per_locale:
prnt(" '%s':%s," % (prefix, _stable_dict_repr(prefixdata[prefix])), file=outfile)
else:
prnt(" '%s':%s," % (prefix, _tuple_repr(prefixdata[prefix])), file=outfile)
prnt("}", file=outfile)
return longest_prefix
chunk_keys = sorted_keys[chunk_index:chunk_index + chunk_size]
chunk_data = {}
for key in chunk_keys:
chunk_data[key] = prefixdata[key]
chunk_file = os.path.join(outdirname, 'data%d.py' % chunk_num)
chunk_longest = output_prefixdata_chunk(
chunk_data, chunk_file, module_prefix, per_locale)
if chunk_longest > longest_prefix:
longest_prefix = chunk_longest
with open(outfilename, "w") as outfile:
if per_locale:
prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
else:
prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
prnt(COPYRIGHT_NOTICE, file=outfile)
prnt("%s_DATA = {}" % varprefix, file=outfile)
for chunk_num in range(total_chunks):
prnt("from .data%d import data" % chunk_num, file=outfile)
prnt("%s_DATA.update(data)" % varprefix, file=outfile)
prnt("del data", file=outfile)
prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix), file=outfile)