~elementary-os/ubuntu-package-imports/ubiquity-bionic

« back to all changes in this revision

Viewing changes to ubiquity/keyboard_detector.py

  • Committer: RabbitBot
  • Date: 2018-02-05 14:44:42 UTC
  • Revision ID: rabbitbot@elementary.io-20180205144442-vt0fvth7zus90wjh
Initial import, version 17.10.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class KeyboardDetector:
 
2
    UNKNOWN, PRESS_KEY, KEY_PRESENT, KEY_PRESENT_P, RESULT = list(range(5))
 
3
 
 
4
    def __init__(self):
 
5
        self.current_step = -1
 
6
        f = '/usr/share/console-setup/pc105.tree'
 
7
        self.fp = open(f)
 
8
 
 
9
        # Dictionary of keycode -> step.
 
10
        self.keycodes = {}
 
11
        self.symbols = []
 
12
        self.present = -1
 
13
        self.not_present = -1
 
14
        self.result = ''
 
15
 
 
16
    def __del__(self):
 
17
        if self.fp:
 
18
            self.fp.close()
 
19
 
 
20
    def read_step(self, step):
 
21
        if self.current_step != -1:
 
22
            valid_steps = (
 
23
                list(self.keycodes.values()) +
 
24
                [self.present] + [self.not_present])
 
25
            if step not in valid_steps:
 
26
                raise KeyError('invalid argument')
 
27
            if self.result:
 
28
                raise Exception('already done')
 
29
 
 
30
        step_type = KeyboardDetector.UNKNOWN
 
31
        self.keycodes = {}
 
32
        self.symbols = []
 
33
        self.present = -1
 
34
        self.not_present = -1
 
35
        self.result = ''
 
36
 
 
37
        for line in self.fp:
 
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:])
 
42
                    return step_type
 
43
                else:
 
44
                    self.current_step = int(line[5:])
 
45
            elif self.current_step != step:
 
46
                continue
 
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:
 
52
                    raise Exception
 
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:
 
58
                    raise Exception
 
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
 
64
                # keyboard.
 
65
                if step_type == KeyboardDetector.UNKNOWN:
 
66
                    step_type = KeyboardDetector.KEY_PRESENT
 
67
                else:
 
68
                    raise Exception
 
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
 
75
                else:
 
76
                    raise Exception
 
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):
 
83
                    raise Exception
 
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):
 
90
                    raise Exception
 
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()
 
97
                return step_type
 
98
            else:
 
99
                raise Exception