Merge "Use python 3 in ./tools/localedata/extract_icu_data.py"

This commit is contained in:
Victor Chang
2022-06-07 13:56:24 +00:00
committed by Gerrit Code Review

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# #
# Copyright 2016 The Android Open Source Project. All Rights Reserved. # Copyright 2016 The Android Open Source Project. All Rights Reserved.
# #
@@ -61,7 +61,7 @@ def read_likely_subtags(input_file_name):
# would be chosen.) # would be chosen.)
} }
for line in input_file: for line in input_file:
line = unicode(line, 'UTF-8').strip(u' \n\uFEFF').encode('UTF-8') line = line.strip(u' \n\uFEFF')
if line.startswith('//'): if line.startswith('//'):
continue continue
if '{' in line and '}' in line: if '{' in line and '}' in line:
@@ -118,26 +118,26 @@ def pack_to_uint32(locale):
def dump_script_codes(all_scripts): def dump_script_codes(all_scripts):
"""Dump the SCRIPT_CODES table.""" """Dump the SCRIPT_CODES table."""
print 'const char SCRIPT_CODES[][4] = {' print('const char SCRIPT_CODES[][4] = {')
for index, script in enumerate(all_scripts): for index, script in enumerate(all_scripts):
print " /* %-2d */ {'%c', '%c', '%c', '%c'}," % ( print(" /* %-2d */ {'%c', '%c', '%c', '%c'}," % (
index, script[0], script[1], script[2], script[3]) index, script[0], script[1], script[2], script[3]))
print '};' print('};')
print print()
def dump_script_data(likely_script_dict, all_scripts): def dump_script_data(likely_script_dict, all_scripts):
"""Dump the script data.""" """Dump the script data."""
print print()
print 'const std::unordered_map<uint32_t, uint8_t> LIKELY_SCRIPTS({' print('const std::unordered_map<uint32_t, uint8_t> LIKELY_SCRIPTS({')
for locale in sorted(likely_script_dict.keys()): for locale in sorted(likely_script_dict.keys()):
script = likely_script_dict[locale] script = likely_script_dict[locale]
print ' {0x%08Xu, %2du}, // %s -> %s' % ( print(' {0x%08Xu, %2du}, // %s -> %s' % (
pack_to_uint32(locale), pack_to_uint32(locale),
all_scripts.index(script), all_scripts.index(script),
locale.replace('_', '-'), locale.replace('_', '-'),
script) script))
print '});' print('});')
def pack_to_uint64(locale): def pack_to_uint64(locale):
@@ -152,13 +152,13 @@ def pack_to_uint64(locale):
def dump_representative_locales(representative_locales): def dump_representative_locales(representative_locales):
"""Dump the set of representative locales.""" """Dump the set of representative locales."""
print print()
print 'std::unordered_set<uint64_t> REPRESENTATIVE_LOCALES({' print('std::unordered_set<uint64_t> REPRESENTATIVE_LOCALES({')
for locale in sorted(representative_locales): for locale in sorted(representative_locales):
print ' 0x%08XLLU, // %s' % ( print(' 0x%08XLLU, // %s' % (
pack_to_uint64(locale), pack_to_uint64(locale),
locale) locale))
print '});' print('});')
def read_and_dump_likely_data(icu_data_dir): def read_and_dump_likely_data(icu_data_dir):
@@ -220,30 +220,30 @@ def get_likely_script(locale, likely_script_dict):
def dump_parent_data(script_organized_dict): def dump_parent_data(script_organized_dict):
"""Dump information for parents of locales.""" """Dump information for parents of locales."""
sorted_scripts = sorted(script_organized_dict.keys()) sorted_scripts = sorted(script_organized_dict.keys())
print print()
for script in sorted_scripts: for script in sorted_scripts:
parent_dict = script_organized_dict[script] parent_dict = script_organized_dict[script]
print ('const std::unordered_map<uint32_t, uint32_t> %s_PARENTS({' print ('const std::unordered_map<uint32_t, uint32_t> %s_PARENTS({'
% escape_script_variable_name(script.upper())) % escape_script_variable_name(script.upper()))
for locale in sorted(parent_dict.keys()): for locale in sorted(parent_dict.keys()):
parent = parent_dict[locale] parent = parent_dict[locale]
print ' {0x%08Xu, 0x%08Xu}, // %s -> %s' % ( print(' {0x%08Xu, 0x%08Xu}, // %s -> %s' % (
pack_to_uint32(locale), pack_to_uint32(locale),
pack_to_uint32(parent), pack_to_uint32(parent),
locale.replace('_', '-'), locale.replace('_', '-'),
parent.replace('_', '-')) parent.replace('_', '-')))
print '});' print('});')
print print()
print 'const struct {' print('const struct {')
print ' const char script[4];' print(' const char script[4];')
print ' const std::unordered_map<uint32_t, uint32_t>* map;' print(' const std::unordered_map<uint32_t, uint32_t>* map;')
print '} SCRIPT_PARENTS[] = {' print('} SCRIPT_PARENTS[] = {')
for script in sorted_scripts: for script in sorted_scripts:
print " {{'%c', '%c', '%c', '%c'}, &%s_PARENTS}," % ( print(" {{'%c', '%c', '%c', '%c'}, &%s_PARENTS}," % (
script[0], script[1], script[2], script[3], script[0], script[1], script[2], script[3],
escape_script_variable_name(script.upper())) escape_script_variable_name(script.upper())))
print '};' print('};')
def dump_parent_tree_depth(parent_dict): def dump_parent_tree_depth(parent_dict):
@@ -256,8 +256,8 @@ def dump_parent_tree_depth(parent_dict):
depth += 1 depth += 1
max_depth = max(max_depth, depth) max_depth = max(max_depth, depth)
assert max_depth < 5 # Our algorithms assume small max_depth assert max_depth < 5 # Our algorithms assume small max_depth
print print()
print 'const size_t MAX_PARENT_DEPTH = %d;' % max_depth print('const size_t MAX_PARENT_DEPTH = %d;' % max_depth)
def read_and_dump_parent_data(icu_data_dir, likely_script_dict): def read_and_dump_parent_data(icu_data_dir, likely_script_dict):
@@ -281,8 +281,8 @@ def main():
source_root, source_root,
'external', 'icu', 'icu4c', 'source', 'data') 'external', 'icu', 'icu4c', 'source', 'data')
print '// Auto-generated by %s' % sys.argv[0] print('// Auto-generated by %s' % sys.argv[0])
print print()
likely_script_dict = read_and_dump_likely_data(icu_data_dir) likely_script_dict = read_and_dump_likely_data(icu_data_dir)
read_and_dump_parent_data(icu_data_dir, likely_script_dict) read_and_dump_parent_data(icu_data_dir, likely_script_dict)