Skip to content

Commit

Permalink
Merge pull request #5195 from AdamWill/gis-switched-layouts
Browse files Browse the repository at this point in the history
Simplify keyboard layout handling, rely on localed more
  • Loading branch information
jkonecny12 committed Oct 2, 2023
2 parents 717cb14 + e53efc2 commit 726dfbc
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 150 deletions.
6 changes: 0 additions & 6 deletions pyanaconda/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ def set_x_keyboard_defaults(localization_proxy, xkl_wrapper):
if can_configure_keyboard():
xkl_wrapper.replace_layouts(new_layouts)

# the console layout configured should be "native" by default,
# setting that explicitly for non-ascii layouts where we prepend "us"
# refer: https://bugzilla.redhat.com/show_bug.cgi?id=1912609
if len(new_layouts) >= 2 and not langtable.supports_ascii(new_layouts[1]):
localization_proxy.VirtualConsoleKeymap = new_layouts[1]

if len(new_layouts) >= 2 and not localization_proxy.LayoutSwitchOptions:
# initialize layout switching if needed
localization_proxy.LayoutSwitchOptions = ["grp:alt_shift_toggle"]
Expand Down
27 changes: 0 additions & 27 deletions pyanaconda/modules/localization/live_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ def read_keyboard_layouts(self):
"""
pass

@abstractmethod
def read_current_keyboard_layout(self):
"""Read keyboard layout currently used.
It is the best candidate for the virtual console keymap.
:return: keyboard layout used for virtual_console (e.g: LUKS during boot)
:rtype: str
"""
pass

@staticmethod
def _run_as_liveuser(argv):
"""Run the command in a system as liveuser user.
Expand Down Expand Up @@ -88,22 +77,6 @@ def read_keyboard_layouts(self):
result = self._convert_to_xkb_format(sources)
return result

def read_current_keyboard_layout(self):
"""Read keyboard layout currently used.
It is the best candidate for the virtual console keymap.
:return: keyboard layout used for virtual_console (e.g: LUKS during boot)
:rtype: str
"""
command_args = ["gsettings", "get", "org.gnome.desktop.input-sources", "mru-sources"]
sources = self._run_as_liveuser(command_args)
result = self._convert_to_xkb_format(sources)
# take first recently used which is the currently used
if result:
return result[0]
return ""

def _convert_to_xkb_format(self, sources):
# convert input "[('xkb', 'us'), ('xkb', 'cz+qwerty')]\n"
# to a python list of '["us", "cz (qwerty)"]'
Expand Down
2 changes: 1 addition & 1 deletion pyanaconda/modules/localization/localed.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def set_layouts(self, layouts_variants, options=None, convert=False):

self._localed_proxy.SetX11Keyboard(
layouts_str,
"",
"pc105",
variants_str,
options_str,
convert,
Expand Down
29 changes: 7 additions & 22 deletions pyanaconda/modules/localization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
def get_missing_keyboard_configuration(localed_wrapper, x_layouts, vc_keymap):
"""Get keyboard configuration if not set by user.
Algorith works as this:
1. Check if keyboard configuration is not already set by user
Algorithm works as this:
1. Check if full keyboard configuration is already set
-> return these
2. Check if keyboard confifuration can be obtained from live system
-> read layouts
-> convert currently used live layout to virtual console keymap by localed
2. If no X layouts, check if they can be obtained from live system
-> read X layouts
3a. If still no configuration is present
-> use DEFAULT_KEYBOARD
-> use DEFAULT_KEYBOARD as console layout and fall through to...
3b. If only one of the keyboard layout or virtual console keymap is set
-> convert one to the other by localed
Expand All @@ -52,11 +51,8 @@ def get_missing_keyboard_configuration(localed_wrapper, x_layouts, vc_keymap):
live_keyboard = get_live_keyboard_instance()
# layouts are not set by user, we should take a look for live configuration if available
if not x_layouts and live_keyboard:
log.debug("Keyboad configuration from Live system is available")
x_layouts, vc_keymap = _resolve_missing_from_live(localed_wrapper,
live_keyboard,
x_layouts,
vc_keymap)
log.debug("Keyboard configuration from Live system is available")
x_layouts = live_keyboard.read_keyboard_layouts()

if not vc_keymap and not x_layouts:
log.debug("Using default value %s for missing virtual console keymap", DEFAULT_KEYBOARD)
Expand All @@ -68,17 +64,6 @@ def get_missing_keyboard_configuration(localed_wrapper, x_layouts, vc_keymap):
return x_layouts, vc_keymap


def _resolve_missing_from_live(localed_wrapper, live_keyboard, x_layouts, vc_keymap):
x_layouts = live_keyboard.read_keyboard_layouts()

if not vc_keymap:
live_layout = live_keyboard.read_current_keyboard_layout()
if live_layout:
vc_keymap = localed_wrapper.convert_layouts([live_layout])

return x_layouts, vc_keymap


def _resolve_missing_by_conversion(localed_wrapper, x_layouts, vc_keymap):
if not vc_keymap:
vc_keymap = localed_wrapper.convert_layouts(x_layouts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@ def _check_gnome_shell_layouts_conversion(self, mocked_exec_with_capture, system
["get", "org.gnome.desktop.input-sources", "sources"]
)

def _check_gnome_shell_current_layout_conversion(self, mocked_exec_with_capture, system_input,
output):
mocked_exec_with_capture.reset_mock()
mocked_exec_with_capture.return_value = system_input

gs = GnomeShellKeyboard()

assert gs.read_current_keyboard_layout() == output
mocked_exec_with_capture.assert_called_once_with(
"gsettings",
["get", "org.gnome.desktop.input-sources", "mru-sources"]
)

@patch("pyanaconda.modules.localization.live_keyboard.execWithCaptureAsLiveUser")
def test_gnome_shell_keyboard(self, mocked_exec_with_capture):
"""Test GnomeShellKeyboard live instance layouts."""
Expand Down Expand Up @@ -102,55 +89,3 @@ def test_gnome_shell_keyboard(self, mocked_exec_with_capture):
system_input=r"wrong input",
output=[]
)

@patch("pyanaconda.modules.localization.live_keyboard.execWithCaptureAsLiveUser")
def test_gnome_shell_current_keyboard_layout(self, mocked_exec_with_capture):
"""Test GnomeShellKeyboard live instance current layout."""
# test one simple layout is set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'us')]",
output="us"
)

# test one complex layout is set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty')]",
output="cz (qwerty)"
)

# test multiple layouts are set
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz+qwerty'), ('xkb', 'us')]",
output="cz (qwerty)"
)

# test layouts with ibus (ibus is ignored)
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('xkb', 'cz'), ('ibus', 'libpinyin')]",
output="cz"
)

# test layouts with ibus first (ibus should be skipped)
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('ibus', 'libpinyin'), ('xkb', 'us')]",
output="us"
)

# test only ibus layout
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"[('ibus', 'libpinyin')]",
output=""
)

# test wrong input
self._check_gnome_shell_current_layout_conversion(
mocked_exec_with_capture=mocked_exec_with_capture,
system_input=r"wrong input",
output=""
)
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,10 @@ def _create_localed_mock(self,
else:
localed.convert_keymap.assert_called_once_with(expected_convert_keymap_input)

def _create_live_keyboard_mock(self, layouts, current_layout):
def _create_live_keyboard_mock(self, layouts):
live_keyboard_mock = Mock()

live_keyboard_mock.read_keyboard_layouts.return_value = layouts
live_keyboard_mock.read_current_keyboard_layout.return_value = current_layout

return live_keyboard_mock

Expand Down Expand Up @@ -351,8 +350,8 @@ def test_get_missing_keyboard_configuration(self):
)

def test_get_missing_keyboard_configuration_from_live(self):
"""Test the gt_missing_keyboard_configuration from Live system."""
# Take layouts from Live system but their are empty
"""Test the get_missing_keyboard_configuration from Live system."""
# Take layouts from Live system but they are empty
with self._create_localed_mock(
convert_layouts_output="",
convert_keymap_output=[DEFAULT_KEYBOARD],
Expand All @@ -365,26 +364,9 @@ def test_get_missing_keyboard_configuration_from_live(self):
result_x_layouts=[DEFAULT_KEYBOARD],
result_vc_keymap=DEFAULT_KEYBOARD,
localed=mocked_localed,
live_keyboard=self._create_live_keyboard_mock(layouts=[],
current_layout="")
live_keyboard=self._create_live_keyboard_mock(layouts=[])
)
# Take layouts and current_layout from Live system
with self._create_localed_mock(
convert_layouts_output="cz",
convert_keymap_output=[DEFAULT_KEYBOARD],
expected_convert_layouts_input=["cz"],
expected_convert_keymap_input=None
) as mocked_localed:
self._get_missing_keyboard_configuration_test(
input_x_layouts=[],
input_vc_keymap="",
result_x_layouts=["cz", "us"],
result_vc_keymap="cz",
localed=mocked_localed,
live_keyboard=self._create_live_keyboard_mock(layouts=["cz", "us"],
current_layout="cz")
)
# Take layouts only from Live system (vc_keymap is converted from live layouts)
# Take layouts from Live system (vc_keymap is converted from live layouts)
with self._create_localed_mock(
convert_layouts_output="cz",
convert_keymap_output=[],
Expand All @@ -397,8 +379,7 @@ def test_get_missing_keyboard_configuration_from_live(self):
result_x_layouts=["cz", "us"],
result_vc_keymap="cz",
localed=mocked_localed,
live_keyboard=self._create_live_keyboard_mock(layouts=["cz", "us"],
current_layout="")
live_keyboard=self._create_live_keyboard_mock(layouts=["cz", "us"])
)
# Layouts are set by user but vc_keymap not (convert layouts to VC without live)
with self._create_localed_mock(
Expand All @@ -413,8 +394,7 @@ def test_get_missing_keyboard_configuration_from_live(self):
result_x_layouts=["cz"],
result_vc_keymap="cz",
localed=mocked_localed,
live_keyboard=self._create_live_keyboard_mock(layouts=[],
current_layout="")
live_keyboard=self._create_live_keyboard_mock(layouts=[])
)
# VC keymap is set by user but layouts are taken from Live
with self._create_localed_mock(
Expand All @@ -429,8 +409,7 @@ def test_get_missing_keyboard_configuration_from_live(self):
result_x_layouts=["us"],
result_vc_keymap="cz",
localed=mocked_localed,
live_keyboard=self._create_live_keyboard_mock(layouts=["us"],
current_layout="")
live_keyboard=self._create_live_keyboard_mock(layouts=["us"])
)

@patch("pyanaconda.modules.localization.runtime.conf")
Expand Down

0 comments on commit 726dfbc

Please sign in to comment.