Entire collection of phone number metadata retrieved from XML
| 549 | |
| 550 | |
| 551 | class XPhoneNumberMetadata(UnicodeMixin): |
| 552 | """Entire collection of phone number metadata retrieved from XML""" |
| 553 | def __init__(self, filename, short_data): |
| 554 | # Load the XML data from the given filename |
| 555 | with open(filename, "r") as infile: |
| 556 | xtree = etree.parse(infile) |
| 557 | # Move to the top-level element of interest |
| 558 | xterritories = xtree.find(TOP_XPATH) |
| 559 | # Iterate over the child elements, as there isn't any complex nesting or |
| 560 | # tree structures in the XML DTD. |
| 561 | self.territory = {} |
| 562 | for xterritory in xterritories: |
| 563 | if xterritory.tag == TERRITORY_TAG: |
| 564 | terrobj = XTerritory(xterritory, short_data) |
| 565 | id = terrobj.identifier() # like "US" for countries, "800" for non-geo |
| 566 | if id in self.territory: |
| 567 | raise Exception("Duplicate entry for %s" % id) |
| 568 | self.territory[id] = terrobj |
| 569 | else: |
| 570 | raise Exception("Unexpected element %s found" % xterritory.tag) |
| 571 | self.alt_territory = None |
| 572 | self.short_data = short_data |
| 573 | |
| 574 | def add_alternate_formats(self, filename): |
| 575 | """Add phone number alternate format metadata retrieved from XML""" |
| 576 | with open(filename, "r") as infile: |
| 577 | xtree = etree.parse(infile) |
| 578 | self.alt_territory = {} # country_code to XAlternateTerritory |
| 579 | xterritories = xtree.find(TOP_XPATH) |
| 580 | for xterritory in xterritories: |
| 581 | if xterritory.tag == TERRITORY_TAG: |
| 582 | terrobj = XAlternateTerritory(xterritory) |
| 583 | id = str(terrobj.country_code) |
| 584 | if id in self.alt_territory: |
| 585 | raise Exception("Duplicate entry for %s" % id) |
| 586 | self.alt_territory[id] = terrobj |
| 587 | else: |
| 588 | raise Exception("Unexpected element %s found" % xterritory.tag) |
| 589 | |
| 590 | def __unicode__(self): |
| 591 | return u("\n").join([u("%s: %s") % (country_id, territory) for country_id, territory in self.territory.items()]) |
| 592 | |
| 593 | def emit_metadata_for_region_py(self, region, region_filename, module_prefix): |
| 594 | """Emit Python code generating the metadata for the given region""" |
| 595 | terrobj = self.territory[region] |
| 596 | with open(region_filename, "w") as outfile: |
| 597 | prnt(_REGION_METADATA_PROLOG % {'region': terrobj.identifier(), 'module': module_prefix}, file=outfile) |
| 598 | prnt("PHONE_METADATA_%s = %s" % (terrobj.identifier(), terrobj), file=outfile) |
| 599 | |
| 600 | def emit_alt_formats_for_cc_py(self, cc, cc_filename, module_prefix): |
| 601 | """Emit Python code generating the alternate format metadata for the given country code""" |
| 602 | terrobj = self.alt_territory[cc] |
| 603 | with open(cc_filename, "w") as outfile: |
| 604 | prnt(_ALT_FORMAT_METADATA_PROLOG % (cc, module_prefix), file=outfile) |
| 605 | prnt("PHONE_ALT_FORMAT_%s = %s" % (cc, terrobj), file=outfile) |
| 606 | |
| 607 | def emit_metadata_py(self, datadir, module_prefix): |
| 608 | """Emit Python code for the phone number metadata to the given file, and |