Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/phonenumbers/phonenumber.py: 53%

92 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:30 +0000

1"""PhoneNumber object definition""" 

2 

3# Based on original Java code and protocol buffer: 

4# resources/phonenumber.proto 

5# java/src/com/google/i18n/phonenumbers/Phonenumber.java 

6# Copyright (C) 2010-2011 The Libphonenumber Authors 

7# 

8# Licensed under the Apache License, Version 2.0 (the "License"); 

9# you may not use this file except in compliance with the License. 

10# You may obtain a copy of the License at 

11# 

12# http://www.apache.org/licenses/LICENSE-2.0 

13# 

14# Unless required by applicable law or agreed to in writing, software 

15# distributed under the License is distributed on an "AS IS" BASIS, 

16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

17# See the License for the specific language governing permissions and 

18# limitations under the License. 

19from .util import UnicodeMixin, ImmutableMixin, mutating_method 

20from .util import to_long, unicod, rpr, force_unicode, u 

21 

22 

23class CountryCodeSource(object): 

24 """The source from which a country code is derived.""" 

25 # Default value returned if this is not set, because the phone number was 

26 # created using parse(keep_raw_input=False). 

27 UNSPECIFIED = 0 

28 

29 # The country_code is derived based on a phone number with a leading "+", 

30 # e.g. the French number "+33 1 42 68 53 00". 

31 FROM_NUMBER_WITH_PLUS_SIGN = 1 

32 

33 # The country_code is derived based on a phone number with a leading IDD, 

34 # e.g. the French number "011 33 1 42 68 53 00", as it is dialled 

35 # from US. 

36 FROM_NUMBER_WITH_IDD = 5 

37 

38 # The country_code is derived based on a phone number without a leading 

39 # "+", e.g. the French number "33 1 42 68 53 00" when default_country is 

40 # supplied as France. 

41 FROM_NUMBER_WITHOUT_PLUS_SIGN = 10 

42 

43 # The country_code is derived NOT based on the phone number itself, but 

44 # from the default_country parameter provided in the parsing function by 

45 # the clients. This happens mostly for numbers written in the national 

46 # format (without country code). For example, this would be set when 

47 # parsing the French number "01 42 68 53 00", when default_country is 

48 # supplied as France. 

49 FROM_DEFAULT_COUNTRY = 20 

50 

51 @classmethod 

52 def to_string(cls, val): 

53 """Return a string representation of a CountryCodeSource value""" 

54 if val == CountryCodeSource.UNSPECIFIED: 

55 return u("UNSPECIFIED") 

56 elif val == CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN: 

57 return u("FROM_NUMBER_WITH_PLUS_SIGN") 

58 elif val == CountryCodeSource.FROM_NUMBER_WITH_IDD: 

59 return u("FROM_NUMBER_WITH_IDD") 

60 elif val == CountryCodeSource.FROM_DEFAULT_COUNTRY: 

61 return u("FROM_DEFAULT_COUNTRY") 

62 else: 

63 return u("INVALID (%d)" % val) 

64 

65 

66class PhoneNumber(UnicodeMixin): 

67 """Class representing international telephone numbers. 

68 

69 This class is hand-created based on phonenumber.proto. Please refer 

70 to that file for detailed descriptions of the meaning of each field. 

71 """ 

72 

73 def __init__(self, 

74 country_code=None, 

75 national_number=None, 

76 extension=None, 

77 italian_leading_zero=None, 

78 number_of_leading_zeros=None, 

79 raw_input=None, 

80 country_code_source=CountryCodeSource.UNSPECIFIED, 

81 preferred_domestic_carrier_code=None): 

82 # The country calling code for this number, as defined by the 

83 # International Telecommunication Union (ITU). For example, this would 

84 # be 1 for NANPA countries, and 33 for France. 

85 # 

86 # None if not set, of type int otherwise. 

87 if country_code is None: 

88 self.country_code = None 

89 else: 

90 self.country_code = int(country_code) 

91 

92 # Number does not contain National(trunk) prefix. 

93 # National (significant) Number is defined in International 

94 # Telecommunication Union (ITU) Recommendation E.164. It is a 

