Debian Codegraph @

gen_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 test script grabs a number of interesting key maps from
12 keymapper.fakemaps, generates a decision tree, saves that in an
13 in-memory file, and then runs each key map against the file's
14 interpreter to see if the correct map is returned.
15 """
16
17 from __future__ import print_function
18
19 from keymapper.parse.maps import EmptyMapError, MapInputError
20 from keymapper.tree import Tree, gen_report
21 import keymapper.tree
22 from keymapper.file import FileReporter
23 from keymapper.graph import GraphReporter
24 from keymapper.script import Script
25 from keymapper.fakequery import FakeQuery
26 from keymapper.parse.linux import parse_file as parse_linux
27 from keymapper.parse.linux import FileProblem, add_dir
28
29 import io
30 import sys
31 from optparse import OptionParser
32
33 # from subprocess import Popen, PIPE
34
35 if sys.version_info[0] >= 3:
36 # Force encoding to UTF-8 even in non-UTF-8 locales.
37 sys.stdout = io.TextIOWrapper(
38 sys.stdout.detach(), encoding="UTF-8", line_buffering=True
39 )
40 sys.stderr = io.TextIOWrapper(
41 sys.stderr.detach(), encoding="UTF-8", line_buffering=True
42 )
43 else:
44 import codecs
45
46 sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
47 sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
48
49 parser = OptionParser(usage="%prog list...", version="%prog 0.1")
50 parser.remove_option("-h")
51 # parser.remove_option("--help")
52 parser.add_option("-?", "--help", action="help", help="show this help text")
53 parser.add_option(
54 "-v", "--verbose", action="count", dest="verbose", help="be more verbose"
55 )
56 parser.add_option(
57 "-m",
58 "--minlen",
59 action="store",
60 dest="minlen",
61 type="int",
62 default=30,
63 help="Too-short Keymaps are skipped (default: 30 entries)",
64 )
65 parser.add_option(
66 "-g",
67 "--graph",
68 action="store_const",
69 dest="format",
70 const="graphviz",
71 help="generate a hopefully-nice-looking .dot file",
72 )
73 parser.add_option(
74 "--maps",
75 action="store_const",
76 dest="format",
77 const="mapdump",
78 help="print the to-be-processed keymaps",
79 )
80 parser.add_option(
81 "-i",
82 "--installer",
83 action="store_const",
84 dest="iformat",
85 const="d-i",
86 help="Input files are in d-i map form",
87 )
88 parser.add_option(
89 "-I",
90 "--inc",
91 "--include",
92 action="append",
93 dest="dirs",
94 help="add a directory to the search path",
95 )
96 parser.add_option(
97 "-o",
98 "--output",
99 action="store",
100 dest="filename",
101 help="output file (default: stdout)",
102 )
103 parser.add_option(
104 "-f",
105 "--filter",
106 action="store",
107 dest="filter",
108 help="Include only the branches leading to these keymaps",
109 )
110 parser.add_option(
111 "-u",
112 "--useonly",
113 action="store",
114 dest="useonly",
115 help="Start generating the tree based only on these keymaps",
116 )
117 parser.add_option(
118 "-s", "--skip", action="store", dest="skip", help="keymaps to skip"
119 )
120 parser.add_option(
121 "-t",
122 "--test",
123 action="store_true",
124 dest="test",
125 help="Test the generated maps",
126 )
127 parser.add_option(
128 "--interactive",
129 action="store_true",
130 dest="interactive",
131 help="Ask user to choose among indistinguishable keymaps",
132 )
133 parser.add_option(
134 "--no-altgr",
135 action="store_false",
136 dest="altgr",
137 help="Do not consider AltGr keys",
138 default=True,
139 )
140
141 (opts, args) = parser.parse_args()
142 if not args:
143 parser.error("no arguments supplied")
144 sys.exit(1)
145 if opts.test and opts.format:
146 parser.error("You can only test scripts")
147 sys.exit(1)
148 if opts.test and not opts.filename:
149 parser.error("You can only test scripts if you write them to a file")
150 sys.exit(1)
151
152 t = Tree()
153
154 keymapper.tree.trace = opts.verbose
155 keymapper.tree.interactive = opts.interactive
156
157 if opts.dirs:
158 for d in opts.dirs:
159 add_dir(d)
160
161 if opts.filename:
162 out = io.open(opts.filename, "w", encoding="utf-8")
163 else:
164 out = sys.stdout
165
166 known = {}
167 for f in args:
168 for l in open(f):
169 comment = l.find("#")
170 if comment > -1:
171 l = l[:comment]
172 l = l.strip()
173 if l == "":
174 continue
175 name = l.split()
176 if opts.iformat == "d-i":
177 name = name[1]
178 else:
179 name = name[0]
180 if opts.useonly and name not in opts.useonly.split(","):
181 continue
182 if opts.skip and name in opts.skip.split(","):
183 continue
184 if name in known:
185 continue
186 known[name] = 1
187
188 # code = codecs.getreader(code)
189 # pipe = Popen(("loadkeys","-q","-M",name), stdout=PIPE).stdout
190 # pipe = code(pipe)
191
192 try:
193 if opts.verbose:
194 print("Parsing:", name)
195 # map = parse_map(name,pipe,opts.altgr)
196 map = parse_linux(name, opts.altgr)
197 except EmptyMapError:
198 print("Map '%s' skipped: empty" % name, file=sys.stderr)
199 except MapInputError as exc:
200 n, l, p = exc.args
201 print(
202 "Map '%s' skipped: data format error at %d:%d" % (n, l, p),
203 file=sys.stderr,
204 )
205 except UnicodeDecodeError:
206 print("Map '%s' skipped: codec error" % name, file=sys.stderr)
207 except FileProblem as e:
208 print("... skipped '%s'" % e.args, file=sys.stderr)
209 else:
210 if opts.minlen > len(map):
211 print(
212 "... skipped '%s': only %d keys" % (map, len(map)),
213 file=sys.stderr,
214 )
215 continue
216
217 if opts.format == "mapdump":
218 print(map.dump(), file=out)
219 else:
220 t.add(map)
221
222 if opts.format == "mapdump":
223 sys.exit(0)
224
225 l = ()
226 if opts.filter:
227 l = opts.filter.split(",")
228 t.gen_tree(*l)
229
230 if opts.format == "graphviz":
231 gen_report(t, GraphReporter(out))
232 else:
233 gen_report(t, FileReporter(out))
234
235 if opts.test:
236 buf = codecs.getreader("utf-8")(open(opts.filename))
237 err = 0
238 for i in range(0, 3):
239 for k in t:
240 buf.seek(0, 0)
241 print("Testing keymap %s" % (k.dump(),))
242 s = Script(buf, FakeQuery(k))
243 name = s.run()
244 if name != k.name:
245 print("SCRIPT ERROR: %s != %s" % (name, k.name))
246 err += 1
247 else:
248 print("... OK.")
249 print()
250 if err:
251 print("There are problems!", file=sys.stderr)
252 sys.exit(1)
253 # done!