def _standalone(argv):
    """Parse the given XML file and emit generated code."""
    alternate = None
    short_data = False
    try:
        opts, args = getopt.getopt(argv, "hsa:", ("help", "short", "alt="))
    except getopt.GetoptError:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
        elif opt in ("-s", "--short"):
            short_data = True
        elif opt in ("-a", "--alt"):
            alternate = arg
        else:
            prnt("Unknown option %s" % opt, file=sys.stderr)
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)

    if len(args) != 3:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    pmd = XPhoneNumberMetadata(args[0], short_data)
    if alternate is not None:
        pmd.add_alternate_formats(alternate)
    pmd.emit_metadata_py(args[1], args[2])
Ejemplo n.º 2
0
def _standalone(argv):
    """Parse the given input directory and emit generated code."""
    varprefix = "GEOCODE"
    per_locale = True
    separator = None
    try:
        opts, args = getopt.getopt(argv, "hv:fs:",
                                   ("help", "var=", "flat", "sep="))
    except getopt.GetoptError:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
        elif opt in ("-v", "--var"):
            varprefix = arg
        elif opt in ("-f", "--flat"):
            per_locale = False
        elif opt in ("-s", "--sep"):
            separator = arg
        else:
            prnt("Unknown option %s" % opt, file=sys.stderr)
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
    if len(args) != 3:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    if per_locale:
        prefixdata = load_locale_prefixdata(args[0], separator=separator)
    else:
        prefixdata = {}
        load_locale_prefixdata_file(prefixdata, args[0], separator=separator)
    output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale)
Ejemplo n.º 3
0
 def emit_alt_formats_for_cc_py(self, cc, cc_filename, module_prefix):
     """Emit Python code generating the alternate format metadata for the given country code"""
     terrobj = self.alt_territory[cc]
     with open(cc_filename, "w") as outfile:
         prnt(_ALT_FORMAT_METADATA_PROLOG % (cc, module_prefix),
              file=outfile)
         prnt("PHONE_ALT_FORMAT_%s = %s" % (cc, terrobj), file=outfile)
Ejemplo n.º 4
0
def _standalone(argv):
    """Parse the given XML file and emit generated code."""
    alternate = None
    short_data = False
    try:
        opts, args = getopt.getopt(argv, "hlsa:",
                                   ("help", "lax", "short", "alt="))
    except getopt.GetoptError:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
        elif opt in ("-s", "--short"):
            short_data = True
        elif opt in ("-l", "--lax"):
            global lax
            lax = True
        elif opt in ("-a", "--alt"):
            alternate = arg
        else:
            prnt("Unknown option %s" % opt, file=sys.stderr)
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)

    if len(args) != 3:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    pmd = XPhoneNumberMetadata(args[0], short_data)
    if alternate is not None:
        pmd.add_alternate_formats(alternate)
    pmd.emit_metadata_py(args[1], args[2])
def _standalone(argv):
    """Parse the given input directory and emit generated code."""
    varprefix = "GEOCODE"
    per_locale = True
    separator = None
    try:
        opts, args = getopt.getopt(argv, "hv:fs:", ("help", "var=", "flat", "sep="))
    except getopt.GetoptError:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
        elif opt in ("-v", "--var"):
            varprefix = arg
        elif opt in ("-f", "--flat"):
            per_locale = False
        elif opt in ("-s", "--sep"):
            separator = arg
        else:
            prnt("Unknown option %s" % opt, file=sys.stderr)
            prnt(__doc__, file=sys.stderr)
            sys.exit(1)
    if len(args) != 3:
        prnt(__doc__, file=sys.stderr)
        sys.exit(1)
    if per_locale:
        prefixdata = load_locale_prefixdata(args[0], separator=separator)
    else:
        prefixdata = {}
        load_locale_prefixdata_file(prefixdata, args[0], separator=separator)
    output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale)
 def _doTestNumberNonMatchesForLeniency(self, testCases, leniency):
     matchFoundCount = 0
     for test in testCases:
         iterator = self.findNumbersForLeniency(test.rawString, test.region, leniency)
         match = iterator.next() if iterator.has_next() else None
         if match is not None:
             matchFoundCount += 1
             prnt("Match found in %s for leniency: %s" % (test, leniency), file=sys.stderr)
     self.assertEqual(0, matchFoundCount)
 def testGlobalNetworkNumbers(self):
     PhoneMetadata.load_all()
     for callingCode in PhoneMetadata._country_code_metadata.keys():
         exampleNumber = phonenumberutil.example_number_for_non_geo_entity(callingCode)
         self.assertTrue(exampleNumber is not None,
                         msg="No example phone number for calling code %s" % callingCode)
         if not phonenumberutil.is_valid_number(exampleNumber):
             self.invalid_cases.append(exampleNumber)
             prnt("Failed validation for %s" % exampleNumber, file=sys.stderr)
     self.assertEqual(0, len(self.invalid_cases))
 def testGlobalNetworkNumbers(self):
     PhoneMetadata.load_all()
     for callingCode in PhoneMetadata._country_code_metadata.keys():
         exampleNumber = phonenumberutil.example_number_for_non_geo_entity(callingCode)
         self.assertTrue(exampleNumber is not None,
                         msg="No example phone number for calling code %s" % callingCode)
         if not phonenumberutil.is_valid_number(exampleNumber):
             self.invalid_cases.append(exampleNumber)
             prnt("Failed validation for %s" % exampleNumber, file=sys.stderr)
     self.assertEqual(0, len(self.invalid_cases))
