2018-02-07 21:37:22 +00:00
|
|
|
# Copyright 2018 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2024-04-28 21:28:40 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3.
|
2018-02-07 21:37:22 +00:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2024-04-28 21:28:40 +00:00
|
|
|
# GNU General Public License for more details.
|
2018-02-07 21:37:22 +00:00
|
|
|
#
|
2024-04-28 21:28:40 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2018-02-07 21:37:22 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# The keyboard autodetection process is driven by the data in
|
|
|
|
# /usr/share/console-setup/pc105.tree. This code parses that data into
|
|
|
|
# subclasses of Step.
|
|
|
|
|
2021-03-15 20:57:47 +00:00
|
|
|
"""Parses the pc105.tree file into Steps"""
|
2018-05-23 21:15:54 +00:00
|
|
|
|
2021-03-15 20:57:47 +00:00
|
|
|
from subiquity.common.types import (
|
|
|
|
StepKeyPresent,
|
|
|
|
StepPressKey,
|
|
|
|
StepResult,
|
|
|
|
)
|
2018-02-07 21:37:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PC105Tree:
|
|
|
|
# This is adapted (quite heavily) from the code in ubiquity.
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.steps = {}
|
|
|
|
|
|
|
|
def _add_step_from_lines(self, lines):
|
2021-03-15 20:57:47 +00:00
|
|
|
step_cls = None
|
|
|
|
args = None
|
|
|
|
step_index = None
|
2018-02-07 21:37:22 +00:00
|
|
|
for line in lines:
|
|
|
|
if line.startswith('STEP '):
|
2021-03-15 20:57:47 +00:00
|
|
|
step_index = line[5:].strip()
|
2018-02-07 21:37:22 +00:00
|
|
|
elif line.startswith('PRESS '):
|
|
|
|
# Ask the user to press a character on the keyboard.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is None:
|
|
|
|
step_cls = StepPressKey
|
|
|
|
args = {
|
|
|
|
'symbols': [],
|
|
|
|
'keycodes': {},
|
|
|
|
}
|
|
|
|
elif step_cls is not StepPressKey:
|
2018-02-07 21:37:22 +00:00
|
|
|
raise Exception
|
2021-03-15 20:57:47 +00:00
|
|
|
args['symbols'].append(line[6:].strip())
|
2018-02-07 21:37:22 +00:00
|
|
|
elif line.startswith('CODE '):
|
|
|
|
# Direct the evaluating code to process step ## next if the
|
|
|
|
# user has pressed a key which returned that keycode.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is not StepPressKey:
|
2018-02-07 21:37:22 +00:00
|
|
|
raise Exception
|
|
|
|
keycode = int(line[5:line.find(' ', 5)])
|
2021-03-15 20:57:47 +00:00
|
|
|
s = line[line.find(' ', 5) + 1:].strip()
|
|
|
|
args['keycodes'][keycode] = s
|
2018-02-07 21:37:22 +00:00
|
|
|
elif line.startswith('FIND '):
|
|
|
|
# Ask the user whether that character is present on their
|
|
|
|
# keyboard.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is None:
|
|
|
|
step_cls = StepKeyPresent
|
|
|
|
args = {
|
|
|
|
'symbol': line[5:].strip(),
|
|
|
|
}
|
2018-02-07 21:37:22 +00:00
|
|
|
else:
|
|
|
|
raise Exception
|
|
|
|
elif line.startswith('FINDP '):
|
|
|
|
# Equivalent to FIND, except that the user is asked to consider
|
|
|
|
# only the primary symbols (i.e. Plain and Shift).
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is None:
|
|
|
|
step_cls = StepKeyPresent
|
|
|
|
args = {'symbol': line[5:].strip()}
|
2018-02-07 21:37:22 +00:00
|
|
|
else:
|
|
|
|
raise Exception
|
|
|
|
elif line.startswith('YES '):
|
|
|
|
# Direct the evaluating code to process step ## next if the
|
|
|
|
# user does have this key.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is not StepKeyPresent:
|
2018-02-07 21:37:22 +00:00
|
|
|
raise Exception
|
2021-03-15 20:57:47 +00:00
|
|
|
args['yes'] = line[4:].strip()
|
2018-02-07 21:37:22 +00:00
|
|
|
elif line.startswith('NO '):
|
|
|
|
# Direct the evaluating code to process step ## next if the
|
|
|
|
# user does not have this key.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is not StepKeyPresent:
|
2018-02-07 21:37:22 +00:00
|
|
|
raise Exception
|
2021-03-15 20:57:47 +00:00
|
|
|
args['no'] = line[3:].strip()
|
2018-02-07 21:37:22 +00:00
|
|
|
elif line.startswith('MAP '):
|
|
|
|
# This step uniquely identifies a keymap.
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is None:
|
|
|
|
step_cls = StepResult
|
|
|
|
arg = line[4:].strip()
|
|
|
|
if ':' in arg:
|
|
|
|
layout, variant = arg.split(':')
|
|
|
|
else:
|
|
|
|
layout, variant = arg, ''
|
|
|
|
args = {'layout': layout, 'variant': variant}
|
2018-02-07 21:37:22 +00:00
|
|
|
else:
|
|
|
|
raise Exception
|
|
|
|
else:
|
|
|
|
raise Exception
|
2021-03-15 20:57:47 +00:00
|
|
|
if step_cls is None or step_index is None:
|
2018-02-07 21:37:22 +00:00
|
|
|
raise Exception
|
2021-03-15 20:57:47 +00:00
|
|
|
self.steps[step_index] = step_cls(**args)
|
2018-02-07 21:37:22 +00:00
|
|
|
|
|
|
|
def read_steps(self):
|
|
|
|
cur_step_lines = []
|
|
|
|
with open('/usr/share/console-setup/pc105.tree') as fp:
|
|
|
|
for line in fp:
|
|
|
|
if line.startswith('STEP '):
|
|
|
|
if cur_step_lines:
|
|
|
|
self._add_step_from_lines(cur_step_lines)
|
|
|
|
cur_step_lines = [line]
|
|
|
|
else:
|
|
|
|
cur_step_lines.append(line)
|
|
|
|
if cur_step_lines:
|
|
|
|
self._add_step_from_lines(cur_step_lines)
|