|
1 | 1 | """Phone number to carrier mapping functionality |
2 | 2 |
|
3 | 3 | >>> import phonenumbers |
4 | | ->>> from phonenumbers.carrier import description_for_number |
| 4 | +>>> from phonenumbers.carrier import name_for_number |
5 | 5 | >>> ro_number = phonenumbers.parse("+40721234567", "RO") |
6 | | ->>> str(description_for_number(ro_number, "en")) |
| 6 | +>>> str(name_for_number(ro_number, "en")) |
7 | 7 | 'Vodafone' |
8 | | ->>> str(description_for_number(ro_number, "fr")) # fall back to English |
| 8 | +>>> str(name_for_number(ro_number, "fr")) # fall back to English |
9 | 9 | 'Vodafone' |
10 | 10 |
|
11 | 11 | """ |
|
26 | 26 | # limitations under the License. |
27 | 27 |
|
28 | 28 | from .phonenumberutil import PhoneNumberType, number_type |
| 29 | +from .phonenumberutil import region_code_for_number |
| 30 | +from .phonenumberutil import is_mobile_number_portable_region |
29 | 31 | from .prefix import prefix_description_for_number |
30 | 32 | try: |
31 | 33 | from .carrierdata import CARRIER_DATA, CARRIER_LONGEST_PREFIX |
|
44 | 46 | raise |
45 | 47 |
|
46 | 48 |
|
47 | | -def description_for_valid_number(numobj, lang, script=None, region=None): |
48 | | - """Return a text description of a PhoneNumber object for the given language. |
| 49 | +def name_for_valid_number(numobj, lang, script=None, region=None): |
| 50 | + """Returns a carrier name for the given PhoneNumber object, in the |
| 51 | + language provided. |
49 | 52 |
|
50 | | - The description consists of the name of the carrier the number was |
51 | | - originally allocated to, however if the country supports mobile number |
52 | | - portability the number might not belong to the returned carrier |
53 | | - anymore. If no mapping is found an empty string is returned. |
| 53 | + The carrier name is the one the number was originally allocated to, |
| 54 | + however if the country supports mobile number portability the number might |
| 55 | + not belong to the returned carrier anymore. If no mapping is found an |
| 56 | + empty string is returned. |
54 | 57 |
|
55 | 58 | This method assumes the validity of the number passed in has already been |
56 | 59 | checked, and that the number is suitable for carrier lookup. We consider |
57 | 60 | mobile and pager numbers possible candidates for carrier lookup. |
58 | 61 |
|
59 | 62 | Arguments: |
60 | | - numobj -- The PhoneNumber object for which we want to get a text description. |
| 63 | + numobj -- The PhoneNumber object for which we want to get a carrier name. |
61 | 64 | lang -- A 2-letter lowercase ISO 639-1 language code for the language in |
62 | 65 | which the description should be returned (e.g. "en") |
63 | 66 | script -- A 4-letter titlecase (first letter uppercase, rest lowercase) |
64 | 67 | ISO script code as defined in ISO 15924, separated by an |
65 | 68 | underscore (e.g. "Hant") |
66 | 69 | region -- A 2-letter uppercase ISO 3166-1 country code (e.g. "GB") |
67 | 70 |
|
68 | | - Returns a text description in the given language code, for the given phone |
69 | | - number, or an empty string if no description is available.""" |
| 71 | + Returns a carrier name in the given language code, for the given phone |
| 72 | + number, or an empty string if no description is available. |
| 73 | + """ |
70 | 74 | return prefix_description_for_number(CARRIER_DATA, CARRIER_LONGEST_PREFIX, |
71 | 75 | numobj, lang, script, region) |
72 | 76 |
|
73 | 77 |
|
74 | | -def description_for_number(numobj, lang, script=None, region=None): |
75 | | - """Return a text description of a PhoneNumber object for the given language. |
| 78 | +def name_for_number(numobj, lang, script=None, region=None): |
| 79 | + """Returns a carrier name for the given PhoneNumber object, in the |
| 80 | + language provided. |
76 | 81 |
|
77 | | - The description consists of the name of the carrier the number was |
78 | | - originally allocated to, however if the country supports mobile number |
79 | | - portability the number might not belong to the returned carrier |
80 | | - anymore. If no mapping is found an empty string is returned. |
| 82 | + The carrier name is the one the number was originally allocated to, |
| 83 | + however if the country supports mobile number portability the number might |
| 84 | + not belong to the returned carrier anymore. If no mapping is found an |
| 85 | + empty string is returned. |
81 | 86 |
|
82 | 87 | This function explicitly checks the validity of the number passed in |
83 | 88 |
|
84 | 89 | Arguments: |
85 | | - numobj -- The PhoneNumber object for which we want to get a text description. |
| 90 | + numobj -- The PhoneNumber object for which we want to get a carrier name. |
86 | 91 | lang -- A 2-letter lowercase ISO 639-1 language code for the language in |
87 | 92 | which the description should be returned (e.g. "en") |
88 | 93 | script -- A 4-letter titlecase (first letter uppercase, rest lowercase) |
89 | 94 | ISO script code as defined in ISO 15924, separated by an |
90 | 95 | underscore (e.g. "Hant") |
91 | 96 | region -- A 2-letter uppercase ISO 3166-1 country code (e.g. "GB") |
92 | 97 |
|
93 | | - Returns a text description in the given language code, for the given phone |
94 | | - number, or an empty string if no description is available.""" |
| 98 | + Returns a carrier name in the given language code, for the given phone |
| 99 | + number, or an empty string if no description is available. |
| 100 | + """ |
95 | 101 | ntype = number_type(numobj) |
96 | 102 | if _is_mobile(ntype): |
97 | | - return description_for_valid_number(numobj, lang, script, region) |
| 103 | + return name_for_valid_number(numobj, lang, script, region) |
98 | 104 | return "" |
99 | 105 |
|
100 | 106 |
|
| 107 | +def safe_display_name(numobj, lang, script=None, region=None): |
| 108 | + """Gets the name of the carrier for the given PhoneNumber object only when |
| 109 | + it is 'safe' to display to users. A carrier name is onsidered safe if the |
| 110 | + number is valid and for a region that doesn't support mobile number |
| 111 | + portability (http://en.wikipedia.org/wiki/Mobile_number_portability). |
| 112 | +
|
| 113 | +
|
| 114 | + This function explicitly checks the validity of the number passed in |
| 115 | +
|
| 116 | + Arguments: |
| 117 | + numobj -- The PhoneNumber object for which we want to get a carrier name. |
| 118 | + lang -- A 2-letter lowercase ISO 639-1 language code for the language in |
| 119 | + which the description should be returned (e.g. "en") |
| 120 | + script -- A 4-letter titlecase (first letter uppercase, rest lowercase) |
| 121 | + ISO script code as defined in ISO 15924, separated by an |
| 122 | + underscore (e.g. "Hant") |
| 123 | + region -- A 2-letter uppercase ISO 3166-1 country code (e.g. "GB") |
| 124 | +
|
| 125 | + Returns a carrier name that is safe to display to users, or the empty string. |
| 126 | + """ |
| 127 | + if is_mobile_number_portable_region(region_code_for_number(numobj)): |
| 128 | + return "" |
| 129 | + return name_for_number(numobj, lang, script, region) |
| 130 | + |
| 131 | + |
101 | 132 | def _is_mobile(ntype): |
102 | 133 | """Checks if the supplied number type supports carrier lookup""" |
103 | 134 | return (ntype == PhoneNumberType.MOBILE or |
|
0 commit comments