Skip to content

Commit 51dd95f

Browse files
committed
Generate on-demand loaders for metadata
1 parent 4f976c7 commit 51dd95f

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

tools/python/buildmetadatafromxml.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def u(s):
8282

8383
# Boilerplate text for generated Python files
8484
METADATA_FILE_PROLOG = '"""Auto-generated file, do not edit by hand."""'
85+
METADATA_FILE_IMPORT = "from %(module)s.phonemetadata import PhoneMetadata\n"
8586
_COUNTRY_CODE_TO_REGION_CODE_PROLOG = '''
8687
# A mapping from a country code to the region codes which
8788
# denote the country/region represented by that country code.
@@ -519,8 +520,18 @@ def emit_metadata_py(self, datadir, module_prefix):
519520
with open(modulefilename, "w") as outfile:
520521
prnt(METADATA_FILE_PROLOG, file=outfile)
521522
prnt(COPYRIGHT_NOTICE, file=outfile)
523+
prnt(METADATA_FILE_IMPORT % {'module': module_prefix}, file=outfile)
522524
for country_id in sorted(self.territory.keys()):
523-
prnt("from .region_%s import PHONE_METADATA_%s" % (country_id, country_id), file=outfile)
525+
terrobj = self.territory[country_id]
526+
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)
530+
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)
524535
if self.alt_territory is not None:
525536
for country_code in sorted(self.alt_territory.keys()):
526537
prnt("from .alt_format_%s import PHONE_ALT_FORMAT_%s" % (country_code, country_code), file=outfile)

0 commit comments

Comments
 (0)