Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/phonenumbers/phonenumber.py: 34%

83 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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 

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 

52class PhoneNumber(UnicodeMixin): 

53 """Class representing international telephone numbers. 

54 

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

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

57 """ 

58 

59 def __init__(self, 

60 country_code=None, 

61 national_number=None, 

62 extension=None, 

63 italian_leading_zero=None, 

64 number_of_leading_zeros=None, 

65 raw_input=None, 

66 country_code_source=CountryCodeSource.UNSPECIFIED, 

67 preferred_domestic_carrier_code=None): 

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

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

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

71 # 

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

73 if country_code is None: 73 ↛ 76line 73 didn't jump to line 76, because the condition on line 73 was never false

74 self.country_code = None 

75 else: 

76 self.country_code = int(country_code) 

77 

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

79 # National (significant) Number is defined in International 

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

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

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

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

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

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

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

87 # contain the National(trunk) prefix. 

88 # 

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

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

91 # alphanumeric spellings). 

92 

93 if national_number is None: 93 ↛ 96line 93 didn't jump to line 96, because the condition on line 93 was never false

94 self.national_number = None 

95 else: 

96 self.national_number = to_long(national_number) 

97 

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

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

100 # digits. 

101 # 

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

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

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

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

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

107 

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

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

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

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

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

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

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

115 # details. 

116 # 

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

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

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

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

121 # and also set the number of leading zeros. 

122 # 

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

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

125 # 

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

127 if italian_leading_zero is None: 127 ↛ 130line 127 didn't jump to line 130, because the condition on line 127 was never false

128 self.italian_leading_zero = None 

129 else: 

130 self.italian_leading_zero = bool(italian_leading_zero) 

131 

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

133 if number_of_leading_zeros is None: 133 ↛ 136line 133 didn't jump to line 136, because the condition on line 133 was never false

134 self.number_of_leading_zeros = None 

135 else: 

136 self.number_of_leading_zeros = int(number_of_leading_zeros) 

137 

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

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

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

141 # ignored by most clients. 

142 

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

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

145 # could be used to store alphanumerical numbers such as 

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

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

148 

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

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

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

152 self.country_code_source = country_code_source # CountryCodeSource.VALUE 

153 if self.country_code_source is None: # pragma no cover 153 ↛ 154line 153 didn't jump to line 154, because the condition on line 153 was never true

154 self.country_code_source = CountryCodeSource.UNSPECIFIED 

155 

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

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

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

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

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

161 # a domestic landline phone and vice versa. 

162 # 

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

164 # as well. 

165 self.preferred_domestic_carrier_code = force_unicode(preferred_domestic_carrier_code) 

166 # None or Unicode string 

167 

168 def clear(self): 

169 """Erase the contents of the object""" 

170 self.country_code = None 

171 self.national_number = None 

172 self.extension = None 

173 self.italian_leading_zero = None 

174 self.number_of_leading_zeros = None 

175 self.raw_input = None 

176 self.country_code_source = CountryCodeSource.UNSPECIFIED 

177 self.preferred_domestic_carrier_code = None 

178 

179 def merge_from(self, other): 

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

181 if other.country_code is not None: 

182 self.country_code = other.country_code 

183 if other.national_number is not None: 

184 self.national_number = other.national_number 

185 if other.extension is not None: 

186 self.extension = other.extension 

187 if other.italian_leading_zero is not None: 

188 self.italian_leading_zero = other.italian_leading_zero 

189 if other.number_of_leading_zeros is not None: 

190 self.number_of_leading_zeros = other.number_of_leading_zeros 

191 if other.raw_input is not None: 

192 self.raw_input = other.raw_input 

193 if other.country_code_source is not CountryCodeSource.UNSPECIFIED: 

194 self.country_code_source = other.country_code_source 

195 if other.preferred_domestic_carrier_code is not None: 

196 self.preferred_domestic_carrier_code = other.preferred_domestic_carrier_code 

197 

198 def __eq__(self, other): 

199 if not isinstance(other, PhoneNumber): 

200 return False 

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

202 self.national_number == other.national_number and 

203 self.extension == other.extension and 

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

205 self.number_of_leading_zeros == other.number_of_leading_zeros and 

206 self.raw_input == other.raw_input and 

207 self.country_code_source == other.country_code_source and 

208 self.preferred_domestic_carrier_code == other.preferred_domestic_carrier_code) 

209 

210 def __ne__(self, other): 

211 return not self.__eq__(other) 

212 

213 def __repr__(self): 

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

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

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

217 (type(self).__name__, 

218 self.country_code, 

219 self.national_number, 

220 rpr(self.extension), 

221 self.italian_leading_zero, 

222 self.number_of_leading_zeros, 

223 self.country_code_source, 

224 rpr(self.preferred_domestic_carrier_code))) 

225 

226 def __unicode__(self): 

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

228 (self.country_code, self.national_number)) 

229 if self.italian_leading_zero is not None: 

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

231 if self.number_of_leading_zeros is not None: 

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

233 if self.extension is not None: 

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

235 if self.country_code_source is not CountryCodeSource.UNSPECIFIED: 

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

237 if self.preferred_domestic_carrier_code is not None: 

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

239 self.preferred_domestic_carrier_code) 

240 return result 

241 

242 

243class FrozenPhoneNumber(PhoneNumber, ImmutableMixin): 

244 """Immutable version of PhoneNumber""" 

245 def __hash__(self): 

246 return hash((self.country_code, 

247 self.national_number, 

248 self.extension, 

249 bool(self.italian_leading_zero), 

250 self.number_of_leading_zeros, 

251 self.raw_input, 

252 self.country_code_source, 

253 self.preferred_domestic_carrier_code)) 

254 

255 @mutating_method 

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

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

258 # Copy constructor 

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

260 else: 

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