[tor-commits] [stem/master] Expanding Config class' set() method
atagar at torproject.org
atagar at torproject.org
Mon Jan 23 05:56:18 UTC 2012
commit a0be1932e96e9e52a12a4bbc49e72a059c005f78
Author: Damian Johnson <atagar at torproject.org>
Date: Sun Jan 22 13:16:01 2012 -0800
Expanding Config class' set() method
Users of the set method would usually expect it to overwrite our current
configuration value rather than append to it (thanks to Sathyanarayanan for the
catch). Also expanding it to handle list or tuple values.
---
stem/util/conf.py | 21 +++++++++++++++------
1 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/stem/util/conf.py b/stem/util/conf.py
index 132dbce..d4e9775 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -172,7 +172,7 @@ class Config():
log.debug("Config entry '%s' is expected to be of the format 'Key Value', defaulting to '%s' -> ''" % (line, line))
key, value = line, ""
- self.set(key, value)
+ self.set(key, value, False)
self._path = path
self._contents_lock.release()
@@ -261,18 +261,27 @@ class Config():
return set(self.get_keys()).difference(self._requested_keys)
- def set(self, key, value):
+ def set(self, key, value, overwrite = True):
"""
Appends the given key/value configuration mapping, behaving the same as if
we'd loaded this from a configuration file.
Arguments:
- key (str) - key for the configuration mapping
- value (str) - value we're setting the mapping to
+ key (str) - key for the configuration mapping
+ value (str or list) - value we're setting the mapping to
+ overwrite (bool) - replaces the previous value if true, otherwise
+ the values are appended
"""
- if key in self._contents: self._contents[key].append(value)
- else: self._contents[key] = [value]
+ if isinstance(value, str):
+ if not overwrite and key in self._contents: self._contents[key].append(value)
+ else: self._contents[key] = [value]
+ elif isinstance(value, list) or isinstance(value, tuple):
+ if not overwrite and key in self._contents:
+ self._contents[key] += value
+ else: self._contents[key] = value
+ else:
+ raise ValueError("Config.set() only accepts str, list, or tuple. Provided value was a '%s'" % type(value))
def get(self, key, default = None):
"""
More information about the tor-commits
mailing list