4747
4848# Convention: variables beginning with 'x' are XML objects
4949
50+ REGION_CODE_FOR_NON_GEO_ENTITY = "001"
51+
5052# Top-level XML element containing data
5153TOP_XPATH = "territories"
5254# XML element name for the territory element
@@ -262,8 +264,8 @@ def __init__(self, xterritory):
262264 id = xterritory .attrib ['id' ]
263265 self .o = PhoneMetadata (id , register = False )
264266 self .o .country_code = int (xterritory .attrib ['countryCode' ])
265- self .o .international_prefix = xterritory .attrib ['internationalPrefix' ]
266267 # Retrieve the IMPLIED attributes
268+ self .o .international_prefix = xterritory .get ('internationalPrefix' , None )
267269 self .o .leading_digits = xterritory .get ('leadingDigits' , None )
268270 self .o .preferred_international_prefix = xterritory .get ('preferredInternationalPrefix' , None )
269271 self .o .national_prefix = xterritory .get ('nationalPrefix' , None )
@@ -347,6 +349,15 @@ def __init__(self, xterritory):
347349 # national formats.
348350 self .o .intl_number_format = []
349351
352+ def identifier (self ):
353+ if self .o .id == REGION_CODE_FOR_NON_GEO_ENTITY :
354+ # For non-geographical country calling codes (e.g. +800), use the
355+ # country calling codes instead of the region code to form the
356+ # file name.
357+ return str (self .o .country_code )
358+ else :
359+ return self .o .id
360+
350361 def __unicode__ (self ):
351362 return u (self .o )
352363
@@ -365,9 +376,10 @@ def __init__(self, filename):
365376 for xterritory in xterritories :
366377 if xterritory .tag == TERRITORY_TAG :
367378 terrobj = XTerritory (xterritory )
368- if terrobj .o .id in self .territory :
369- raise Exception ("Duplicate entry for %s" % terrobj .o .id )
370- self .territory [terrobj .o .id ] = terrobj
379+ id = terrobj .identifier () # like "US" for countries, "800" for non-geo
380+ if id in self .territory :
381+ raise Exception ("Duplicate entry for %s" % id )
382+ self .territory [id ] = terrobj
371383 else :
372384 raise Exception ("Unexpected element %s found" % xterritory .tag )
373385
@@ -378,8 +390,8 @@ def emit_metadata_for_region_py(self, region, region_filename, module_prefix):
378390 """Emit Python code generating the metadata for the given region"""
379391 terrobj = self .territory [region ]
380392 with open (region_filename , "w" ) as outfile :
381- prnt (_REGION_METADATA_PROLOG % {'region' : terrobj .o . id , 'module' : module_prefix }, file = outfile )
382- prnt ("PHONE_METADATA_%s = %s" % (terrobj .o . id , terrobj ), file = outfile )
393+ prnt (_REGION_METADATA_PROLOG % {'region' : terrobj .identifier () , 'module' : module_prefix }, file = outfile )
394+ prnt ("PHONE_METADATA_%s = %s" % (terrobj .identifier () , terrobj ), file = outfile )
383395
384396 def emit_metadata_py (self , datadir , module_prefix ):
385397 """Emit Python code for the phone number metadata to the given file, and
@@ -411,9 +423,9 @@ def emit_metadata_py(self, datadir, module_prefix):
411423 if country_code not in country_code_to_region_code :
412424 country_code_to_region_code [country_code ] = []
413425 if terrobj .o .main_country_for_code :
414- country_code_to_region_code [country_code ].insert (0 , country_id )
426+ country_code_to_region_code [country_code ].insert (0 , terrobj . o . id )
415427 else :
416- country_code_to_region_code [country_code ].append (country_id )
428+ country_code_to_region_code [country_code ].append (terrobj . o . id )
417429
418430 for country_code in sorted (country_code_to_region_code .keys ()):
419431 country_ids = country_code_to_region_code [country_code ]
0 commit comments