Do not recreate properties in Config class - fixes the home dir expansion

This commit is contained in:
Jan Kaluza
2017-05-09 14:20:06 +02:00
parent f1fc573821
commit 22db6bb64a
2 changed files with 20 additions and 15 deletions

View File

@@ -350,22 +350,23 @@ class Config(object):
raise Exception("Configuration item's name is not allowed: %s" % key)
# Create the empty self._key attribute, so we can assign to it.
setattr(self, "_" + key, None)
if not hasattr(self, "_" + key):
setattr(self, "_" + key, None)
# Create self.key property to access the self._key attribute.
# Use the setifok_func if available for the attribute.
setifok_func = '_setifok_{}'.format(key)
if hasattr(self, setifok_func):
setx = lambda self, val: getattr(self, setifok_func)(val)
elif value_type == Path:
# For paths, expanduser.
setx = lambda self, val: setattr(
self, "_" + key, os.path.expanduser(val))
else:
setx = lambda self, val: setattr(self, "_" + key, val)
getx = lambda self: getattr(self, "_" + key)
delx = lambda self: delattr(self, "_" + key)
setattr(Config, key, property(getx, setx, delx))
# Create self.key property to access the self._key attribute.
# Use the setifok_func if available for the attribute.
setifok_func = '_setifok_{}'.format(key)
if hasattr(self, setifok_func):
setx = lambda self, val: getattr(self, setifok_func)(val)
elif value_type == Path:
# For paths, expanduser.
setx = lambda self, val: setattr(
self, "_" + key, os.path.expanduser(val))
else:
setx = lambda self, val: setattr(self, "_" + key, val)
getx = lambda self: getattr(self, "_" + key)
delx = lambda self: delattr(self, "_" + key)
setattr(Config, key, property(getx, setx, delx))
# managed/registered configuration items
if key in self._defaults:

View File

@@ -35,3 +35,7 @@ class TestConfig(unittest.TestCase):
test_dir = "~/modulebuild/builds"
conf.mock_resultsdir = test_dir
self.assertEqual(conf.mock_resultsdir, os.path.expanduser(test_dir))
test_dir = "~/modulebuild/builds"
conf.cache_dir = test_dir
self.assertEqual(conf.cache_dir, os.path.expanduser(test_dir))