[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [nyx/master] Localize sort functionality in panel's sort function



commit 77e53454fb75f2477f3ad0473a241bfc770a432b
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Aug 30 12:14:56 2015 -0700

    Localize sort functionality in panel's sort function
    
    Merging the get_sort_value() into the sort function. Nice simplification.
---
 nyx/connections/conn_panel.py |   39 ++++++++++++++++++++++++++++++++-------
 nyx/connections/entries.py    |   38 --------------------------------------
 2 files changed, 32 insertions(+), 45 deletions(-)

diff --git a/nyx/connections/conn_panel.py b/nyx/connections/conn_panel.py
index b9c4980..f28c943 100644
--- a/nyx/connections/conn_panel.py
+++ b/nyx/connections/conn_panel.py
@@ -158,18 +158,43 @@ class ConnectionPanel(panel.Panel, threading.Thread):
         ordering_keys = [entries.SortAttr.keys()[entries.SortAttr.index_of(v)] for v in ordering]
         nyx_config.set('features.connection.order', ', '.join(ordering_keys))
 
-      def sort_type(attr):
+      def sort_value(entry, attr):
         if attr == entries.SortAttr.LISTING:
           if self.get_listing_type() == entries.ListingType.IP_ADDRESS:
-            return entries.SortAttr.IP_ADDRESS
+            attr = entries.SortAttr.IP_ADDRESS
           elif self.get_listing_type() == entries.ListingType.FINGERPRINT:
-            return entries.SortAttr.FINGERPRINT
+            attr = entries.SortAttr.FINGERPRINT
           elif self.get_listing_type() == entries.ListingType.NICKNAME:
-            return entries.SortAttr.NICKNAME
-
-        return attr
+            attr = entries.SortAttr.NICKNAME
+
+        connection_line = entry.get_lines()[0]
+
+        if attr == entries.SortAttr.IP_ADDRESS:
+          if entry.is_private():
+            return 255 ** 4  # orders at the end
+
+          ip_value = 0
+
+          for octet in connection_line.connection.remote_address.split('.'):
+            ip_value = ip_value * 255 + int(octet)
+
+          return ip_value * 65536 + connection_line.connection.remote_port
+        elif attr == entries.SortAttr.PORT:
+          return connection_line.connection.remote_port
+        elif attr == entries.SortAttr.FINGERPRINT:
+          return connection_line.get_fingerprint('UNKNOWN')
+        elif attr == entries.SortAttr.NICKNAME:
+          return connection_line.get_nickname('z' * 20)
+        elif attr == entries.SortAttr.CATEGORY:
+          return conn_entry.Category.index_of(entry.get_type())
+        elif attr == entries.SortAttr.UPTIME:
+          return connection_line.connection.start_time
+        elif attr == entries.SortAttr.COUNTRY:
+          return '' if entry.is_private() else connection_line.get_locale('')
+        else:
+          return ''
 
-      self._entries.sort(key = lambda i: [i.get_sort_value(sort_type(attr)) for attr in CONFIG['features.connection.order']])
+      self._entries.sort(key = lambda i: [sort_value(i, attr) for attr in CONFIG['features.connection.order']])
       self._entry_lines = list(itertools.chain.from_iterable([entry.get_lines() for entry in self._entries]))
 
   def get_listing_type(self):
diff --git a/nyx/connections/entries.py b/nyx/connections/entries.py
index b30ae43..20d19a8 100644
--- a/nyx/connections/entries.py
+++ b/nyx/connections/entries.py
@@ -113,44 +113,6 @@ class Entry(object):
 
     return self._lines
 
-  @lru_cache()
-  def get_sort_value(self, attr):
-    """
-    Value for sorting by a given attribute.
-
-    :param SortAtt attr: attribute to be sorted by
-
-    :returns: comparable object by the given attribute
-    """
-
-    connection_line = self._lines[0]
-
-    if attr == SortAttr.IP_ADDRESS:
-      if self.is_private():
-        return 255 ** 4  # orders at the end
-
-      ip_value = 0
-
-      for octet in connection_line.connection.remote_address.split('.'):
-        ip_value = ip_value * 255 + int(octet)
-
-      return ip_value * 65536 + connection_line.connection.remote_port
-    elif attr == SortAttr.PORT:
-      return connection_line.connection.remote_port
-    elif attr == SortAttr.FINGERPRINT:
-      return connection_line.get_fingerprint('UNKNOWN')
-    elif attr == SortAttr.NICKNAME:
-      return connection_line.get_nickname('z' * 20)
-    elif attr == SortAttr.CATEGORY:
-      import nyx.connections.conn_entry
-      return nyx.connections.conn_entry.Category.index_of(self.get_type())
-    elif attr == SortAttr.UPTIME:
-      return connection_line.connection.start_time
-    elif attr == SortAttr.COUNTRY:
-      return '' if self.is_private() else connection_line.get_locale('')
-    else:
-      return ''
-
 
 class ConnectionPanelLine:
   """



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits