Debian Codegraph @

x2console_keymap

raw
1 #!/usr/bin/python3
2
3 # Copyright (c) 2005 by Matthias Urlichs <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of Version 2 of the GNU General Public License as
7 # published by the Free Software Foundation. See the file COPYING.txt
8 # or (on Debian systems) /usr/share/common-licenses/GPL-2 for details.
9
10 """\
11 This script analyzes an X11 keymap and prints the equivalent console
12 keymap name, based on a decision tree.
13 """
14
15 from __future__ import print_function
16
17 from keymapper.parse.x11 import parse_file as parse_x11
18 from keymapper.fakequery import FakeQuery
19 from keymapper.script import Script, DupMap, NoMap
20 from optparse import OptionParser
21
22 import io
23 import sys
24
25 import keymapper.tree
26
27 if sys.version_info[0] >= 3:
28 # Force encoding to UTF-8 even in non-UTF-8 locales.
29 sys.stdout = io.TextIOWrapper(
30 sys.stdout.detach(), encoding="UTF-8", line_buffering=True
31 )
32 sys.stderr = io.TextIOWrapper(
33 sys.stderr.detach(), encoding="UTF-8", line_buffering=True
34 )
35 else:
36 import codecs
37
38 sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
39 sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
40
41 keymapper.tree.trace = 1
42
43 parser = OptionParser(usage="%prog tree_file x11name...", version="%prog 0.1")
44 parser.remove_option("-h")
45 # parser.remove_option("--help")
46 parser.add_option("-?", "--help", action="help", help="show this help text")
47 parser.add_option(
48 "-v", "--verbose", action="count", dest="verbose", help="be more verbose"
49 )
50 parser.add_option(
51 "-g",
52 "--graph",
53 action="store_const",
54 dest="format",
55 const="graphviz",
56 help="generate a hopefully-nice-looking .dot file",
57 )
58 (opts, args) = parser.parse_args()
59
60 with io.open(args[0], encoding="utf-8") as f:
61 script = Script(f, verbose=opts.verbose)
62 rev = {}
63
64
65 def rev_add(b, a):
66 if a not in rev:
67 rev[a] = set()
68 rev[a].add(b)
69
70
71 for x11map in args[1:]:
72 km = parse_x11(x11map)
73 if km is None:
74 continue
75 try:
76 consolemap = script.run(FakeQuery(km, verbose=opts.verbose))
77 except DupMap as err:
78 for m in err.args[0]:
79 rev_add(km.name, m)
80 except NoMap:
81 print("# %s could not be mapped." % (km.name,))
82 else:
83 rev_add(km.name, consolemap)
84
85 for a, b in sorted(rev.items()):
86 print(a, " ".join(sorted(b)))