[tor-commits] [arm/master] Derive entry classes from CLI and support conversion.
atagar at torproject.org
atagar at torproject.org
Fri Jul 15 01:07:02 UTC 2011
commit 27ff1d8672e09c38571dd79bce4fc911d61ee6d2
Author: Kamran Riaz Khan <krkhan at inspirated.com>
Date: Sat Jul 9 18:35:08 2011 +0500
Derive entry classes from CLI and support conversion.
Connection panel entry classes are derived from the CLI counterparts.
The CLI instances are "converted" to GUI via class introspection while
traversing the entry lines in connection panel.
This will facilitate overloading getListingEntry() for returning
rows for GTK+ treestore instead of lines for curses rendering.
Cache keys now use __class__ attribute since that refers to the GUI
entry classes.
---
src/gui/connections/__init__.py | 2 +-
src/gui/connections/circEntry.py | 21 +++++++++++++++++++++
src/gui/connections/connEntry.py | 17 +++++++++++++++++
src/gui/connections/connPanel.py | 38 +++++++++++++++++++++++++++++---------
4 files changed, 68 insertions(+), 10 deletions(-)
diff --git a/src/gui/connections/__init__.py b/src/gui/connections/__init__.py
index 3102ead..e573074 100644
--- a/src/gui/connections/__init__.py
+++ b/src/gui/connections/__init__.py
@@ -2,5 +2,5 @@
Connections.
"""
-__all__ = ["connPanel"]
+__all__ = ["connPanel", "circEntry", "connEntry"]
diff --git a/src/gui/connections/circEntry.py b/src/gui/connections/circEntry.py
new file mode 100644
index 0000000..63436cf
--- /dev/null
+++ b/src/gui/connections/circEntry.py
@@ -0,0 +1,21 @@
+"""
+Connection panel entries for client circuits.
+"""
+
+from cli.connections import circEntry
+
+class CircEntry(circEntry.CircEntry):
+ @classmethod
+ def convertToGui(self, instance):
+ instance.__class__ = self
+
+class CircHeaderLine(circEntry.CircHeaderLine):
+ @classmethod
+ def convertToGui(self, instance):
+ instance.__class__ = self
+
+class CircLine(circEntry.CircLine):
+ @classmethod
+ def convertToGui(self, instance):
+ instance.__class__ = self
+
diff --git a/src/gui/connections/connEntry.py b/src/gui/connections/connEntry.py
new file mode 100644
index 0000000..4db0a10
--- /dev/null
+++ b/src/gui/connections/connEntry.py
@@ -0,0 +1,17 @@
+"""
+Connection panel entries related to actual connections to or from the system
+(ie, results seen by netstat, lsof, etc).
+"""
+
+from cli.connections import connEntry
+
+class ConnectionEntry(connEntry.ConnectionEntry):
+ @classmethod
+ def convertToGui(self, instance):
+ instance.__class__ = self
+
+class ConnectionLine(connEntry.ConnectionLine):
+ @classmethod
+ def convertToGui(self, instance):
+ instance.__class__ = self
+
diff --git a/src/gui/connections/connPanel.py b/src/gui/connections/connPanel.py
index 29f18b7..d8d987a 100644
--- a/src/gui/connections/connPanel.py
+++ b/src/gui/connections/connPanel.py
@@ -11,13 +11,32 @@ from collections import deque
import gobject
import gtk
-from cli.connections import circEntry, connEntry
+from cli.connections import (circEntry as cliCircEntry, connEntry as cliConnEntry)
from cli.connections.connPanel import ConnectionPanel as CliConnectionPanel
+from gui.connections import circEntry, connEntry
from TorCtl import TorCtl
from util import connections, sysTools, uiTools, torTools
REFRESH_RATE = 3
+def convertToGui(instance):
+ cliToGuiMap = [ (cliCircEntry.CircEntry, circEntry.CircEntry),
+ (cliCircEntry.CircHeaderLine, circEntry.CircHeaderLine),
+ (cliCircEntry.CircLine, circEntry.CircLine),
+ (cliConnEntry.ConnectionEntry, connEntry.ConnectionEntry),
+ (cliConnEntry.ConnectionLine, connEntry.ConnectionLine)]
+
+ for (cliClass, guiClass) in cliToGuiMap:
+ if isinstance(instance, cliClass):
+ guiClass.convertToGui(instance)
+ break
+
+def calculateCacheKey(entryLine):
+ local = (entryLine.local.ipAddr, entryLine.local.port)
+ foreign = (entryLine.foreign.ipAddr, entryLine.foreign.port)
+
+ return (entryLine.__class__, local, foreign)
+
class ConnectionPanel(CliConnectionPanel):
def __init__(self, builder):
CliConnectionPanel.__init__(self, None)
@@ -46,13 +65,12 @@ class ConnectionPanel(CliConnectionPanel):
# first pass checks whether we have enough entries cached to not update the treeview
index = 0
for line in self._entryLines:
- local = "%s:%s" % (line.local.ipAddr, line.local.port)
- foreign = "%s:%s" % (line.foreign.ipAddr, line.foreign.port)
+ convertToGui(line)
+ cacheKey = calculateCacheKey(line)
- cachekey = (local, foreign, isinstance(line, circEntry.CircHeaderLine))
- if self.cache.has_key(cachekey):
+ if self.cache.has_key(cacheKey):
timeLabel = "%d s" % (time.time() - line.startTime)
- treestore.set_value(self.cache[cachekey], 2, timeLabel)
+ treestore.set_value(self.cache[cacheKey], 2, timeLabel)
else:
break
@@ -61,11 +79,13 @@ class ConnectionPanel(CliConnectionPanel):
if index == len(self._entryLines):
return True
+ # one of the entries was not found in cache, clear and repopulate the treestore
treestore.clear()
-
headeriter = None
for line in self._entryLines:
+ convertToGui(line)
+
if isinstance(line, connEntry.ConnectionLine) and line.isUnresolvedApp():
self._resolveApps()
@@ -82,8 +102,8 @@ class ConnectionPanel(CliConnectionPanel):
else:
currentiter = treestore.append(None, row)
- cachekey = (local, foreign, isinstance(line, circEntry.CircHeaderLine))
- self.cache[cachekey] = currentiter
+ cacheKey = calculateCacheKey(line)
+ self.cache[cacheKey] = currentiter
self.valsLock.release()
More information about the tor-commits
mailing list