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

281 statements  

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

1"""PhoneMetadata object definitions""" 

2 

3# Based on original Java code and protocol buffer: 

4# resources/phonemetadata.proto 

5# java/src/com/google/i18n/phonenumbers/Phonemetadata.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. 

19import threading 

20from .util import UnicodeMixin, ImmutableMixin, mutating_method 

21from .util import u, unicod, rpr, force_unicode 

22 

23REGION_CODE_FOR_NON_GEO_ENTITY = u("001") 

24 

25 

26class NumberFormat(UnicodeMixin, ImmutableMixin): 

27 """Representation of way that a phone number can be formatted for output""" 

28 @mutating_method 

29 def __init__(self, 

30 pattern=None, 

31 format=None, 

32 leading_digits_pattern=None, 

33 national_prefix_formatting_rule=None, 

34 national_prefix_optional_when_formatting=None, 

35 domestic_carrier_code_formatting_rule=None): 

36 # pattern is a regex that is used to match the national (significant) 

37 # number. For example, the pattern "(20)(\d{4})(\d{4})" will match 

38 # number "2070313000", which is the national (significant) number for 

39 # Google London. Note the presence of the parentheses, which are 

40 # capturing groups what specifies the grouping of numbers. 

41 self.pattern = force_unicode(pattern) # Unicode string holding regexp 

42 

43 # format specifies how the national (significant) number matched by 

44 # pattern should be formatted. Using the same example as above, format 

45 # could contain "$1 $2 $3", meaning that the number should be 

46 # formatted as "20 7031 3000". Each $x is replaced by the numbers 

47 # captured by group x in the regex specified by pattern. 

48 self.format = force_unicode(format) # None or Unicode string 

49 

50 # This field is a regex that is used to match a certain number of 

51 # digits at the beginning of the national (significant) number. When 

52 # the match is successful, the accompanying pattern and format should 

53 # be used to format this number. For example, if 

54 # leading_digits="[1-3]|44", then all the national numbers starting 

55 # with 1, 2, 3 or 44 should be formatted using the accompanying 

56 # pattern and format. 

57 # 

58 # The first leading_digits_pattern matches up to the first three digits 

59 # of the national (significant) number; the next one matches the first 

60 # four digits, then the first five and so on, until the 

61 # leading_digits_pattern can uniquely identify one pattern and format 

62 # to be used to format the number. 

63 # 

64 # In the case when only one formatting pattern exists, no 

65 # leading_digits_pattern is needed. 

66 self.leading_digits_pattern = [] # list of Unicode strings holding regexps 

67 if leading_digits_pattern is not None: 

68 self.leading_digits_pattern = [force_unicode(p) for p in leading_digits_pattern] 

69 

70 # This field specifies how the national prefix ($NP) together with the 

71 # first group ($FG) in the national significant number should be 

72 # formatted in the NATIONAL format when a national prefix exists for a 

73 # certain country. For example, when this field contains "($NP$FG)", a 

74 # number from Beijing, China (whose $NP = 0), which would by default 

75 # be formatted without national prefix as 10 1234 5678 in NATIONAL 

76 # format, will instead be formatted as (010) 1234 5678; to format it 

77 # as (0)10 1234 5678, the field would contain "($NP)$FG". Note $FG 

78 # should always be present in this field, but $NP can be omitted. For 

79 # example, having "$FG" could indicate the number should be formatted 

80 # in NATIONAL format without the national prefix. This is commonly 

81 # used to override the rule specified for the territory in the XML 

82 # file. 

83 # 

84 # When this field is missing, a number will be formatted without 

85 # national prefix in NATIONAL format. This field does not affect how a 

86 # number is formatted in other formats, such as INTERNATIONAL. 

87 self.national_prefix_formatting_rule = force_unicode(national_prefix_formatting_rule) # None or Unicode string 

88 

89 # This field specifies whether the $NP can be omitted when formatting 

90 # a number in national format, even though it usually wouldn't be. For 

91 # example, a UK number would be formatted by our library as 020 XXXX 

92 # XXXX. If we have commonly seen this number written by people without 

93 # the leading 0, for example as (20) XXXX XXXX, this field would be 

94 # set to true. This will be inherited from the value set for the 

95 # territory in the XML file, unless a national_prefix_formatting_rule 

96 # is defined specifically for this NumberFormat. 

97 if national_prefix_optional_when_formatting is not None: 

98 self.national_prefix_optional_when_formatting = bool(national_prefix_optional_when_formatting) 

99 else: 

100 self.national_prefix_optional_when_formatting = None 

101 

102 # This field specifies how any carrier code ($CC) together with the 

103 # first group ($FG) in the national significant number should be 

104 # formatted when format_with_carrier_code is called, if carrier codes 

105 # are used for a certain country. 

106 self.domestic_carrier_code_formatting_rule = force_unicode(domestic_carrier_code_formatting_rule) # None or Unicode string 

107 

108 def merge_from(self, other): 

109 """Merge information from another NumberFormat object into this one.""" 

110 if other.pattern is not None: 

111 self.pattern = other.pattern 

112 if other.format is not None: 

113 self.format = other.format 

114 self.leading_digits_pattern.extend(other.leading_digits_pattern) 

115 if other.national_prefix_formatting_rule is not None: 

116 self.national_prefix_formatting_rule = other.national_prefix_formatting_rule 

117 if other.national_prefix_optional_when_formatting is not None: 

118 self.national_prefix_optional_when_formatting = other.national_prefix_optional_when_formatting 

119 if other.domestic_carrier_code_formatting_rule is not None: 

120 self.domestic_carrier_code_formatting_rule = other.domestic_carrier_code_formatting_rule 

121 

122 def __eq__(self, other): 

123 if not isinstance(other, NumberFormat): 

124 return False 

125 return (repr(self) == repr(other)) 

126 

127 def __ne__(self, other): 

128 return not self.__eq__(other) 

129 

130 def __repr__(self): 

131 return str(self) 

132 

133 def __unicode__(self): 

134 # Generate a string that is valid Python input for the constructor. 

135 # Note that we use rpr (variant of repr), which generates its own quotes. 

136 result = unicod("NumberFormat(pattern=%s, format=%s") % (rpr(self.pattern), rpr(self.format)) 

137 if self.leading_digits_pattern: 

138 result += (unicod(", leading_digits_pattern=[%s]") % 

139 unicod(", ").join([rpr(ld) for ld in self.leading_digits_pattern])) 

140 if self.national_prefix_formatting_rule is not None: 

141 result += unicod(", national_prefix_formatting_rule=%s") % rpr(self.national_prefix_formatting_rule) 

142 if self.national_prefix_optional_when_formatting is not None: 

143 result += unicod(", national_prefix_optional_when_formatting=%s") % str(self.national_prefix_optional_when_formatting) 

144 if self.domestic_carrier_code_formatting_rule is not None: 

145 result += unicod(", domestic_carrier_code_formatting_rule=%s") % rpr(self.domestic_carrier_code_formatting_rule) 

146 result += unicod(")") 

147 return result 

148 

149 

150class PhoneNumberDesc(UnicodeMixin, ImmutableMixin): 

151 """Class representing the description of a set of phone numbers.""" 

152 @mutating_method 

153 def __init__(self, 

154 national_number_pattern=None, 

155 example_number=None, 

156 possible_length=None, 

157 possible_length_local_only=None): 

158 # The national_number_pattern is the pattern that a valid national 

159 # significant number would match. This specifies information such as 

160 # its total length and leading digits. 

161 self.national_number_pattern = force_unicode(national_number_pattern) # None or Unicode string holding regexp 

162 

163 # An example national significant number for the specific type. It 

164 # should not contain any formatting information. 

165 self.example_number = force_unicode(example_number) # None or Unicode string 

