11#!/usr/bin/env python
2- """Script to read the libphonenumber geocoding metadata and generate Python code.
2+ """Script to read the libphonenumber per-prefix metadata and generate Python code.
33
44Invocation:
5- buildgeocodingdata .py indir outfile
5+ buildprefixdata .py indir outfile
66
7- Processes all of the geocoding data under the given input directory and emit
7+ Processes all of the per-prefix data under the given input directory and emit
88generated Python code.
99"""
1010
11- # Based on original geocoding data files from libphonenumber:
12- # resources/geocoding/*/*.txt
11+ # Based on original metadata data files from libphonenumber:
12+ # resources/geocoding/*/*.txt, resources/carrier/*/*.txt
1313# Copyright (C) 2011 The Libphonenumber Authors
1414#
1515# Licensed under the Apache License, Version 2.0 (the "License");
2828import sys
2929import glob
3030import re
31+ import getopt
3132import datetime
3233
3334if sys .version_info >= (3 , 0 ):
@@ -47,13 +48,13 @@ def prnt(*args, **kwargs):
4748 def u (s ):
4849 return unicode (s )
4950
50- GEODATA_SUFFIX = ".txt"
51+ PREFIXDATA_SUFFIX = ".txt"
5152BLANK_LINE_RE = re .compile (r'^\s*$' , re .UNICODE )
5253COMMENT_LINE_RE = re .compile (r'^\s*#.*$' , re .UNICODE )
53- DATA_LINE_RE = re .compile (r'^(?P<prefix>\d+)\|(?P<location>.*)$' , re .UNICODE )
54+ DATA_LINE_RE = re .compile (r'^\+? (?P<prefix>\d+)\|(?P<location>.*)$' , re .UNICODE )
5455
5556# Boilerplate header
56- GEODATA_FILE_PROLOG = '''"""Geocoding data, mapping each prefix to a dict of locale:locationname .
57+ PREFIXDATA_FILE_PROLOG = '''"""Per-prefix data, mapping each prefix to a dict of locale:name .
5758
5859Auto-generated file, do not edit by hand.
5960"""
@@ -76,8 +77,8 @@ def u(s):
7677""" % datetime .datetime .now ().year
7778
7879
79- def load_geodata_file ( geodata , filename , locale , overall_prefix ):
80- """Load geocoding data from the given file, for the given locale and prefix.
80+ def load_prefixdata_file ( prefixdata , filename , locale , overall_prefix ):
81+ """Load per-prefix data from the given file, for the given locale and prefix.
8182
8283 We assume that this file:
8384 - is encoded in UTF-8
@@ -100,9 +101,9 @@ def load_geodata_file(geodata, filename, locale, overall_prefix):
100101 if not prefix .startswith (overall_prefix ):
101102 raise Exception ("%s:%d: Prefix %s is not within %s" %
102103 (filename , lineno , prefix , overall_prefix ))
103- if prefix not in geodata :
104- geodata [prefix ] = {}
105- geodata [prefix ][locale ] = location
104+ if prefix not in prefixdata :
105+ prefixdata [prefix ] = {}
106+ prefixdata [prefix ][locale ] = location
106107 elif BLANK_LINE_RE .match (uline ):
107108 pass
108109 elif COMMENT_LINE_RE .match (uline ):
@@ -112,21 +113,21 @@ def load_geodata_file(geodata, filename, locale, overall_prefix):
112113 (filename , lineno , line ))
113114
114115
115- def load_geodata (indir ):
116- """Load geocoding data from the given top-level directory.
116+ def load_prefixdata (indir ):
117+ """Load per-prefix data from the given top-level directory.
117118
118- Geocoding data is assumed to be held in files <indir>/<locale>/<prefix>.txt.
119+ Prefix data is assumed to be held in files <indir>/<locale>/<prefix>.txt.
119120 The same prefix may occur in multiple files, giving the location's name in
120121 different locales.
121122 """
122- geodata = {} # prefix => dict mapping location to location name
123+ prefixdata = {} # prefix => dict mapping location to location name
123124 for locale in os .listdir (indir ):
124125 if not os .path .isdir (os .path .join (indir , locale )):
125126 continue
126- for filename in glob .glob (os .path .join (indir , locale , "*%s" % GEODATA_SUFFIX )):
127+ for filename in glob .glob (os .path .join (indir , locale , "*%s" % PREFIXDATA_SUFFIX )):
127128 overall_prefix , ext = os .path .splitext (os .path .basename (filename ))
128- load_geodata_file ( geodata , filename , locale , overall_prefix )
129- return geodata
129+ load_prefixdata_file ( prefixdata , filename , locale , overall_prefix )
130+ return prefixdata
130131
131132
132133def _stable_dict_repr (strdict ):
@@ -137,28 +138,44 @@ def _stable_dict_repr(strdict):
137138 return "{%s}" % ", " .join (lines )
138139
139140
140- def output_geodata_code ( geodata , outfilename ):
141- """Output the geocoding data in Python form to the given file """
141+ def output_prefixdata_code ( prefixdata , outfilename , varprefix ):
142+ """Output the per-prefix data in Python form to the given file """
142143 with open (outfilename , "w" ) as outfile :
143144 longest_prefix = 0
144- prnt (GEODATA_FILE_PROLOG , file = outfile )
145+ prnt (PREFIXDATA_FILE_PROLOG , file = outfile )
145146 prnt (COPYRIGHT_NOTICE , file = outfile )
146- prnt ("GEOCODE_DATA = {" , file = outfile )
147- for prefix in sorted (geodata .keys ()):
147+ prnt ("%s_DATA = {" % varprefix , file = outfile )
148+ for prefix in sorted (prefixdata .keys ()):
148149 if len (prefix ) > longest_prefix :
149150 longest_prefix = len (prefix )
150- prnt (" '%s':%s," % (prefix , _stable_dict_repr (geodata [prefix ])), file = outfile )
151+ prnt (" '%s':%s," % (prefix , _stable_dict_repr (prefixdata [prefix ])), file = outfile )
151152 prnt ("}" , file = outfile )
152- prnt ("GEOCODE_LONGEST_PREFIX = %d" % longest_prefix , file = outfile )
153+ prnt ("%s_LONGEST_PREFIX = %d" % ( varprefix , longest_prefix ) , file = outfile )
153154
154155
155156def _standalone (argv ):
156157 """Parse the given input directory and emit generated code."""
157- if len (argv ) != 2 :
158+ varprefix = "GEOCODE"
159+ try :
160+ opts , args = getopt .getopt (argv , "hv:" , ("help" , "var=" ))
161+ except getopt .GetoptError :
158162 prnt (__doc__ , file = sys .stderr )
159163 sys .exit (1 )
160- geodata = load_geodata (argv [0 ])
161- output_geodata_code (geodata , argv [1 ])
164+ for opt , arg in opts :
165+ if opt in ("-h" , "--help" ):
166+ prnt (__doc__ , file = sys .stderr )
167+ sys .exit (1 )
168+ elif opt in ("-v" , "--var" ):
169+ varprefix = arg
170+ else :
171+ prnt ("Unknown option %s" % opt , file = sys .stderr )
172+ prnt (__doc__ , file = sys .stderr )
173+ sys .exit (1 )
174+ if len (args ) != 2 :
175+ prnt (__doc__ , file = sys .stderr )
176+ sys .exit (1 )
177+ prefixdata = load_prefixdata (args [0 ])
178+ output_prefixdata_code (prefixdata , args [1 ], varprefix )
162179
163180
164181if __name__ == "__main__" :
0 commit comments