| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Generate a C++ header from ibus_input_methods.txt. | 6 """Generate a C++ header from ibus_input_methods.txt. |
| 7 | 7 |
| 8 This program generates a C++ header file containing the information on | 8 This program generates a C++ header file containing the information on |
| 9 available ibus engines. It parses ibus_input_methods.txt, and then | 9 available input methods. It parses input_methods.txt, and then generates a |
| 10 generates a static array definition from the information extracted. The | 10 static array definition from the information extracted. The input and output |
| 11 input and output file names are specified on the command line. | 11 file names are specified on the command line. |
| 12 | 12 |
| 13 Run it like: | 13 Run it like: |
| 14 gen_ibus_input_methods.py ibus_input_methods.txt ibus_input_methods.h | 14 gen_input_methods.py input_methods.txt input_methods.h |
| 15 | 15 |
| 16 It will produce output that looks like: | 16 It will produce output that looks like: |
| 17 | 17 |
| 18 // This file is automatically generated by gen_engines.py | 18 // This file is automatically generated by gen_input_methods.py |
| 19 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 19 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 20 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 20 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 namespace input_method { | 23 namespace input_method { |
| 24 | 24 |
| 25 struct IBusEngineInfo { | 25 struct InputMethodsInfo { |
| 26 const char* input_method_id; | 26 const char* input_method_id; |
| 27 const char* language_code; | 27 const char* language_code; |
| 28 const char* xkb_keyboard_id; | 28 const char* xkb_keyboard_id; |
| 29 const char* keyboard_overlay_id; | |
| 30 }; | 29 }; |
| 31 const IBusEngineInfo kIBusEngines[] = { | 30 const InputMethodsInfo kInputMethods[] = { |
| 32 {"mozc-chewing", "zh-TW", "us", "zh-TW"}, | 31 {"mozc-chewing", "zh-TW", "us"}, |
| 33 {"xkb:us::eng", "eng", "us", "en_US"}, | 32 {"xkb:us::eng", "en-US", "us"}, |
| 34 {"xkb:us:dvorak:eng", "eng", "us(dvorak)", "en_US_dvorak"}, | 33 {"xkb:us:dvorak:eng", "en-US", "us(dvorak)"}, |
| 35 {"xkb:be::fra", "fra", "be", "fr"}, | 34 {"xkb:be::fra", "fr", "be"}, |
| 36 {"xkb:br::por", "por", "br", "pt_BR"}, | 35 {"xkb:br::por", "pt-BR", "br"}, |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 } // namespace input_method | 38 } // namespace input_method |
| 40 } // namespace chromeos | 39 } // namespace chromeos |
| 41 | 40 |
| 42 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 41 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 43 | 42 |
| 44 """ | 43 """ |
| 45 | 44 |
| 46 import fileinput | 45 import fileinput |
| 47 import re | 46 import re |
| 48 import sys | 47 import sys |
| 49 | 48 |
| 50 OUTPUT_HEADER = """// Automatically generated by gen_ibus_input_methods.py | 49 OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py |
| 51 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 50 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 52 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 51 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 53 | 52 |
| 54 namespace chromeos { | 53 namespace chromeos { |
| 55 namespace input_method { | 54 namespace input_method { |
| 56 | 55 |
| 57 struct IBusEngineInfo { | 56 struct InputMethodsInfo { |
| 58 const char* input_method_id; | 57 const char* input_method_id; |
| 59 const char* language_code; | 58 const char* language_code; |
| 60 const char* xkb_layout_id; | 59 const char* xkb_layout_id; |
| 61 }; | 60 }; |
| 62 const IBusEngineInfo kIBusEngines[] = { | 61 const InputMethodsInfo kInputMethods[] = { |
| 63 """ | 62 """ |
| 64 | 63 |
| 65 CPP_FORMAT = '#if %s\n' | 64 CPP_FORMAT = '#if %s\n' |
| 66 ENGINE_FORMAT = (' {"%(input_method_id)s", "%(language_code)s", ' + | 65 ENGINE_FORMAT = (' {"%(input_method_id)s", "%(language_code)s", ' + |
| 67 '"%(xkb_layout_id)s"},\n') | 66 '"%(xkb_layout_id)s"},\n') |
| 68 | 67 |
| 69 OUTPUT_FOOTER = """ | 68 OUTPUT_FOOTER = """ |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 } // namespace input_method | 71 } // namespace input_method |
| 73 } // namespace chromeos | 72 } // namespace chromeos |
| 74 | 73 |
| 75 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_INPUT_METHODS_H_ | 74 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_ |
| 76 """ | 75 """ |
| 77 | 76 |
| 78 def CreateEngineHeader(engines): | 77 def CreateEngineHeader(engines): |
| 79 """Create the header file from a list of engines. | 78 """Create the header file from a list of engines. |
| 80 | 79 |
| 81 Arguments: | 80 Arguments: |
| 82 engines: list of ibus engine objects | 81 engines: list of engine objects |
| 83 Returns: | 82 Returns: |
| 84 The text of a C++ header file containing the engine data. | 83 The text of a C++ header file containing the engine data. |
| 85 """ | 84 """ |
| 86 output = [] | 85 output = [] |
| 87 output.append(OUTPUT_HEADER) | 86 output.append(OUTPUT_HEADER) |
| 88 for engine in engines: | 87 for engine in engines: |
| 89 if engine.has_key('if'): | 88 if engine.has_key('if'): |
| 90 output.append(CPP_FORMAT % engine['if']) | 89 output.append(CPP_FORMAT % engine['if']) |
| 91 output.append(ENGINE_FORMAT % engine) | 90 output.append(ENGINE_FORMAT % engine) |
| 92 if engine.has_key('if'): | 91 if engine.has_key('if'): |
| 93 output.append('#endif\n') | 92 output.append('#endif\n') |
| 94 output.append(OUTPUT_FOOTER) | 93 output.append(OUTPUT_FOOTER) |
| 95 | 94 |
| 96 return "".join(output) | 95 return "".join(output) |
| 97 | 96 |
| 98 | 97 |
| 99 def main(argv): | 98 def main(argv): |
| 100 if len(argv) != 3: | 99 if len(argv) != 3: |
| 101 print 'Usage: gen_ibus_input_methods.py [whitelist] [output]' | 100 print 'Usage: gen_input_methods.py [whitelist] [output]' |
| 102 sys.exit(1) | 101 sys.exit(1) |
| 103 engines = [] | 102 engines = [] |
| 104 for line in fileinput.input(sys.argv[1]): | 103 for line in fileinput.input(sys.argv[1]): |
| 105 line = line.strip() | 104 line = line.strip() |
| 106 if not line or re.match(r'#', line): | 105 if not line or re.match(r'#', line): |
| 107 continue | 106 continue |
| 108 columns = line.split() | 107 columns = line.split() |
| 109 assert len(columns) == 3 or len(columns) == 4, "Invalid format: " + line | 108 assert len(columns) == 3 or len(columns) == 4, "Invalid format: " + line |
| 110 engine = {} | 109 engine = {} |
| 111 engine['input_method_id'] = columns[0] | 110 engine['input_method_id'] = columns[0] |
| 112 engine['xkb_layout_id'] = columns[1] | 111 engine['xkb_layout_id'] = columns[1] |
| 113 engine['language_code'] = columns[2] | 112 engine['language_code'] = columns[2] |
| 114 if len(columns) == 4: | 113 if len(columns) == 4: |
| 115 engine['if'] = columns[3] | 114 engine['if'] = columns[3] |
| 116 engines.append(engine) | 115 engines.append(engine) |
| 117 | 116 |
| 118 output = CreateEngineHeader(engines) | 117 output = CreateEngineHeader(engines) |
| 119 output_file = open(sys.argv[2], 'w') | 118 output_file = open(sys.argv[2], 'w') |
| 120 output_file.write(output) | 119 output_file.write(output) |
| 121 | 120 |
| 122 | 121 |
| 123 if __name__ == '__main__': | 122 if __name__ == '__main__': |
| 124 main(sys.argv) | 123 main(sys.argv) |
| OLD | NEW |