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.
14 gen_input_methods.py input_methods.txt input_methods.h
16 It will produce output that looks like:
18 // This file is automatically generated by gen_input_methods.py
19 #ifndef CHROMEOS_IME_INPUT_METHODS_H_
20 #define CHROMEOS_IME_INPUT_METHODS_H_
23 namespace input_method {
25 struct InputMethodsInfo {
26 const char* input_method_id;
27 const char* language_code;
28 const char* xkb_keyboard_id;
29 const char* indicator;
30 bool is_login_keyboard;
32 const InputMethodsInfo kInputMethods[] = {
33 {"xkb:us::eng", "en-US", "us", "US", true},
34 {"xkb:us:dvorak:eng", "en-US", "us(dvorak)", "DV", true},
35 {"xkb:be::fra", "fr", "be", "BE", true},
36 {"xkb:br::por", "pt-BR", "br", "BR", true},
37 {"xkb:ru::rus", "ru", "ru", "RU", false},
40 } // namespace input_method
41 } // namespace chromeos
43 #endif // CHROMEOS_IME_INPUT_METHODS_H_
51 OUTPUT_HEADER
= """// Automatically generated by gen_input_methods.py
52 #ifndef CHROMEOS_IME_INPUT_METHODS_H_
53 #define CHROMEOS_IME_INPUT_METHODS_H_
56 namespace input_method {
58 struct InputMethodsInfo {
59 const char* input_method_id;
60 const char* language_code;
61 const char* xkb_layout_id;
62 const char* indicator;
63 bool is_login_keyboard;
65 const InputMethodsInfo kInputMethods[] = {
68 CPP_FORMAT
= '#if %s\n'
69 ENGINE_FORMAT
= (' {"%(input_method_id)s", "%(language_code)s", ' +
70 '"%(xkb_layout_id)s", "%(indicator)s", ' +
71 '%(is_login_keyboard)s},\n')
76 } // namespace input_method
77 } // namespace chromeos
79 #endif // CHROMEOS_IME_INPUT_METHODS_H_
82 def CreateEngineHeader(engines
):
83 """Create the header file from a list of engines.
86 engines: list of engine objects
88 The text of a C++ header file containing the engine data.
91 output
.append(OUTPUT_HEADER
)
92 for engine
in engines
:
93 if engine
.has_key('if'):
94 output
.append(CPP_FORMAT
% engine
['if'])
95 output
.append(ENGINE_FORMAT
% engine
)
96 if engine
.has_key('if'):
97 output
.append('#endif\n')
98 output
.append(OUTPUT_FOOTER
)
100 return "".join(output
)
105 print 'Usage: gen_input_methods.py [whitelist] [output]'
108 for line
in fileinput
.input(sys
.argv
[1]):
110 if not line
or re
.match(r
'#', line
):
112 columns
= line
.split()
113 assert len(columns
) == 4 or len(columns
) == 5, "Invalid format: " + line
115 engine
['input_method_id'] = columns
[0]
116 engine
['xkb_layout_id'] = columns
[1]
117 engine
['language_code'] = columns
[2]
118 engine
['indicator'] = columns
[3]
119 is_login_keyboard
= "false"
120 if len(columns
) == 5:
121 assert columns
[4] == "login", "Invalid attribute: " + columns
[4]
122 is_login_keyboard
= "true"
123 engine
['is_login_keyboard'] = is_login_keyboard
124 engines
.append(engine
)
126 output
= CreateEngineHeader(engines
)
127 output_file
= open(sys
.argv
[2], 'w')
128 output_file
.write(output
)
131 if __name__
== '__main__':