@@ -83,6 +83,18 @@ def u(s):
8383# Boilerplate text for generated Python files
8484METADATA_FILE_PROLOG = '"""Auto-generated file, do not edit by hand."""'
8585METADATA_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