[tor-commits] [stem/master] Config support for multi-line entries

atagar at torproject.org atagar at torproject.org
Tue Jan 24 17:28:58 UTC 2012


commit 1f5b05766ce0c2533d8bd2861d142cc6523e4410
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Jan 24 09:26:59 2012 -0800

    Config support for multi-line entries
    
    Adding support for multi-line values in the configuration files. I've wanted
    this for a while, mostly for user facing messages. This still needs an integ
    test.
---
 run_tests.py      |   19 ++-----------------
 stem/util/conf.py |   25 ++++++++++++++++++++++++-
 test/settings.cfg |   16 ++++++++++++++++
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 9e9eb00..6972e16 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -43,6 +43,7 @@ CONFIG = {
   "argument.log": None,
   "argument.tor": "tor",
   "argument.no_color": False,
+  "msg.help": "",
   "target.config": {},
   "target.description": {},
   "target.prereq": {},
@@ -91,22 +92,6 @@ INTEG_TESTS = (
   test.integ.connection.connect.TestConnect,
 )
 
-# TODO: move into settings.cfg when we have multi-line options
-HELP_MSG = """Usage runTests.py [OPTION]
-Runs tests for the stem library.
-
-  -u, --unit            runs unit tests
-  -i, --integ           runs integration tests
-  -c, --config PATH     path to a custom test configuration
-  -t, --target TARGET   comma separated list of extra targets for integ tests
-  -l, --log RUNLEVEL    includes logging output with test results, runlevels:
-                          TRACE, DEBUG, INFO, NOTICE, WARN, ERROR
-      --tor PATH        custom tor binary to run testing against
-      --no-color        displays testing output without color
-  -h, --help            presents this help
-
-  Integration targets:"""
-
 def load_user_configuration(test_config):
   """
   Parses our commandline arguments, loading our custom test configuration if
@@ -155,7 +140,7 @@ def load_user_configuration(test_config):
       # Prints usage information and quits. This includes a listing of the
       # valid integration targets.
       
-      print HELP_MSG
+      print CONFIG["msg.help"]
       
       # gets the longest target length so we can show the entries in columns
       target_name_length = max(map(len, Target))
diff --git a/stem/util/conf.py b/stem/util/conf.py
index 8a1fcfc..67bd253 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -11,6 +11,13 @@ ignored. For instance:
   blankEntry.example
 
 would be loaded as four entries, the last one's value being an empty string.
+Mulit-line entries can be defined my providing an entry followed by lines with
+a '|' prefix. For instance...
+
+  msg.greeting
+  |This is a multi-line message
+  |exclaiming about the wonders
+  |and awe that is pepperjack!
 
 get_config - Singleton for getting configurations
 Config - Custom configuration.
@@ -173,8 +180,11 @@ class Config():
     
     self._contents_lock.acquire()
     self._raw_contents = read_contents
+    remainder = list(self._raw_contents)
     
-    for line in self._raw_contents:
+    while remainder:
+      line = remainder.pop(0)
+      
       # strips any commenting or excess whitespace
       comment_start = line.find("#")
       if comment_start != -1: line = line[:comment_start]
@@ -189,6 +199,19 @@ class Config():
           log.debug("Config entry '%s' is expected to be of the format 'Key Value', defaulting to '%s' -> ''" % (line, line))
           key, value = line, ""
         
+        if not value:
+          # this might be a multi-line entry, try processing it as such
+          multiline_buffer = []
+          
+          while remainder and remainder[0].lstrip().startswith("|"):
+            content = remainder.pop(0).lstrip()[1:] # removes '\s+|' prefix
+            content = content.rstrip("\n")          # trailing newline
+            multiline_buffer.append(content)
+          
+          if multiline_buffer:
+            self.set(key, "\n".join(multiline_buffer), False)
+            continue
+        
         self.set(key, value, False)
     
     self._path = path
diff --git a/test/settings.cfg b/test/settings.cfg
index 19f5187..e004a98 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -6,6 +6,22 @@
 # target.* - Attributes of the integration testing targets. This helps
 #            determine what is ran when the user runs with '--target'.
 
+msg.help
+|Usage runTests.py [OPTION]
+|Runs tests for the stem library.
+|
+|  -u, --unit            runs unit tests
+|  -i, --integ           runs integration tests
+|  -c, --config PATH     path to a custom test configuration
+|  -t, --target TARGET   comma separated list of extra targets for integ tests
+|  -l, --log RUNLEVEL    includes logging output with test results, runlevels:
+|                          TRACE, DEBUG, INFO, NOTICE, WARN, ERROR
+|      --tor PATH        custom tor binary to run testing against
+|      --no-color        displays testing output without color
+|  -h, --help            presents this help
+|
+|  Integration targets:
+
  ##################
 # CATEGORY: TARGET #
  ##################



More information about the tor-commits mailing list