|
From: Arve K. <arv...@gm...> - 2008-01-06 21:37:06
|
Hello
I found I missed the ability to restore entries to their default value in
ConfigObj, so that you can differentiate between actualy deleting an entry
and just restoring it to the default state. My suggestion is to track
sections' default values with an additional attribute "default_values", and
to add a method "restore_default" for restoring a particular entry. I also
considered restoring default values as part of __delitem__, but figured this
might be too implicit and breaking backwards compatibility.
I hope to see this integrated with ConfigObj upstream as I need it in my own
project :_)
--- configobj.py_orig 2008-01-06 22:33:58.000000000 +0100
+++ configobj.py 2008-01-06 22:34:29.000000000 +0100
@@ -498,7 +498,8 @@
self._cs_section_comments = {}
self._cs_section_inline_comments = {}
# for defaults
- self.defaults = []
+ self.defaults = [] # Entries that have their default value
+ self.default_values = {} # The original default values
#
# we do this explicitly so that __setitem__ is used properly
# (rather than just passing to ``dict.__init__``)
@@ -751,6 +752,18 @@
else:
self[key] = val
+ def restore_default(self, key):
+ """
+ Restore default value.
+
+ If there is no default value for this key, ValueError is raised.
+ """
+ if key not in self.default_values:
+ raise ValueError(key)
+ if key not in self.defaults:
+ self.defaults.append(key)
+ dict.__setitem__(self, key, self.default_values[key])
+
def rename(self, oldkey, newkey):
"""
Change a keyname to another, without changing position in sequence.
@@ -2103,6 +2116,7 @@
section[entry] = check
if not copy and missing and entry not in section.defaults:
section.defaults.append(entry)
+ section.default_values[entry] = check
#
# Missing sections will have been created as empty ones when the
# configspec was read.
Cheers,
Arve
|