File: gen_input_methods.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rwxr-xr-x 2,596 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
# Copyright 2012 The Chromium Authors
# 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 login XKB
layout IDs. 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.

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

It will produce output that looks like:

// Automatically generated by gen_input_methods.py
#ifndef CHROMEOS_IME_INPUT_METHODS_H_
#define CHROMEOS_IME_INPUT_METHODS_H_

namespace chromeos {
namespace input_method {

const char* const kLoginXkbLayoutIds[] = {
  "us",
  "us(dvorak)",
  "be",
  "br",
};

}  // namespace input_method
}  // namespace chromeos

#endif  // CHROMEOS_IME_INPUT_METHODS_H_

"""

from __future__ import print_function

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 {

const char* const kLoginXkbLayoutIds[] = {
"""
ENGINE_FORMAT = ('  "%s",\n')
OUTPUT_FOOTER = """
};

}  // namespace input_method
}  // namespace chromeos

#endif  // CHROMEOS_IME_INPUT_METHODS_H_
"""

def CreateEngineHeader(login_xkb_layout_ids):
  """Create the header file from a list of login XKB layout IDs.

  Arguments:
    login_xkb_layout_ids: list of login XKB layout IDs
  Returns:
    The text of a C++ header file containing the login XKB layout IDs.
  """
  output = []
  output.append(OUTPUT_HEADER)
  for login_xkb_layout_id in login_xkb_layout_ids:
    output.append(ENGINE_FORMAT % login_xkb_layout_id)
  output.append(OUTPUT_FOOTER)

  return "".join(output)


def main(argv):
  if len(argv) != 3:
    print('Usage: gen_input_methods.py [input_methods.txt] [output]')
    sys.exit(1)
  login_xkb_layout_ids = []
  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
    if len(columns) == 5:
      assert columns[4] == "login", "Invalid attribute: " + columns[4]
      login_xkb_layout_ids.append(columns[1])

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


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