Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
added has_changed to Option to tag modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
twobraids committed Jan 14, 2015
1 parent c384f6b commit 33f7f7b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions configman/config_manager.py
Expand Up @@ -639,6 +639,7 @@ def _overlay_expand(self):
# the value source. This assignment may come
# via acquisition, so the key given may not have
# been an exact match for what was returned.
opt.has_changed = opt.default != val_src_dict[key]
opt.default = val_src_dict[key]
if key in all_reference_values:
# make sure that this value gets propagated to keys
Expand Down
11 changes: 10 additions & 1 deletion configman/option.py
Expand Up @@ -68,6 +68,7 @@ def __init__(
not_for_definition=False,
reference_value_from=None,
secret=False,
has_changed=False,
):
self.name = name
self.short_form = short_form
Expand Down Expand Up @@ -95,6 +96,7 @@ def __init__(
self.not_for_definition = not_for_definition
self.reference_value_from = reference_value_from
self.secret = secret
self.has_changed = has_changed

#--------------------------------------------------------------------------
def __str__(self):
Expand Down Expand Up @@ -146,8 +148,11 @@ def set_value(self, val=None):
val = self.default
if isinstance(val, basestring):
try:
self.value = self.from_string_converter(val)
new_value = self.from_string_converter(val)
self.has_changed = new_value != self.value
self.value = new_value
except TypeError:
self.has_changed = val != self.value
self.value = val
except ValueError:
error_message = "In '%s', '%s' fails to convert '%s'" % (
Expand All @@ -157,10 +162,12 @@ def set_value(self, val=None):
)
raise CannotConvertError(error_message)
elif isinstance(val, Option):
self.has_changed = val.default != self.value
self.value = val.default
elif isinstance(val, collections.Mapping) and 'default' in val:
self.set_value(val["default"])
else:
self.has_changed = val != self.value
self.value = val

#--------------------------------------------------------------------------
Expand Down Expand Up @@ -192,6 +199,7 @@ class B(A):
if self.default is None or force:
self.default = val
self.set_value(val)
self.has_changed = True
else:
raise OptionError(
"cannot override existing default without using the 'force' "
Expand All @@ -216,6 +224,7 @@ def copy(self):
not_for_definition=self.not_for_definition,
reference_value_from=self.reference_value_from,
secret=self.secret,
has_changed=self.has_changed,
)
return o

Expand Down
47 changes: 47 additions & 0 deletions configman/tests/test_config_manager.py
Expand Up @@ -2510,3 +2510,50 @@ def __init__(inner_self, config):
self.assertEqual(statement.value,
'wilma married fred using password @$*$&26Ht '
'but divorced because of arg2.')

#--------------------------------------------------------------------------
def test_configmanger_set_has_changed_successfully(self):

n = config_manager.Namespace()
n.add_option(
name='dwight',
default=0
)
n.add_option(
name='wilma',
default=0
)
n.add_option(
name='sarita',
default=0
)
n.add_option(
name='robert',
default=0
)

config = config_manager.ConfigurationManager(
n,
[
{
"dwight": 20,
},
{
"dwight": 22,
"wilma": 10
},
{
"wilma": 0,
"robert": 16,
},
],
use_admin_controls=False,
use_auto_help=False,
argv_source=[]
)
self.assertTrue(config.option_definitions.dwight.has_changed)
self.assertFalse(config.option_definitions.wilma.has_changed)
self.assertFalse(config.option_definitions.sarita.has_changed)
self.assertTrue(config.option_definitions.robert.has_changed)


11 changes: 7 additions & 4 deletions configman/tests/test_val_for_configobj.py
Expand Up @@ -211,6 +211,9 @@ def test_write_ini(self):
n = self._some_namespaces()
c = ConfigurationManager(
[n],
[{
"c.fred": "just like George Jetson",
}],
use_admin_controls=True,
#use_config_files=False,
use_auto_help=False,
Expand All @@ -222,7 +225,7 @@ def test_write_ini(self):
[c]
# husband from Flintstones
#fred=stupid, deadly
fred=just like George Jetson
# wife from Flintstones
#wilma=waspish's
Expand Down Expand Up @@ -274,7 +277,7 @@ def test_write_ini_with_reference_value_froms(
'yyy': {
'password': 'dwight and wilma'
}
}
},
}
c = ConfigurationManager(
[n],
Expand All @@ -300,7 +303,7 @@ def test_write_ini_with_reference_value_froms(
[c]
# husband from Flintstones
#fred=stupid, deadly
#fred="stupid, deadly"
# wife from Flintstones
#wilma=waspish's
Expand Down Expand Up @@ -331,7 +334,7 @@ def test_write_ini_with_reference_value_froms(
# the password
# see "xxx.yyy.password" for the default or override it here
#password=dwight and wilma
password=dwight and wilma
""")
out = StringIO()
c.write_conf(for_configobj, opener=stringIO_context_wrapper(out))
Expand Down
7 changes: 6 additions & 1 deletion configman/value_sources/for_configobj.py
Expand Up @@ -250,11 +250,16 @@ def _write_ini(source_dict, namespace_name=None, level=0, indent_size=4,
)
)

if an_option.likely_to_be_changed:
if an_option.likely_to_be_changed or an_option.has_changed:
option_format = '%s%s=%s\n'
else:
option_format = '%s#%s=%s\n'

if isinstance(option_value, basestring) and ',' in option_value:
# quote lists unless they're already quoted
if option_value[0] not in '\'"':
option_value = '"%s"' % option_value

print >>output_stream, option_format % (
indent_spacer,
an_option.name,
Expand Down

0 comments on commit 33f7f7b

Please sign in to comment.