|
3 | 3 |
|
4 | 4 | If you use this library, and want to be notified about important changes, |
5 | 5 | please sign up to the libphonenumber mailing list at |
6 | | -http://groups.google.com/group/libphonenumber-discuss/about. |
| 6 | +https://groups.google.com/forum/#!aboutgroup/libphonenumber-discuss. |
7 | 7 |
|
8 | 8 | NOTE: A lot of methods in this module require Region Code strings. These must |
9 | 9 | be provided using CLDR two-letter region-code format. These should be in |
10 | 10 | upper-case. The list of the codes can be found here: |
11 | 11 | http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm |
12 | | -
|
13 | | -author: Shaopeng Jia (original Java version) |
14 | | -author: David Drysdale (Python version) |
15 | 12 | """ |
16 | 13 | # Based on original Java code: |
17 | 14 | # java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java |
|
186 | 183 | [(_c, _c) for _c in _ALPHA_MAPPINGS.keys()], |
187 | 184 | **_ASCII_DIGITS_MAP)) |
188 | 185 |
|
189 | | -# Pattern that makes it easy to distinguish whether a region has a unique |
190 | | -# international dialing prefix or not. If a region has a unique international |
191 | | -# prefix (e.g. 011 in USA), it will be represented as a string that contains a |
192 | | -# sequence of ASCII digits. If there are multiple available international |
193 | | -# prefixes in a region, they will be represented as a regex string that always |
194 | | -# contains character(s) other than ASCII digits. Note this regex also |
195 | | -# includes tilde, which signals waiting for the tone. |
196 | | -_UNIQUE_INTERNATIONAL_PREFIX = re.compile(u("[\\d]+(?:[~\u2053\u223C\uFF5E][\\d]+)?")) |
| 186 | +# Pattern that makes it easy to distinguish whether a region has a single international dialing |
| 187 | +# prefix or not. If a region has a single international prefix (e.g. 011 in USA), it will be |
| 188 | +# represented as a string that contains a sequence of ASCII digits, and possibly a tilde, which |
| 189 | +# signals waiting for the tone. If there are multiple available international prefixes in a |
| 190 | +# region, they will be represented as a regex string that always contains one or more characters |
| 191 | +# that are not ASCII digits or a tilde. |
| 192 | +_SINGLE_INTERNATIONAL_PREFIX = re.compile(u("[\\d]+(?:[~\u2053\u223C\uFF5E][\\d]+)?")) |
197 | 193 |
|
198 | 194 | # Regular expression of acceptable punctuation found in phone numbers. This |
199 | | -# excludes punctuation found as a leading character only. This consists of |
200 | | -# dash characters, white space characters, full stops, slashes, square |
201 | | -# brackets, parentheses and tildes. It also includes the letter 'x' as that is |
202 | | -# found as a placeholder for carrier information in some phone numbers. Full-width |
203 | | -# variants are also present. |
| 195 | +# excludes punctuation found as a leading character only. |
| 196 | + |
| 197 | +# Regular expression of acceptable punctuation found in phone numbers, used to find numbers in |
| 198 | +# text and to decide what is a viable phone number. This excludes diallable characters. |
| 199 | +# This consists of dash characters, white space characters, full stops, slashes, square brackets, |
| 200 | +# parentheses and tildes. It also includes the letter 'x' as that is found as a placeholder for |
| 201 | +# carrier information in some phone numbers. Full-width variants are also present. |
204 | 202 | _VALID_PUNCTUATION = (u("-x\u2010-\u2015\u2212\u30FC\uFF0D-\uFF0F ") + |
205 | 203 | u("\u00A0\u00AD\u200B\u2060\u3000()\uFF08\uFF09\uFF3B\uFF3D.\\[\\]/~\u2053\u223C\uFF5E")) |
206 | 204 |
|
@@ -346,9 +344,11 @@ def _create_extn_pattern(single_extn_symbols): |
346 | 344 | # use \d, so that the first group actually used in the pattern will be |
347 | 345 | # matched. |
348 | 346 | _FIRST_GROUP_PATTERN = re.compile(u(r"(\\\d)")) |
349 | | -_NP_PATTERN = re.compile(u("\\$NP")) |
350 | | -_FG_PATTERN = re.compile(u("\\$FG")) |
351 | | -_CC_PATTERN = re.compile(u("\\$CC")) |
| 347 | +# Constants used in the formatting rules to represent the national prefix, first group and |
| 348 | +# carrier code respectively. |
| 349 | +_NP_STRING = "$NP" |
| 350 | +_FG_STRING = "$FG" |
| 351 | +_CC_STRING = "$CC" |
352 | 352 |
|
353 | 353 | # A pattern that is used to determine if the national prefix formatting rule |
354 | 354 | # has the first group only, i.e., does not start with the national |
@@ -572,7 +572,7 @@ def _normalize(number): |
572 | 572 | keypad. The keypad used here is the one defined in ITU |
573 | 573 | Recommendation E.161. This is only done if there are 3 or more |
574 | 574 | letters in the number, to lessen the risk that such letters are |
575 | | - typos - otherwise alpha characters are stripped. |
| 575 | + typos. |
576 | 576 | - For other numbers: |
577 | 577 | - Wide-ascii digits are converted to normal ASCII (European) digits. |
578 | 578 | - Arabic-Indic numerals are converted to European numerals. |
@@ -802,7 +802,20 @@ def _normalize_helper(number, replacements, remove_non_matches): |
802 | 802 | return U_EMPTY_STRING.join(normalized_number) |
803 | 803 |
|
804 | 804 |
|
| 805 | +def supported_calling_codes(): |
| 806 | + """Returns all country calling codes the library has metadata for, covering |
| 807 | + both non-geographical entities (global network calling codes) and those |
| 808 | + used for geographical entities. This could be used to populate a drop-down |
| 809 | + box of country calling codes for a phone-number widget, for instance. |
| 810 | +
|
| 811 | + Returns an unordered set of the country calling codes for every geographica |
| 812 | + and non-geographical entity the library supports. |
| 813 | + """ |
| 814 | + return set(COUNTRY_CODE_TO_REGION_CODE.keys()) |
| 815 | + |
| 816 | + |
805 | 817 | def _desc_has_possible_number_data(desc): |
| 818 | + |
806 | 819 | """Returns true if there is any possible number data set for a particular PhoneNumberDesc.""" |
807 | 820 | # If this is empty, it means numbers of this type inherit from the "general desc" -> the value |
808 | 821 | # "-1" means that no numbers exist for this type. |
@@ -915,7 +928,7 @@ def _is_valid_region_code(region_code): |
915 | 928 |
|
916 | 929 |
|
917 | 930 | def _has_valid_country_calling_code(country_calling_code): |
918 | | - return (country_calling_code in _COUNTRY_CODE_TO_REGION_CODE) |
| 931 | + return (country_calling_code in COUNTRY_CODE_TO_REGION_CODE) |
919 | 932 |
|
920 | 933 |
|
921 | 934 | def format_number(numobj, num_format): |
@@ -1019,14 +1032,8 @@ def format_by_pattern(numobj, number_format, user_defined_formats): |
1019 | 1032 | if national_prefix: |
1020 | 1033 | # Replace $NP with national prefix and $FG with the first |
1021 | 1034 | # group (\1) matcher. |
1022 | | - np_formatting_rule = re.sub(_NP_PATTERN, |
1023 | | - national_prefix, |
1024 | | - np_formatting_rule, |
1025 | | - count=1) |
1026 | | - np_formatting_rule = re.sub(_FG_PATTERN, |
1027 | | - unicod("\\\\1"), |
1028 | | - np_formatting_rule, |
1029 | | - count=1) |
| 1035 | + np_formatting_rule = np_formatting_rule.replace(_NP_STRING, national_prefix) |
| 1036 | + np_formatting_rule = np_formatting_rule.replace(_FG_STRING, unicod("\\1")) |
1030 | 1037 | num_format_copy.national_prefix_formatting_rule = np_formatting_rule |
1031 | 1038 | else: |
1032 | 1039 | # We don't want to have a rule for how to format the national |
@@ -1279,7 +1286,7 @@ def format_out_of_country_calling_number(numobj, region_calling_from): |
1279 | 1286 | # format of the number is returned, unless there is a preferred |
1280 | 1287 | # international prefix. |
1281 | 1288 | i18n_prefix_for_formatting = U_EMPTY_STRING |
1282 | | - i18n_match = fullmatch(_UNIQUE_INTERNATIONAL_PREFIX, international_prefix) |
| 1289 | + i18n_match = fullmatch(_SINGLE_INTERNATIONAL_PREFIX, international_prefix) |
1283 | 1290 | if i18n_match: |
1284 | 1291 | i18n_prefix_for_formatting = international_prefix |
1285 | 1292 | elif metadata_for_region_calling_from.preferred_international_prefix is not None: |
@@ -1517,7 +1524,7 @@ def format_out_of_country_keeping_alpha_chars(numobj, region_calling_from): |
1517 | 1524 | # is returned, unless there is a preferred international prefix. |
1518 | 1525 | if metadata_for_region_calling_from is not None: |
1519 | 1526 | international_prefix = metadata_for_region_calling_from.international_prefix |
1520 | | - i18n_match = fullmatch(_UNIQUE_INTERNATIONAL_PREFIX, international_prefix) |
| 1527 | + i18n_match = fullmatch(_SINGLE_INTERNATIONAL_PREFIX, international_prefix) |
1521 | 1528 | if i18n_match: |
1522 | 1529 | i18n_prefix_for_formatting = international_prefix |
1523 | 1530 | else: |
@@ -1633,10 +1640,7 @@ def _format_nsn_using_pattern(national_number, formatting_pattern, number_format |
1633 | 1640 | # Replace the $CC in the formatting rule with the desired |
1634 | 1641 | # carrier code. |
1635 | 1642 | cc_format_rule = formatting_pattern.domestic_carrier_code_formatting_rule |
1636 | | - cc_format_rule = re.sub(_CC_PATTERN, |
1637 | | - carrier_code, |
1638 | | - cc_format_rule, |
1639 | | - count=1) |
| 1643 | + cc_format_rule = cc_format_rule.replace(_CC_STRING, carrier_code) |
1640 | 1644 |
|
1641 | 1645 | # Now replace the $FG in the formatting rule with the |
1642 | 1646 | # first group and the carrier code combined in the |
@@ -2834,8 +2838,7 @@ def parse(number, region=None, keep_raw_input=False, |
2834 | 2838 | # If no extracted country calling code, use the region supplied |
2835 | 2839 | # instead. The national number is just the normalized version of the |
2836 | 2840 | # number we were given to parse. |
2837 | | - national_number = _normalize(national_number) |
2838 | | - normalized_national_number += national_number |
| 2841 | + normalized_national_number += _normalize(national_number) |
2839 | 2842 | if region is not None: |
2840 | 2843 | country_code = metadata.country_code |
2841 | 2844 | numobj.country_code = country_code |
|
0 commit comments