Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: components/cronet/tools/generate_accept_languages.py

Issue 2960673003: [cronet] replace filter with comprehension in build script (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2017 The Chromium Authors. All rights reserved. 1 # Copyright 2017 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 # This script generates a header containing a dictionary from locales to 5 # This script generates a header containing a dictionary from locales to
6 # accept language strings from chromium's .xtb files. It is not very 6 # accept language strings from chromium's .xtb files. It is not very
7 # robust at the moment, and makes some assumptions about the format of 7 # robust at the moment, and makes some assumptions about the format of
8 # the files, including at least the following: 8 # the files, including at least the following:
9 # * assumes necessary data is contained only with files of the form 9 # * assumes necessary data is contained only with files of the form
10 # components/strings/components_locale_settings_${LANG}.xtb 10 # components/strings/components_locale_settings_${LANG}.xtb
11 # * assumes ${LANG} is identified in the lang attribute of the root 11 # * assumes ${LANG} is identified in the lang attribute of the root
12 # element of the file's xml data 12 # element of the file's xml data
13 # * assumes that there is only one relevant element with the 13 # * assumes that there is only one relevant element with the
14 # IDS_ACCEPT_LANGUAGES attribute 14 # IDS_ACCEPT_LANGUAGES attribute
15 15
16 import os, re, sys 16 import os, re, sys
17 from xml.etree import ElementTree 17 from xml.etree import ElementTree
18 18
19 STRINGS_DIR = sys.argv[2] + 'components/strings/' 19 STRINGS_DIR = sys.argv[2] + 'components/strings/'
20 20
21 def extract_accept_langs(filename): 21 def extract_accept_langs(filename):
22 tree = ElementTree.parse(STRINGS_DIR + filename).getroot() 22 tree = ElementTree.parse(STRINGS_DIR + filename).getroot()
23 for child in tree: 23 for child in tree:
24 if child.get('id') == 'IDS_ACCEPT_LANGUAGES': 24 if child.get('id') == 'IDS_ACCEPT_LANGUAGES':
25 return tree.get('lang'), child.text 25 return tree.get('lang'), child.text
26 26
27 def gen_accept_langs_table(): 27 def gen_accept_langs_table():
28 return dict(filter(None, (extract_accept_langs(filename) 28 return dict(accept_langs for accept_langs in (extract_accept_langs(filename)
29 for filename in os.listdir(STRINGS_DIR) 29 for filename in os.listdir(STRINGS_DIR)
30 if re.match(r'components_locale_settings_\S+.xtb', filename)))) 30 if re.match(r'components_locale_settings_\S+.xtb', filename))
31 if accept_langs)
xunjieli 2017/06/27 17:51:45 This is getting rather long. Can you separate it o
31 32
32 HEADER = "NSDictionary* acceptLangs = @{" 33 HEADER = "NSDictionary* acceptLangs = @{"
33 def LINE(locale, accept_langs): 34 def LINE(locale, accept_langs):
34 return ' @"' + locale + '" : @"' + accept_langs + '",' 35 return ' @"' + locale + '" : @"' + accept_langs + '",'
35 FOOTER = "};" 36 FOOTER = "};"
36 37
37 def main(): 38 def main():
38 with open(sys.argv[1] + "/accept_languages_table.h", "w+") as f: 39 with open(sys.argv[1] + "/accept_languages_table.h", "w+") as f:
39 print >>f, HEADER 40 print >>f, HEADER
40 for (locale, accept_langs) in gen_accept_langs_table().items(): 41 for (locale, accept_langs) in gen_accept_langs_table().items():
41 print >>f, LINE(locale, accept_langs) 42 print >>f, LINE(locale, accept_langs)
42 print >>f, FOOTER 43 print >>f, FOOTER
43 44
44 if __name__ == "__main__": 45 if __name__ == "__main__":
45 main() 46 main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698