from gi.repository import Gio, GLib LOCALED_SERVICE = "org.freedesktop.locale1" LOCALED_OBJECT_PATH = "/org/freedesktop/locale1" LOCALED_IFACE = "org.freedesktop.locale1" DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties" DEFAULT_TIMEOUT = -1 # indicates that the default timeout should be used class LocaledWrapperError(Exception): """Exception class for reporting Localed-related problems""" pass class LocaledWrapper(object): """ Class wrapping systemd-localed daemon functionality. """ def __init__(self): bus = Gio.BusType.SYSTEM flags = Gio.DBusProxyFlags.NONE info = None # no minimal version needed service = LOCALED_SERVICE object_path = LOCALED_OBJECT_PATH interface = LOCALED_IFACE cancellable = None # won't be cancelled try: self._proxy = Gio.DBusProxy.new_for_bus_sync(bus, flags, info, service, object_path, interface, cancellable) except TypeError: raise LocaledWrapperError("Failed to connect to localed over DBus") def set_and_convert_keymap(self, keymap): """ Method that sets VConsole keymap and returns X11 layout and variant that (systemd-localed thinks) match given keymap best. @return: string containing "layout (variant)" or "layout" if variant is missing @rtype: string """ # args: keymap, keymap_toggle, convert, user_interaction # where convert indicates whether the keymap should be converted # to X11 layout and user_interaction indicates whether PolicyKit # should ask for credentials or not args = GLib.Variant('(ssbb)', (keymap, "", True, False)) self._proxy.call_sync("SetVConsoleKeyboard", args, Gio.DBusCallFlags.NONE, DEFAULT_TIMEOUT, None) layout = self._proxy.get_cached_property("X11Layout").unpack() variant = self._proxy.get_cached_property("X11Variant").unpack() return _join_layout_variant(layout, variant) def set_and_convert_layout(self, layout_variant): """ Method that sets X11 layout and variant (for later X sessions) and returns VConsole keymap that (systemd-localed thinks) matches given layout and variant best. @return: a keymap matching layout and variant best @rtype: string """ (layout, variant) = _parse_layout_variant(layout_variant) # args: layout, model, variant, options, convert, user_interaction # where convert indicates whether the keymap should be converted # to X11 layout and user_interaction indicates whether PolicyKit # should ask for credentials or not args = GLib.Variant("(ssssbb)", (layout, "", variant, "", True, False)) self._proxy.call_sync("SetX11Keyboard", args, Gio.DBusCallFlags.NONE, DEFAULT_TIMEOUT, None) return self._proxy.get_cached_property("VConsoleKeymap").unpack() if __name__ == "__main__": a = LocaledWrapper() print a._proxy.get_cached_property_names()