Ejemplo n.º 9
0
 def emit_metadata_for_region_py(self, region, region_filename,
                                 module_prefix):
     """Emit Python code generating the metadata for the given region"""
     terrobj = self.territory[region]
     with open(region_filename, "w") as outfile:
         prnt(_REGION_METADATA_PROLOG % {
             'region': terrobj.identifier(),
             'module': module_prefix
         },
              file=outfile)
         prnt("PHONE_METADATA_%s = %s" % (terrobj.identifier(), terrobj),
              file=outfile)
 def testSmsServiceShortNumbers(self):
     wrongTagCounter = 0
     for regionCode in shortnumberinfo.SUPPORTED_SHORT_REGIONS:
         metadata = PhoneMetadata.short_metadata_for_region(regionCode, None)
         desc = metadata.sms_services
         if desc is not None and desc.example_number is not None:
             exampleNumber = desc.example_number
             smsServiceNumber = phonenumberutil.parse(exampleNumber, regionCode)
             if (not shortnumberinfo.is_possible_short_number_for_region(smsServiceNumber, regionCode) or
                 not shortnumberinfo.is_sms_service_for_region(smsServiceNumber, regionCode)):
                 wrongTagCounter += 1
                 prnt("SMS service test failed for %s" % regionCode, file=sys.stderr)
     self.assertEqual(0, wrongTagCounter)
 def testCarrierSpecificShortNumbers(self):
     wrongTagCounter = 0
     for regionCode in shortnumberinfo.SUPPORTED_SHORT_REGIONS:
         metadata = PhoneMetadata.short_metadata_for_region(regionCode, None)
         desc = metadata.carrier_specific
         if desc is not None and desc.example_number is not None:
             exampleNumber = desc.example_number
             carrierSpecificNumber = phonenumberutil.parse(exampleNumber, regionCode)
             if (not shortnumberinfo.is_possible_short_number_for_region(carrierSpecificNumber, regionCode) or
                 not shortnumberinfo.is_carrier_specific_for_region(carrierSpecificNumber, regionCode)):
                 wrongTagCounter += 1
                 prnt("Carrier-specific test failed for %s" % regionCode, file=sys.stderr)
     self.assertEqual(0, wrongTagCounter)
 def _checkNumbersValidAndCorrectType(self, exampleNumberRequestedType,
                                      possibleExpectedTypes):
     """
     Arguments:
     exampleNumberRequestedType -- type we are requesting an example number for
     possibleExpectedTypes -- acceptable types that this number should match, such as
           FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
     """
     for regionCode in phonenumberutil.SUPPORTED_REGIONS:
         exampleNumber = phonenumberutil.example_number_for_type(
             regionCode, exampleNumberRequestedType)
         if exampleNumber is not None:
             if not phonenumberutil.is_valid_number(exampleNumber):
                 self.invalid_cases.append(exampleNumber)
                 prnt("Failed validation for %s" % exampleNumber,
                      file=sys.stderr)
             else:
                 # We know the number is valid, now we check the type.
                 exampleNumberType = phonenumberutil.number_type(
                     exampleNumber)
                 if exampleNumberType not in possibleExpectedTypes:
                     self.wrong_type_cases.append(exampleNumber)
                     prnt("Wrong type for %s: got %s" %
                          (exampleNumber, exampleNumberType),
                          file=sys.stderr)
                     prnt("Expected types: ", file=sys.stderr)
                     for phone_type in possibleExpectedTypes:
                         prnt("  %s" % phone_type, file=sys.stderr)
 def _doTestNumberMatchesForLeniency(self, testCases, leniency):
     noMatchFoundCount = 0
     wrongMatchFoundCount = 0
     for test in testCases:
         iterator = self.findNumbersForLeniency(test.rawString, test.region, leniency)
         match = iterator.next() if iterator.has_next() else None
         if match is None:
             noMatchFoundCount += 1
             prnt("No match found in  %s for leniency: %s" % (test, leniency), file=sys.stderr)
         else:
             if test.rawString != match.raw_string:
                 wrongMatchFoundCount += 1
                 prnt("Found wrong match in test %s. Found %s" % (test, match), file=sys.stderr)
     self.assertEqual(0, noMatchFoundCount)
     self.assertEqual(0, wrongMatchFoundCount)
 def testEmergency(self):
     wrongTypeCounter = 0
     for regionCode in phonenumberutil.SUPPORTED_SHORT_REGIONS:
         metadata = PhoneMetadata.short_metadata_for_region(regionCode, None)
         desc = metadata.emergency
         if desc.example_number is not None:
             exampleNumber = desc.example_number
             if (not fullmatch(re.compile(desc.possible_number_pattern), exampleNumber) or
                 not is_emergency_number(exampleNumber, regionCode)):
                 wrongTypeCounter += 1
                 prnt("Emergency example number test failed for %s" % regionCode, file=sys.stderr)
             elif shortnumberinfo.expected_cost_for_region(exampleNumber, regionCode) != ShortNumberCost.TOLL_FREE:
                 wrongTypeCounter += 1
                 prnt("Emergency example number not toll free for %s" % regionCode, file=sys.stderr)
     self.assertEqual(0, wrongTypeCounter)
 def testCarrierSpecificShortNumbers(self):
     wrongTagCounter = 0
     for regionCode in phonenumberutil.SUPPORTED_SHORT_REGIONS:
         # Test the carrier-specific tag.
         metadata = PhoneMetadata.short_metadata_for_region(regionCode, None)
         desc = metadata.carrier_specific
         if desc.example_number is not None:
             exampleNumber = desc.example_number
             carrierSpecificNumber = phonenumberutil.parse(exampleNumber, regionCode)
             if (not fullmatch(re.compile(desc.possible_number_pattern), exampleNumber) or
                 not shortnumberinfo.is_carrier_specific(carrierSpecificNumber)):
                 wrongTagCounter += 1
                 prnt("Carrier-specific test failed for %s" % regionCode, file=sys.stderr)
         # TODO: Test other tags here
     self.assertEqual(0, wrongTagCounter)
 def testEmergency(self):
     wrongTypeCounter = 0
     for regionCode in shortnumberinfo.SUPPORTED_SHORT_REGIONS:
         metadata = PhoneMetadata.short_metadata_for_region(regionCode, None)
         desc = metadata.emergency
         if desc is not None and desc.example_number is not None:
             exampleNumber = desc.example_number
             phoneNumber = phonenumberutil.parse(exampleNumber, regionCode)
             if (not is_possible_short_number_for_region(phoneNumber, regionCode) or
                 not is_emergency_number(exampleNumber, regionCode)):
                 wrongTypeCounter += 1
                 prnt("Emergency example number test failed for %s" % regionCode, file=sys.stderr)
             elif shortnumberinfo.expected_cost_for_region(phoneNumber, regionCode) != ShortNumberCost.TOLL_FREE:
                 wrongTypeCounter += 1
                 prnt("Emergency example number not toll free for %s" % regionCode, file=sys.stderr)
     self.assertEqual(0, wrongTypeCounter)
    def testCanBeInternationallyDialled(self):
        for regionCode in phonenumberutil.SUPPORTED_REGIONS:
            exampleNumber = None
            metadata = PhoneMetadata.metadata_for_region(regionCode, None)
            desc = None
            if metadata is not None:
                desc = metadata.no_international_dialling
            try:
                if desc is not None and desc.example_number is not None:
                    exampleNumber = phonenumberutil.parse(desc.example_number, regionCode)

            except NumberParseException:
                _, e, _ = sys.exc_info()
                prnt("Failed parse: %s" % e, file=sys.stderr)

            if (exampleNumber is not None and
                phonenumberutil.can_be_internationally_dialled(exampleNumber)):
                self.wrong_type_cases.append(exampleNumber)
                prnt("Number %s should not be internationally diallable" % exampleNumber, file=sys.stderr)
        self.assertEqual(0, len(self.wrong_type_cases))
    def testCanBeInternationallyDialled(self):
        for regionCode in phonenumberutil.SUPPORTED_REGIONS:
            exampleNumber = None
            metadata = PhoneMetadata.metadata_for_region(regionCode, None)
            desc = None
            if metadata is not None:
                desc = metadata.no_international_dialling
            try:
                if desc.example_number is not None:
                    exampleNumber = phonenumberutil.parse(desc.example_number, regionCode)

            except NumberParseException:
                _, e, _ = sys.exc_info()
                prnt("Failed parse: %s" % e, file=sys.stderr)

            if (exampleNumber is not None and
                phonenumberutil._can_be_internationally_dialled(exampleNumber)):
                self.wrong_type_cases.append(exampleNumber)
                prnt("Number %s should not be internationally diallable" % exampleNumber, file=sys.stderr)
        self.assertEqual(0, len(self.wrong_type_cases))
 def testCarrierSpecificShortNumbers(self):
     wrongTagCounter = 0
     for regionCode in phonenumberutil.SUPPORTED_SHORT_REGIONS:
         # Test the carrier-specific tag.
         metadata = PhoneMetadata.short_metadata_for_region(
             regionCode, None)
         desc = metadata.carrier_specific
         if desc.example_number is not None:
             exampleNumber = desc.example_number
             carrierSpecificNumber = phonenumberutil.parse(
                 exampleNumber, regionCode)
             if (not fullmatch(re.compile(desc.possible_number_pattern),
                               exampleNumber)
                     or not shortnumberinfo.is_carrier_specific(
                         carrierSpecificNumber)):
                 wrongTagCounter += 1
                 prnt("Carrier-specific test failed for %s" % regionCode,
                      file=sys.stderr)
         # TODO: Test other tags here
     self.assertEqual(0, wrongTagCounter)
 def testShortNumbersValidAndCorrectCost(self):
     invalid_string_cases = []
     for regionCode in phonenumberutil.SUPPORTED_SHORT_REGIONS:
         exampleShortNumber = shortnumberinfo._example_short_number(
             regionCode)
         phoneNumber = phonenumberutil.parse(exampleShortNumber, regionCode)
         if not shortnumberinfo.is_valid_short_number_for_region(
                 phoneNumber, regionCode):
             invalid_string_case = "region_code: %s, national_number: %s" % (
                 regionCode, exampleShortNumber)
             invalid_string_cases.append(invalid_string_case)
             prnt("Failed validation from string %s" % invalid_string_case,
                  file=sys.stderr)
         if not shortnumberinfo.is_valid_short_number(phoneNumber):
             self.invalid_cases.append(phoneNumber)
             prnt("Failed validation for %s" % phoneNumber, file=sys.stderr)
         for cost in [
                 ShortNumberCost.TOLL_FREE, ShortNumberCost.STANDARD_RATE,
                 ShortNumberCost.PREMIUM_RATE, ShortNumberCost.UNKNOWN_COST
         ]:
             exampleShortNumber = shortnumberinfo._example_short_number_for_cost(
                 regionCode, cost)
             if exampleShortNumber != "":
                 phoneNumber = phonenumberutil.parse(
                     exampleShortNumber, regionCode)
                 exampleCost = shortnumberinfo.expected_cost_for_region(
                     phoneNumber, regionCode)
                 if cost != exampleCost:
                     self.wrong_type_cases.append(phoneNumber)
                     prnt("Wrong cost for %s: got %s, expected: %s" %
                          (phoneNumber, exampleCost, cost),
                          file=sys.stderr)
     self.assertEqual(0, len(invalid_string_cases))
     self.assertEqual(0, len(self.invalid_cases))
     self.assertEqual(0, len(self.wrong_type_cases))
 def _checkNumbersValidAndCorrectType(self,
                                      exampleNumberRequestedType,
                                      possibleExpectedTypes):
     """
     Arguments:
     exampleNumberRequestedType -- type we are requesting an example number for
     possibleExpectedTypes -- acceptable types that this number should match, such as
           FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
     """
     for regionCode in phonenumberutil.SUPPORTED_REGIONS:
         exampleNumber = phonenumberutil.example_number_for_type(regionCode, exampleNumberRequestedType)
         if exampleNumber is not None:
             if not phonenumberutil.is_valid_number(exampleNumber):
                 self.invalid_cases.append(exampleNumber)
                 prnt("Failed validation for %s" % exampleNumber, file=sys.stderr)
             else:
                 # We know the number is valid, now we check the type.
                 exampleNumberType = phonenumberutil.number_type(exampleNumber)
                 if exampleNumberType not in possibleExpectedTypes:
                     self.wrong_type_cases.append(exampleNumber)
                     prnt("Wrong type for %s: got %s" % (exampleNumber, exampleNumberType), file=sys.stderr)
                     prnt("Expected types: ", file=sys.stderr)
                     for phone_type in possibleExpectedTypes:
                         prnt("  %s" % phone_type, file=sys.stderr)
 def testShortNumbersValidAndCorrectCost(self):
     invalid_string_cases = []
     for regionCode in phonenumberutil.SUPPORTED_SHORT_REGIONS:
         exampleShortNumber = shortnumberinfo._example_short_number(regionCode)
         phoneNumber = phonenumberutil.parse(exampleShortNumber, regionCode)
         if not shortnumberinfo.is_valid_short_number_for_region(phoneNumber, regionCode):
             invalid_string_case = "region_code: %s, national_number: %s" % (regionCode, exampleShortNumber)
             invalid_string_cases.append(invalid_string_case)
             prnt("Failed validation from string %s" % invalid_string_case, file=sys.stderr)
         if not shortnumberinfo.is_valid_short_number(phoneNumber):
             self.invalid_cases.append(phoneNumber)
             prnt("Failed validation for %s" % phoneNumber, file=sys.stderr)
         for cost in [ShortNumberCost.TOLL_FREE, ShortNumberCost.STANDARD_RATE,
                      ShortNumberCost.PREMIUM_RATE, ShortNumberCost.UNKNOWN_COST]:
             exampleShortNumber = shortnumberinfo._example_short_number_for_cost(regionCode, cost)
             if exampleShortNumber != "":
                 phoneNumber = phonenumberutil.parse(exampleShortNumber, regionCode)
                 if cost != shortnumberinfo.expected_cost_for_region(phoneNumber, regionCode):
                     self.wrong_type_cases.append(phoneNumber)
                     prnt("Wrong cost for %s" % phoneNumber, file=sys.stderr)
     self.assertEqual(0, len(invalid_string_cases))
     self.assertEqual(0, len(self.invalid_cases))
     self.assertEqual(0, len(self.wrong_type_cases))
 def emit_metadata_for_region_py(self, region, region_filename, module_prefix):
     """Emit Python code generating the metadata for the given region"""
     terrobj = self.territory[region]
     with open(region_filename, "w") as outfile:
         prnt(_REGION_METADATA_PROLOG % {'region': terrobj.identifier(), 'module': module_prefix}, file=outfile)
         prnt("PHONE_METADATA_%s = %s" % (terrobj.identifier(), terrobj), file=outfile)
