Home | History | Annotate | Download | only in ime
      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 ibus_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 CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
     20 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_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 };
     30 const InputMethodsInfo kInputMethods[] = {
     31   {"mozc-chewing", "zh-TW", "us"},
     32   {"xkb:us::eng", "en-US", "us"},
     33   {"xkb:us:dvorak:eng", "en-US", "us(dvorak)"},
     34   {"xkb:be::fra", "fr", "be"},
     35   {"xkb:br::por", "pt-BR", "br"},
     36 };
     37 
     38 }  // namespace input_method
     39 }  // namespace chromeos
     40 
     41 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
     42 
     43 """
     44 
     45 import fileinput
     46 import re
     47 import sys
     48 
     49 OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py
     50 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
     51 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
     52 
     53 namespace chromeos {
     54 namespace input_method {
     55 
     56 struct InputMethodsInfo {
     57   const char* input_method_id;
     58   const char* language_code;
     59   const char* xkb_layout_id;
     60 };
     61 const InputMethodsInfo kInputMethods[] = {
     62 """
     63 
     64 CPP_FORMAT = '#if %s\n'
     65 ENGINE_FORMAT = ('  {"%(input_method_id)s", "%(language_code)s", ' +
     66                  '"%(xkb_layout_id)s"},\n')
     67 
     68 OUTPUT_FOOTER = """
     69 };
     70 
     71 }  // namespace input_method
     72 }  // namespace chromeos
     73 
     74 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
     75 """
     76 
     77 def CreateEngineHeader(engines):
     78   """Create the header file from a list of engines.
     79 
     80   Arguments:
     81     engines: list of engine objects
     82   Returns:
     83     The text of a C++ header file containing the engine data.
     84   """
     85   output = []
     86   output.append(OUTPUT_HEADER)
     87   for engine in engines:
     88     if engine.has_key('if'):
     89       output.append(CPP_FORMAT % engine['if'])
     90     output.append(ENGINE_FORMAT % engine)
     91     if engine.has_key('if'):
     92       output.append('#endif\n')
     93   output.append(OUTPUT_FOOTER)
     94 
     95   return "".join(output)
     96 
     97 
     98 def main(argv):
     99   if len(argv) != 3:
    100     print 'Usage: gen_input_methods.py [whitelist] [output]'
    101     sys.exit(1)
    102   engines = []
    103   for line in fileinput.input(sys.argv[1]):
    104     line = line.strip()
    105     if not line or re.match(r'#', line):
    106       continue
    107     columns = line.split()
    108     assert len(columns) == 3 or len(columns) == 4, "Invalid format: " + line
    109     engine = {}
    110     engine['input_method_id'] = columns[0]
    111     engine['xkb_layout_id'] = columns[1]
    112     engine['language_code'] = columns[2]
    113     if len(columns) == 4:
    114       engine['if'] = columns[3]
    115     engines.append(engine)
    116 
    117   output = CreateEngineHeader(engines)
    118   output_file = open(sys.argv[2], 'w')
    119   output_file.write(output)
    120 
    121 
    122 if __name__ == '__main__':
    123   main(sys.argv)
    124