[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Cleanup for stem.util.conf's save() method



commit e3b78e898dcfd4d33356c7e791fb92e6c7128484
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Feb 8 19:15:05 2012 -0800

    Cleanup for stem.util.conf's save() method
    
    Minor changes including:
    - fixing a 'self.path' assignment to be 'self._path'
    - making the path optional for load() as well
    - raising a ValueError for both load() and save() if we use an undefined path
    - whitespace fixes
    - added argument to save() pydoc
    - replacing integ test with one that explicitely tests the three types of
      values it might handle
---
 stem/util/conf.py       |   41 +++++++++++++++++++------------
 test/integ/util/conf.py |   61 +++++++++++++++++-----------------------------
 2 files changed, 48 insertions(+), 54 deletions(-)

diff --git a/stem/util/conf.py b/stem/util/conf.py
index 6eac9a1..7d086f4 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -194,7 +194,7 @@ class Config():
     # keys that have been requested (used to provide unused config contents)
     self._requested_keys = set()
   
-  def load(self, path):
+  def load(self, path = None):
     """
     Reads in the contents of the given path, adding its configuration values
     to our current contents.
@@ -204,10 +204,16 @@ class Config():
     
     Raises:
       IOError if we fail to read the file (it doesn't exist, insufficient
-      permissions, etc)
+        permissions, etc)
+      ValueError if we don't have a default path and none was provided
     """
     
-    with open(path, "r") as config_file:
+    if path:
+      self._path = path
+    elif not self._path:
+      raise ValueError("Unable to load configuration: no path provided")
+    
+    with open(self._path, "r") as config_file:
       read_contents = config_file.readlines()
     
     self._contents_lock.acquire()
@@ -246,32 +252,36 @@ class Config():
         
         self.set(key, value, False)
     
-    self._path = path
     self._contents_lock.release()
   
-  # TODO: pending improvements...
-  # - integ testing 
   def save(self, path = None):
     """
     Saves configuration contents to the config file or to the path
-    specified.
-
-    If path is not None, then the default path for this config handler
-    updated to the argument passed.
+    specified. If a path is provided then it replaces the configuration
+    location that we track.
+    
+    Arguments:
+      path (str) - location to be saved to
+    
+    Raises:
+      ValueError if we don't have a default path and none was provided
     """
     
-    self._contents_lock.acquire()
-
     if path:
-      self.path = path
-      
+      self._path = path
+    elif not self._path:
+      raise ValueError("Unable to save configuration: no path provided")
+    
+    self._contents_lock.acquire()
+    
     with open(self._path, 'w') as output_file:
       for entry_key in sorted(self.keys()):
         for entry_value in self.get_value(entry_key, multiple = True):
           # check for multi line entries
           if "\n" in entry_value: entry_value = "\n|" + entry_value.replace("\n", "\n|")
+          
           output_file.write('%s %s\n' % (entry_key, entry_value))
-                    
+    
     self._contents_lock.release()
   
   def clear(self):
@@ -281,7 +291,6 @@ class Config():
     """
     
     self._contents_lock.acquire()
-    self._path = None
     self._contents.clear()
     self._raw_contents = []
     self._requested_keys = set()
diff --git a/test/integ/util/conf.py b/test/integ/util/conf.py
index 314fa80..12d31b8 100644
--- a/test/integ/util/conf.py
+++ b/test/integ/util/conf.py
@@ -39,6 +39,12 @@ multiline.entry.squashed_bottom
 |and a ho hum
 """
 
+HERALD_POEM = """
+What a beautiful morning,
+what a beautiful day.
+Why are those arrows",
+coming my way?!?"""
+
 def _get_test_config_path():
   return os.path.join(test.runner.get_runner().get_test_dir(), "integ_test_cfg")
 
@@ -104,47 +110,26 @@ class TestConf(unittest.TestCase):
       self.assertEquals("la de da\nand a ho hum", test_config.get("multiline.entry.%s" % entry))
     
     self.assertEquals("", test_config.get("multiline.entry.empty"))
-
-  def test_save_multiline(self):
+  
+  def test_save(self):
     """
-    Tests the save method with multi-line configuration files.
+    Saves then reloads a configuration with several types of values.
     """
-
-    test_config_path = _make_config(MULTILINE_CONF)
-    test_config = stem.util.conf.get_config("integ_testing")
-    test_config.load(test_config_path)
-
-    test_config.save()
-    test_config.clear()
-
+    
+    # makes a configuration with a variety of types
     test_config = stem.util.conf.get_config("integ_testing")
-    test_config.load(test_config_path)
-
-    for entry in ("simple", "leading_whitespace", "squashed_top", "squashed_bottom"):
-      self.assertEquals("la de da\nand a ho hum", test_config.get("multiline.entry.%s" % entry))
     
-    self.assertEquals("", test_config.get("multiline.entry.empty"))
-
-  def test_save_singleline(self):
-    """
-    Tests the save method with mingle-line configuration files.
-    """
-    ssh_config = {"login.user": "atagar",
-                  "login.password": "pepperjack_is_awesome!",
-                  "destination.ip": "127.0.0.1",
-                  "destination.port": 22,
-                  "startup.run": []}
+    test_config.set("single_value", "yup, I'm there")
+    test_config.set("multiple_values", "a", False)
+    test_config.set("multiple_values", "b", False)
+    test_config.set("multiple_values", "c", False)
+    test_config.set("multiline_value", HERALD_POEM)
     
-    test_config_path = _make_config(EXAMPLE_CONF)
-    user_config = stem.util.conf.get_config("integ_testing")
-    user_config.load(test_config_path)
-        
-    user_config.set("destination.port", '22')
-    user_config.set("destination.ip", "127.0.0.1")
-
-    user_config.save()
-    user_config.clear()
-    user_config.load(test_config_path)
+    test_config.save(_get_test_config_path())
+    test_config.clear()
+    test_config.load()
+    
+    self.assertEquals("yup, I'm there", test_config.get_value("single_value"))
+    self.assertEquals(["a", "b", "c"], test_config.get_value("multiple_values", multiple = True))
+    self.assertEquals(HERALD_POEM, test_config.get_value("multiline_value"))
 
-    self.assertEquals('22', user_config.get("destination.port"))
-    self.assertEquals("127.0.0.1", user_config.get("destination.ip"))



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits