summaryrefslogtreecommitdiffstats
path: root/chromeos/ime/gen_input_methods.py
blob: 90218c4c6fa174422587c12d5314224d36ca3178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Generate a C++ header from input_methods.txt.

This program generates a C++ header file containing the information on
available input methods.  It parses input_methods.txt, and then generates a
static array definition from the information extracted. The input and output
file names are specified on the command line.
The header input_methods.h is used in input_method_whitelist.cc which is for
testing purpose.

Run it like:
  gen_input_methods.py input_methods.txt input_methods.h

It will produce output that looks like:

// This file is automatically generated by gen_input_methods.py
#ifndef CHROMEOS_IME_INPUT_METHODS_H_
#define CHROMEOS_IME_INPUT_METHODS_H_

namespace chromeos {
namespace input_method {

struct InputMethodsInfo {
  const char* input_method_id;
  const char* language_code;
  const char* xkb_keyboard_id;
  const char* indicator;
  bool is_login_keyboard;
};
const InputMethodsInfo kInputMethods[] = {
  {"xkb:us::eng", "en-US", "us", "US", true},
  {"xkb:us:dvorak:eng", "en-US", "us(dvorak)", "DV", true},
  {"xkb:be::fra", "fr", "be", "BE", true},
  {"xkb:br::por", "pt-BR", "br", "BR", true},
  {"xkb:ru::rus", "ru", "ru", "RU", false},
};

}  // namespace input_method
}  // namespace chromeos

#endif  // CHROMEOS_IME_INPUT_METHODS_H_

"""

import fileinput
import re
import sys

OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py
#ifndef CHROMEOS_IME_INPUT_METHODS_H_
#define CHROMEOS_IME_INPUT_METHODS_H_

namespace chromeos {
namespace input_method {

struct InputMethodsInfo {
  const char* input_method_id;
  const char* language_code;
  const char* xkb_layout_id;
  const char* indicator;
  bool is_login_keyboard;
};
const InputMethodsInfo kInputMethods[] = {
"""

CPP_FORMAT = '#if %s\n'
ENGINE_FORMAT = ('  {"%(input_method_id)s", "%(language_code)s", ' +
                 '"%(xkb_layout_id)s", "%(indicator)s", ' +
                 '%(is_login_keyboard)s},\n')

OUTPUT_FOOTER = """
};

}  // namespace input_method
}  // namespace chromeos

#endif  // CHROMEOS_IME_INPUT_METHODS_H_
"""

def CreateEngineHeader(engines):
  """Create the header file from a list of engines.

  Arguments:
    engines: list of engine objects
  Returns:
    The text of a C++ header file containing the engine data.
  """
  output = []
  output.append(OUTPUT_HEADER)
  for engine in engines:
    if engine.has_key('if'):
      output.append(CPP_FORMAT % engine['if'])
    output.append(ENGINE_FORMAT % engine)
    if engine.has_key('if'):
      output.append('#endif\n')
  output.append(OUTPUT_FOOTER)

  return "".join(output)


def main(argv):
  if len(argv) != 3:
    print 'Usage: gen_input_methods.py [whitelist] [output]'
    sys.exit(1)
  engines = []
  for line in fileinput.input(sys.argv[1]):
    line = line.strip()
    if not line or re.match(r'#', line):
      continue
    columns = line.split()
    assert len(columns) == 4 or len(columns) == 5, "Invalid format: " + line
    engine = {}
    engine['input_method_id'] = columns[0]
    engine['xkb_layout_id'] = columns[1]
    engine['language_code'] = columns[2]
    engine['indicator'] = columns[3]
    is_login_keyboard = "false"
    if len(columns) == 5:
      assert columns[4] == "login", "Invalid attribute: " + columns[4]
      is_login_keyboard = "true"
    engine['is_login_keyboard'] = is_login_keyboard
    engines.append(engine)

  output = CreateEngineHeader(engines)
  output_file = open(sys.argv[2], 'w')
  output_file.write(output)


if __name__ == '__main__':
  main(sys.argv)