diff options
author | Chmouel Boudjnah <chmouel@chmouel.com> | 2013-11-23 00:49:37 +0100 |
---|---|---|
committer | Chmouel Boudjnah <chmouel@enovance.com> | 2013-11-24 15:35:49 +0100 |
commit | b207aaca07733b1c5719e182c244906d20bf28b9 (patch) | |
tree | a86a8c61965329435b1701a476a9f8857b1f9623 /swiftbench/utils.py | |
parent | 64b976e139ecfeafa83889a9c6d6615d30ba5635 (diff) | |
download | swift-bench-b207aaca07733b1c5719e182c244906d20bf28b9.tar.gz |
Make swift-bench not depending on swift.
Remove the dependence on swift, import the only needed functions from
swift.common.utils to swiftbench.utils
Add tests for utils using mock instead.
Change-Id: I1b69dce750b55f3ee0e999fb5a7100cf811f7ebe
Diffstat (limited to 'swiftbench/utils.py')
-rw-r--r-- | swiftbench/utils.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/swiftbench/utils.py b/swiftbench/utils.py new file mode 100644 index 0000000..14611d9 --- /dev/null +++ b/swiftbench/utils.py @@ -0,0 +1,79 @@ +# Copyright (c) 2010-2013 OpenStack Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +from ConfigParser import ConfigParser, RawConfigParser + +# Used when reading config values +TRUE_VALUES = set(('true', '1', 'yes', 'on', 't', 'y')) + + +# NOTE(chmouel): Imported from swift without the modular directory feature. +def readconf(conf_path, section_name=None, log_name=None, defaults=None, + raw=False): + """ + Read config file(s) and return config items as a dict + + :param conf_path: path to config file, or a file-like object + (hasattr readline) + :param section_name: config section to read (will return all sections if + not defined) + :param log_name: name to be used with logging (will use section_name if + not defined) + :param defaults: dict of default values to pre-populate the config with + :returns: dict of config items + """ + if defaults is None: + defaults = {} + if raw: + c = RawConfigParser(defaults) + else: + c = ConfigParser(defaults) + if hasattr(conf_path, 'readline'): + c.readfp(conf_path) + else: + success = c.read(conf_path) + if not success: + print "Unable to read config from %s" % conf_path + sys.exit(1) + if section_name: + if c.has_section(section_name): + conf = dict(c.items(section_name)) + else: + print "Unable to find %s config section in %s" % \ + (section_name, conf_path) + sys.exit(1) + if "log_name" not in conf: + if log_name is not None: + conf['log_name'] = log_name + else: + conf['log_name'] = section_name + else: + conf = {} + for s in c.sections(): + conf.update({s: dict(c.items(s))}) + if 'log_name' not in conf: + conf['log_name'] = log_name + conf['__file__'] = conf_path + return conf + + +def config_true_value(value): + """ + Returns True if the value is either True or a string in TRUE_VALUES. + Returns False otherwise. + """ + return value is True or \ + (isinstance(value, basestring) and value.lower() in TRUE_VALUES) |