[tor-commits] [arm/master] Python black-magic to connect lists with liststores.

atagar at torproject.org atagar at torproject.org
Thu Aug 11 15:27:56 UTC 2011


commit df0e5d0ca45f0c304fab0362ce7327cea0b2df5e
Author: Kamran Riaz Khan <krkhan at inspirated.com>
Date:   Sat Jul 23 06:20:05 2011 +0500

    Python black-magic to connect lists with liststores.
    
    The ConfigPanel.confImportantContents is patched via Python property()
    in such a way that assigning lists to it automatically updates
    the liststore. The setter/getter is really ugly and hard to read, and
    serves only the purpose that the CLI portion does not need to be
    changed at all for this to work.
    
    Once library/CLI/GUI is decoupled this obscure piece of dark magic
    shall be gone for good.
---
 src/gui/configPanel.py |   25 +++++++++++++++++++++++--
 src/util/gtkTools.py   |    7 +++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/gui/configPanel.py b/src/gui/configPanel.py
index 46f9aa7..e0fb8dc 100644
--- a/src/gui/configPanel.py
+++ b/src/gui/configPanel.py
@@ -23,14 +23,35 @@ class ConfContents(gtkTools.ListWrapper):
 
     return row
 
-class ConfigPanel(CliConfigPanel):
+class ConfigPanel(object, CliConfigPanel):
   def __init__(self, builder):
     CliConfigPanel.__init__(self, None, State.TOR)
 
     self.builder = builder
 
     listStore = self.builder.get_object('liststore_config')
-    self.confImportantContents = ConfContents(self.confImportantContents, listStore)
+    self._confImportantContents = ConfContents(self.confImportantContents, listStore)
+
+    self.confImportantContents = self.confImportantContents[-5:]
+
+  @property
+  def confImportantContents(self):
+    if hasattr(self, '_confImportantContents'):
+      return self._confImportantContents
+    else:
+      return []
+
+  @confImportantContents.setter
+  def confImportantContents(self, value):
+    if hasattr(self, '_confImportantContents'):
+      try:
+        self._confImportantContents.empty()
+      except AttributeError:
+        pass
+      for entry in value:
+        self._confImportantContents.append(entry)
+    else:
+      self._confImportantContents = value
 
   def pack_widgets(self):
     treeView = self.builder.get_object('treeview_config')
diff --git a/src/util/gtkTools.py b/src/util/gtkTools.py
index 2d00621..5ad321e 100644
--- a/src/util/gtkTools.py
+++ b/src/util/gtkTools.py
@@ -32,6 +32,10 @@ class ListWrapper(object):
     self.container.append(entry)
     gobject.idle_add(self.__model_append, entry)
 
+  def empty(self):
+    self.container = []
+    gobject.idle_add(self.__model_clear)
+
   def __str__(self):
     return str(self.container)
 
@@ -62,6 +66,9 @@ class ListWrapper(object):
     row = self._create_row_from_entry(entry)
     self.model.append(row)
 
+  def __model_clear(self):
+    self.model.clear()
+
   def __model_del(self, key):
     treeIter = self.model.get_iter(key)
     self.model.remove(treeIter)





More information about the tor-commits mailing list