gen_input_methods.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. """Generate a C++ header from input_methods.txt.
  6. This program generates a C++ header file containing the information on login XKB
  7. layout IDs. It parses input_methods.txt, and then generates a static array
  8. definition from the information extracted. The input and output file names are
  9. specified on the command line.
  10. Run it like:
  11. gen_input_methods.py input_methods.txt input_methods.h
  12. It will produce output that looks like:
  13. // Automatically generated by gen_input_methods.py
  14. #ifndef CHROMEOS_IME_INPUT_METHODS_H_
  15. #define CHROMEOS_IME_INPUT_METHODS_H_
  16. namespace chromeos {
  17. namespace input_method {
  18. const char* const kLoginXkbLayoutIds[] = {
  19. "us",
  20. "us(dvorak)",
  21. "be",
  22. "br",
  23. };
  24. } // namespace input_method
  25. } // namespace chromeos
  26. #endif // CHROMEOS_IME_INPUT_METHODS_H_
  27. """
  28. from __future__ import print_function
  29. import fileinput
  30. import re
  31. import sys
  32. OUTPUT_HEADER = """// Automatically generated by gen_input_methods.py
  33. #ifndef CHROMEOS_IME_INPUT_METHODS_H_
  34. #define CHROMEOS_IME_INPUT_METHODS_H_
  35. namespace chromeos {
  36. namespace input_method {
  37. const char* const kLoginXkbLayoutIds[] = {
  38. """
  39. ENGINE_FORMAT = (' "%s",\n')
  40. OUTPUT_FOOTER = """
  41. };
  42. } // namespace input_method
  43. } // namespace chromeos
  44. #endif // CHROMEOS_IME_INPUT_METHODS_H_
  45. """
  46. def CreateEngineHeader(login_xkb_layout_ids):
  47. """Create the header file from a list of login XKB layout IDs.
  48. Arguments:
  49. login_xkb_layout_ids: list of login XKB layout IDs
  50. Returns:
  51. The text of a C++ header file containing the login XKB layout IDs.
  52. """
  53. output = []
  54. output.append(OUTPUT_HEADER)
  55. for login_xkb_layout_id in login_xkb_layout_ids:
  56. output.append(ENGINE_FORMAT % login_xkb_layout_id)
  57. output.append(OUTPUT_FOOTER)
  58. return "".join(output)
  59. def main(argv):
  60. if len(argv) != 3:
  61. print('Usage: gen_input_methods.py [input_methods.txt] [output]')
  62. sys.exit(1)
  63. login_xkb_layout_ids = []
  64. for line in fileinput.input(sys.argv[1]):
  65. line = line.strip()
  66. if not line or re.match(r'#', line):
  67. continue
  68. columns = line.split()
  69. assert len(columns) == 4 or len(columns) == 5, "Invalid format: " + line
  70. if len(columns) == 5:
  71. assert columns[4] == "login", "Invalid attribute: " + columns[4]
  72. login_xkb_layout_ids.append(columns[1])
  73. output = CreateEngineHeader(login_xkb_layout_ids)
  74. output_file = open(sys.argv[2], 'w')
  75. output_file.write(output)
  76. if __name__ == '__main__':
  77. main(sys.argv)