[PATCH 2/4] Short-circuit layouts matching

Vratislav Podzimek vpodzime at redhat.com
Wed Nov 13 12:46:51 UTC 2013


If no string is entered, all layouts should match. Also move the have_word_match
function to a better place making it reusable.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/iutil.py                  | 20 ++++++++++++++++++++
 pyanaconda/ui/gui/spokes/keyboard.py | 22 +++++-----------------
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index f13fb10..dbdb64b 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -769,3 +769,23 @@ def get_mount_paths(devnode):
     majmin = "%d:%d" % (os.major(devno),os.minor(devno))
     mountinfo = (line.split() for line in open("/proc/self/mountinfo"))
     return [info[4] for info in mountinfo if info[2] == majmin]
+
+def have_word_match(str1, str2):
+    """Tells if all words from str1 exist in str2 or not."""
+
+    if str1 is None or str2 is None:
+        # None never matches
+        return False
+
+    if str1 == "":
+        # empty string matches everything except from None
+        return True
+    elif str2 == "":
+        # non-empty string cannot be found in an empty string
+        return False
+
+    str1 = str1.lower()
+    str1_words = str1.split()
+    str2 = str2.lower()
+
+    return all(word in str2 for word in str1_words)
diff --git a/pyanaconda/ui/gui/spokes/keyboard.py b/pyanaconda/ui/gui/spokes/keyboard.py
index 609bbaa..1294072 100644
--- a/pyanaconda/ui/gui/spokes/keyboard.py
+++ b/pyanaconda/ui/gui/spokes/keyboard.py
@@ -33,6 +33,7 @@ from pyanaconda.constants import DEFAULT_KEYBOARD, THREAD_KEYBOARD_INIT, THREAD_
 from pyanaconda.ui.communication import hubQ
 from pyanaconda.iutil import strip_accents
 from pyanaconda.threads import threadMgr, AnacondaThread
+from pyanaconda.iutil import have_word_match
 
 import locale as locale_mod
 
@@ -68,28 +69,15 @@ class AddLayoutDialog(GUIObject):
         self._chosen_layouts = []
 
     def matches_entry(self, model, itr, user_data=None):
-        def have_word_match(str1, str2):
-            """Tells if all words from str1 exist in str2 or not."""
-
-            if str1 is None or str2 is None:
-                return False
-
-            str1 = str1.lower()
-            str1_words = str1.split()
-
-            try:
-                str2 = str2.lower()
-                for word in str1_words:
-                    str2.index(word)
-                return True
-            except ValueError:
-                return False
+        entry_text = self._entry.get_text()
+        if not entry_text:
+            # everything matches empty string
+            return True
 
         value = model[itr][0]
         eng_value = self._xkl_wrapper.get_layout_variant_description(value, xlated=False)
         xlated_value = self._xkl_wrapper.get_layout_variant_description(value)
         translit_value = strip_accents(xlated_value).lower()
-        entry_text = self._entry.get_text()
         translit_text = strip_accents(unicode(entry_text, "utf-8")).lower()
 
         return have_word_match(entry_text, eng_value) or have_word_match(entry_text, xlated_value) \
-- 
1.8.4.2



More information about the anaconda-patches mailing list