Skip to content

Commit 9054bd2

Browse files
committed
Merge code and metadata changes from upstream r603.
Main code change is to rename shortnumberutil to shortnumberinfo, and add some new entrypoints therein.
1 parent 19b267f commit 9054bd2

17 files changed

Lines changed: 7040 additions & 3109 deletions

python/phonenumbers/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@
113113
region_codes_for_country_code,
114114
region_code_for_number,
115115
truncate_too_long_number,)
116-
from .shortnumberutil import connects_to_emergency_number, is_emergency_number, ShortNumberCost
116+
from .shortnumberinfo import (ShortNumberCost,
117+
is_possible_short_number,
118+
is_possible_short_number_object,
119+
is_valid_short_number,
120+
is_valid_short_number_object,
121+
expected_cost,
122+
connects_to_emergency_number,
123+
is_emergency_number,
124+
is_carrier_specific)
117125
from .phonenumbermatcher import PhoneNumberMatch, PhoneNumberMatcher, Leniency
118126

119127

@@ -251,7 +259,17 @@ def description_for_valid_number(*args, **kwargs):
251259
'region_code_for_number',
252260
'truncate_too_long_number',
253261
# end of items from phonenumberutil.py
254-
'connects_to_emergency_number', 'is_emergency_number', 'ShortNumberCost',
262+
# items from shortnumberinfo.py
263+
'ShortNumberCost',
264+
'is_possible_short_number',
265+
'is_possible_short_number_object',
266+
'is_valid_short_number',
267+
'is_valid_short_number_object',
268+
'expected_cost',
269+
'connects_to_emergency_number',
270+
'is_emergency_number',
271+
'is_carrier_specific',
272+
# end of items from shortnumberinfo.py
255273
'PhoneNumberMatch', 'PhoneNumberMatcher', 'Leniency',
256274
'country_name_for_number',
257275
'description_for_number',

python/phonenumbers/asyoutypeformatter.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,14 @@ def _append_national_number(self, national_number):
435435
def _attempt_to_choose_formatting_pattern(self):
436436
"""Attempts to set the formatting template and returns a string which
437437
contains the formatted version of the digits entered so far."""
438-
# We start to attempt to format only when as least MIN_LEADING_DIGITS_LENGTH digits of national
438+
# We start to attempt to format only when at least MIN_LEADING_DIGITS_LENGTH digits of national
439439
# number (excluding national prefix) have been entered.
440440
if len(self._national_number) >= _MIN_LEADING_DIGITS_LENGTH:
441441
self._get_available_formats(self._national_number[:_MIN_LEADING_DIGITS_LENGTH])
442+
# See if the accrued digits can be formatted properly already.
443+
formatted_number = self._attempt_to_format_accrued_digits()
444+
if len(formatted_number) > 0:
445+
return formatted_number
442446
if self._maybe_create_new_template():
443447
return self._input_accrued_national_number()
444448
else:
@@ -482,7 +486,9 @@ def _remove_national_prefix_from_national_number(self):
482486
elif self._current_metadata.national_prefix_for_parsing is not None:
483487
npp_re = re.compile(self._current_metadata.national_prefix_for_parsing)
484488
m = npp_re.match(self._national_number)
485-
if m:
489+
# Since some national prefix patterns are entirely optional, check
490+
# that a national prefix could actually be extracted.
491+
if m and m.end() > 0:
486492
# When the national prefix is detected, we use international
487493
# formatting rules instead of national ones, because national
488494
# formatting rules could contain local formatting rules for

