From 7ff47a81615cc9ea866c00e96c807bddc3782996 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Thu, 11 Aug 2016 14:52:12 -0300 Subject: [PATCH] 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: Cleber Rosa --- avocado/core/settings.py | 16 ++++++++++++++-- selftests/unit/test_settings.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/avocado/core/settings.py b/avocado/core/settings.py index cd7dc7bc..278781e0 100644 --- a/avocado/core/settings.py +++ b/avocado/core/settings.py @@ -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) diff --git a/selftests/unit/test_settings.py b/selftests/unit/test_settings.py index 7b01b053..83ba2459 100644 --- a/selftests/unit/test_settings.py +++ b/selftests/unit/test_settings.py @@ -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']) -- GitLab