Ejemplo n.º 24
0
    def emit_metadata_py(self, datadir, module_prefix):
        """Emit Python code for the phone number metadata to the given file, and
        to a data/ subdirectory in the same directory as that file."""

        if not os.path.isdir(datadir):
            os.mkdir(datadir)
        modulefilename = os.path.join(datadir, '__init__.py')

        # First, generate all of the individual per-region files in that directory
        for country_id in sorted(self.territory.keys()):
            filename = os.path.join(datadir, "region_%s.py" % country_id)
            self.emit_metadata_for_region_py(country_id, filename,
                                             module_prefix)

        # Same for any per-country-code alternate format files
        if self.alt_territory is not None:
            for country_code in sorted(self.alt_territory.keys()):
                filename = os.path.join(datadir,
                                        "alt_format_%s.py" % country_code)
                self.emit_alt_formats_for_cc_py(country_code, filename,
                                                module_prefix)

        # Now build a module file that includes them all
        with open(modulefilename, "w") as outfile:
            prnt(METADATA_FILE_PROLOG, file=outfile)
            prnt(COPYRIGHT_NOTICE, file=outfile)
            prnt(METADATA_FILE_IMPORT % {'module': module_prefix},
                 file=outfile)
            nongeo_codes = []
            country_codes = []
            for country_id in sorted(self.territory.keys()):
                terrobj = self.territory[country_id]
                if terrobj.o.id == REGION_CODE_FOR_NON_GEO_ENTITY:
                    nongeo_codes.append(country_id)  # int
                else:
                    country_codes.append("'%s'" % country_id)  # quoted string
            prnt("_AVAILABLE_REGION_CODES = [%s]" % ",".join(country_codes),
                 file=outfile)
            if len(nongeo_codes) > 0:
                prnt("_AVAILABLE_NONGEO_COUNTRY_CODES = [%s]" %
                     ", ".join(nongeo_codes),
                     file=outfile)
            register_prefix = "short_" if self.short_data else ""
            prnt(METADATA_FILE_LOOP % {'prefix': register_prefix},
                 file=outfile)
            if len(nongeo_codes) > 0:
                prnt(METADATA_NONGEO_FILE_LOOP, file=outfile)

            if self.alt_territory is not None:
                for country_code in sorted(self.alt_territory.keys()):
                    prnt("from .alt_format_%s import PHONE_ALT_FORMAT_%s" %
                         (country_code, country_code),
                         file=outfile)
                prnt("_ALT_NUMBER_FORMATS = {%s}" % ", ".join([
                    "%s: PHONE_ALT_FORMAT_%s" % (cc, cc)
                    for cc in sorted(self.alt_territory.keys())
                ]),
                     file=outfile)

            # Build up a map from country code (int) to list of region codes (ISO 3166-1 alpha 2)
            country_code_to_region_code = {}
            for country_id in sorted(self.territory.keys()):
                terrobj = self.territory[country_id]
                if terrobj.o.country_code is not None:
                    country_code = int(terrobj.o.country_code)
                    if country_code not in country_code_to_region_code:
                        country_code_to_region_code[country_code] = []
                    if terrobj.o.main_country_for_code:
                        country_code_to_region_code[country_code].insert(
                            0, terrobj.o.id)
                    else:
                        country_code_to_region_code[country_code].append(
                            terrobj.o.id)

            # Emit the mapping from country code to region code if nonempty.
            if len(country_code_to_region_code.keys()) > 0:
                prnt(_COUNTRY_CODE_TO_REGION_CODE_PROLOG, file=outfile)
                prnt("_COUNTRY_CODE_TO_REGION_CODE = {", file=outfile)
                for country_code in sorted(country_code_to_region_code.keys()):
                    country_ids = country_code_to_region_code[country_code]
                    prnt('    %d: ("%s",),' %
                         (country_code, '", "'.join(country_ids)),
                         file=outfile)
                prnt("}", file=outfile)
 def emit_alt_formats_for_cc_py(self, cc, cc_filename, module_prefix):
     """Emit Python code generating the alternate format metadata for the given country code"""
     terrobj = self.alt_territory[cc]
     with open(cc_filename, "w") as outfile:
         prnt(_ALT_FORMAT_METADATA_PROLOG % (cc, module_prefix), file=outfile)
         prnt("PHONE_ALT_FORMAT_%s = %s" % (cc, terrobj), file=outfile)
    def emit_metadata_py(self, datadir, module_prefix):
        """Emit Python code for the phone number metadata to the given file, and
        to a data/ subdirectory in the same directory as that file."""

        if not os.path.isdir(datadir):
            os.mkdir(datadir)
        modulefilename = os.path.join(datadir, '__init__.py')

        # First, generate all of the individual per-region files in that directory
        for country_id in sorted(self.territory.keys()):
            filename = os.path.join(datadir, "region_%s.py" % country_id)
            self.emit_metadata_for_region_py(country_id, filename, module_prefix)

        # Same for any per-country-code alternate format files
        if self.alt_territory is not None:
            for country_code in sorted(self.alt_territory.keys()):
                filename = os.path.join(datadir, "alt_format_%s.py" % country_code)
                self.emit_alt_formats_for_cc_py(country_code, filename, module_prefix)

        # Now build a module file that includes them all
        with open(modulefilename, "w") as outfile:
            prnt(METADATA_FILE_PROLOG, file=outfile)
            prnt(COPYRIGHT_NOTICE, file=outfile)
            prnt(METADATA_FILE_IMPORT % {'module': module_prefix}, file=outfile)
            nongeo_codes = []
            country_codes = []
            for country_id in sorted(self.territory.keys()):
                terrobj = self.territory[country_id]
                if terrobj.o.id == REGION_CODE_FOR_NON_GEO_ENTITY:
                    nongeo_codes.append(country_id)  # int
                else:
                    country_codes.append("'%s'" % country_id)  # quoted string
            prnt("_AVAILABLE_REGION_CODES = [%s]" % ",".join(country_codes), file=outfile)
            if len(nongeo_codes) > 0:
                prnt("_AVAILABLE_NONGEO_COUNTRY_CODES = [%s]" % ", ".join(nongeo_codes), file=outfile)
            register_prefix = "short_" if self.short_data else ""
            prnt(METADATA_FILE_LOOP % {'prefix': register_prefix}, file=outfile)
            if len(nongeo_codes) > 0:
                prnt(METADATA_NONGEO_FILE_LOOP, file=outfile)

            if self.alt_territory is not None:
                for country_code in sorted(self.alt_territory.keys()):
                    prnt("from .alt_format_%s import PHONE_ALT_FORMAT_%s" % (country_code, country_code), file=outfile)
                prnt("_ALT_NUMBER_FORMATS = {%s}" %
                     ", ".join(["%s: PHONE_ALT_FORMAT_%s" % (cc, cc) for cc in sorted(self.alt_territory.keys())]),
                     file=outfile)

            # Build up a map from country code (int) to list of region codes (ISO 3166-1 alpha 2)
            country_code_to_region_code = {}
            for country_id in sorted(self.territory.keys()):
                terrobj = self.territory[country_id]
                if terrobj.o.country_code is not None:
                    country_code = int(terrobj.o.country_code)
                    if country_code not in country_code_to_region_code:
                        country_code_to_region_code[country_code] = []
                    if terrobj.o.main_country_for_code:
                        country_code_to_region_code[country_code].insert(0, terrobj.o.id)
                    else:
                        country_code_to_region_code[country_code].append(terrobj.o.id)

            # Emit the mapping from country code to region code if nonempty.
            if len(country_code_to_region_code.keys()) > 0:
                prnt(_COUNTRY_CODE_TO_REGION_CODE_PROLOG, file=outfile)
                prnt("_COUNTRY_CODE_TO_REGION_CODE = {", file=outfile)
                for country_code in sorted(country_code_to_region_code.keys()):
                    country_ids = country_code_to_region_code[country_code]
                    prnt('    %d: ("%s",),' % (country_code, '", "'.join(country_ids)), file=outfile)
                prnt("}", file=outfile)
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix, per_locale):
    """Output the per-prefix data in Python form to the given file """
    sorted_keys = sorted(prefixdata.keys())
    total_keys = len(sorted_keys)
    total_chunks = int(math.ceil(total_keys / float(PREFIXDATA_CHUNK_SIZE)))

    outdirname = os.path.dirname(outfilename)
    longest_prefix = 0
    for chunk_num in range(total_chunks):
        chunk_index = PREFIXDATA_CHUNK_SIZE * chunk_num
        chunk_keys = sorted_keys[chunk_index:chunk_index + PREFIXDATA_CHUNK_SIZE]
        chunk_data = {}
        for key in chunk_keys:
            chunk_data[key] = prefixdata[key]
        chunk_file = os.path.join(outdirname, 'data%d.py' % chunk_num)
        chunk_longest = output_prefixdata_chunk(
            chunk_data, chunk_file, module_prefix, per_locale)
        if chunk_longest > longest_prefix:
            longest_prefix = chunk_longest

    with open(outfilename, "w") as outfile:
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("%s_DATA = {}" % varprefix, file=outfile)
        for chunk_num in range(total_chunks):
            prnt("from .data%d import data" % chunk_num, file=outfile)
            prnt("%s_DATA.update(data)" % varprefix, file=outfile)
        prnt("del data", file=outfile)
        prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix), file=outfile)
Ejemplo n.º 28
0
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix,
                           per_locale):
    """Output the per-prefix data in Python form to the given file """
    sorted_keys = sorted(prefixdata.keys())
    total_keys = len(sorted_keys)
    total_chunks = int(math.ceil(total_keys / float(PREFIXDATA_CHUNK_SIZE)))

    outdirname = os.path.dirname(outfilename)
    longest_prefix = 0
    for chunk_num in range(total_chunks):
        chunk_index = PREFIXDATA_CHUNK_SIZE * chunk_num
        chunk_keys = sorted_keys[chunk_index:chunk_index +
                                 PREFIXDATA_CHUNK_SIZE]
        chunk_data = {}
        for key in chunk_keys:
            chunk_data[key] = prefixdata[key]
        chunk_file = os.path.join(outdirname, 'data%d.py' % chunk_num)
        chunk_longest = output_prefixdata_chunk(chunk_data, chunk_file,
                                                module_prefix, per_locale)
        if chunk_longest > longest_prefix:
            longest_prefix = chunk_longest

    with open(outfilename, "w") as outfile:
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("%s_DATA = {}" % varprefix, file=outfile)
        for chunk_num in range(total_chunks):
            prnt("from .data%d import data" % chunk_num, file=outfile)
            prnt("%s_DATA.update(data)" % varprefix, file=outfile)
        prnt("del data", file=outfile)
        prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix),
             file=outfile)
