@@ -85,14 +85,15 @@ def u(s):
8585METADATA_FILE_IMPORT = "from %(module)s.phonemetadata import PhoneMetadata\n "
8686METADATA_FILE_LOOP = '''
8787def _load_region(code):
88- __import__("region_%s" % code, globals(), locals(),
89- fromlist=["PHONE_METADATA_%s" % code], level=1)
88+ __import__("region_%% s" % % code, globals(), locals(),
89+ fromlist=["PHONE_METADATA_%% s" % % code], level=1)
9090
91+ for region_code in _AVAILABLE_REGION_CODES:
92+ PhoneMetadata.register_%(prefix)sregion_loader(region_code, _load_region)
93+ '''
94+ METADATA_NONGEO_FILE_LOOP = '''
9195for country_code in _AVAILABLE_NONGEO_COUNTRY_CODES:
9296 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)
9697'''
9798
9899_COUNTRY_CODE_TO_REGION_CODE_PROLOG = '''
@@ -295,8 +296,7 @@ def __unicode__(self):
295296
296297class XPhoneNumberDesc (UnicodeMixin ):
297298 """Parse PhoneNumberDesc object from XML element"""
298- def __init__ (self , xtag ,
299- template = None , fill_na = True ):
299+ def __init__ (self , xtag , template = None , fill_na = True ):
300300 self .o = PhoneNumberDesc ()
301301 self .o ._mutable = True
302302 self .o .national_number_pattern = None
@@ -350,10 +350,10 @@ def __unicode__(self):
350350
351351class XTerritory (UnicodeMixin ):
352352 """Parse PhoneMetadata from XML element (territory)"""
353- def __init__ (self , xterritory ):
353+ def __init__ (self , xterritory , short_data ):
354354 # Retrieve the REQUIRED attributes
355355 id = xterritory .attrib ['id' ]
356- self .o = PhoneMetadata (id , register = False )
356+ self .o = PhoneMetadata (id , short_data = short_data , register = False )
357357 self .o ._mutable = True
358358 if 'countryCode' in xterritory .attrib :
359359 self .o .country_code = int (xterritory .attrib ['countryCode' ])
@@ -461,7 +461,7 @@ def __unicode__(self):
461461
462462class XPhoneNumberMetadata (UnicodeMixin ):
463463 """Entire collection of phone number metadata retrieved from XML"""
464- def __init__ (self , filename ):
464+ def __init__ (self , filename , short_data ):
465465 # Load the XML data from the given filename
466466 with open (filename , "r" ) as infile :
467467 xtree = etree .parse (infile )
@@ -472,14 +472,15 @@ def __init__(self, filename):
472472 self .territory = {}
473473 for xterritory in xterritories :
474474 if xterritory .tag == TERRITORY_TAG :
475- terrobj = XTerritory (xterritory )
475+ terrobj = XTerritory (xterritory , short_data )
476476 id = terrobj .identifier () # like "US" for countries, "800" for non-geo
477477 if id in self .territory :
478478 raise Exception ("Duplicate entry for %s" % id )
479479 self .territory [id ] = terrobj
480480 else :
481481 raise Exception ("Unexpected element %s found" % xterritory .tag )
482482 self .alt_territory = None
483+ self .short_data = short_data
483484
484485 def add_alternate_formats (self , filename ):
485486 """Add phone number alternate format metadata retrieved from XML"""
@@ -546,20 +547,22 @@ def emit_metadata_py(self, datadir, module_prefix):
546547 nongeo_codes .append (country_id ) # int
547548 else :
548549 country_codes .append ("'%s'" % country_id ) # quoted string
549- prnt ("_AVAILABLE_NONGEO_COUNTRY_CODES = [%s]" % ", " .join (nongeo_codes ), file = outfile )
550550 prnt ("_AVAILABLE_REGION_CODES = [%s]" % "," .join (country_codes ), file = outfile )
551- prnt (METADATA_FILE_LOOP , file = outfile )
551+ if len (nongeo_codes ) > 0 :
552+ prnt ("_AVAILABLE_NONGEO_COUNTRY_CODES = [%s]" % ", " .join (nongeo_codes ), file = outfile )
553+ register_prefix = "short_" if self .short_data else ""
554+ prnt (METADATA_FILE_LOOP % {'prefix' : register_prefix }, file = outfile )
555+ if len (nongeo_codes ) > 0 :
556+ prnt (METADATA_NONGEO_FILE_LOOP , file = outfile )
552557
553558 if self .alt_territory is not None :
554559 for country_code in sorted (self .alt_territory .keys ()):
555560 prnt ("from .alt_format_%s import PHONE_ALT_FORMAT_%s" % (country_code , country_code ), file = outfile )
556561 prnt ("_ALT_NUMBER_FORMATS = {%s}" %
557562 ", " .join (["%s: PHONE_ALT_FORMAT_%s" % (cc , cc ) for cc in sorted (self .alt_territory .keys ())]),
558563 file = outfile )
559- # Emit the mapping from country code to region code
560- prnt (_COUNTRY_CODE_TO_REGION_CODE_PROLOG , file = outfile )
561- prnt ("_COUNTRY_CODE_TO_REGION_CODE = {" , file = outfile )
562- # Build up the map
564+
565+ # Build up a map from country code (int) to list of region codes (ISO 3166-1 alpha 2)
563566 country_code_to_region_code = {}
564567 for country_id in sorted (self .territory .keys ()):
565568 terrobj = self .territory [country_id ]
@@ -572,24 +575,31 @@ def emit_metadata_py(self, datadir, module_prefix):
572575 else :
573576 country_code_to_region_code [country_code ].append (terrobj .o .id )
574577
575- for country_code in sorted (country_code_to_region_code .keys ()):
576- country_ids = country_code_to_region_code [country_code ]
577- prnt (' %d: ("%s",),' % (country_code , '", "' .join (country_ids )), file = outfile )
578- prnt ("}" , file = outfile )
578+ # Emit the mapping from country code to region code if nonempty.
579+ if len (country_code_to_region_code .keys ()) > 0 :
580+ prnt (_COUNTRY_CODE_TO_REGION_CODE_PROLOG , file = outfile )
581+ prnt ("_COUNTRY_CODE_TO_REGION_CODE = {" , file = outfile )
582+ for country_code in sorted (country_code_to_region_code .keys ()):
583+ country_ids = country_code_to_region_code [country_code ]
584+ prnt (' %d: ("%s",),' % (country_code , '", "' .join (country_ids )), file = outfile )
585+ prnt ("}" , file = outfile )
579586
580587
581588def _standalone (argv ):
582589 """Parse the given XML file and emit generated code."""
583590 alternate = None
591+ short_data = False
584592 try :
585- opts , args = getopt .getopt (argv , "ha :" , ("help" , "alt=" ))
593+ opts , args = getopt .getopt (argv , "hsa :" , ("help" , "short " , "alt=" ))
586594 except getopt .GetoptError :
587595 prnt (__doc__ , file = sys .stderr )
588596 sys .exit (1 )
589597 for opt , arg in opts :
590598 if opt in ("-h" , "--help" ):
591599 prnt (__doc__ , file = sys .stderr )
592600 sys .exit (1 )
601+ elif opt in ("-s" , "--short" ):
602+ short_data = True
593603 elif opt in ("-a" , "--alt" ):
594604 alternate = arg
595605 else :
@@ -600,7 +610,7 @@ def _standalone(argv):
600610 if len (args ) != 3 :
601611 prnt (__doc__ , file = sys .stderr )
602612 sys .exit (1 )
603- pmd = XPhoneNumberMetadata (args [0 ])
613+ pmd = XPhoneNumberMetadata (args [0 ], short_data )
604614 if alternate is not None :
605615 pmd .add_alternate_formats (alternate )
606616 pmd .emit_metadata_py (args [1 ], args [2 ])
0 commit comments