[PATCH 3/4] Improve XklWrapper's API

Vratislav Podzimek vpodzime at redhat.com
Fri Sep 13 10:59:40 UTC 2013


Instead of exposing internal structures, the XklWrapper class should provide
methods to get descriptions for layout-variants and switching options.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/keyboard.py               | 76 ++++++++++++++++++++++++------------
 pyanaconda/ui/gui/spokes/keyboard.py | 18 ++++-----
 2 files changed, 60 insertions(+), 34 deletions(-)

diff --git a/pyanaconda/keyboard.py b/pyanaconda/keyboard.py
index 4967cb4..1f6cb5a 100644
--- a/pyanaconda/keyboard.py
+++ b/pyanaconda/keyboard.py
@@ -38,6 +38,8 @@ import re
 import shutil
 import ctypes
 
+from collections import namedtuple
+
 from pyanaconda import iutil
 from pyanaconda import flags
 from pyanaconda.safe_dbus import dbus_call_safe_sync, dbus_get_property_safe_sync
@@ -59,6 +61,9 @@ LAYOUT_VARIANT_RE = re.compile(r'^\s*(\w+)\s*' # layout plus
                                r'(?:(?:\(\s*(\w+)\s*\))' # variant in parentheses
                                r'|(?:$))\s*') # or nothing
 
+# namedtuple for information about a keyboard layout (its language and description)
+LayoutInfo = namedtuple("LayoutInfo", ["lang", "desc"])
+
 class KeyboardConfigError(Exception):
     """Exception class for keyboard configuration related problems"""
 
@@ -406,14 +411,8 @@ class XklWrapper(object):
         self.configreg = Xkl.ConfigRegistry.get_instance(self._engine)
         self.configreg.load(False)
 
-        self._switching_options = list()
-
-        #we want to display layouts as 'language (description)'
-        self.name_to_show_str = dict()
-
-        #we want to display layout switching options as e.g. "Alt + Shift" not
-        #as "grp:alt_shift_toggle"
-        self.switch_to_show_str = dict()
+        self._layout_infos = dict()
+        self._switch_opt_infos = dict()
 
         #this might take quite a long time
         self.configreg.foreach_language(self._get_language_variants, None)
@@ -432,12 +431,9 @@ class XklWrapper(object):
 
         #if this layout has already been added for some other language,
         #do not add it again (would result in duplicates in our lists)
-        if name not in self.name_to_show_str:
-            if lang:
-                self.name_to_show_str[name] = "%s (%s)" % (lang.encode("utf-8"),
-                                                    description.encode("utf-8"))
-            else:
-                self.name_to_show_str[name] = "%s" % description.encode("utf-8")
+        if name not in self._layout_infos:
+            self._layout_infos[name] = LayoutInfo(lang.encode("utf-8"),
+                                                  description.encode("utf-8"))
 
     def _get_country_variant(self, c_reg, item, subitem, country):
         if subitem:
@@ -448,12 +444,9 @@ class XklWrapper(object):
             description = item_str(item.description)
 
         # if the layout was not added with any language, add it with a country
-        if name not in self.name_to_show_str:
-            if country:
-                self.name_to_show_str[name] = "%s (%s)" % (country.encode("utf-8"),
-                                                    description.encode("utf-8"))
-            else:
-                self.name_to_show_str[name] = "%s" % description.encode("utf-8")
+        if name not in self._layout_infos:
+            self._layout_infos[name] = LayoutInfo(country.encode("utf-8"),
+                                                  description.encode("utf-8"))
 
     def _get_language_variants(self, c_reg, item, user_data=None):
         lang_name, lang_desc = item_str(item.name), item_str(item.description)
@@ -471,8 +464,7 @@ class XklWrapper(object):
         desc = item_str(item.description)
         name = item_str(item.name)
 
-        self._switching_options.append(name)
-        self.switch_to_show_str[name] = desc.encode("utf-8")
+        self._switch_opt_infos[name] = desc.encode("utf-8")
 
     def get_current_layout(self):
         """
@@ -503,12 +495,46 @@ class XklWrapper(object):
     def get_available_layouts(self):
         """A generator yielding layouts (no need to store them as a bunch)"""
 
-        return self.name_to_show_str.iterkeys()
+        return self._layout_infos.iterkeys()
 
     def get_switching_options(self):
         """Method returning list of available layout switching options"""
 
-        return self._switching_options
+        return self._switch_opt_infos.iterkeys()
+
+    def get_layout_variant_description(self, layout_variant, with_lang=True):
+        """
+        Get description of the given layout-variant.
+
+        :param layout_variant: layout-variant specification (e.g. 'cz (qwerty)')
+        :type layout_variant: str
+        :param with_lang: whether to include language of the layout-variant (if defined)
+                          in the description or not
+        :type with_lang: bool
+        :return: description of the layout-variant specification (e.g. 'Czech (qwerty)')
+        :rtype: str
+
+        """
+
+        layout_info = self._layout_infos[layout_variant]
+        if with_lang and layout_info.lang:
+            return "%s (%s)" % (iutil.upcase_first_letter(layout_info.lang),
+                                layout_info.desc)
+        else:
+            return layout_info.desc
+
+    def get_switch_opt_description(self, switch_opt):
+        """
+        Get description of the given layout switching option.
+
+        :param switch_opt: switching option name/ID (e.g. 'grp:alt_shift_toggle')
+        :type switch_opt: str
+        :return: description of the layout switching option (e.g. 'Alt + Shift')
+        :rtype: str
+
+        """
+
+        return self._switch_opt_infos[switch_opt]
 
     def activate_default_layout(self):
         """
@@ -522,7 +548,7 @@ class XklWrapper(object):
     def is_valid_layout(self, layout):
         """Return if given layout is valid layout or not"""
 
-        return layout in self.name_to_show_str
+        return layout in self._layout_infos
 
     def add_layout(self, layout):
         """
diff --git a/pyanaconda/ui/gui/spokes/keyboard.py b/pyanaconda/ui/gui/spokes/keyboard.py
index 0cc8e48..bbc59d3 100644
--- a/pyanaconda/ui/gui/spokes/keyboard.py
+++ b/pyanaconda/ui/gui/spokes/keyboard.py
@@ -39,11 +39,11 @@ __all__ = ["KeyboardSpoke"]
 LAYOUT_SWITCHING_INFO = N_("%s to switch layouts.")
 
 def _show_layout(column, renderer, model, itr, wrapper):
-    value = wrapper.name_to_show_str[model[itr][0]]
+    value = wrapper.get_layout_variant_description(model[itr][0])
     renderer.set_property("text", value)
 
 def _show_description(column, renderer, model, itr, wrapper):
-    value = wrapper.switch_to_show_str[model[itr][0]]
+    value = wrapper.get_switch_opt_description(model[itr][0])
     if model[itr][1]:
         value = "<b>%s</b>" % value
     renderer.set_property("markup", value)
@@ -61,7 +61,7 @@ class AddLayoutDialog(GUIObject):
 
     def matches_entry(self, model, itr, user_data=None):
         value = model[itr][0]
-        value = self._xkl_wrapper.name_to_show_str[value]
+        value = self._xkl_wrapper.get_layout_variant_description(value)
         entry_text = self._entry.get_text()
         if entry_text is not None:
             entry_text = entry_text.lower()
@@ -87,8 +87,8 @@ class AddLayoutDialog(GUIObject):
 
         value1 = model[itr1][0]
         value2 = model[itr2][0]
-        show_str1 = self._xkl_wrapper.name_to_show_str[value1]
-        show_str2 = self._xkl_wrapper.name_to_show_str[value2]
+        show_str1 = self._xkl_wrapper.get_layout_variant_description(value1)
+        show_str2 = self._xkl_wrapper.get_layout_variant_description(value2)
 
         if show_str1 < show_str2:
             return -1
@@ -223,8 +223,8 @@ class ConfigureSwitchingDialog(GUIObject):
 
         value1 = model[itr1][0]
         value2 = model[itr2][0]
-        show_str1 = self._xkl_wrapper.switch_to_show_str[value1]
-        show_str2 = self._xkl_wrapper.switch_to_show_str[value2]
+        show_str1 = self._xkl_wrapper.get_switch_opt_description(value1)
+        show_str2 = self._xkl_wrapper.get_switch_opt_description(value2)
 
         if show_str1 < show_str2:
             return -1
@@ -297,7 +297,7 @@ class KeyboardSpoke(NormalSpoke):
     @property
     def status(self):
         # We don't need to check that self._store is empty, because that isn't allowed.
-        return self._xkl_wrapper.name_to_show_str[self._store[0][0]]
+        return self._xkl_wrapper.get_layout_variant_description(self._store[0][0])
 
     def initialize(self):
         NormalSpoke.initialize(self)
@@ -379,7 +379,7 @@ class KeyboardSpoke(NormalSpoke):
     def _refresh_switching_info(self):
         if self.data.keyboard.switch_options:
             first_option = self.data.keyboard.switch_options[0]
-            desc = self._xkl_wrapper.switch_to_show_str[first_option]
+            desc = self._xkl_wrapper.get_switch_opt_description(first_option)
 
             self._layoutSwitchLabel.set_text(_(LAYOUT_SWITCHING_INFO) % desc)
         else:
-- 
1.7.11.7



More information about the anaconda-patches mailing list