[tor-commits] [stem/master] Supporting directories with Config's load() method

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


commit d2c60a8fe9a136ceaa7e502d3cbf2d7938d48f3d
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jun 14 14:08:49 2014 -0700

    Supporting directories with Config's load() method
    
    Loading all files within a directory if provided to the Config class' load()
    method.
---
 docs/change_log.rst |    4 ++++
 stem/util/conf.py   |   18 +++++++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index ae0db05..2159c28 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -40,6 +40,10 @@ Unreleased
 The following are only available within Stem's `git repository
 <download.html>`_.
 
+ * **Utilities**
+
+  * Added support for directories to :func:`stem.util.conf.Config.load`.
+
  * **Interpreter**
 
   * The /info command errored for relays without contact information.
diff --git a/stem/util/conf.py b/stem/util/conf.py
index 702c27f..57bc724 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -157,6 +157,7 @@ Here's an expanation of what happened...
     +- get_value - provides the value for a given key as a string
 """
 
+import os
 import threading
 
 from stem.util import log
@@ -453,10 +454,14 @@ class Config(object):
   def load(self, path = None):
     """
     Reads in the contents of the given path, adding its configuration values
-    to our current contents.
+    to our current contents. If the path is a directory then this loads each
+    of the files, recursively.
 
-    :param str path: file path to be loaded, this uses the last loaded path if
-      not provided
+    .. versionchanged:: 1.3.0
+       Added support for directories.
+
+    :param str path: file or directory path to be loaded, this uses the last
+      loaded path if not provided
 
     :raises:
       * **IOError** if we fail to read the file (it doesn't exist, insufficient
@@ -469,6 +474,13 @@ class Config(object):
     elif not self._path:
       raise ValueError('Unable to load configuration: no path provided')
 
+    if os.path.isdir(self._path):
+      for root, dirnames, filenames in os.walk(self._path):
+        for filename in filenames:
+          self.load(os.path.join(root, filename))
+
+      return
+
     with open(self._path, 'r') as config_file:
       read_contents = config_file.readlines()
 





More information about the tor-commits mailing list