[tor-commits] [arm/master] Adding a tor_controller() helper function

atagar at torproject.org atagar at torproject.org
Fri Dec 27 18:05:37 UTC 2013


commit af95ca94bd4618b82c06ce709bdc16994b5846d9
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Dec 27 09:31:35 2013 -0800

    Adding a tor_controller() helper function
    
    Helper function for fetching our stem controller without going through the
    torTools module (which I'm aiming to deprecate).
---
 arm/starter.py       |    5 ++---
 arm/util/__init__.py |   31 +++++++++++++++++++++++++++++++
 arm/util/tracker.py  |    6 ++----
 3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/arm/starter.py b/arm/starter.py
index a2a6d4d..af760f7 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -21,7 +21,6 @@ import arm.controller
 import arm.logPanel
 import arm.util.panel
 import arm.util.torConfig
-import arm.util.torTools
 import arm.util.tracker
 import arm.util.uiTools
 
@@ -33,7 +32,7 @@ import stem.util.connection
 import stem.util.log
 import stem.util.system
 
-from arm.util import msg, trace, notice, warn, load_settings
+from arm.util import init_controller, msg, trace, info, notice, warn, load_settings
 
 SETTINGS_PATH = os.path.join(os.path.dirname(__file__), 'settings.cfg')
 
@@ -112,7 +111,7 @@ def main():
   try:
     controller = _get_controller(args)
     _authenticate(controller, CONFIG['tor.password'])
-    arm.util.torTools.getConn().init(controller)
+    init_controller(controller)
   except ValueError as exc:
     print exc
     exit(1)
diff --git a/arm/util/__init__.py b/arm/util/__init__.py
index 9106caa..2603349 100644
--- a/arm/util/__init__.py
+++ b/arm/util/__init__.py
@@ -8,11 +8,41 @@ __all__ = ["connections", "panel", "sysTools", "textInput", "torConfig", "torToo
 
 import os
 
+import arm.util.torTools
+
 import stem.util.conf
 import stem.util.log
 
+TOR_CONTROLLER = None
 BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-2])
 
+
+def tor_controller():
+  """
+  Singleton for getting our tor controller connection.
+
+  :returns: :class:`~stem.control.Controller` arm is using
+  """
+
+  return TOR_CONTROLLER
+
+
+def init_controller(controller):
+  """
+  Registers an initialized tor controller.
+
+  :param stem.control.Controller controller: tor controller for arm to use
+  """
+
+  global TOR_CONTROLLER
+  TOR_CONTROLLER = controller
+
+  # TODO: Our controller() method will gradually replace the torTools module,
+  # but until that we need to initialize it too.
+
+  arm.util.torTools.getConn().init(controller)
+
+
 def msg(message, **attr):
   """
   Provides the given message.
@@ -75,6 +105,7 @@ def load_settings():
 
     config.set('settings_loaded', 'true')
 
+
 def _log(runlevel, message, **attr):
   """
   Logs the given message, formatted with optional attributes.
diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index 2bb87dd..3874d4c 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -39,12 +39,10 @@ import collections
 import time
 import threading
 
-import arm.util.torTools
-
 from stem.control import State
 from stem.util import conf, connection, log, proc, str_tools, system
 
-from arm.util import debug, info, notice
+from arm.util import tor_controller, debug, info, notice
 
 CONFIG = conf.config_dict('arm', {
   'queries.resources.rate': 5,
@@ -210,7 +208,7 @@ class Daemon(threading.Thread):
     self._pause_condition = threading.Condition()
     self._halt = False  # terminates thread if true
 
-    controller = arm.util.torTools.getConn().controller
+    controller = tor_controller()
     controller.add_status_listener(self._tor_status_listener)
     self._tor_status_listener(controller, State.INIT, None)
 





More information about the tor-commits mailing list