Skip to content

Commit 348f07e

Browse files
committed
Pull alternate number formats into matcher
1 parent a69093a commit 348f07e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

python/phonenumbers/phonenumbermatcher.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737
from .phonenumber import CountryCodeSource
3838
from .phonemetadata import PhoneMetadata
3939

40+
# Import auto-generated data structures
41+
try:
42+
from .data import _ALT_NUMBER_FORMATS
43+
except ImportError: # pragma no cover
44+
# Before the generated code exists, the data/ directory is empty.
45+
# The generation process imports this module, creating a circular
46+
# dependency. The hack below works around this.
47+
import os
48+
import sys
49+
if (os.path.basename(sys.argv[0]) == "buildmetadatafromxml.py" or
50+
os.path.basename(sys.argv[0]) == "buildgeocodingdata.py"):
51+
print >> sys.stderr, "Failed to import generated data (but OK as during autogeneration)"
52+
_ALT_NUMBER_FORMATS = {}
53+
else:
54+
raise
55+
4056

4157
def _limit(lower, upper):
4258
"""Returns a regular expression quantifier with an upper and lower limit."""
@@ -312,9 +328,9 @@ def _check_number_grouping_is_valid(numobj, candidate, checker):
312328
if checker(numobj, normalized_candidate, formatted_number_groups):
313329
return True
314330
# If this didn't pass, see if there are any alternate formats, and try them instead.
315-
alternate_formats = None # TODO: write equivalent to: MetadataManager.getAlternateFormatsForCountry(numobj.country_code)
331+
alternate_formats = _ALT_NUMBER_FORMATS.get(numobj.country_code, None)
316332
if alternate_formats is not None:
317-
for alternate_format in alternate_formats.number_formats:
333+
for alternate_format in alternate_formats:
318334
formatted_number_groups = _get_national_number_groups(numobj, alternate_format)
319335
if checker(numobj, normalized_candidate, formatted_number_groups):
320336
return True

0 commit comments

Comments
 (0)