forked from daviddrysdale/python-phonenumbers
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshortnumberinfo.py
More file actions
362 lines (297 loc) · 15.1 KB
/
Copy pathshortnumberinfo.py
File metadata and controls
362 lines (297 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""Methods for getting information about short phone numbers,
such as short codes and emergency numbers.
Note most commercial short numbers are not handled here, but by phonenumberutil.py
"""
# Based on original Java code:
# java/src/com/google/i18n/phonenumbers/ShortNumberInfo.java
# Copyright (C) 2013 The Libphonenumber Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from .re_util import fullmatch
from .util import U_EMPTY_STRING
from .phonemetadata import PhoneMetadata
from .phonenumberutil import _extract_possible_number, _PLUS_CHARS_PATTERN
from .phonenumberutil import normalize_digits_only, region_codes_for_country_code
from .phonenumberutil import national_significant_number
from .phonenumberutil import _is_number_possible_for_desc, _is_number_matching_desc
# In these countries, if extra digits are added to an emergency number, it no longer connects
# to the emergency service.
_REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT = set(["BR", "CL", "NI"])
class ShortNumberCost(object):
"""Cost categories of short numbers."""
TOLL_FREE = 0
STANDARD_RATE = 1
PREMIUM_RATE = 2
UNKNOWN_COST = 3
def is_possible_short_number_for_region(short_number, region_dialing_from):
"""Check whether a short number is a possible number when dialled from a
region, given the number in the form of a string, and the region where the
number is dialed from. This provides a more lenient check than
is_valid_short_number_for_region.
Arguments:
short_number -- the short number to check as a string
region_dialing_from -- the region from which the number is dialed
Return whether the number is a possible short number.
"""
metadata = PhoneMetadata.short_metadata_for_region(region_dialing_from)
if metadata is None:
return False
general_desc = metadata.general_desc
return _is_number_possible_for_desc(short_number, general_desc)
def is_possible_short_number(numobj):
"""Check whether a short number is a possible number.
If a country calling code is shared by multiple regions, this returns True
if it's possible in any of them. This provides a more lenient check than
is_valid_short_number.
Arguments:
numobj -- the short number to check
Return whether the number is a possible short number.
"""
region_codes = region_codes_for_country_code(numobj.country_code)
short_number = national_significant_number(numobj)
for region in region_codes:
metadata = PhoneMetadata.short_metadata_for_region(region)
if _is_number_possible_for_desc(short_number, metadata.general_desc):
return True
return False
def is_valid_short_number_for_region(short_number, region_dialing_from):
"""Tests whether a short number matches a valid pattern in a region.
Note that this doesn't verify the number is actually in use, which is
impossible to tell by just looking at the number itself.
Arguments:
short_number -- the short number to check as a string
region_dialing_from -- the region from which the number is dialed
Return whether the short number matches a valid pattern
"""
metadata = PhoneMetadata.short_metadata_for_region(region_dialing_from)
if metadata is None:
return False
general_desc = metadata.general_desc
if (general_desc.national_number_pattern is None or
not _is_number_matching_desc(short_number, general_desc)):
return False
short_number_desc = metadata.short_code
if short_number_desc.national_number_pattern is None: # pragma no cover
return False
return _is_number_matching_desc(short_number, short_number_desc)
def is_valid_short_number(numobj):
"""Tests whether a short number matches a valid pattern.
If a country calling code is shared by multiple regions, this returns True
if it's valid in any of them. Note that this doesn't verify the number is
actually in use, which is impossible to tell by just looking at the number
itself. See is_valid_short_number_for_region for details.
Arguments:
numobj - the short number for which we want to test the validity
Return whether the short number matches a valid pattern
"""
region_codes = region_codes_for_country_code(numobj.country_code)
short_number = national_significant_number(numobj)
region_code = _region_code_for_short_number_from_region_list(numobj, region_codes)
if len(region_codes) > 1 and region_code is not None:
# If a matching region had been found for the phone number from among two or more regions,
# then we have already implicitly verified its validity for that region.
return True
return is_valid_short_number_for_region(short_number, region_code)
def expected_cost_for_region(short_number, region_dialing_from):
"""Gets the expected cost category of a short number when dialled from a
region (however, nothing is implied about its validity). If it is
important that the number is valid, then its validity must first be
checked using is_valid_short_number_for_region. Note that emergency
numbers are always considered toll-free.
Example usage:
short_number = "110"
region_code = "FR"
if phonenumbers.is_valid_short_number_for_region(short_number, region_code):
cost = phonenumbers.expected_cost(short_number, region_code) # ShortNumberCost
# Do something with the cost information here.
Arguments:
short_number -- the short number for which we want to know the expected cost category
region_dialing_from -- the region from which the number is dialed
Return the expected cost category for that region of the short
number. Returns UNKNOWN_COST if the number does not match a cost
category. Note that an invalid number may match any cost category.
"""
# Note that region_dialing_from may be None, in which case metadata will also be None.
metadata = PhoneMetadata.short_metadata_for_region(region_dialing_from)
if metadata is None:
return ShortNumberCost.UNKNOWN_COST
# The cost categories are tested in order of decreasing expense, since if
# for some reason the patterns overlap the most expensive matching cost
# category should be returned.
if _is_number_matching_desc(short_number, metadata.premium_rate):
return ShortNumberCost.PREMIUM_RATE
if _is_number_matching_desc(short_number, metadata.standard_rate):
return ShortNumberCost.STANDARD_RATE
if _is_number_matching_desc(short_number, metadata.toll_free):
return ShortNumberCost.TOLL_FREE
if is_emergency_number(short_number, region_dialing_from):
# Emergency numbers are implicitly toll-free.
return ShortNumberCost.TOLL_FREE
return ShortNumberCost.UNKNOWN_COST
def expected_cost(numobj):
"""Gets the expected cost category of a short number (however, nothing is
implied about its validity). If the country calling code is unique to a
region, this method behaves exactly the same as
get_expected_cost_for_region. However, if the country calling code is
shared by multiple regions, then it returns the highest cost in the
sequence PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason
for the position of UNKNOWN_COST in this order is that if a number is
UNKNOWN_COST in one region but STANDARD_RATE or TOLL_FREE in another, its
expected cost cannot be estimated as one of the latter since it might be a
PREMIUM_RATE number.
For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in
Canada, the expected cost returned by this method will be STANDARD_RATE,
since the NANPA countries share the same country calling code.
Note: If the region from which the number is dialed is known, it is highly preferable to call
expected_cost_for_region instead.
Arguments:
numobj -- the short number for which we want to know the expected cost category
Return the highest expected cost category of the short number in the
region(s) with the given country calling code
"""
region_codes = region_codes_for_country_code(numobj.country_code)
if len(region_codes) == 0:
return ShortNumberCost.UNKNOWN_COST
short_number = national_significant_number(numobj)
if len(region_codes) == 1:
return expected_cost_for_region(short_number, region_codes[0])
cost = ShortNumberCost.TOLL_FREE
for region_code in region_codes:
cost_for_region = expected_cost_for_region(short_number, region_code)
if cost_for_region == ShortNumberCost.PREMIUM_RATE:
return ShortNumberCost.PREMIUM_RATE
elif cost_for_region == ShortNumberCost.UNKNOWN_COST:
return ShortNumberCost.UNKNOWN_COST
elif cost_for_region == ShortNumberCost.STANDARD_RATE:
if cost != ShortNumberCost.UNKNOWN_COST:
cost = ShortNumberCost.STANDARD_RATE
elif cost_for_region == ShortNumberCost.TOLL_FREE:
# Do nothing
pass
else: # pragma no cover
raise Exception("Unrecognized cost for region: %s", cost_for_region)
return cost
def _region_code_for_short_number_from_region_list(numobj, region_codes):
"""Helper method to get the region code for a given phone number, from a list of possible region
codes. If the list contains more than one region, the first region for which the number is
valid is returned.
"""
if len(region_codes) == 0:
return None
elif len(region_codes) == 1:
return region_codes[0]
national_number = national_significant_number(numobj)
for region_code in region_codes:
metadata = PhoneMetadata.short_metadata_for_region(region_code)
if metadata is not None and _is_number_matching_desc(national_number, metadata.short_code):
# The number is valid for this region.
return region_code
return None
def _example_short_number(region_code):
"""Gets a valid short number for the specified region.
Arguments:
region_code -- the region for which an example short number is needed.
Returns a valid short number for the specified region. Returns an empty
string when the metadata does not contain such information.
"""
metadata = PhoneMetadata.short_metadata_for_region(region_code)
if metadata is None:
return U_EMPTY_STRING
desc = metadata.short_code
if desc.example_number is not None:
return desc.example_number
return U_EMPTY_STRING
def _example_short_number_for_cost(region_code, cost):
"""Gets a valid short number for the specified cost category.
Arguments:
region_code -- the region for which an example short number is needed.
cost -- the cost category of number that is needed.
Returns a valid short number for the specified region and cost
category. Returns an empty string when the metadata does not contain such
information, or the cost is UNKNOWN_COST.
"""
metadata = PhoneMetadata.short_metadata_for_region(region_code)
if metadata is None:
return U_EMPTY_STRING
desc = None
if cost == ShortNumberCost.TOLL_FREE:
desc = metadata.toll_free
elif cost == ShortNumberCost.STANDARD_RATE:
desc = metadata.standard_rate
elif cost == ShortNumberCost.PREMIUM_RATE:
desc = metadata.premium_rate
else:
# ShortNumberCost.UNKNOWN_COST numbers are computed by the process of
# elimination from the other cost categoried.
pass
if desc is not None and desc.example_number is not None:
return desc.example_number
return U_EMPTY_STRING
def connects_to_emergency_number(number, region_code):
"""Returns whether the number might be used to connect to an emergency
service in the given region.
This function takes into account cases where the number might contain
formatting, or might have additional digits appended (when it is okay to
do that in the region specified).
Arguments:
number -- The phone number to test.
region_code -- The region where the phone number is being dialed.
Returns whether the number might be used to connect to an emergency
service in the given region.
"""
return _matches_emergency_number_helper(number, region_code, True) # Allows prefix match
def is_emergency_number(number, region_code):
"""Returns true if the number exactly matches an emergency service number
in the given region.
This method takes into account cases where the number might contain
formatting, but doesn't allow additional digits to be appended.
Arguments:
number -- The phone number to test.
region_code -- The region where the phone number is being dialed.
Returns if the number exactly matches an emergency services number in the
given region.
"""
return _matches_emergency_number_helper(number, region_code, False) # Doesn't allow prefix match
def _matches_emergency_number_helper(number, region_code, allow_prefix_match):
number = _extract_possible_number(number)
if _PLUS_CHARS_PATTERN.match(number):
# Returns False if the number starts with a plus sign. We don't
# believe dialing the country code before emergency numbers
# (e.g. +1911) works, but later, if that proves to work, we can add
# additional logic here to handle it.
return False
metadata = PhoneMetadata.short_metadata_for_region(region_code.upper(), None)
if metadata is None or metadata.emergency is None:
return False
emergency_number_pattern = re.compile(metadata.emergency.national_number_pattern)
normalized_number = normalize_digits_only(number)
if not allow_prefix_match or region_code in _REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT:
return fullmatch(emergency_number_pattern, normalized_number) is not None
else:
return emergency_number_pattern.match(normalized_number) is not None
def is_carrier_specific(numobj):
"""Given a valid short number, determines whether it is carrier-specific
(however, nothing is implied about its validity). If it is important that
the number is valid, then its validity must first be checked using
is_valid_short_number or is_valid_short_number_for_region.
Arguments:
numobj -- the valid short number to check
Returns whether the short number is carrier-specific (assuming the input
was a valid short number).
"""
region_codes = region_codes_for_country_code(numobj.country_code)
region_code = _region_code_for_short_number_from_region_list(numobj, region_codes)
national_number = national_significant_number(numobj)
metadata = PhoneMetadata.short_metadata_for_region(region_code)
return (metadata is not None and _is_number_matching_desc(national_number, metadata.carrier_specific))