95 # language/country-neutral representation of a phone number at a 

96 # country level. For countries which have the concept of an "area 

97 # code" or "national destination code", this is included in the 

98 # National (significant) Number. Although the ITU says the maximum 

99 # length should be 15, we have found longer numbers in some countries 

100 # e.g. Germany. Note that the National (significant) Number does not 

101 # contain the National(trunk) prefix. 

102 # 

103 # None if not set, of type long otherwise (and so it will never 

104 # contain any formatting (hypens, spaces, parentheses), nor any 

105 # alphanumeric spellings). 

106 

107 if national_number is None: 

108 self.national_number = None 

109 else: 

110 self.national_number = to_long(national_number) 

111 

112 # Extension is not standardized in ITU recommendations, except for 

113 # being defined as a series of numbers with a maximum length of 40 

114 # digits. 

115 # 

116 # When present, it is a Unicode string to accommodate for the 

117 # possible use of a leading zero in the extension (organizations 

118 # have complete freedom to do so, as there is no standard defined). 

119 # However, only ASCII digits should be stored here. 

120 self.extension = force_unicode(extension) # None or Unicode '[0-9]+' 

121 

122 # In some countries, the national (significant) number starts with one 

123 # or more "0"s without this being a national prefix or trunk code of 

124 # some kind. For example, the leading zero in the national 

125 # (significant) number of an Italian phone number indicates the number 

126 # is a fixed-line number. There have been plans to migrate fixed-line 

127 # numbers to start with the digit two since December 2000, but it has 

128 # not happened yet. See http://en.wikipedia.org/wiki/%2B39 for more 

129 # details. 

130 # 

131 # These fields can be safely ignored (there is no need to set them) 

132 # for most countries. Some limited number of countries behave like 

133 # Italy - for these cases, if the leading zero(s) of a number would be 

134 # retained even when dialling internationally, set this flag to true, 

135 # and also set the number of leading zeros. 

136 # 

137 # Clients who use the parsing functionality of the i18n phone number 

138 # libraries will have these fields set if necessary automatically. 

139 # 

140 # None if not set, of type bool otherwise: 

141 if italian_leading_zero is None: 

142 self.italian_leading_zero = None 

143 else: 

144 self.italian_leading_zero = bool(italian_leading_zero) 

145 

146 # None if not set, of type int otherwise. 

147 if number_of_leading_zeros is None: 

148 self.number_of_leading_zeros = None 

149 else: 

150 self.number_of_leading_zeros = int(number_of_leading_zeros) 

151 

152 # The next few fields are non-essential fields for a phone number. 

153 # They retain extra information about the form the phone number was 

154 # in when it was provided to us to parse. They can be safely 

155 # ignored by most clients. 

156 

157 # This field is used to store the raw input string containing phone 

158 # numbers before it was canonicalized by the library. For example, it 

159 # could be used to store alphanumerical numbers such as 

160 # "1-800-GOOG-411". 

161 self.raw_input = force_unicode(raw_input) # None or Unicode string 

162 

163 # The source from which the country_code is derived. This is not set 

164 # in the general parsing method, but in the method that parses and 

165 # keeps raw_input. New fields could be added upon request. 

166 self.country_code_source = country_code_source # CountryCodeSource.VALUE 

167 if self.country_code_source is None: # pragma no cover 

168 self.country_code_source = CountryCodeSource.UNSPECIFIED 

169 

170 # The carrier selection code that is preferred when calling this 

171 # phone number domestically. This also includes codes that need to 

172 # be dialed in some countries when calling from landlines to mobiles 

173 # or vice versa. For example, in Columbia, a "3" needs to be dialed 

174 # before the phone number itself when calling from a mobile phone to 

175 # a domestic landline phone and vice versa. 

176 # 

177 # Note this is the "preferred" code, which means other codes may work 

178 # as well. 

179 self.preferred_domestic_carrier_code = force_unicode(preferred_domestic_carrier_code) 

180 # None or Unicode string 

181 

182 def clear(self): 

183 """Erase the contents of the object""" 

184 self.country_code = None 

185 self.national_number = None 

186 self.extension = None 

