From fba246701628e365bac82c7926e964adbd457967 Mon Sep 17 00:00:00 2001 From: fuzzyman Date: Sun, 22 Nov 2009 23:46:38 +0000 Subject: Addition of get_extra_values function and test. --- configobj.py | 27 ++++++++++++++++++++++++--- docs/configobj.txt | 2 ++ functionaltests/test_validate_errors.py | 15 ++++++++++++++- 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) -- cgit v1.2.1