python/phonenumbers/phonemetadata.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def __init__(self,
303303
voicemail=None,
304304
short_code=None,
305305
standard_rate=None,
306+
carrier_specific=None,
306307
no_international_dialling=None,
307308
country_code=None,
308309
international_prefix=None,
@@ -342,6 +343,7 @@ def __init__(self,
342343
self.voicemail = voicemail # None or PhoneNumberDesc
343344
self.short_code = short_code # None or PhoneNumberDesc
344345
self.standard_rate = standard_rate # None or PhoneNumberDesc
346+
self.carrier_specific = carrier_specific # None or PhoneNumberDesc
345347

346348
# The rules here distinguish the numbers that are only able to be
347349
# dialled nationally.
@@ -544,6 +546,8 @@ def __unicode__(self):
544546
result += ",\n short_code=%s" % self.short_code
545547
if self.standard_rate is not None:
546548
result += ",\n standard_rate=%s" % self.standard_rate
549+
if self.carrier_specific is not None:
550+
result += ",\n carrier_specific=%s" % self.carrier_specific
547551
if self.no_international_dialling is not None:
548552
result += ",\n no_international_dialling=%s" % self.no_international_dialling
549553

python/phonenumbers/phonenumbermatcher.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
from .phonenumberutil import MatchType, NumberParseException, PhoneNumberFormat
3131
from .phonenumberutil import is_possible_number, is_valid_number, parse
3232
from .phonenumberutil import normalize_digits_only, national_significant_number
33-
from .phonenumberutil import format_nsn_using_pattern
33+
from .phonenumberutil import format_nsn_using_pattern, ndd_prefix_for_region
3434
from .phonenumberutil import format_number, is_number_match, region_code_for_country_code
3535
from .phonenumberutil import _maybe_strip_national_prefix_carrier_code
3636
from .phonenumberutil import choose_formatting_pattern_for_number
37+
from .phonenumberutil import _formatting_rule_has_first_group_only
3738
from .phonenumber import CountryCodeSource
3839
from .phonemetadata import PhoneMetadata
3940

@@ -236,12 +237,19 @@ def _all_number_groups_remain_grouped(numobj, normalized_candidate, formatted_nu
236237
# Moves from_index forward.
237238
from_index += len(formatted_number_group)
238239
if (ii == 0 and from_index < len(normalized_candidate)):
239-
# We are at the position right after the NDC.
240-
if normalized_candidate[from_index].isdigit():
240+
# We are at the position right after the NDC. We get the region
241+
# used for formatting information based on the country code in the
242+
# phone number, rather than the number itself, as we do not need
243+
# to distinguish between different countries with the same country
244+
# calling code and this is faster.
245+
region = region_code_for_country_code(numobj.country_code)
246+
if (ndd_prefix_for_region(region, True) is not None and
247+
normalized_candidate[from_index].isdigit()):
241248
# This means there is no formatting symbol after the NDC. In
242249
# this case, we only accept the number if there is no
243250
# formatting symbol at all in the number, except for
244-
# extensions.
251+
# extensions. This is only important for countries with
252+
# national prefixes.
245253
nsn = national_significant_number(numobj)
246254
return normalized_candidate[(from_index - len(formatted_number_group)):].startswith(nsn)
247255
# The check here makes sure that we haven't mistakenly already used the extension to
@@ -409,13 +417,7 @@ def _is_national_prefix_present_if_required(numobj):
409417
# The national-prefix is optional in these cases, so we don't need
410418
# to check if it was present.
411419
return True
412-
# Remove the first-group symbol.
413-
candidate_national_prefix_rule = format_rule.national_prefix_formatting_rule
414-
# We assume that the first-group symbol will never be _before_ the
415-
# national prefix.
416-
candidate_national_prefix_rule = candidate_national_prefix_rule[:candidate_national_prefix_rule.find("\\1")]
417-
candidate_national_prefix_rule = normalize_digits_only(candidate_national_prefix_rule)
418-
if len(candidate_national_prefix_rule) == 0:
420+
if _formatting_rule_has_first_group_only(format_rule.national_prefix_formatting_rule):
419421
# National Prefix not needed for this number.
420422
return True
421423
# Normalize the remainder.

python/phonenumbers/phonenumberutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ def _maybe_extract_country_code(number, metadata, keep_raw_input, numobj):
20972097
full_number = number
20982098
# Set the default prefix to be something that will never match.
20992099
possible_country_idd_prefix = u"NonMatch"
2100-
if metadata is not None:
2100+
if metadata is not None and metadata.international_prefix is not None:
21012101
possible_country_idd_prefix = metadata.international_prefix
21022102

21032103
country_code_source, full_number = _maybe_strip_i18n_prefix_and_normalize(full_number,

0 commit comments

Comments
 (0)