Skip to content

Commit 8fd6ed4

Browse files
committed
Cope with missing country code in metadata.
This is needed to parse the new ShortNumberMetadata.xml file.
1 parent 42828a0 commit 8fd6ed4

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

python/phonenumbers/phonemetadata.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,8 @@ def __repr__(self):
487487

488488
def __unicode__(self):
489489
# Generate a string that is valid Python input for the constructor
490-
country_code = self.country_code
491-
if country_code is None:
492-
country_code = -1
493-
result = (u"PhoneMetadata(id='%s', country_code=%d, international_prefix=%r" %
494-
(self.id, country_code, self.international_prefix))
490+
result = (u"PhoneMetadata(id='%s', country_code=%r, international_prefix=%r" %
491+
(self.id, self.country_code, self.international_prefix))
495492
result += ",\n general_desc=%s" % self.general_desc
496493
result += ",\n fixed_line=%s" % self.fixed_line
497494
result += ",\n mobile=%s" % self.mobile

python/tests/phonenumberutiltest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2476,7 +2476,7 @@ def testMetadataAsString(self):
24762476
leading_digits='123',
24772477
leading_zero_possible=True,
24782478
register=False)
2479-
self.assertEqual("""PhoneMetadata(id='XX', country_code=-1, international_prefix='9123',
2479+
self.assertEqual("""PhoneMetadata(id='XX', country_code=None, international_prefix='9123',
24802480
general_desc=PhoneNumberDesc(example_number='12'),
24812481
fixed_line=None,
24822482
mobile=None,

tools/python/buildmetadatafromxml.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ def __init__(self, xterritory):
355355
id = xterritory.attrib['id']
356356
self.o = PhoneMetadata(id, register=False)
357357
self.o._mutable = True
358-
self.o.country_code = int(xterritory.attrib['countryCode'])
358+
if 'countryCode' in xterritory.attrib:
359+
self.o.country_code = int(xterritory.attrib['countryCode'])
360+
else:
361+
self.o.country_code = None
359362
# Retrieve the IMPLIED attributes
360363
self.o.international_prefix = xterritory.get('internationalPrefix', None)
361364
self.o.leading_digits = xterritory.get('leadingDigits', None)
@@ -558,13 +561,14 @@ def emit_metadata_py(self, datadir, module_prefix):
558561
country_code_to_region_code = {}
559562
for country_id in sorted(self.territory.keys()):
560563
terrobj = self.territory[country_id]
561-
country_code = int(terrobj.o.country_code)
562-
if country_code not in country_code_to_region_code:
563-
country_code_to_region_code[country_code] = []
564-
if terrobj.o.main_country_for_code:
565-
country_code_to_region_code[country_code].insert(0, terrobj.o.id)
566-
else:
567-
country_code_to_region_code[country_code].append(terrobj.o.id)
564+
if terrobj.o.country_code is not None:
565+
country_code = int(terrobj.o.country_code)
566+
if country_code not in country_code_to_region_code:
567+
country_code_to_region_code[country_code] = []
568+
if terrobj.o.main_country_for_code:
569+
country_code_to_region_code[country_code].insert(0, terrobj.o.id)
570+
else:
571+
country_code_to_region_code[country_code].append(terrobj.o.id)
568572

569573
for country_code in sorted(country_code_to_region_code.keys()):
570574
country_ids = country_code_to_region_code[country_code]

0 commit comments

Comments
 (0)