[PATCH 3/3] Provide our own sorting functions for regions and timezones (#1025029)

Vratislav Podzimek vpodzime at redhat.com
Mon Nov 4 13:57:18 UTC 2013


We want to put the Etc region to the end of the list and sort GMT+-X zones
numerically.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/ui/gui/spokes/datetime_spoke.glade | 33 ++++++++-------
 pyanaconda/ui/gui/spokes/datetime_spoke.py    | 58 ++++++++++++++++++++++++++-
 2 files changed, 75 insertions(+), 16 deletions(-)

diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.glade b/pyanaconda/ui/gui/spokes/datetime_spoke.glade
index 6f935e7..a2c453b 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.glade
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.glade
@@ -28,6 +28,23 @@
     <property name="inline_completion">True</property>
     <signal name="match-selected" handler="on_completion_match_selected" object="cityCombobox" swapped="no"/>
   </object>
+  <object class="GtkListStore" id="regions">
+    <columns>
+      <!-- column-name name -->
+      <column type="gchararray"/>
+      <!-- column-name xlated -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
+  <object class="GtkTreeModelSort" id="regionsSort">
+    <property name="model">regions</property>
+  </object>
+  <object class="GtkEntryCompletion" id="regionCompletion">
+    <property name="model">regionsSort</property>
+    <property name="text_column">0</property>
+    <property name="inline_completion">True</property>
+    <signal name="match-selected" handler="on_completion_match_selected" object="regionCombobox" swapped="no"/>
+  </object>
   <object class="GtkImage" id="configImage">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
@@ -63,20 +80,6 @@
       <column type="gchararray"/>
     </columns>
   </object>
-  <object class="GtkListStore" id="regions">
-    <columns>
-      <!-- column-name name -->
-      <column type="gchararray"/>
-      <!-- column-name xlated -->
-      <column type="gchararray"/>
-    </columns>
-  </object>
-  <object class="GtkEntryCompletion" id="regionCompletion">
-    <property name="model">regions</property>
-    <property name="text_column">0</property>
-    <property name="inline_completion">True</property>
-    <signal name="match-selected" handler="on_completion_match_selected" object="regionCombobox" swapped="no"/>
-  </object>
   <object class="AnacondaSpokeWindow" id="datetimeWindow">
     <property name="startup_id">filler</property>
     <property name="can_focus">False</property>
@@ -146,7 +149,7 @@
                               <object class="GtkComboBox" id="regionCombobox">
                                 <property name="visible">True</property>
                                 <property name="can_focus">False</property>
-                                <property name="model">regions</property>
+                                <property name="model">regionsSort</property>
                                 <property name="has_entry">True</property>
                                 <property name="entry_text_column">1</property>
                                 <signal name="changed" handler="on_region_changed" object="regionsEntry" swapped="no"/>
diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
index da33246..6fa7048 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
@@ -45,7 +45,9 @@ from pyanaconda.threads import threadMgr, AnacondaThread
 
 import datetime
 import os
+import re
 import threading
+import locale as locale_mod
 
 __all__ = ["DatetimeSpoke"]
 
@@ -55,6 +57,57 @@ SERVER_QUERY = 2
 
 DEFAULT_TZ = "America/New_York"
 
+SPLIT_NUMBER_SUFFIX_RE = re.compile(r'([^0-9]*)([-+])([0-9]+)')
+
+def _compare_regions(model, itr1, itr2, data=None):
+    """Compare two regions from given model and iterators."""
+
+    # sort the Etc timezones to the end
+    if model[itr1][0] == "Etc" and model[itr2][0] == "Etc":
+        return 0
+    elif model[itr1][0] == "Etc":
+        return 1
+    elif model[itr2][0] == "Etc":
+        return -1
+    else:
+        # otherwise compare the translated names
+        return = locale_mod.strcoll(model[itr1][1], model[itr2][1])
+
+def _compare_cities(model, itr1, itr2, data=None):
+    """Compare two cities from given model and iterators."""
+
+    # if there are "cities" ending with numbers (like GMT+-X), we need to sort
+    # them based on their numbers
+    val1 = model[itr1][1]
+    val2 = model[itr2][1]
+
+    match1 = SPLIT_NUMBER_SUFFIX_RE.match(val1)
+    match2 = SPLIT_NUMBER_SUFFIX_RE.match(val2)
+
+    if match1 is None and match2 is None:
+        # no +-X suffix, just compare the strings
+        return locale_mod.strcoll(val1, val2)
+
+    if match1 is None or match2 is None:
+        # one with the +-X suffix, compare the prefixes
+        if match1:
+            prefix, sign, suffix = match1.groups()
+            return locale_mod.strcoll(prefix, val2)
+        else:
+            prefix, sign, suffix = match2.groups()
+            return locale_mod.strcoll(val1, prefix)
+
+    # both have the +-X suffix
+    prefix1, sign1, suffix1 = match1.groups()
+    prefix2, sign2, suffix2 = match2.groups()
+
+    if prefix1 == prefix2:
+        # same prefixes, let signs determine
+        return cmp(int(sign1 + suffix1), int(sign2 + suffix2))
+    else:
+        # compare prefixes
+        return locale_mod.strcoll(prefix1, prefix2)
+
 class NTPconfigDialog(GUIObject):
     builderObjects = ["ntpConfigDialog", "addImage", "serversStore"]
     mainWidgetName = "ntpConfigDialog"
@@ -327,7 +380,10 @@ class DatetimeSpoke(FirstbootSpokeMixIn, NormalSpoke):
         self._citiesFilter.set_visible_func(self.city_in_region, None)
 
         self._citiesSort = self.builder.get_object("citiesSort")
-        self._citiesSort.set_sort_column_id(1, Gtk.SortType.ASCENDING)
+        self._citiesSort.set_default_sort_func(_compare_cities, None)
+
+        self._regionsSort = self.builder.get_object("regionsSort")
+        self._regionsSort.set_default_sort_func(_compare_regions, None)
 
         self._hoursLabel = self.builder.get_object("hoursLabel")
         self._minutesLabel = self.builder.get_object("minutesLabel")
-- 
1.7.11.7



More information about the anaconda-patches mailing list