Update V8 to version 4.6.54.
[chromium-blink-merge.git] / chromeos / ime / gen_input_methods.py
blob90218c4c6fa174422587c12d5314224d36ca3178
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Generate a C++ header from input_methods.txt.
8 This program generates a C++ header file containing the information on
9 available input methods. It parses input_methods.txt, and then generates a
10 static array definition from the information extracted. The input and output
11 file names are specified on the command line.
12 The header input_methods.h is used in input_method_whitelist.cc which is for
13 testing purpose.
15 Run it like:
16 gen_input_methods.py input_methods.txt input_methods.h
18 It will produce output that looks like:
20 // This file is automatically generated by gen_input_methods.py
21 #ifndef CHROMEOS_IME_INPUT_METHODS_H_
22 #define CHROMEOS_IME_INPUT_METHODS_H_
24 namespace chromeos {
25 namespace input_method {
27 struct InputMethodsInfo {
28 const char* input_method_id;
29 const char* language_code;
30 const char* xkb_keyboard_id;
31 const char* indicator;
32 bool is_login_keyboard;
34 const InputMethodsInfo kInputMethods[] = {
35 {"xkb:us::eng", "en-US", "us", "US", true},
36 {"xkb:us:dvorak:eng", "en-US", "us(dvorak)", "DV", true},
37 {"xkb:be::fra", "fr", "be", "BE", true},
38 {"xkb:br::por", "pt-BR", "br", "BR", true},
39 {"xkb:ru::rus", "ru", "ru", "RU", false},
42 } // namespace input_method
43 } // namespace chromeos
45 #endif // CHROMEOS_IME_INPUT_METHODS_H_
47 """
49 import fileinput
50 import re
51 import sys
53 OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py
54 #ifndef CHROMEOS_IME_INPUT_METHODS_H_
55 #define CHROMEOS_IME_INPUT_METHODS_H_
57 namespace chromeos {
58 namespace input_method {
60 struct InputMethodsInfo {
61 const char* input_method_id;
62 const char* language_code;
63 const char* xkb_layout_id;
64 const char* indicator;
65 bool is_login_keyboard;
67 const InputMethodsInfo kInputMethods[] = {
68 """
70 CPP_FORMAT = '#if %s\n'
71 ENGINE_FORMAT = (' {"%(input_method_id)s", "%(language_code)s", ' +
72 '"%(xkb_layout_id)s", "%(indicator)s", ' +
73 '%(is_login_keyboard)s},\n')
75 OUTPUT_FOOTER = """
78 } // namespace input_method
79 } // namespace chromeos
81 #endif // CHROMEOS_IME_INPUT_METHODS_H_
82 """
84 def CreateEngineHeader(engines):
85 """Create the header file from a list of engines.
87 Arguments:
88 engines: list of engine objects
89 Returns:
90 The text of a C++ header file containing the engine data.
91 """
92 output = []
93 output.append(OUTPUT_HEADER)
94 for engine in engines:
95 if engine.has_key('if'):
96 output.append(CPP_FORMAT % engine['if'])
97 output.append(ENGINE_FORMAT % engine)
98 if engine.has_key('if'):
99 output.append('#endif\n')
100 output.append(OUTPUT_FOOTER)
102 return "".join(output)
105 def main(argv):
106 if len(argv) != 3:
107 print 'Usage: gen_input_methods.py [whitelist] [output]'
108 sys.exit(1)
109 engines = []
110 for line in fileinput.input(sys.argv[1]):
111 line = line.strip()
112 if not line or re.match(r'#', line):
113 continue
114 columns = line.split()
115 assert len(columns) == 4 or len(columns) == 5, "Invalid format: " + line
116 engine = {}
117 engine['input_method_id'] = columns[0]
118 engine['xkb_layout_id'] = columns[1]
119 engine['language_code'] = columns[2]
120 engine['indicator'] = columns[3]
121 is_login_keyboard = "false"
122 if len(columns) == 5:
123 assert columns[4] == "login", "Invalid attribute: " + columns[4]
124 is_login_keyboard = "true"
125 engine['is_login_keyboard'] = is_login_keyboard
126 engines.append(engine)
128 output = CreateEngineHeader(engines)
129 output_file = open(sys.argv[2], 'w')
130 output_file.write(output)
133 if __name__ == '__main__':
134 main(sys.argv)