[tor-commits] [arm/master] Loading internal settings via _load_settings()

atagar at torproject.org atagar at torproject.org
Sun Sep 15 22:29:21 UTC 2013


commit a20f237e2e0d9cdf53a839e80d92d6cca60ebe08
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Sep 15 12:15:10 2013 -0700

    Loading internal settings via _load_settings()
    
    Using a helper to load our internal settings, and erroring out (rather than
    warning) if our settings.cfg can't be read.
---
 arm/settings.cfg |    4 ++--
 arm/starter.py   |   40 +++++++++++++++++++++++++++++-----------
 test/starter.py  |    3 +++
 3 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/arm/settings.cfg b/arm/settings.cfg
index 627905c..a14d345 100644
--- a/arm/settings.cfg
+++ b/arm/settings.cfg
@@ -19,8 +19,8 @@ msg.help
 |arm -e we -c /tmp/cfg   use this configuration file with 'WARN'/'ERR' events
 
 msg.wrong_port_type
-|Please check in your torrc that %i is the ControlPort. Maybe you configured
-|it to be the ORPort or SocksPort instead?
+|Please check in your torrc that {port} is the ControlPort. Maybe you
+|configured it to be the ORPort or SocksPort instead?
 
 msg.wrong_socket_type
 |Unable to connect to tor. Are you sure the interface you specified belongs to
diff --git a/arm/starter.py b/arm/starter.py
index c804745..289d099 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -46,8 +46,6 @@ CONFIG = stem.util.conf.config_dict("arm", {
   'msg.unable_to_read_config': '',
 })
 
-NO_INTERNAL_CFG_MSG = "Failed to load the parsing configuration. This will be problematic for a few things like torrc validation and log duplication detection (%s)"
-
 # torrc entries that are scrubbed when dumping
 PRIVATE_TORRC_ENTRIES = ["HashedControlPassword", "Bridge", "HiddenServiceDir"]
 
@@ -76,15 +74,29 @@ ARGS = {
 OPT = "gi:s:c:dbe:vh"
 OPT_EXPANDED = ["interface=", "socket=", "config=", "debug", "blind", "event=", "version", "help"]
 
-try:
-  pathPrefix = os.path.dirname(sys.argv[0])
-  if pathPrefix and not pathPrefix.endswith("/"):
-    pathPrefix = pathPrefix + "/"
+IS_SETTINGS_LOADED = False
 
-  config = stem.util.conf.get_config("arm")
-  config.load("%sarm/settings.cfg" % pathPrefix)
-except IOError, exc:
-  stem.util.log.warn(NO_INTERNAL_CFG_MSG % exc.strerror)
+
+def _load_settings():
+  """
+  Loads arms internal settings from its 'settings.cfg'. This comes bundled with
+  arm and should be considered to be an error if it can't be loaded. If the
+  settings have already been loaded then this is a no-op.
+
+  :raises: **ValueError** if the settings can't be loaded
+  """
+
+  global IS_SETTINGS_LOADED
+
+  if not IS_SETTINGS_LOADED:
+    config = stem.util.conf.get_config("arm")
+    settings_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
+
+    try:
+      config.load(settings_path)
+      IS_SETTINGS_LOADED = True
+    except IOError as exc:
+      raise ValueError("Unable to load arm's internal configuration (%s): %s" % (settings_path, exc))
 
 
 def _get_args(argv):
@@ -192,7 +204,7 @@ def _authenticate(controller, password):
     control_socket = controller.get_socket()
 
     if isinstance(control_socket, stem.socket.ControlPort):
-      raise ValueError(CONFIG['msg.wrong_port_type'] % control_socket.get_port())
+      raise ValueError(CONFIG['msg.wrong_port_type'].format(port = control_socket.get_port()))
     else:
       raise ValueError(CONFIG['msg.wrong_socket_type'])
   except stem.connection.UnrecognizedAuthMethods as exc:
@@ -256,6 +268,12 @@ def _dumpConfig():
 def main():
   startTime = time.time()
 
+  try:
+    _load_settings()
+  except ValueError as exc:
+    print exc
+    sys.exit(1)
+
   # attempts to fetch attributes for parsing tor's logs, configuration, etc
   
   config = stem.util.conf.get_config("arm")
diff --git a/test/starter.py b/test/starter.py
index d1c90cf..724a318 100644
--- a/test/starter.py
+++ b/test/starter.py
@@ -8,6 +8,7 @@ import unittest
 from mock import Mock, patch
 
 from arm.starter import (
+  _load_settings,
   _get_args,
   _get_controller,
   _authenticate,
@@ -18,6 +19,8 @@ import stem
 import stem.connection
 import stem.socket
 
+_load_settings()
+
 class TestArgumentParsing(unittest.TestCase):
   def test_that_we_get_default_values(self):
     args = _get_args([])





More information about the tor-commits mailing list