[tor-commits] [arm/master] Taking advantage of Stem's @uses_settings

atagar at torproject.org atagar at torproject.org
Mon Jun 16 01:10:16 UTC 2014


commit fe89d773374fb35f01d4d32afa47e757c8c4203c
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jun 15 18:05:17 2014 -0700

    Taking advantage of Stem's @uses_settings
    
    Dropping our load_settings() helper function in favor of Stem's @uses_settings
    annotation. Once correction this makes is that if we fail to load our interal
    settings we printed an error using msg(). We can't - msg() *uses* our internal
    settings after all. :P
---
 arm/config/strings.cfg |    1 -
 arm/starter.py         |    9 ++-------
 arm/util/__init__.py   |   35 ++++++++++++++---------------------
 run_tests.py           |   33 +++++++++++++++++++++------------
 4 files changed, 37 insertions(+), 41 deletions(-)

diff --git a/arm/config/strings.cfg b/arm/config/strings.cfg
index 3ac3b3c..25970d0 100644
--- a/arm/config/strings.cfg
+++ b/arm/config/strings.cfg
@@ -13,7 +13,6 @@
 
 msg.wrap {text}
 
-msg.config.unable_to_load_settings Unable to load arm's internal configurations: {error}
 msg.config.unable_to_read_file Failed to load configuration (using defaults): "{error}"
 msg.config.nothing_loaded No armrc loaded, using defaults. You can customize arm by placing a configuration file at {path} (see the armrc.sample for its options).
 
diff --git a/arm/starter.py b/arm/starter.py
index ba7c9af..8d27024 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -27,21 +27,16 @@ import stem.util.conf
 import stem.util.log
 import stem.util.system
 
-from arm.util import BASE_DIR, init_controller, msg, trace, info, notice, warn, load_settings
+from arm.util import BASE_DIR, init_controller, msg, trace, info, notice, warn, uses_settings
 
 CONFIG = stem.util.conf.get_config('arm')
 
 
+ at uses_settings
 def main():
   CONFIG.set('start_time', str(int(time.time())))
 
   try:
-    load_settings()
-  except IOError as exc:
-    print msg('config.unable_to_load_settings', error = exc)
-    sys.exit(1)
-
-  try:
     args = arm.arguments.parse(sys.argv[1:])
     CONFIG.set('startup.events', args.logged_events)
   except ValueError as exc:
diff --git a/arm/util/__init__.py b/arm/util/__init__.py
index d606a43..3d3f725 100644
--- a/arm/util/__init__.py
+++ b/arm/util/__init__.py
@@ -4,9 +4,16 @@ application's status, making cross platform system calls, parsing tor data,
 and safely working with curses (hiding some of the gory details).
 """
 
-__all__ = ["connections", "panel", "sysTools", "text_input", "tor_config", "tor_tools", "tracker", "ui_tools"]
+__all__ = [
+  'panel',
+  'text_input',
+  'tor_config',
+  'tracker',
+  'ui_tools',
+]
 
 import os
+import sys
 
 import stem
 import stem.connection
@@ -17,6 +24,12 @@ import stem.util.log
 TOR_CONTROLLER = None
 BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-2])
 
+try:
+  uses_settings = stem.util.conf.uses_settings('arm', os.path.join(BASE_DIR, 'config'), lazy_load = False)
+except IOError as exc:
+  print "Unable to load arm's internal configurations: {error}".format(error = exc)
+  sys.exit(1)
+
 
 def tor_controller():
   """
@@ -82,26 +95,6 @@ def error(msg, **attr):
   _log(stem.util.log.ERROR, msg, **attr)
 
 
-def load_settings():
-  """
-  Loads arms internal settings. This should be treated as a fatal failure if
-  unsuccessful.
-
-  :raises: **IOError** if we're unable to read or parse our internal
-    configurations
-  """
-
-  config = stem.util.conf.get_config('arm')
-
-  if not config.get('settings_loaded', False):
-    config_dir = os.path.join(BASE_DIR, 'config')
-
-    for config_file in os.listdir(config_dir):
-      config.load(os.path.join(config_dir, config_file))
-
-    config.set('settings_loaded', 'true')
-
-
 def _log(runlevel, message, **attr):
   """
   Logs the given message, formatted with optional attributes.
diff --git a/run_tests.py b/run_tests.py
index 31e82fb..63de9be 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -13,7 +13,7 @@ import unittest
 import stem.util.conf
 import stem.util.test_tools
 
-from arm.util import load_settings
+from arm.util import uses_settings
 
 ARM_BASE = os.path.dirname(__file__)
 
@@ -25,16 +25,15 @@ SRC_PATHS = [os.path.join(ARM_BASE, path) for path in (
 )]
 
 
+ at uses_settings
 def main():
-  load_settings()
-
-  test_config = stem.util.conf.get_config("test")
-  test_config.load(os.path.join(ARM_BASE, "test", "settings.cfg"))
+  test_config = stem.util.conf.get_config('test')
+  test_config.load(os.path.join(ARM_BASE, 'test', 'settings.cfg'))
 
   orphaned_pyc = stem.util.test_tools.clean_orphaned_pyc(ARM_BASE)
 
   for path in orphaned_pyc:
-    print "Deleted orphaned pyc file: %s" % path
+    print 'Deleted orphaned pyc file: %s' % path
 
   tests = unittest.defaultTestLoader.discover('test', pattern='*.py')
   test_runner = unittest.TextTestRunner()
@@ -45,24 +44,34 @@ def main():
   static_check_issues = {}
 
   if stem.util.test_tools.is_pyflakes_available():
-    for path, issues in stem.util.test_tools.get_pyflakes_issues(SRC_PATHS).items():
+    pyflakes_issues = stem.util.test_tools.get_pyflakes_issues(SRC_PATHS)
+
+    for path, issues in pyflakes_issues.items():
       for issue in issues:
         static_check_issues.setdefault(path, []).append(issue)
 
   if stem.util.test_tools.is_pep8_available():
-    for path, issues in stem.util.test_tools.get_stylistic_issues(SRC_PATHS, check_two_space_indents = True, check_newlines = True, check_trailing_whitespace = True, check_exception_keyword = True).items():
+    pep8_issues = stem.util.test_tools.get_stylistic_issues(
+      SRC_PATHS,
+      check_two_space_indents = True,
+      check_newlines = True,
+      check_trailing_whitespace = True,
+      check_exception_keyword = True,
+    )
+
+    for path, issues in pep8_issues.items():
       for issue in issues:
         static_check_issues.setdefault(path, []).append(issue)
 
   if static_check_issues:
-    print "STATIC CHECKS"
+    print 'STATIC CHECKS'
 
     for file_path in static_check_issues:
-      print "* %s" % file_path
+      print '* %s' % file_path
 
       for line_number, msg in static_check_issues[file_path]:
-        line_count = "%-4s" % line_number
-        print "  line %s - %s" % (line_count, msg)
+        line_count = '%-4s' % line_number
+        print '  line %s - %s' % (line_count, msg)
 
       print
 



More information about the tor-commits mailing list