Ejemplo n.º 29
0
    with open(filename, "r") as infile:
        for line in infile:
            m = CLASS_RE.match(line)
            if m:
                grepped_all.add(m.group(1))
            m = FUNCTION_RE.match(line)
            if m:
                grepped_all.add(m.group(1))
            m = CONSTANT_RE.match(line)
            if m:
                grepped_all.add(m.group(1))

# Pull in the declared identifiers
code_all = (set(phonenumbers.__all__) | set(phonenumbers.geocoder.__all__)
            | set(phonenumbers.carrier.__all__)
            | set(phonenumbers.timezone.__all__))

# Compare
code_not_grepped = (code_all - grepped_all)
grepped_not_code = (grepped_all - code_all)
if len(code_not_grepped) > 0:
    prnt("Found the following in __all__ but not in grepped code:",
         file=sys.stderr)
    for identifier in code_not_grepped:
        prnt("  %s" % identifier, file=sys.stderr)
if len(grepped_not_code) > 0:
    prnt("Found the following in grepped code but not in __all__:",
         file=sys.stderr)
    for identifier in grepped_not_code:
        prnt("  %s" % identifier, file=sys.stderr)
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix,
                           per_locale, chunks):
    """Output the per-prefix data in Python form to the given file """
    sorted_keys = sorted(prefixdata.keys())
    total_keys = len(sorted_keys)
    if chunks == -1:
        chunk_size = PREFIXDATA_CHUNK_SIZE
        total_chunks = int(math.ceil(total_keys / float(chunk_size)))
    else:
        chunk_size = int(math.ceil(total_keys / float(chunks)))
        total_chunks = chunks

    outdirname = os.path.dirname(outfilename)
    longest_prefix = 0
    for chunk_num in range(total_chunks):
        chunk_index = chunk_size * chunk_num
        chunk_keys = sorted_keys[chunk_index:chunk_index + chunk_size]
        chunk_data = {}
        for key in chunk_keys:
            chunk_data[key] = prefixdata[key]
        chunk_file = os.path.join(outdirname, 'data%d.py' % chunk_num)
        chunk_longest = output_prefixdata_chunk(chunk_data, chunk_file,
                                                module_prefix, per_locale)
        if chunk_longest > longest_prefix:
            longest_prefix = chunk_longest

    with open(outfilename, "w") as outfile:
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("%s_DATA = {}" % varprefix, file=outfile)
        for chunk_num in range(total_chunks):
            prnt("from .data%d import data" % chunk_num, file=outfile)
            prnt("%s_DATA.update(data)" % varprefix, file=outfile)
        prnt("del data", file=outfile)
        prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix),
             file=outfile)

    # Emit corresponding typing info.
    with open(outfilename + "i", "w") as pyifile:
        if per_locale:
            prnt("%s_DATA: dict[str, dict[str, str]]" % varprefix,
                 file=pyifile)
        else:
            prnt("%s_DATA: dict[str, tuple[str, ...]]" % varprefix,
                 file=pyifile)
        prnt("%s_LONGEST_PREFIX: int" % varprefix, file=pyifile)
