1
class KeyboardDetector:
2
UNKNOWN, PRESS_KEY, KEY_PRESENT, KEY_PRESENT_P, RESULT = list(range(5))
6
f = '/usr/share/console-setup/pc105.tree'
9
# Dictionary of keycode -> step.
20
def read_step(self, step):
21
if self.current_step != -1:
23
list(self.keycodes.values()) +
24
[self.present] + [self.not_present])
25
if step not in valid_steps:
26
raise KeyError('invalid argument')
28
raise Exception('already done')
30
step_type = KeyboardDetector.UNKNOWN
38
if line.startswith('STEP '):
39
# This line starts a new step.
40
if self.current_step == step:
41
self.current_step = int(line[5:])
44
self.current_step = int(line[5:])
45
elif self.current_step != step:
47
elif line.startswith('PRESS '):
48
# Ask the user to press a character on the keyboard.
49
if step_type == KeyboardDetector.UNKNOWN:
50
step_type = KeyboardDetector.PRESS_KEY
51
if step_type != KeyboardDetector.PRESS_KEY:
53
self.symbols.append(line[6:].strip())
54
elif line.startswith('CODE '):
55
# Direct the evaluating code to process step ## next if the
56
# user has pressed a key which returned that keycode.
57
if step_type != KeyboardDetector.PRESS_KEY:
59
keycode = int(line[5:line.find(' ', 5)])
60
s = int(line[line.find(' ', 5) + 1:])
61
self.keycodes[keycode] = s
62
elif line.startswith('FIND '):
63
# Ask the user whether that character is present on their
65
if step_type == KeyboardDetector.UNKNOWN:
66
step_type = KeyboardDetector.KEY_PRESENT
69
self.symbols = [line[5:].strip()]
70
elif line.startswith('FINDP '):
71
# Equivalent to FIND, except that the user is asked to consider
72
# only the primary symbols (i.e. Plain and Shift).
73
if step_type == KeyboardDetector.UNKNOWN:
74
step_type = KeyboardDetector.KEY_PRESENT_P
77
self.symbols = [line[6:].strip()]
78
elif line.startswith('YES '):
79
# Direct the evaluating code to process step ## next if the
80
# user does have this key.
81
if (step_type != KeyboardDetector.KEY_PRESENT_P and
82
step_type != KeyboardDetector.KEY_PRESENT):
84
self.present = int(line[4:].strip())
85
elif line.startswith('NO '):
86
# Direct the evaluating code to process step ## next if the
87
# user does not have this key.
88
if (step_type != KeyboardDetector.KEY_PRESENT_P and
89
step_type != KeyboardDetector.KEY_PRESENT):
91
self.not_present = int(line[3:].strip())
92
elif line.startswith('MAP '):
93
# This step uniquely identifies a keymap.
94
if step_type == KeyboardDetector.UNKNOWN:
95
step_type = KeyboardDetector.RESULT
96
self.result = line[4:].strip()