summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfuzzyman <devnull@localhost>2009-11-22 23:46:38 +0000
committerfuzzyman <devnull@localhost>2009-11-22 23:46:38 +0000
commitfba246701628e365bac82c7926e964adbd457967 (patch)
treeb94618feca9e3a13d2c557258a530718be3bdb45
parent4dbe21e088aa09c19a248e74e27296c86419766f (diff)
downloadconfigobj-fba246701628e365bac82c7926e964adbd457967.tar.gz
Addition of get_extra_values function and test.
-rw-r--r--configobj.py27
-rw-r--r--docs/configobj.txt2
-rw-r--r--functionaltests/test_validate_errors.py15
3 files changed, 40 insertions, 4 deletions
diff --git a/configobj.py b/configobj.py
index 10f4759..e4cffbc 100644
--- a/configobj.py
+++ b/configobj.py
@@ -115,6 +115,7 @@ __all__ = (
'UnreprError',
'UnknownType',
'flatten_errors',
+ 'get_extra_values'
)
DEFAULT_INTERPOLATION = 'configparser'
@@ -2366,9 +2367,7 @@ def flatten_errors(cfg, res, levels=None, results=None):
(This is a recursive function, so you shouldn't use the ``levels`` or
``results`` arguments - they are used by the function.)
- Returns a list of keys that failed. Each member of the list is a tuple :
-
- ::
+ Returns a list of keys that failed. Each member of the list is a tuple::
([list of sections...], key, result)
@@ -2478,4 +2477,26 @@ def flatten_errors(cfg, res, levels=None, results=None):
return results
+def get_extra_values(conf, _prepend=None):
+ """
+ Find all the values and sections not in the configspec from a validated
+ ConfigObj.
+
+ It returns a list of tuples where each tuple
+
+ NOTE: If you call ``get_extra_values`` on a ConfigObj instance that hasn't
+ been validated it will return an empty list.
+ """
+ out = []
+
+ if _prepend is None:
+ _prepend = ()
+
+ out.extend(_prepend + (name,) for name in conf.extra_values)
+ for name in conf.sections:
+ if name not in conf.extra_values:
+ out.extend(get_extra_values(conf[name], _prepend + (name,)))
+ return out
+
+
"""*A programming language is a medium of expression.* - Paul Graham"""
diff --git a/docs/configobj.txt b/docs/configobj.txt
index 2301430..4db2da5 100644
--- a/docs/configobj.txt
+++ b/docs/configobj.txt
@@ -2292,6 +2292,8 @@ From version 4 it lists all releases and changes.
* String interpolation now works in list value members
* After validation any additional entries not in the configspec are listed in
the ``extra_values`` section member
+* Addition of the ``get_extra_values`` function for finding all extra values
+ in a validated ConfigObj instance
* Deprecated the use of the ``options`` dictionary in the ConfigObj constructor
and added explicit keyword arguments instead. Use **options if you want
to initialise a ConfigObj instance from a dictionary
diff --git a/functionaltests/test_validate_errors.py b/functionaltests/test_validate_errors.py
index 06f855b..307ff53 100644
--- a/functionaltests/test_validate_errors.py
+++ b/functionaltests/test_validate_errors.py
@@ -1,6 +1,6 @@
import os
import unittest
-from configobj import ConfigObj
+from configobj import ConfigObj, get_extra_values
from validate import Validator
thisdir = os.path.dirname(os.path.join(os.getcwd(), __file__))
@@ -43,7 +43,20 @@ class TestValidateErrors(unittest.TestCase):
self.assertEqual(conf['section']['sub-section'].extra_values,
['extra'])
+
+ def test_get_extra_values(self):
+ conf = ConfigObj(inipath, configspec=specpath)
+
+ conf.validate(Validator(), preserve_errors=True)
+ extra_values = get_extra_values(conf)
+ expected = sorted([
+ ('extra',),
+ ('extra-section',),
+ ('section', 'sub-section', 'extra'),
+ ('section', 'extra-sub-section'),
+ ])
+ self.assertEqual(sorted(extra_values), expected)