Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3278 from keymanapp/feat/issue-2728
feat(linux): Install keyboard on Gnome
- Loading branch information
Showing
11 changed files
with
412 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
[flake8] | ||
max-line-length = 120 | ||
ignore = | ||
E402, # module level import not at top of file | ||
W504, # line break after binary operator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/python3 | ||
import logging | ||
import os | ||
from gi.repository import Gio | ||
|
||
from gi.overrides.GLib import Variant | ||
|
||
|
||
class GnomeKeyboardsUtil(): | ||
def __init__(self): | ||
self.input_sources = Gio.Settings.new("org.gnome.desktop.input-sources") | ||
|
||
def read_input_sources(self): | ||
sourcesVal = self.input_sources.get_value("sources") | ||
sources = self._convert_variant_to_array(sourcesVal) | ||
return sources | ||
|
||
def write_input_sources(self, sources): | ||
sourcesVal = self._convert_array_to_variant(sources) | ||
self.input_sources.set_value("sources", sourcesVal) | ||
|
||
def _convert_variant_to_array(self, variant): | ||
if variant is None: | ||
return [] | ||
|
||
values = [] | ||
# a(ss) | ||
nChildren = variant.n_children() | ||
for i in range(nChildren): | ||
# (ss) | ||
val = variant.get_child_value(i) | ||
typeVariant = val.get_child_value(0) | ||
type = typeVariant.get_string() | ||
idVariant = val.get_child_value(1) | ||
id = idVariant.get_string() | ||
values.append((type, id)) | ||
return values | ||
|
||
def _convert_array_to_variant(self, array): | ||
if len(array) == 0: | ||
return None | ||
|
||
children = [] | ||
for (type, id) in array: | ||
typeVariant = Variant.new_string(type) | ||
idVariant = Variant.new_string(id) | ||
child = Variant.new_tuple(typeVariant, idVariant) | ||
children.append(child) | ||
return Variant.new_array(None, children) | ||
|
||
|
||
__is_gnome_shell = None | ||
|
||
|
||
def is_gnome_shell(): | ||
global __is_gnome_shell | ||
|
||
if __is_gnome_shell is None: | ||
code = os.system('pidof gnome-shell >/dev/null 2>&1') | ||
__is_gnome_shell = (code == 0) | ||
return __is_gnome_shell | ||
|
||
|
||
def _reset_gnome_shell(): | ||
# used in unit tests | ||
global __is_gnome_shell | ||
|
||
__is_gnome_shell = None | ||
|
||
|
||
def get_keyboard_id(keyboard, packageDir, ignore_language=False): | ||
kmx_file = os.path.join(packageDir, keyboard['id'] + ".kmx") | ||
if not ignore_language and "languages" in keyboard and len(keyboard["languages"]) > 0: | ||
logging.debug(keyboard["languages"][0]) | ||
return "%s:%s" % (keyboard["languages"][0]['id'], kmx_file) | ||
return kmx_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/python3 | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from keyman_config.gnome_keyboards_util import GnomeKeyboardsUtil | ||
from keyman_config.gnome_keyboards_util import is_gnome_shell, _reset_gnome_shell | ||
|
||
|
||
class GnomeKeyboardsUtilTests(unittest.TestCase): | ||
def setUp(self): | ||
_reset_gnome_shell() | ||
patcher = patch('keyman_config.ibus_util.Gio.Settings.new') | ||
self.MockSettingsClass = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
def test_ConvertArrayToVariantToArray_Empty(self): | ||
# Setup | ||
children = [] | ||
sut = GnomeKeyboardsUtil() | ||
# Execute | ||
variant = sut._convert_array_to_variant(children) | ||
array = sut._convert_variant_to_array(variant) | ||
|
||
# Verify | ||
self.assertEqual(array, children) | ||
|
||
def test_ConvertArrayToVariantToArray_OneElement(self): | ||
# Setup | ||
children = [('t1', 'id1')] | ||
sut = GnomeKeyboardsUtil() | ||
# Execute | ||
variant = sut._convert_array_to_variant(children) | ||
array = sut._convert_variant_to_array(variant) | ||
|
||
# Verify | ||
self.assertEqual(array, children) | ||
|
||
def test_ConvertArrayToVariantToArray_MultipleElements(self): | ||
# Setup | ||
children = [('t1', 'id1'), ('t2', 'id2')] | ||
sut = GnomeKeyboardsUtil() | ||
# Execute | ||
variant = sut._convert_array_to_variant(children) | ||
array = sut._convert_variant_to_array(variant) | ||
|
||
# Verify | ||
self.assertEqual(array, children) | ||
|
||
@patch.object(GnomeKeyboardsUtil, '_convert_variant_to_array') | ||
def test_ReadInputSources(self, convertVariantToArrayMethod): | ||
# Setup | ||
keyboards = [('xkb', 'en')] | ||
mock_settingsInstance = self.MockSettingsClass.return_value | ||
mock_settingsInstance.get_value.return_value = keyboards | ||
convertVariantToArrayMethod.side_effect = lambda value: value | ||
sut = GnomeKeyboardsUtil() | ||
# Execute | ||
result = sut.read_input_sources() | ||
# Verify | ||
self.assertEqual(result, keyboards) | ||
|
||
@patch.object(GnomeKeyboardsUtil, '_convert_array_to_variant') | ||
def test_WriteInputSources(self, convertArrayToVariantMethod): | ||
# Setup | ||
convertArrayToVariantMethod.side_effect = lambda value: value | ||
sut = GnomeKeyboardsUtil() | ||
keyboards = [('xkb', 'en'), ('ibus', 'fooDir/foo1.kmx'), ('ibus', 'fooDir/foo2.kmx')] | ||
# Execute | ||
sut.write_input_sources(keyboards) | ||
# Verify | ||
self.MockSettingsClass.return_value.set_value.assert_called_once_with( | ||
"sources", keyboards) | ||
|
||
@patch('keyman_config.install_kmp.os.system') | ||
def test_IsGnomeShell_RunningGnomeShell(self, mockSystem): | ||
# Setup | ||
mockSystem.return_value = 0 | ||
# Execute/Verify | ||
self.assertEqual(is_gnome_shell(), True) | ||
|
||
@patch('keyman_config.install_kmp.os.system') | ||
def test_IsGnomeShell_NotRunningGnomeShell(self, mockSystem): | ||
# Setup | ||
mockSystem.return_value = 1 | ||
# Execute/Verify | ||
self.assertEqual(is_gnome_shell(), False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.