Skip to content

Commit a1c78ec

Browse files
committed
buildprefixdata: allow control of number of chunks
1 parent c9d27f9 commit a1c78ec

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

tools/python/buildprefixdata.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
generated Python code.
99
1010
Options:
11-
--var XXX : use this prefix for variable names in generated code
12-
--flat : don't do per-locale processing
13-
--sep C : expect metadata to be a list with C as separator
11+
--var XXX : use this prefix for variable names in generated code
12+
--flat : don't do per-locale processing
13+
--sep C : expect metadata to be a list with C as separator
14+
--chunks N : divide metadata into N chunks
1415
"""
1516

1617
# Based on original metadata data files from libphonenumber:
@@ -160,17 +161,22 @@ def _tuple_repr(data):
160161
return "(%s)" % ", ".join([rpr(x) for x in data])
161162

162163

163-
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix, per_locale):
164+
def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix, per_locale, chunks):
164165
"""Output the per-prefix data in Python form to the given file """
165166
sorted_keys = sorted(prefixdata.keys())
166167
total_keys = len(sorted_keys)
167-
total_chunks = int(math.ceil(total_keys / float(PREFIXDATA_CHUNK_SIZE)))
168+
if chunks == -1:
169+
chunk_size = PREFIXDATA_CHUNK_SIZE
170+
total_chunks = int(math.ceil(total_keys / float(chunk_size)))
171+
else:
172+
chunk_size = int(math.ceil(total_keys / float(chunks)))
173+
total_chunks = chunks
168174

169175
outdirname = os.path.dirname(outfilename)
170176
longest_prefix = 0
171177
for chunk_num in range(total_chunks):
172-
chunk_index = PREFIXDATA_CHUNK_SIZE * chunk_num
173-
chunk_keys = sorted_keys[chunk_index:chunk_index + PREFIXDATA_CHUNK_SIZE]
178+
chunk_index = chunk_size * chunk_num
179+
chunk_keys = sorted_keys[chunk_index:chunk_index + chunk_size]
174180
chunk_data = {}
175181
for key in chunk_keys:
176182
chunk_data[key] = prefixdata[key]
@@ -219,8 +225,9 @@ def _standalone(argv):
219225
varprefix = "GEOCODE"
220226
per_locale = True
221227
separator = None
228+
chunks = -1
222229
try:
223-
opts, args = getopt.getopt(argv, "hv:fs:", ("help", "var=", "flat", "sep="))
230+
opts, args = getopt.getopt(argv, "hv:fs:c:", ("help", "var=", "flat", "sep=", "chunks="))
224231
except getopt.GetoptError:
225232
prnt(__doc__, file=sys.stderr)
226233
sys.exit(1)
@@ -234,6 +241,8 @@ def _standalone(argv):
234241
per_locale = False
235242
elif opt in ("-s", "--sep"):
236243
separator = arg
244+
elif opt in ("-c", "--chunks"):
245+
chunks = int(arg)
237246
else:
238247
prnt("Unknown option %s" % opt, file=sys.stderr)
239248
prnt(__doc__, file=sys.stderr)
@@ -246,7 +255,7 @@ def _standalone(argv):
246255
else:
247256
prefixdata = {}
248257
load_locale_prefixdata_file(prefixdata, args[0], separator=separator)
249-
output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale)
258+
output_prefixdata_code(prefixdata, args[1], args[2], varprefix, per_locale, chunks)
250259

251260

252261
if __name__ == "__main__":

0 commit comments

Comments
 (0)