avocado.settings: Introduce docstrings

Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 95c2336f
...@@ -18,15 +18,27 @@ config_path_intree = os.path.join(_config_path_intree, config_filename) ...@@ -18,15 +18,27 @@ config_path_intree = os.path.join(_config_path_intree, config_filename)
class SettingsError(Exception): class SettingsError(Exception):
"""
Base settings error.
"""
pass pass
class SettingsValueError(SettingsError): class SettingsValueError(SettingsError):
"""
Error thrown when we could not convert successfully a key to a value.
"""
pass pass
class ConfigFileNotFound(SettingsError): class ConfigFileNotFound(SettingsError):
"""
Error thrown when the main settings file could not be found.
"""
def __init__(self, path_list): def __init__(self, path_list):
super(ConfigFileNotFound, self).__init__() super(ConfigFileNotFound, self).__init__()
self.path_list = path_list self.path_list = path_list
...@@ -79,9 +91,18 @@ def convert_value_type(key, section, value, value_type): ...@@ -79,9 +91,18 @@ def convert_value_type(key, section, value, value_type):
class Settings(object): class Settings(object):
"""
Simple wrapper around ConfigParser, with a key type conversion available.
"""
no_default = object() no_default = object()
def __init__(self, config_path=None): def __init__(self, config_path=None):
"""
Constructor. Tries to find the main settings file and load it.
:param config_path: Path to a config file. Useful for unittesting.
"""
self.config = ConfigParser.ConfigParser() self.config = ConfigParser.ConfigParser()
self.intree = False self.intree = False
if config_path is None: if config_path is None:
...@@ -105,6 +126,17 @@ class Settings(object): ...@@ -105,6 +126,17 @@ class Settings(object):
self.config.read(self.config_path) self.config.read(self.config_path)
def _handle_no_value(self, section, key, default): def _handle_no_value(self, section, key, default):
"""
What to do if key in section has no value.
:param section: Config file section.
:param key: Config file key, relative to section.
:param default: Default value for key, in case it does not exist.
:returns: Default value, if a default value was provided.
:raises: SettingsError, in case no default was provided.
"""
if default is self.no_default: if default is self.no_default:
msg = ("Value '%s' not found in section '%s'" % msg = ("Value '%s' not found in section '%s'" %
(key, section)) (key, section))
...@@ -114,6 +146,21 @@ class Settings(object): ...@@ -114,6 +146,21 @@ class Settings(object):
def get_value(self, section, key, key_type=str, default=no_default, def get_value(self, section, key, key_type=str, default=no_default,
allow_blank=False): allow_blank=False):
"""
Get value from key in a given config file section.
:param section: Config file section.
:param key: Config file key, relative to section.
:param key_type: Type of key.
It can be either of: str, int, float, bool, list
:param default: Default value for the key, if none found.
:param allow_blank: Whether an empty value for the key is allowed.
:returns: value, if one available in the config.
default value, if one provided.
:raises: SettingsError, in case no default was provided.
"""
try: try:
val = self.config.get(section, key) val = self.config.get(section, key)
except ConfigParser.Error: except ConfigParser.Error:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册