Skip to content

Commit a0e87ae

Browse files
committed
Use dynamic __import__ rather than lots of generated import fns
1 parent 0378f62 commit a0e87ae

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

python/phonenumbers/phonemetadata.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def metadata_for_region(kls, region_code, default=None):
229229
loader = kls._region_available.get(region_code, None)
230230
if loader is not None:
231231
# Region metadata is available but has not yet been loaded. Do so now.
232-
loader()
232+
loader(region_code)
233233
kls._region_available[region_code] = None
234234
return kls._region_metadata.get(region_code, default)
235235

@@ -238,7 +238,7 @@ def metadata_for_nongeo_region(kls, country_code, default=None):
238238
loader = kls._country_code_available.get(country_code, None)
239239
if loader is not None:
240240
# Region metadata is available but has not yet been loaded. Do so now.
241-
loader()
241+
loader(country_code)
242242
kls._country_code_available[country_code] = None
243243
return kls._country_code_metadata.get(country_code, default)
244244

@@ -263,11 +263,11 @@ def load_all(kls):
263263
# Use .items() not .iteritems() because we would invalidate the iterator
264264
for region_code, loader in kls._region_available.items():
265265
if loader is not None: # pragma no cover
266-
loader()
266+
loader(region_code)
267267
kls._region_available[region_code] = None
268268
for country_code, loader in kls._country_code_available.items():
269269
if loader is not None:
270-
loader()
270+
loader(country_code)
271271
kls._country_code_available[region_code] = None
272272

273273
@mutating_method

tools/python/buildmetadatafromxml.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ def u(s):
8383
# Boilerplate text for generated Python files
8484
METADATA_FILE_PROLOG = '"""Auto-generated file, do not edit by hand."""'
8585
METADATA_FILE_IMPORT = "from %(module)s.phonemetadata import PhoneMetadata\n"
86+
METADATA_FILE_LOOP = '''
87+
def _load_region(code):
88+
__import__("region_%s" % code, globals(), locals(),
89+
fromlist=["PHONE_METADATA_%s" % code], level=1)
90+
91+
for country_code in _AVAILABLE_NONGEO_COUNTRY_CODES:
92+
PhoneMetadata.register_nongeo_region_loader(country_code, _load_region)
93+
94+
for region_code in _AVAILABLE_REGION_CODES:
95+
PhoneMetadata.register_region_loader(region_code, _load_region)
96+
'''
97+
8698
_COUNTRY_CODE_TO_REGION_CODE_PROLOG = '''
8799
# A mapping from a country code to the region codes which
88100
# denote the country/region represented by that country code.
@@ -521,17 +533,18 @@ def emit_metadata_py(self, datadir, module_prefix):
521533
prnt(METADATA_FILE_PROLOG, file=outfile)
522534
prnt(COPYRIGHT_NOTICE, file=outfile)
523535
prnt(METADATA_FILE_IMPORT % {'module': module_prefix}, file=outfile)
536+
nongeo_codes = []
537+
country_codes = []
524538
for country_id in sorted(self.territory.keys()):
525539
terrobj = self.territory[country_id]
526540
if terrobj.o.id == REGION_CODE_FOR_NON_GEO_ENTITY:
527-
prnt("def _load_nongeo_region_%s():" % country_id, file=outfile);
528-
prnt(" from .region_%s import PHONE_METADATA_%s" % (country_id, country_id), file=outfile)
529-
prnt("PhoneMetadata.register_nongeo_region_loader(%s, _load_nongeo_region_%s)" % (country_id, country_id), file=outfile)
541+
nongeo_codes.append(country_id) # int
530542
else:
531-
prnt("def _load_region_%s():" % country_id, file=outfile);
532-
prnt(" from .region_%s import PHONE_METADATA_%s" % (country_id, country_id), file=outfile)
533-
prnt("PhoneMetadata.register_region_loader('%s', _load_region_%s)" % (country_id, country_id), file=outfile)
534-
prnt("", file=outfile)
543+
country_codes.append("'%s'" % country_id) # quoted string
544+
prnt("_AVAILABLE_NONGEO_COUNTRY_CODES = [%s]" % ", ".join(nongeo_codes), file=outfile)
545+
prnt("_AVAILABLE_REGION_CODES = [%s]" % ",".join(country_codes), file=outfile)
546+
prnt(METADATA_FILE_LOOP, file=outfile)
547+
535548
if self.alt_territory is not None:
536549
for country_code in sorted(self.alt_territory.keys()):
537550
prnt("from .alt_format_%s import PHONE_ALT_FORMAT_%s" % (country_code, country_code), file=outfile)

0 commit comments

Comments
 (0)