commit 93e1e8a1c4c5c52f09a9b974f868a223a1cda1ca Author: Damian Johnson atagar@torproject.org Date: Tue Jan 1 14:03:53 2013 -0800
Dropping Config's get_int_csv() and get_str_csv()
Many, many moons ago when arm first began it used integer enumerations (as is often the case for python projects). I added get_int_csv() to allow users to configure those enums, and later added get_str_csv() when we moved to our Enum class.
Long story short, both are legacy methods for a bygone age before we had our new config_dict() pattern. We don't use it, and now neither does arm. Time to drop the cruft. --- stem/util/conf.py | 85 +----------------------------------------------- test/unit/util/conf.py | 20 ----------- 2 files changed, 1 insertions(+), 104 deletions(-)
diff --git a/stem/util/conf.py b/stem/util/conf.py index 21106bf..e8d35b6 100644 --- a/stem/util/conf.py +++ b/stem/util/conf.py @@ -131,9 +131,7 @@ Alternatively you can get a read-only dictionary that stays in sync with the |- set - sets the given key/value pair |- unused_keys - provides keys that have never been requested |- get - provides the value for a given key, with type inference - |- get_value - provides the value for a given key as a string - |- get_str_csv - gets a value as a comma separated list of strings - +- get_int_csv - gets a value as a comma separated list of integers + +- get_value - provides the value for a given key as a string """
from __future__ import with_statement @@ -655,85 +653,4 @@ class Config(object): message_id = "stem.util.conf.missing_config_key_%s" % key log.log_once(message_id, log.TRACE, "config entry '%s' not found, defaulting to '%s'" % (key, default)) return default - - def get_str_csv(self, key, default = None, count = None, sub_key = None): - """ - Fetches the given key as a comma separated value. - - :param str key: config setting to be fetched, last if multiple exists - :param object default: value provided if no such key exists or doesn't - match the count - :param int count: if set then the default is returned when the number of - elements doesn't match this value - :param str sub_key: handle the configuration entry as a dictionary and use - this key within it - - :returns: **list** with the stripped values - """ - - if sub_key: conf_value = self.get(key, {}).get(sub_key) - else: conf_value = self.get_value(key) - - if conf_value is None: return default - elif not conf_value.strip(): return [] # empty string - else: - conf_comp = [entry.strip() for entry in conf_value.split(",")] - - # check if the count doesn't match - if count is not None and len(conf_comp) != count: - msg = "Config entry '%s' is expected to be %i comma separated values" % (key, count) - if default is not None and (isinstance(default, list) or isinstance(default, tuple)): - defaultStr = ", ".join([str(i) for i in default]) - msg += ", defaulting to '%s'" % defaultStr - - log.debug(msg) - return default - - return conf_comp - - def get_int_csv(self, key, default = None, count = None, min_value = None, max_value = None, sub_key = None): - """ - Fetches the given comma separated value, returning the default if the - values aren't integers or don't follow the given constraints. - - :param str key: config setting to be fetched, last if multiple exists - :param object default: value provided if no such key exists, doesn't match - the count, values aren't all integers, or doesn't match the bounds - :param int count: checks that the number of values matches this if set - :param int min_value: checks that all values are over this if set - :param int max_value: checks that all values are under this if set - :param str sub_key: handle the configuration entry as a dictionary and use - this key within it - - :returns: **list** with the stripped values - """ - - conf_comp = self.get_str_csv(key, default, count, sub_key) - if conf_comp == default: return default - - # validates the input, setting the error_msg if there's a problem - error_msg = None - base_error_msg = "Config entry '%s' is expected to %%s" % key - - # includes our default value in the message - if default is not None and (isinstance(default, list) or isinstance(default, tuple)): - default_str = ", ".join([str(i) for i in default]) - base_error_msg += ", defaulting to '%s'" % default_str - - for val in conf_comp: - if not val.isdigit(): - error_msg = base_error_msg % "only have integer values" - break - else: - if min_value is not None and int(val) < min_value: - error_msg = base_error_msg % "only have values over %i" % min_value - break - elif max_value is not None and int(val) > max_value: - error_msg = base_error_msg % "only have values less than %i" % max_value - break - - if error_msg: - log.debug(error_msg) - return default - else: return [int(val) for val in conf_comp]
diff --git a/test/unit/util/conf.py b/test/unit/util/conf.py index 36afe10..f3ce915 100644 --- a/test/unit/util/conf.py +++ b/test/unit/util/conf.py @@ -266,24 +266,4 @@ class TestConf(unittest.TestCase):
self.assertEquals(None, test_config.get_value("foo")) self.assertEquals("hello", test_config.get_value("foo", "hello")) - - def test_csv(self): - """ - Tests the get_str_csv and get_int_csv methods. - """ - - test_config = stem.util.conf.get_config("unit_testing") - test_config.set("str_csv_value", "hello, world") - test_config.set("int_csv_value", "1, 2, 3") - test_config.set("not_a_csv_value", "blarg I say!") - - self.assertEquals(["hello", "world"], test_config.get_str_csv("str_csv_value")) - self.assertEquals(["1", "2", "3"], test_config.get_str_csv("int_csv_value")) - self.assertEquals(["blarg I say!"], test_config.get_str_csv("not_a_csv_value")) - self.assertEquals(None, test_config.get_str_csv("not_a_csv_value", count = 5)) - - self.assertEquals(None, test_config.get_int_csv("str_csv_value")) - self.assertEquals([1, 2, 3], test_config.get_int_csv("int_csv_value")) - self.assertEquals(None, test_config.get_int_csv("int_csv_value", min_value = 4)) - self.assertEquals(None, test_config.get_int_csv("not_a_csv_value"))