Ejemplo n.º 31
0
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix,
                           per_locale):
    """Output the per-prefix data in Python form to the given file """
    with open(outfilename, "w") as outfile:
        longest_prefix = 0
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("%s_DATA = {" % varprefix, file=outfile)
        for prefix in sorted(prefixdata.keys()):
            if len(prefix) > longest_prefix:
                longest_prefix = len(prefix)
            if per_locale:
                prnt(" '%s':%s," %
                     (prefix, _stable_dict_repr(prefixdata[prefix])),
                     file=outfile)
            else:
                prnt(" '%s':%s," % (prefix, _tuple_repr(prefixdata[prefix])),
                     file=outfile)
        prnt("}", file=outfile)
        prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix),
             file=outfile)
def output_prefixdata_chunk(prefixdata, outfilename, module_prefix,
                            per_locale):
    with open(outfilename, "w") as outfile:
        longest_prefix = 0
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix},
                 file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("data = {", file=outfile)
        for prefix in sorted(prefixdata.keys()):
            if len(prefix) > longest_prefix:
                longest_prefix = len(prefix)
            if per_locale:
                prnt(" '%s':%s," %
                     (prefix, _stable_dict_repr(prefixdata[prefix])),
                     file=outfile)
            else:
                prnt(" '%s':%s," % (prefix, _tuple_repr(prefixdata[prefix])),
                     file=outfile)
        prnt("}", file=outfile)
    return longest_prefix
def output_prefixdata_chunk(prefixdata, outfilename, module_prefix, per_locale):
    with open(outfilename, "w") as outfile:
        longest_prefix = 0
        if per_locale:
            prnt(PREFIXDATA_LOCALE_FILE_PROLOG % {'module': module_prefix}, file=outfile)
        else:
            prnt(PREFIXDATA_FILE_PROLOG % {'module': module_prefix}, file=outfile)
        prnt(COPYRIGHT_NOTICE, file=outfile)
        prnt("data = {", file=outfile)
        for prefix in prefixdata.keys():
            if len(prefix) > longest_prefix:
                longest_prefix = len(prefix)
            if per_locale:
                prnt(" '%s':%s," % (prefix, _stable_dict_repr(prefixdata[prefix])), file=outfile)
            else:
                prnt(" '%s':%s," % (prefix, _tuple_repr(prefixdata[prefix])), file=outfile)
        prnt("}", file=outfile)
    return longest_prefix