166 

167 # These represent the lengths a phone number from this region can be. They 

168 # will be sorted from smallest to biggest. Note that these lengths are for 

169 # the full number, without country calling code or national prefix. For 

170 # example, for the Swiss number +41789270000, in local format 0789270000, 

171 # this would be 9. 

172 # This could be used to highlight tokens in a text that may be a phone 

173 # number, or to quickly prune numbers that could not possibly be a phone 

174 # number for this locale. 

175 if possible_length is None: 175 ↛ 176line 175 didn't jump to line 176, because the condition on line 175 was never true

176 possible_length = () 

177 self.possible_length = possible_length # sequence of int 

178 

179 # These represent the lengths that only local phone numbers (without an area 

180 # code) from this region can be. They will be sorted from smallest to 

181 # biggest. For example, since the American number 456-1234 may be locally 

182 # diallable, although not diallable from outside the area, 7 could be a 

183 # possible value. 

184 # This could be used to highlight tokens in a text that may be a phone 

185 # number. 

186 # To our knowledge, area codes are usually only relevant for some fixed-line 

187 # and mobile numbers, so this field should only be set for those types of 

188 # numbers (and the general description) - however there are exceptions for 

189 # NANPA countries. 

190 if possible_length_local_only is None: 

191 possible_length_local_only = () 

192 self.possible_length_local_only = possible_length_local_only # sequence of int 

193 

194 def merge_from(self, other): 

195 """Merge information from another PhoneNumberDesc object into this one.""" 

196 if other.national_number_pattern is not None: 

197 self.national_number_pattern = other.national_number_pattern 

198 if other.example_number is not None: 

199 self.example_number = other.example_number 

200 

201 def __eq__(self, other): 

202 if not isinstance(other, PhoneNumberDesc): 

203 return False 

204 return (repr(self) == repr(other)) 

205 

206 def __ne__(self, other): 

207 return not self.__eq__(other) 

208 

209 def __repr__(self): 

210 return str(self) 

211 

212 def __unicode__(self): 

213 # Generate a string that is valid Python input for constructor 

214 result = unicod("PhoneNumberDesc(") 

215 sep = unicod("") 

216 if self.national_number_pattern is not None: 

217 result += unicod("%snational_number_pattern=%s") % (sep, rpr(self.national_number_pattern)) 

218 sep = unicod(", ") 

219 if self.example_number is not None: 

220 result += unicod("%sexample_number=%s") % (sep, rpr(self.example_number)) 

221 sep = unicod(", ") 

222 if self.possible_length: 

223 result += unicod("%spossible_length=%s") % (sep, tuple(self.possible_length)) 

224 sep = unicod(", ") 

225 if self.possible_length_local_only: 

226 result += unicod("%spossible_length_local_only=%s") % (sep, tuple(self.possible_length_local_only)) 

227 sep = unicod(", ") 

228 result += unicod(")") 

229 return result 

230 

231 

232def _same_pattern(left, right): 

233 if left is None and right is None: 

234 return True 

235 if left is None or right is None: 235 ↛ 236line 235 didn't jump to line 236, because the condition on line 235 was never true

236 return False 

237 return (left.national_number_pattern == right.national_number_pattern) 

238 

239 

240class PhoneMetadata(UnicodeMixin, ImmutableMixin): 

241 """Class representing metadata for international telephone numbers for a region. 

242 

243 This class is hand created based on phonemetadata.proto. Please refer to that file 

244 for detailed descriptions of the meaning of each field. 

245 

246 WARNING: This API isn't stable. It is considered libphonenumber-internal 

247 and can change at any time. We only declare it as public for easy 

248 inclusion in our build tools not in this package. Clients should not 

249 refer to this file, we do not commit to support backwards-compatibility or 

250 to warn about breaking changes. 

251 

252 """ 

253 # Lock that protects the *_available fields while they are being modified. 

254 # The modification involves loading data from a file, so we cannot just 

255 # rely on the GIL. 

256 _metadata_lock = threading.Lock() 

257 # If a region code is a key in this dict, metadata for that region is available. 

258 # The corresponding value of the map is either: 

259 # - a function which loads the region's metadata 

260 # - None, to indicate that the metadata is already loaded 

261 _region_available = {} # ISO 3166-1 alpha 2 => function or None 

262 # Likewise for short number metadata. 

263 _short_region_available = {} # ISO 3166-1 alpha 2 => function or None 

264 # Likewise for non-geo country calling codes. 

265 _country_code_available = {} # country calling code (as int) => function or None 

266 

267 _region_metadata = {} # ISO 3166-1 alpha 2 => PhoneMetadata 

268 _short_region_metadata = {} # ISO 3166-1 alpha 2 => PhoneMetadata 

269 # A mapping from a country calling code for a non-geographical entity to 

270 # the PhoneMetadata for that country calling code. Examples of the country 

271 # calling codes include 800 (International Toll Free Service) and 808 

272 # (International Shared Cost Service). 

273 _country_code_metadata = {} # country calling code (as int) => PhoneMetadata 

274 

275 @classmethod 

276 def metadata_for_region(kls, region_code, default=None): 

277 loader = kls._region_available.get(region_code, None) 

278 if loader is not None: 

279 # Region metadata is available but has not yet been loaded. Do so now. 

280 kls._metadata_lock.acquire() 

281 loader(region_code) 

282 kls._region_available[region_code] = None 

283 kls._metadata_lock.release() 

284 return kls._region_metadata.get(region_code, default) 

285 

286 @classmethod 

287 def short_metadata_for_region(kls, region_code, default=None): 

288 loader = kls._short_region_available.get(region_code, None) 

289 if loader is not None: 

290 # Region short number metadata is available but has not yet been loaded. Do so now. 

291 kls._metadata_lock.acquire() 

292 loader(region_code) 

293 kls._short_region_available[region_code] = None 

294 kls._metadata_lock.release() 

295 return kls._short_region_metadata.get(region_code, default) 

296 

297 @classmethod 

298 def metadata_for_nongeo_region(kls, country_code, default=None): 

299 loader = kls._country_code_available.get(country_code, None) 

300 if loader is not None: 

301 # Region metadata is available but has not yet been loaded. Do so now. 

302 kls._metadata_lock.acquire() 

303 loader(country_code) 

304 kls._country_code_available[country_code] = None 

305 kls._metadata_lock.release() 

306 return kls._country_code_metadata.get(country_code, default) 

307 

308 @classmethod 

309 def metadata_for_region_or_calling_code(kls, country_calling_code, region_code): 

310 if region_code == REGION_CODE_FOR_NON_GEO_ENTITY: 310 ↛ 311line 310 didn't jump to line 311, because the condition on line 310 was never true

311 return kls.metadata_for_nongeo_region(country_calling_code, None) 

312 else: 

313 return kls.metadata_for_region(region_code, None) 

314 

315 @classmethod 

316 def register_region_loader(kls, region_code, loader): 

317 kls._region_available[region_code] = loader 

318 

319 @classmethod 

320 def register_short_region_loader(kls, region_code, loader): 

321 kls._short_region_available[region_code] = loader 

322 

323 @classmethod 

324 def register_nongeo_region_loader(kls, country_code, loader): 

325 kls._country_code_available[country_code] = loader 

326 

327 @classmethod 

328 def load_all(kls): 

329 """Force immediate load of all metadata""" 

330 # Force expansion of contents to lists because we invalidate the iterator 

331 for region_code, region_loader in list(kls._region_available.items()): 

332 if region_loader is not None: # pragma no cover 

333 region_loader(region_code) 

334 kls._region_available[region_code] = None 

335 for country_code, cc_loader in list(kls._country_code_available.items()): 

336 if cc_loader is not None: 

337 cc_loader(country_code) 

338 kls._country_code_available[country_code] = None 

339 

340 @mutating_method 

341 def __init__(self, 

342 id, 

343 general_desc=None, 

344 fixed_line=None, 

345 mobile=None, 

346 toll_free=None, 

347 premium_rate=None, 

348 shared_cost=None, 

349 personal_number=None, 

350 voip=None, 

351 pager=None, 

352 uan=None, 

353 emergency=None, 

354 voicemail=None, 

355 short_code=None, 

356 standard_rate=None, 

357 carrier_specific=None, 

358 sms_services=None, 

359 no_international_dialling=None, 

360 country_code=None, 

361 international_prefix=None, 

362 preferred_international_prefix=None, 

363 national_prefix=None, 

364 preferred_extn_prefix=None, 

365 national_prefix_for_parsing=None, 

366 national_prefix_transform_rule=None, 

367 number_format=None, 

368 intl_number_format=None, 

369 main_country_for_code=False, 

370 leading_digits=None, 

371 leading_zero_possible=False, 

372 mobile_number_portable_region=False, 

373 short_data=False, 

374 register=True): 

375 # The general_desc contains information which is a superset of 

376 # descriptions for all types of phone numbers. If any element is 

377 # missing in the description of a specific type of number, the element 

378 # will inherit from its counterpart in the general_desc. For all types 

379 # that are generally relevant to normal phone numbers, if the whole 

380 # type is missing in the PhoneNumberMetadata XML file, it will not have 

381 # national number data, and the possible lengths will be [-1]. 

382 self.general_desc = general_desc # None or PhoneNumberDesc 

383 self.fixed_line = fixed_line # None or PhoneNumberDesc 

384 self.mobile = mobile # None or PhoneNumberDesc 

385 self.toll_free = toll_free # None or PhoneNumberDesc 

386 self.premium_rate = premium_rate # None or PhoneNumberDesc 

387 self.shared_cost = shared_cost # None or PhoneNumberDesc 

388 self.personal_number = personal_number # None or PhoneNumberDesc 

389 self.voip = voip # None or PhoneNumberDesc 

390 self.pager = pager # None or PhoneNumberDesc 

391 self.uan = uan # None or PhoneNumberDesc 

392 self.emergency = emergency # None or PhoneNumberDesc 

393 self.voicemail = voicemail # None or PhoneNumberDesc 

394 self.short_code = short_code # None or PhoneNumberDesc 

395 self.standard_rate = standard_rate # None or PhoneNumberDesc 

396 self.carrier_specific = carrier_specific # None or PhoneNumberDesc 

397 self.sms_services = sms_services # None or PhoneNumberDesc 

398 

399 # The rules here distinguish the numbers that are only able to be 

400 # dialled nationally. 

401 self.no_international_dialling = no_international_dialling # None or PhoneNumberDesc 

402 

403 # The ISO 3166-1 alpha-2 representation of a country/region, with the 

404 # exception of "country calling codes" used for non-geographical 

405 # entities, such as Universal International Toll Free Number 

406 # (+800). These are all given the ID "001", since this is the numeric 

407 # region code for the world according to UN M.49: 

408 # http://en.wikipedia.org/wiki/UN_M.49 

409 self.id = force_unicode(id) # None or Unicode string 

410 

411 # The country calling code that one would dial from overseas when 

412 # trying to dial a phone number in this country. For example, this 

413 # would be "64" for New Zealand. 

414 self.country_code = country_code # None or int 

415 

416 # The international_prefix of country A is the number that needs to be 

417 # dialled from country A to another country (country B). This is 

418 # followed by the country code for country B. Note that some countries 

419 # may have more than one international prefix, and for those cases, a 

420 # regular expression matching the international prefixes will be 

421 # stored in this field. 

422 self.international_prefix = force_unicode(international_prefix) # None or Unicode string 

423 

424 # If more than one international prefix is present, a preferred prefix 