187 self.italian_leading_zero = None 

188 self.number_of_leading_zeros = None 

189 self.raw_input = None 

190 self.country_code_source = CountryCodeSource.UNSPECIFIED 

191 self.preferred_domestic_carrier_code = None 

192 

193 def merge_from(self, other): 

194 """Merge information from another PhoneNumber object into this one.""" 

195 if other.country_code is not None: 

196 self.country_code = other.country_code 

197 if other.national_number is not None: 

198 self.national_number = other.national_number 

199 if other.extension is not None: 

200 self.extension = other.extension 

201 if other.italian_leading_zero is not None: 

202 self.italian_leading_zero = other.italian_leading_zero 

203 if other.number_of_leading_zeros is not None: 

204 self.number_of_leading_zeros = other.number_of_leading_zeros 

205 if other.raw_input is not None: 

206 self.raw_input = other.raw_input 

207 if other.country_code_source is not CountryCodeSource.UNSPECIFIED: 

208 self.country_code_source = other.country_code_source 

209 if other.preferred_domestic_carrier_code is not None: 

210 self.preferred_domestic_carrier_code = other.preferred_domestic_carrier_code 

211 

212 def __eq__(self, other): 

213 if not isinstance(other, PhoneNumber): 

214 return False 

215 return (self.country_code == other.country_code and 

216 self.national_number == other.national_number and 

217 self.extension == other.extension and 

218 bool(self.italian_leading_zero) == bool(other.italian_leading_zero) and 

219 self.number_of_leading_zeros == other.number_of_leading_zeros and 

220 self.raw_input == other.raw_input and 

221 self.country_code_source == other.country_code_source and 

222 self.preferred_domestic_carrier_code == other.preferred_domestic_carrier_code) 

223 

224 def __ne__(self, other): 

225 return not self.__eq__(other) 

226 

227 def __repr__(self): 

228 return (unicod("%s(country_code=%s, national_number=%s, extension=%s, " + 

229 "italian_leading_zero=%s, number_of_leading_zeros=%s, " + 

230 "country_code_source=%s, preferred_domestic_carrier_code=%s)") % 

231 (type(self).__name__, 

232 self.country_code, 

233 self.national_number, 

234 rpr(self.extension), 

235 self.italian_leading_zero, 

236 self.number_of_leading_zeros, 

237 self.country_code_source, 

238 rpr(self.preferred_domestic_carrier_code))) 

239 

240 def __unicode__(self): 

241 result = (unicod("Country Code: %s National Number: %s") % 

242 (self.country_code, self.national_number)) 

243 if self.italian_leading_zero is not None: 

244 result += unicod(" Leading Zero(s): %s") % self.italian_leading_zero 

245 if self.number_of_leading_zeros is not None: 

246 result += unicod(" Number of leading zeros: %d") % self.number_of_leading_zeros 

247 if self.extension is not None: 

248 result += unicod(" Extension: %s") % self.extension 

249 if self.country_code_source is not CountryCodeSource.UNSPECIFIED: 

250 result += unicod(" Country Code Source: %s") % self.country_code_source 

251 if self.preferred_domestic_carrier_code is not None: 

252 result += (unicod(" Preferred Domestic Carrier Code: %s") % 

253 self.preferred_domestic_carrier_code) 

254 return result 

255 

256 

257class FrozenPhoneNumber(PhoneNumber, ImmutableMixin): 

258 """Immutable version of PhoneNumber""" 

259 def __hash__(self): 

260 return hash((self.country_code, 

261 self.national_number, 

262 self.extension, 

263 bool(self.italian_leading_zero), 

264 self.number_of_leading_zeros, 

265 self.raw_input, 

266 self.country_code_source, 

267 self.preferred_domestic_carrier_code)) 

268 

269 @mutating_method 

270 def __init__(self, *args, **kwargs): 

271 if len(kwargs) == 0 and len(args) == 1 and isinstance(args[0], PhoneNumber): 

272 # Copy constructor 

273 super(FrozenPhoneNumber, self).__init__(**args[0].__dict__) 

274 else: 

275 super(FrozenPhoneNumber, self).__init__(*args, **kwargs)