commit 6c5f0205682877c76d6633abce487673787f5d9b Author: Sathyanarayanan Gunasekaran gsathya.ceg@gmail.com Date: Sat Jan 14 21:32:31 2012 +0530
Minor update to stem/util/conf.py
- More pythonic way of doing File I/O - Use try and catch the exception while parsing the config file instead of using an if-else logic. - Log messages when the (key, value) are not provided in the proper format in the config file. - Typos --- stem/util/conf.py | 14 +++++++------- 1 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/stem/util/conf.py b/stem/util/conf.py index 4c468d7..c226f40 100644 --- a/stem/util/conf.py +++ b/stem/util/conf.py @@ -150,9 +150,8 @@ class Config(): permissions, etc) """
- config_file = open(path, "r") - read_contents = config_file.readlines() - config_file.close() + with open(path, "r") as config_file: + read_contents = config_file.readlines()
self._contents_lock.acquire() self._raw_contents = read_contents @@ -165,11 +164,12 @@ class Config():
# parse the key/value pair if line: - if " " in line: + try: key, value = line.split(" ", 1) - value = value.strip() - else: + 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)
@@ -333,7 +333,7 @@ class Config():
def get_value(self, key, default = None, multiple = False): """ - This provides the currently value associated with a given key. + This provides the current value associated with a given key.
Arguments: key (str) - config setting to be fetched