425 # can be specified here for out-of-country formatting purposes. If 

426 # this field is not present, and multiple international prefixes are 

427 # present, then "+" will be used instead. 

428 self.preferred_international_prefix = force_unicode(preferred_international_prefix) # None or Unicode string 

429 

430 # The national prefix of country A is the number that needs to be 

431 # dialled before the national significant number when dialling 

432 # internally. This would not be dialled when dialling 

433 # internationally. For example, in New Zealand, the number that would 

434 # be locally dialled as 09 345 3456 would be dialled from overseas as 

435 # +64 9 345 3456. In this case, 0 is the national prefix. 

436 self.national_prefix = force_unicode(national_prefix) # None or Unicode string 

437 

438 # The preferred prefix when specifying an extension in this 

439 # country. This is used for formatting only, and if this is not 

440 # specified, a suitable default should be used instead. For example, 

441 # if you wanted extensions to be formatted in the following way: 1 

442 # (365) 345 445 ext. 2345 " ext. " should be the preferred extension 

443 # prefix. 

444 self.preferred_extn_prefix = force_unicode(preferred_extn_prefix) # None or Unicode string 

445 

446 # This field is used for cases where the national prefix of a country 

447 # contains a carrier selection code, and is written in the form of a 

448 # regular expression. For example, to dial the number 2222-2222 in 

449 # Fortaleza, Brazil (area code 85) using the long distance carrier Oi 

450 # (selection code 31), one would dial 0 31 85 2222 2222. Assuming the 

451 # only other possible carrier selection code is 32, the field will 

452 # contain "03[12]". 

453 # 

454 # When it is missing, this field inherits the value of national_prefix, 

455 # if that is present. 

456 self.national_prefix_for_parsing = force_unicode(national_prefix_for_parsing) # None or Unicode string holding regexp 

457 

458 # This field is only populated and used under very rare situations. 

459 # For example, mobile numbers in Argentina are written in two 

460 # completely different ways when dialed in-country and out-of-country 

461 # (e.g. 0343 15 555 1212 is exactly the same number as +54 9 343 555 

462 # 1212). This field is used together with national_prefix_for_parsing 

463 # to transform the number into a particular representation for storing 

464 # in the PhoneNumber class in those rare cases. 

465 self.national_prefix_transform_rule = force_unicode(national_prefix_transform_rule) # None or Unicode string 

466 

467 # Specifies whether the mobile and fixed-line patterns are the same or 

468 # not. This is used to speed up determining phone number type in 

469 # countries where these two types of phone numbers can never be 

470 # distinguished. 

471 self.same_mobile_and_fixed_line_pattern = _same_pattern(self.mobile, self.fixed_line) 

472 

473 # Note that the number format here is used for formatting only, not 

474 # parsing. Hence all the varied ways a user *may* write a number need 

475 # not be recorded - just the ideal way we would like to format it for 

476 # them. When this element is absent, the national significant number 

477 # will be formatted as a whole without any formatting applied. 

478 self.number_format = [] # List of NumberFormat objects 

479 if number_format is not None: 

480 self.number_format = number_format 

481 

482 # This field is populated only when the national significant number is 

483 # formatted differently when it forms part of the INTERNATIONAL format 

484 # and NATIONAL format. A case in point is mobile numbers in Argentina: 

485 # The number, which would be written in INTERNATIONAL format as 

486 # +54 9 343 555 1212, will be written as 0343 15 555 1212 for NATIONAL 

487 # format. In this case, the prefix 9 is inserted when dialling from 

488 # overseas, but otherwise the prefix 0 and the carrier selection code 

489 # 15 (inserted after the area code of 343) is used. 

490 # Note: this field is populated by setting a value for <intlFormat> 

491 # inside the <numberFormat> tag in the XML file. If <intlFormat> is 

492 # not set then it defaults to the same value as the <format> tag. 

493 # 

494 # Examples: 

495 # To set the <intlFormat> to a different value than the <format>: 

496 # <numberFormat pattern=....> 

497 # <format>$1 $2 $3</format> 

498 # <intlFormat>$1-$2-$3</intlFormat> 

499 # </numberFormat> 

500 # 

501 # To have a format only used for national formatting, set <intlFormat> to 

502 # "NA": 

503 # <numberFormat pattern=....> 

504 # <format>$1 $2 $3</format> 

505 # <intlFormat>NA</intlFormat> 

506 # </numberFormat> 

507 self.intl_number_format = [] # List of NumberFormat objects 

508 if intl_number_format is not None: 

509 self.intl_number_format = intl_number_format 

510 

511 # This field is set when this country is considered to be the main 

512 # country for a calling code. It may not be set by more than one 

513 # country with the same calling code, and it should not be set by 

514 # countries with a unique calling code. This can be used to indicate 

515 # that "GB" is the main country for the calling code "44" for example, 

516 # rather than Jersey or the Isle of Man. 

517 self.main_country_for_code = bool(main_country_for_code) 

518 

519 # This field is populated only for countries or regions that share a 

520 # country calling code. If a number matches this pattern, it could 

521 # belong to this region. This is not intended as a replacement for 

522 # is_valid_for_region, and does not mean the number must come from this 

523 # region (for example, 800 numbers are valid for all NANPA countries.) 

524 # This field should be a regular expression of the expected prefix 

525 # match. 

526 self.leading_digits = force_unicode(leading_digits) # None or Unicode string holding regexp 

527 

528 # Deprecated: do not use. Will be deleted when there are no references 

529 # to this later. 

530 self.leading_zero_possible = bool(leading_zero_possible) 

531 

532 # This field is set when this country has implemented mobile number 

533 # portability. This means that transferring mobile numbers between 

534 # carriers is allowed. A consequence of this is that phone prefix to 

535 # carrier mapping is less reliable. 

536 self.mobile_number_portable_region = mobile_number_portable_region # bool 

537 

538 # Record whether this metadata is for short numbers or normal numbers. 

539 self.short_data = short_data # bool 

540 

541 if register: 

542 # Register this instance with the relevant class-wide map 

543 if self.id == REGION_CODE_FOR_NON_GEO_ENTITY: 543 ↛ 544line 543 didn't jump to line 544, because the condition on line 543 was never true

544 if self.country_code in PhoneMetadata._country_code_metadata: 

545 other = PhoneMetadata._country_code_metadata[self.country_code] 

546 if self != other: 

547 raise Exception("Duplicate non-geo PhoneMetadata for %s (from %s:%s)" % (self.country_code, self.id, self.country_code)) 

548 else: 

549 PhoneMetadata._country_code_metadata[self.country_code] = self 

550 elif self.short_data: 550 ↛ 551line 550 didn't jump to line 551, because the condition on line 550 was never true

551 if self.id in PhoneMetadata._short_region_metadata: 

552 other = PhoneMetadata._short_region_metadata[self.id] 

553 if self != other: 

554 raise Exception("Duplicate short PhoneMetadata for %s (from %s:%s)" % (self.id, self.id, self.country_code)) 

555 else: 

556 PhoneMetadata._short_region_metadata[self.id] = self 

557 else: 

558 if self.id in PhoneMetadata._region_metadata: 558 ↛ 559line 558 didn't jump to line 559, because the condition on line 558 was never true

559 other = PhoneMetadata._region_metadata[self.id] 

560 if self != other: 

561 raise Exception("Duplicate PhoneMetadata for %s (from %s:%s)" % (self.id, self.id, self.country_code)) 

562 else: 

563 PhoneMetadata._region_metadata[self.id] = self 

564 

565 def __eq__(self, other): 

566 if not isinstance(other, PhoneMetadata): 

567 return False 

568 return (repr(self) == repr(other)) 

569 

570 def __ne__(self, other): 

