提交 7ff47a81 编写于 作者: C Cleber Rosa

avocado/core/settings.py: allow any settings to use the home dir notation

It's common to use the tilde notation (`~/foo`) to refer to locations
inside the user directory.  Let's introduce a new settings key type,
called `path`, that will automatically expand the tilde notation
if one is found.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 e43e59cc
......@@ -107,6 +107,8 @@ def convert_value_type(value, value_type):
value_type = float
elif value_type == 'list':
value_type = list
elif value_type == 'path':
value_type = os.path.expanduser
if value_type is None:
value_type = str
......@@ -115,6 +117,8 @@ def convert_value_type(value, value_type):
if len(sval) == 0:
if value_type == str:
return ""
elif value_type == os.path.expanduser:
return ""
elif value_type == bool:
return False
elif value_type == int:
......@@ -225,16 +229,24 @@ class Settings(object):
Get value from key in a given config file section.
:param section: Config file section.
:type section: str
:param key: Config file key, relative to section.
:type key: str
:param key_type: Type of key.
It can be either of: str, int, float, bool, list
:type key_type: either string based names representing types,
including `str`, `int`, `float`, `bool`,
`list` and `path`, or the types themselves
limited to :class:`str`, :class:`int`,
:class:`float`, :class:`bool` and
:class:`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.
:raises: SettingsError, in case key is not set and no default
was provided.
"""
try:
val = self.config.get(section, key)
......
......@@ -16,6 +16,8 @@ float_key = 1.25
bool_key = True
list_key = ['I', 'love', 'settings']
empty_key =
path = ~/path/at/home
home_path = ~
"""
......@@ -41,6 +43,21 @@ class SettingsTest(unittest.TestCase):
def testBoolConversion(self):
self.assertTrue(self.settings.get_value('foo', 'bool_key', bool))
def testPathHomeDir(self):
raw_from_settings = '~/path/at/home'
path_from_settings = self.settings.get_value('foo', 'path', 'path')
home_str_from_settings = self.settings.get_value('foo', 'home_path', str)
self.assertEqual(path_from_settings[-13:],
raw_from_settings[-13:])
self.assertGreaterEqual(len(path_from_settings),
len(raw_from_settings))
self.assertEqual(os.path.expanduser(home_str_from_settings),
self.settings.get_value('foo', 'home_path', 'path'))
def testPathOnStrKey(self):
self.assertEqual(self.settings.get_value('foo', 'path', str),
'~/path/at/home')
def testListConversion(self):
self.assertEqual(self.settings.get_value('foo', 'list_key', list),
['I', 'love', 'settings'])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册