MCPcopy
hub / github.com/daviddrysdale/python-phonenumbers / XPhoneNumberDesc

Class XPhoneNumberDesc

tools/python/buildmetadatafromxml.py:316–375  ·  view source on GitHub ↗

Parse PhoneNumberDesc object from XML element

Source from the content-addressed store, hash-verified

314
315
316class XPhoneNumberDesc(UnicodeMixin):
317 """Parse PhoneNumberDesc object from XML element"""
318 def __init__(self, xterritory, tag, template=None, has_possible_lengths=True, has_example_number=True):
319 id = xterritory.attrib['id']
320 xtag = _get_unique_child(xterritory, tag)
321 self.xtag = xtag
322 if xtag is None:
323 # When a PhoneNumberDesc is absent, the upstream Java code builds an object
324 # of form PhoneNumberDesc(national_number_pattern="NA", possible_length=(-1,)).
325 # The Python code uses a desc of None for this case, to keep the generated
326 # code size smaller.
327 self.o = None
328 return
329 self.o = PhoneNumberDesc()
330 self.o._mutable = True
331 self.o.national_number_pattern = None
332 self.o.possible_number_pattern = None # retired
333 # Set possible length info to None for now, to mark that it wasn't specified
334 # for this numberDesc.
335 self.o.possible_length = None
336 self.o.possible_length_local_only = None
337 self.o.example_number = None
338
339 # Always expect a nationalNumberPattern element
340 self.o.national_number_pattern = _dews_re(_get_unique_child_value(xtag, 'nationalNumberPattern'))
341 if self.o.national_number_pattern is None:
342 if lax:
343 if template is not None:
344 self.o.national_number_pattern = template.national_number_pattern
345 else:
346 raise Exception("Missing required nationalNumberPattern element in %s.%s" % (id, tag))
347
348 # An exampleNumber element may be present.
349 example_number = _get_unique_child_value(xtag, 'exampleNumber')
350 if (not lax) and has_example_number and (example_number is None):
351 raise Exception("Missing required exampleNumber element in %s.%s" % (id, tag))
352 if (not has_example_number) and example_number is not None:
353 if lax:
354 example_number = None
355 else:
356 raise Exception("Unexpected exampleNumber element in %s.%s" % (id, tag))
357 self.o.example_number = example_number
358
359 # A possibleLengths element may be present.
360 possible_lengths = _get_unique_child(xtag, 'possibleLengths')
361 if (not lax) and has_possible_lengths and (possible_lengths is None):
362 raise Exception("Missing required possibleLengths element in %s.%s" % (id, tag))
363 if (not has_possible_lengths) and possible_lengths is not None:
364 raise Exception("Unexpected possibleLengths in %s.%s" % (id, tag))
365 if possible_lengths is not None:
366 national_lengths = possible_lengths.attrib['national'] # REQUIRED attribute
367 if national_lengths == "-1":
368 # -1 used to be a special possibleLengths value, no longer allowed.
369 raise Exception("Found unexpected %s.%s.possibleLength.national==-1", (id, tag))
370 self.o.possible_length = _extract_lengths(national_lengths)
371 local_lengths = possible_lengths.get('localOnly', None) # IMPLIED attribute
372 self.o.possible_length_local_only = _extract_lengths(local_lengths)
373

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected