mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-20 06:37:40 +00:00
refactor(linux): Code refactoring
This commit is contained in:
parent
3c77839de3
commit
481cb4a281
3 changed files with 12 additions and 15 deletions
|
|
@ -48,8 +48,8 @@ def get_ibus_keyboard_id(keyboard, packageDir, language=None, ignore_language=Fa
|
|||
return kmx_file
|
||||
if language is not None and language != '':
|
||||
logging.debug(language)
|
||||
return "%s:%s" % (language, kmx_file)
|
||||
return f"{language}:{kmx_file}"
|
||||
if "languages" in keyboard and len(keyboard["languages"]) > 0:
|
||||
logging.debug(keyboard["languages"][0])
|
||||
return "%s:%s" % (keyboard["languages"][0]['id'], kmx_file)
|
||||
return f"{keyboard['languages'][0]['id']}:{kmx_file}"
|
||||
return kmx_file
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import os
|
|||
import subprocess
|
||||
import sys
|
||||
|
||||
from gi.repository import Gio # needs to come before gi.overrides.GLib!
|
||||
from gi.repository import Gio # needs to come before gi.overrides.GLib!
|
||||
from gi.overrides.GLib import Variant
|
||||
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ class GSettings():
|
|||
if variant.get_type_string() == 'as':
|
||||
return variant.get_strv()
|
||||
|
||||
assert(variant.get_type_string() == 'a(ss)')
|
||||
assert variant.get_type_string() == 'a(ss)'
|
||||
|
||||
values = []
|
||||
# Process variant of type "a(ss)" (array of tuples with two strings)
|
||||
|
|
@ -38,10 +38,8 @@ class GSettings():
|
|||
# Process variant of type "(ss)" (tuple with two strings)
|
||||
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))
|
||||
values.append((typeVariant.get_string(), idVariant.get_string()))
|
||||
return values
|
||||
|
||||
def _convert_array_to_variant(self, array, type):
|
||||
|
|
@ -51,7 +49,7 @@ class GSettings():
|
|||
if type == 'as':
|
||||
return Variant('as', array)
|
||||
|
||||
assert(type == 'a(ss)')
|
||||
assert type == 'a(ss)'
|
||||
|
||||
children = []
|
||||
for (type, id) in array:
|
||||
|
|
@ -61,17 +59,17 @@ class GSettings():
|
|||
children.append(child)
|
||||
return Variant.new_array(None, children)
|
||||
|
||||
def get(self, key):
|
||||
def get(self, key: str) -> list[str] | None:
|
||||
if self.is_sudo:
|
||||
args = ['sudo', '-H', '-u', os.environ.get('SUDO_USER'),
|
||||
'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%s/bus' % os.environ.get('SUDO_UID'),
|
||||
f"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/{os.environ.get('SUDO_UID')}/bus",
|
||||
'gsettings', 'get', self.schema_id, key]
|
||||
if sys.version_info.major <= 3 and sys.version_info.minor < 7:
|
||||
# capture_output got added in Python 3.7
|
||||
try:
|
||||
output = subprocess.check_output(args)
|
||||
value = eval(output)
|
||||
except(subprocess.CalledProcessError):
|
||||
except subprocess.CalledProcessError:
|
||||
value = None
|
||||
logging.warning('Could not convert to sources')
|
||||
else:
|
||||
|
|
@ -86,12 +84,12 @@ class GSettings():
|
|||
value = self._convert_variant_to_array(variant)
|
||||
return value
|
||||
|
||||
def set(self, key, value, type):
|
||||
def set(self, key: str, value: list[str], type) -> None:
|
||||
if self.is_sudo:
|
||||
variant = str(value)
|
||||
subprocess.run(
|
||||
['sudo', '-H', '-u', os.environ.get('SUDO_USER'),
|
||||
'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%s/bus' % os.environ.get('SUDO_UID'),
|
||||
f"DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/{os.environ.get('SUDO_UID')}/bus",
|
||||
'gsettings', 'set', self.schema_id, key, variant])
|
||||
else:
|
||||
variant = self._convert_array_to_variant(value, type)
|
||||
|
|
|
|||
|
|
@ -126,8 +126,7 @@ def _start_ibus_daemon(realuser):
|
|||
|
||||
def restart_ibus(bus=None):
|
||||
verify_ibus_daemon(False)
|
||||
realuser = os.environ.get('SUDO_USER')
|
||||
if realuser:
|
||||
if realuser := os.environ.get('SUDO_USER'):
|
||||
# we have been called with `sudo`. Restart ibus for the real user.
|
||||
logging.info('restarting IBus by subprocess for user %s', realuser)
|
||||
subprocess.run(['sudo', '-u', realuser, 'ibus', 'restart'])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue