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. 5 6 """Generate a C++ header from input_methods.txt. 7 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 13 Run it like: 14 gen_input_methods.py input_methods.txt input_methods.h 15 16 It will produce output that looks like: 17 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_ 21 22 namespace chromeos { 23 namespace input_method { 24 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; 31 }; 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}, 38 }; 39 40 } // namespace input_method 41 } // namespace chromeos 42 43 #endif // CHROMEOS_IME_INPUT_METHODS_H_ 44 45 """ 46 47 import fileinput 48 import re 49 import sys 50 51 OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py 52 #ifndef CHROMEOS_IME_INPUT_METHODS_H_ 53 #define CHROMEOS_IME_INPUT_METHODS_H_ 54 55 namespace chromeos { 56 namespace input_method { 57 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; 64 }; 65 const InputMethodsInfo kInputMethods[] = { 66 """ 67 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') 72 73 OUTPUT_FOOTER = """ 74 }; 75 76 } // namespace input_method 77 } // namespace chromeos 78 79 #endif // CHROMEOS_IME_INPUT_METHODS_H_ 80 """ 81 82 def CreateEngineHeader(engines): 83 """Create the header file from a list of engines. 84 85 Arguments: 86 engines: list of engine objects 87 Returns: 88 The text of a C++ header file containing the engine data. 89 """ 90 output = [] 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) 99 100 return "".join(output) 101 102 103 def main(argv): 104 if len(argv) != 3: 105 print 'Usage: gen_input_methods.py [whitelist] [output]' 106 sys.exit(1) 107 engines = [] 108 for line in fileinput.input(sys.argv[1]): 109 line = line.strip() 110 if not line or re.match(r'#', line): 111 continue 112 columns = line.split() 113 assert len(columns) == 4 or len(columns) == 5, "Invalid format: " + line 114 engine = {} 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) 125 126 output = CreateEngineHeader(engines) 127 output_file = open(sys.argv[2], 'w') 128 output_file.write(output) 129 130 131 if __name__ == '__main__': 132 main(sys.argv) 133