[tor-commits] [stem/master] Minor fixes for conf.save()

atagar at torproject.org atagar at torproject.org
Mon Jan 16 06:27:54 UTC 2012


commit 94479aadebcaeee26d57b91e4900dad56edaeb91
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 15 22:13:32 2012 -0800

    Minor fixes for conf.save()
    
    Sorting the keys prior to saving, handling multi-value keys, and trivial
    changes (whitespace, reordering, and putting the strip call in the try block
    which is the only place it's needed).
---
 stem/util/conf.py |   24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/stem/util/conf.py b/stem/util/conf.py
index 0c98206..199a20c 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -15,6 +15,7 @@ would be loaded as four entries, the last one's value being an empty string.
 get_config - Singleton for getting configurations
 Config - Custom configuration.
   |- load - reads a configuration file
+  |- save - writes the current configuration to a file
   |- clear - empties our loaded configuration contents
   |- update - replaces mappings in a dictionary with the config's values
   |- keys - provides keys in the loaded configuration
@@ -166,16 +167,29 @@ class Config():
       if line:
         try:
           key, value = line.split(" ", 1)
+          value = value.strip()
         except ValueError:
           log.debug("Config entry '%s' is expected to be of the format 'Key Value', defaulting to '%s' -> ''" % (line, line))
           key, value = line, ""
-        value = value.strip()
         
         self.set(key, value)
     
     self._path = path
     self._contents_lock.release()
   
+  def save(self):
+    self._contents_lock.acquire()
+    
+    config_keys = self.keys()
+    config_keys.sort()
+    
+    with open(path, 'w') as f:
+      for entry_key in config_keys:
+        for entry_value in self.get_value(entry_key, multiple = True):
+          f.write('%s %s\n' % (entry_key, entry_value))
+    
+    self._contents_lock.release()
+  
   def clear(self):
     """
     Drops the configuration contents and reverts back to a blank, unloaded
@@ -439,11 +453,3 @@ class Config():
       return default
     else: return [int(val) for val in conf_comp]
 
-  def save(self):
-    self._contents_lock.acquire()
-
-    with open(path, 'w') as f:
-      for entry in self.keys():
-        f.write('%s %s\n' % (entry, self.get(entry)))
-
-    self._contents_lock.release()



More information about the tor-commits mailing list