• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8This program generates a C++ header file containing the information on
9available input methods.  It parses input_methods.txt, and then generates a
10static array definition from the information extracted. The input and output
11file names are specified on the command line.
12
13Run it like:
14  gen_input_methods.py input_methods.txt input_methods.h
15
16It 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
22namespace chromeos {
23namespace input_method {
24
25struct InputMethodsInfo {
26  const char* input_method_id;
27  const char* language_code;
28  const char* xkb_keyboard_id;
29  bool is_login_keyboard;
30};
31const InputMethodsInfo kInputMethods[] = {
32  {"xkb:us::eng", "en-US", "us", true},
33  {"xkb:us:dvorak:eng", "en-US", "us(dvorak)", true},
34  {"xkb:be::fra", "fr", "be", true},
35  {"xkb:br::por", "pt-BR", "br", true},
36  {"xkb:ru::rus", "ru", "ru", false},
37};
38
39}  // namespace input_method
40}  // namespace chromeos
41
42#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
43
44"""
45
46import fileinput
47import re
48import sys
49
50OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py
51#ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
52#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
53
54namespace chromeos {
55namespace input_method {
56
57struct InputMethodsInfo {
58  const char* input_method_id;
59  const char* language_code;
60  const char* xkb_layout_id;
61  bool is_login_keyboard;
62};
63const InputMethodsInfo kInputMethods[] = {
64"""
65
66CPP_FORMAT = '#if %s\n'
67ENGINE_FORMAT = ('  {"%(input_method_id)s", "%(language_code)s", ' +
68                 '"%(xkb_layout_id)s", %(is_login_keyboard)s},\n')
69
70OUTPUT_FOOTER = """
71};
72
73}  // namespace input_method
74}  // namespace chromeos
75
76#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHODS_H_
77"""
78
79def CreateEngineHeader(engines):
80  """Create the header file from a list of engines.
81
82  Arguments:
83    engines: list of engine objects
84  Returns:
85    The text of a C++ header file containing the engine data.
86  """
87  output = []
88  output.append(OUTPUT_HEADER)
89  for engine in engines:
90    if engine.has_key('if'):
91      output.append(CPP_FORMAT % engine['if'])
92    output.append(ENGINE_FORMAT % engine)
93    if engine.has_key('if'):
94      output.append('#endif\n')
95  output.append(OUTPUT_FOOTER)
96
97  return "".join(output)
98
99
100def main(argv):
101  if len(argv) != 3:
102    print 'Usage: gen_input_methods.py [whitelist] [output]'
103    sys.exit(1)
104  engines = []
105  for line in fileinput.input(sys.argv[1]):
106    line = line.strip()
107    if not line or re.match(r'#', line):
108      continue
109    columns = line.split()
110    assert len(columns) == 3 or len(columns) == 4, "Invalid format: " + line
111    engine = {}
112    engine['input_method_id'] = columns[0]
113    engine['xkb_layout_id'] = columns[1]
114    engine['language_code'] = columns[2]
115    is_login_keyboard = "false"
116    if len(columns) == 4:
117      assert columns[3] == "login", "Invalid attribute: " + columns[3]
118      is_login_keyboard = "true"
119    engine['is_login_keyboard'] = is_login_keyboard
120    engines.append(engine)
121
122  output = CreateEngineHeader(engines)
123  output_file = open(sys.argv[2], 'w')
124  output_file.write(output)
125
126
127if __name__ == '__main__':
128  main(sys.argv)
129