571 return not self.__eq__(other) 

572 

573 def __repr__(self): 

574 return str(self) 

575 

576 def __unicode__(self): 

577 # Generate a string that is valid Python input for the constructor 

578 result = (unicod("PhoneMetadata(id='%s', country_code=%r, international_prefix=%s") % 

579 (self.id, self.country_code, rpr(self.international_prefix))) 

580 result += unicod(",\n general_desc=%s") % self.general_desc 

581 if self.fixed_line is not None: 

582 result += unicod(",\n fixed_line=%s") % self.fixed_line 

583 if self.mobile is not None: 

584 result += unicod(",\n mobile=%s") % self.mobile 

585 if self.toll_free is not None: 

586 result += unicod(",\n toll_free=%s") % self.toll_free 

587 if self.premium_rate is not None: 

588 result += unicod(",\n premium_rate=%s") % self.premium_rate 

589 if self.shared_cost is not None: 

590 result += unicod(",\n shared_cost=%s") % self.shared_cost 

591 if self.personal_number is not None: 

592 result += unicod(",\n personal_number=%s") % self.personal_number 

593 if self.voip is not None: 

594 result += unicod(",\n voip=%s") % self.voip 

595 if self.pager is not None: 

596 result += unicod(",\n pager=%s") % self.pager 

597 if self.uan is not None: 

598 result += unicod(",\n uan=%s") % self.uan 

599 if self.emergency is not None: 

600 result += unicod(",\n emergency=%s") % self.emergency 

601 if self.voicemail is not None: 

602 result += unicod(",\n voicemail=%s") % self.voicemail 

603 if self.short_code is not None: 

604 result += unicod(",\n short_code=%s") % self.short_code 

605 if self.standard_rate is not None: 

606 result += unicod(",\n standard_rate=%s") % self.standard_rate 

607 if self.carrier_specific is not None: 

608 result += unicod(",\n carrier_specific=%s") % self.carrier_specific 

609 if self.sms_services is not None: 

610 result += unicod(",\n sms_services=%s") % self.sms_services 

611 if self.no_international_dialling is not None: 

612 result += unicod(",\n no_international_dialling=%s") % self.no_international_dialling 

613 

614 if self.preferred_international_prefix is not None: 

615 result += unicod(",\n preferred_international_prefix=%s") % rpr(self.preferred_international_prefix) 

616 if self.national_prefix is not None: 

617 result += unicod(",\n national_prefix=%s") % rpr(self.national_prefix) 

618 if self.preferred_extn_prefix is not None: 

619 result += unicod(",\n preferred_extn_prefix=%s") % rpr(self.preferred_extn_prefix) 

620 if self.national_prefix_for_parsing is not None: 

621 result += unicod(",\n national_prefix_for_parsing=%s") % rpr(self.national_prefix_for_parsing) 

622 if self.national_prefix_transform_rule is not None: 

623 # Note that we use rpr() on self.national_prefix_transform_rule, which generates its own quotes 

624 result += unicod(",\n national_prefix_transform_rule=%s") % rpr(self.national_prefix_transform_rule) 

625 if self.number_format: 

626 result += unicod(",\n number_format=[%s]") % unicod(',\n ').join(map(u, self.number_format)) 

627 if self.intl_number_format: 

628 result += unicod(",\n intl_number_format=[%s]") % unicod(',\n ').join(map(u, self.intl_number_format)) 

629 if self.main_country_for_code: 

630 result += unicod(",\n main_country_for_code=True") 

631 if self.leading_digits is not None: 

632 result += unicod(",\n leading_digits='%s'") % self.leading_digits 

633 if self.leading_zero_possible: 

634 result += unicod(",\n leading_zero_possible=True") 

635 if self.mobile_number_portable_region: 

636 result += unicod(",\n mobile_number_portable_region=True") 

637 if self.short_data: 

638 result += unicod(",\n short_data=True") 

639 result += unicod(")") 

640 return result