Add boolean as a get_value argument

This commit is contained in:
Stefano Rivera 2010-12-21 01:51:10 +02:00
parent 0ad5bd5174
commit 2c1e1c8727
3 changed files with 14 additions and 9 deletions

View File

@ -59,8 +59,8 @@ This specifies the preferred test\-builder, one of
.TP .TP
.B UBUNTUTOOLS_UPDATE_BUILDER .B UBUNTUTOOLS_UPDATE_BUILDER
Boolean. Whether or not to update the test\-builder before each test Whether or not to update the test\-builder before each test build.
build. .RB "One of " yes " or " no " (default).
.TP .TP
.B UBUNTUTOOLS_LPINSTANCE .B UBUNTUTOOLS_LPINSTANCE

View File

@ -67,7 +67,7 @@ class UDTConfig(object):
f.close() f.close()
return config return config
def get_value(self, key, default=None, compat_keys=[]): def get_value(self, key, default=None, boolean=False, compat_keys=[]):
"""Retrieve a value from the environment or configuration files. """Retrieve a value from the environment or configuration files.
keys are prefixed with the script name, falling back to UBUNTUTOOLS for keys are prefixed with the script name, falling back to UBUNTUTOOLS for
package-wide keys. package-wide keys.
@ -90,8 +90,11 @@ class UDTConfig(object):
for store in (os.environ, self.config): for store in (os.environ, self.config):
if k in store: if k in store:
value = store[k] value = store[k]
if value in ('yes', 'no'): if boolean:
value = value == 'yes' if value in ('yes', 'no'):
value = value == 'yes'
else:
continue
return value return value
return default return default

View File

@ -83,9 +83,9 @@ REPEAT=yes
'REPEAT': 'yes', 'REPEAT': 'yes',
}) })
def get_value(self, key, default=None, compat_keys=[]): def get_value(self, *args, **kwargs):
config = UDTConfig(prefix='TEST') config = UDTConfig(prefix='TEST')
return config.get_value(key, default=default, compat_keys=compat_keys) return config.get_value(*args, **kwargs)
def test_defaults(self): def test_defaults(self):
self.assertEqual(self.get_value('BUILDER'), 'pbuilder') self.assertEqual(self.get_value('BUILDER'), 'pbuilder')
@ -120,9 +120,11 @@ REPEAT=yes
def test_boolean(self): def test_boolean(self):
config_files['user'] = "TEST_BOOLEAN=yes" config_files['user'] = "TEST_BOOLEAN=yes"
self.assertEqual(self.get_value('BOOLEAN'), True) self.assertEqual(self.get_value('BOOLEAN', boolean=True), True)
config_files['user'] = "TEST_BOOLEAN=no" config_files['user'] = "TEST_BOOLEAN=no"
self.assertEqual(self.get_value('BOOLEAN'), False) self.assertEqual(self.get_value('BOOLEAN', boolean=True), False)
config_files['user'] = "TEST_BOOLEAN=true"
self.assertEqual(self.get_value('BOOLEAN', boolean=True), None)
def test_nonpackagewide(self): def test_nonpackagewide(self):
config_files['user'] = 'UBUNTUTOOLS_FOOBAR=a' config_files['user'] = 'UBUNTUTOOLS_FOOBAR=a'