configuration: converter() -> get_converter()
This commit is contained in:
parent
f0dc21ec0c
commit
f2614469c5
2 changed files with 18 additions and 18 deletions
|
@ -53,7 +53,7 @@ class OptionBase:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def converter(self, *args, **kwargs):
|
def get_converter(self, *args, **kwargs):
|
||||||
raise NotImplementedError('Subclass should implement this method')
|
raise NotImplementedError('Subclass should implement this method')
|
||||||
|
|
||||||
def translate(self, val):
|
def translate(self, val):
|
||||||
|
@ -83,7 +83,7 @@ class StringOption(OptionBase):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def string_converter(val):
|
def string_converter(val):
|
||||||
return self.translate(str(val))
|
return self.translate(str(val))
|
||||||
return string_converter
|
return string_converter
|
||||||
|
@ -101,7 +101,7 @@ class BooleanOption(OptionBase):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def boolean_converter(val):
|
def boolean_converter(val):
|
||||||
v = val.lower()
|
v = val.lower()
|
||||||
if v not in self.BOOLEAN_STATES:
|
if v not in self.BOOLEAN_STATES:
|
||||||
|
@ -118,7 +118,7 @@ class IntOption(OptionBase):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def int_converter(val):
|
def int_converter(val):
|
||||||
return self.translate(int(val))
|
return self.translate(int(val))
|
||||||
return int_converter
|
return int_converter
|
||||||
|
@ -129,9 +129,9 @@ class LowerBoundIntOption(IntOption):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.lower_bound = lower_bound
|
self.lower_bound = lower_bound
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def lower_bound_converter(val):
|
def lower_bound_converter(val):
|
||||||
converted = super(LowerBoundIntOption, self).converter()(val)
|
converted = super(LowerBoundIntOption, self).get_converter()(val)
|
||||||
if converted < self.lower_bound:
|
if converted < self.lower_bound:
|
||||||
raise ValueError("Invalid {} value: {} < {}".format(
|
raise ValueError("Invalid {} value: {} < {}".format(
|
||||||
self.name, val, self.lower_bound))
|
self.name, val, self.lower_bound))
|
||||||
|
@ -161,7 +161,7 @@ class SelectOption(OptionBase):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.valid_values = valid_values
|
self.valid_values = valid_values
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def select_converter(val):
|
def select_converter(val):
|
||||||
if val in self.valid_values:
|
if val in self.valid_values:
|
||||||
return self.translate(val)
|
return self.translate(val)
|
||||||
|
@ -186,7 +186,7 @@ class DelimitedValuesOption(OptionBase):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.delimiter = delimiter
|
self.delimiter = delimiter
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def delimited_values_converter(val):
|
def delimited_values_converter(val):
|
||||||
vals = re.split(r'(?<!\\)' + self.delimiter, val)
|
vals = re.split(r'(?<!\\)' + self.delimiter, val)
|
||||||
vals = [x for x in vals if x]
|
vals = [x for x in vals if x]
|
||||||
|
@ -220,7 +220,7 @@ class LogLevelOption(OptionBase):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__('__cdist_log_level')
|
super().__init__('__cdist_log_level')
|
||||||
|
|
||||||
def converter(self):
|
def get_converter(self):
|
||||||
def log_level_converter(val):
|
def log_level_converter(val):
|
||||||
try:
|
try:
|
||||||
val = logging.getLevelName(int(val))
|
val = logging.getLevelName(int(val))
|
||||||
|
@ -374,7 +374,7 @@ class Configuration(metaclass=Singleton):
|
||||||
raise ValueError("Invalid option: {}.".format(option))
|
raise ValueError("Invalid option: {}.".format(option))
|
||||||
|
|
||||||
option_object = self.CONFIG_FILE_OPTIONS[section][option]
|
option_object = self.CONFIG_FILE_OPTIONS[section][option]
|
||||||
converter = option_object.converter()
|
converter = option_object.get_converter()
|
||||||
val = config_parser[section][option]
|
val = config_parser[section][option]
|
||||||
newval = converter(val)
|
newval = converter(val)
|
||||||
d[section][option] = newval
|
d[section][option] = newval
|
||||||
|
@ -392,7 +392,7 @@ class Configuration(metaclass=Singleton):
|
||||||
opt = self.ENV_VAR_OPTIONS[option]
|
opt = self.ENV_VAR_OPTIONS[option]
|
||||||
else:
|
else:
|
||||||
opt = self.CONFIG_FILE_OPTIONS[section][dst_opt]
|
opt = self.CONFIG_FILE_OPTIONS[section][dst_opt]
|
||||||
converter = opt.converter()
|
converter = opt.get_converter()
|
||||||
val = env[option]
|
val = env[option]
|
||||||
newval = converter(val)
|
newval = converter(val)
|
||||||
d[dst_opt] = newval
|
d[dst_opt] = newval
|
||||||
|
|
|
@ -72,7 +72,7 @@ class ConfigurationOptionsTestCase(test.CdistTestCase):
|
||||||
option = cc.StringOption('test')
|
option = cc.StringOption('test')
|
||||||
self.assertIsNone(option.translate(''))
|
self.assertIsNone(option.translate(''))
|
||||||
self.assertEqual(option.translate('spam'), 'spam')
|
self.assertEqual(option.translate('spam'), 'spam')
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
self.assertEqual(converter('spam'), 'spam')
|
self.assertEqual(converter('spam'), 'spam')
|
||||||
self.assertIsNone(converter(''))
|
self.assertIsNone(converter(''))
|
||||||
|
|
||||||
|
@ -81,21 +81,21 @@ class ConfigurationOptionsTestCase(test.CdistTestCase):
|
||||||
for x in cc.BooleanOption.BOOLEAN_STATES:
|
for x in cc.BooleanOption.BOOLEAN_STATES:
|
||||||
self.assertEqual(option.translate(x),
|
self.assertEqual(option.translate(x),
|
||||||
cc.BooleanOption.BOOLEAN_STATES[x])
|
cc.BooleanOption.BOOLEAN_STATES[x])
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
self.assertRaises(ValueError, converter, 'of')
|
self.assertRaises(ValueError, converter, 'of')
|
||||||
for x in cc.BooleanOption.BOOLEAN_STATES:
|
for x in cc.BooleanOption.BOOLEAN_STATES:
|
||||||
self.assertEqual(converter(x), cc.BooleanOption.BOOLEAN_STATES[x])
|
self.assertEqual(converter(x), cc.BooleanOption.BOOLEAN_STATES[x])
|
||||||
|
|
||||||
def test_IntOption(self):
|
def test_IntOption(self):
|
||||||
option = cc.IntOption('test')
|
option = cc.IntOption('test')
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
self.assertRaises(ValueError, converter, 'x')
|
self.assertRaises(ValueError, converter, 'x')
|
||||||
for x in range(-5, 10):
|
for x in range(-5, 10):
|
||||||
self.assertEqual(converter(str(x)), x)
|
self.assertEqual(converter(str(x)), x)
|
||||||
|
|
||||||
def test_LowerBoundIntOption(self):
|
def test_LowerBoundIntOption(self):
|
||||||
option = cc.LowerBoundIntOption('test', -1)
|
option = cc.LowerBoundIntOption('test', -1)
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
self.assertRaises(ValueError, converter, -2)
|
self.assertRaises(ValueError, converter, -2)
|
||||||
for x in range(-1, 10):
|
for x in range(-1, 10):
|
||||||
self.assertEqual(converter(str(x)), x)
|
self.assertEqual(converter(str(x)), x)
|
||||||
|
@ -112,21 +112,21 @@ class ConfigurationOptionsTestCase(test.CdistTestCase):
|
||||||
def test_SelectOption(self):
|
def test_SelectOption(self):
|
||||||
valid_values = ('spam', 'eggs', 'ham', )
|
valid_values = ('spam', 'eggs', 'ham', )
|
||||||
option = cc.SelectOption('test', valid_values)
|
option = cc.SelectOption('test', valid_values)
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
self.assertRaises(ValueError, converter, 'spamspam')
|
self.assertRaises(ValueError, converter, 'spamspam')
|
||||||
for x in valid_values:
|
for x in valid_values:
|
||||||
self.assertEqual(converter(x), x)
|
self.assertEqual(converter(x), x)
|
||||||
|
|
||||||
def test_DelimitedValuesOption(self):
|
def test_DelimitedValuesOption(self):
|
||||||
option = cc.DelimitedValuesOption('test', ':')
|
option = cc.DelimitedValuesOption('test', ':')
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
value = 'spam:eggs::ham'
|
value = 'spam:eggs::ham'
|
||||||
self.assertEqual(converter(value), ['spam', 'eggs', 'ham', ])
|
self.assertEqual(converter(value), ['spam', 'eggs', 'ham', ])
|
||||||
self.assertIsNone(converter(''))
|
self.assertIsNone(converter(''))
|
||||||
|
|
||||||
def test_LogLevelOption(self):
|
def test_LogLevelOption(self):
|
||||||
option = cc.LogLevelOption()
|
option = cc.LogLevelOption()
|
||||||
converter = option.converter()
|
converter = option.get_converter()
|
||||||
value = str(logging.DEBUG)
|
value = str(logging.DEBUG)
|
||||||
conv_val = converter(value)
|
conv_val = converter(value)
|
||||||
self.assertEqual(conv_val, cap.VERBOSE_DEBUG)
|
self.assertEqual(conv_val, cap.VERBOSE_DEBUG)
|
||||||
|
|
Loading…
Reference in a new issue