easybuild.tools.configobj module¶
-
exception
easybuild.tools.configobj.ConfigObjError(message='', line_number=None, line='')¶ Bases:
SyntaxErrorThis is the base class for all errors that ConfigObj raises. It is a subclass of SyntaxError.
-
exception
easybuild.tools.configobj.NestingError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorThis error indicates a level of nesting that doesn’t match.
-
exception
easybuild.tools.configobj.ParseError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorThis error indicates that a line is badly written. It is neither a valid
key = valueline, nor a valid section marker line.
-
exception
easybuild.tools.configobj.DuplicateError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorThe keyword or section specified already exists.
-
exception
easybuild.tools.configobj.ConfigspecError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorAn error occurred whilst parsing a configspec.
-
class
easybuild.tools.configobj.ConfigObj(infile=None, options=None, configspec=None, encoding=None, interpolation=True, raise_errors=False, list_values=True, create_empty=False, file_error=False, stringify=True, indent_type=None, default_encoding=None, unrepr=False, write_empty_values=False, _inspec=False)¶ Bases:
easybuild.tools.configobj.SectionAn object to read, create, and write config files.
-
reload()¶ Reload a ConfigObj from file.
This method raises a
ReloadErrorif the ConfigObj doesn’t have a filename attribute pointing to a file.
-
reset()¶ Clear ConfigObj instance and restore to ‘freshly created’ state.
-
validate(validator, preserve_errors=False, copy=False, section=None)¶ Test the ConfigObj against a configspec.
It uses the
validatorobject from validate.py.To run
validateon the current ConfigObj, call:test = config.validate(validator)
(Normally having previously passed in the configspec when the ConfigObj was created - you can dynamically assign a dictionary of checks to the
configspecattribute of a section though).It returns
Trueif everything passes, or a dictionary of pass/fails (True/False). If every member of a subsection passes, it will just have the valueTrue. (It also returnsFalseif all members fail).In addition, it converts the values from strings to their native types if their checks pass (and
stringifyis set).If
preserve_errorsisTrue(Falseis default) then instead of a marking a fail with aFalse, it will preserve the actual exception object. This can contain info about the reason for failure. For example theVdtValueTooSmallErrorindicates that the value supplied was too small. If a value (or section) is missing it will still be marked asFalse.You must have the validate module to use
preserve_errors=True.You can then use the
flatten_errorsfunction to turn your nested results dictionary into a flattened list of failures - useful for displaying meaningful error messages.
-
write(outfile=None, section=None)¶ Write the current ConfigObj as a file
tekNico: FIXME: use StringIO instead of real files
>>> filename = a.filename >>> a.filename = 'test.ini' >>> a.write() >>> a.filename = filename >>> a == ConfigObj('test.ini', raise_errors=True) 1 >>> import os >>> os.remove('test.ini')
-
-
class
easybuild.tools.configobj.SimpleVal¶ Bases:
objectA simple validator. Can be used to check that all members expected are present.
To use it, provide a configspec with all your members in (the value given will be ignored). Pass an instance of
SimpleValto thevalidatemethod of yourConfigObj.validatewill returnTrueif all members are present, or a dictionary with True/False meaning present/missing. (Whole missing sections will be replaced withFalse)-
check(check, member, missing=False)¶ A dummy check method, always returns the value unchanged.
-
-
exception
easybuild.tools.configobj.InterpolationError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorBase class for the two interpolation errors.
-
exception
easybuild.tools.configobj.InterpolationLoopError(option)¶ Bases:
easybuild.tools.configobj.InterpolationErrorMaximum interpolation depth exceeded in string interpolation.
-
exception
easybuild.tools.configobj.MissingInterpolationOption(option)¶ Bases:
easybuild.tools.configobj.InterpolationErrorA value specified for interpolation was missing.
-
exception
easybuild.tools.configobj.RepeatSectionError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorThis error indicates additional sections in a section with a
__many__(repeated) section.
-
exception
easybuild.tools.configobj.ReloadError¶ Bases:
OSErrorA ‘reload’ operation failed. This exception is a subclass of
IOError.
-
exception
easybuild.tools.configobj.UnreprError(message='', line_number=None, line='')¶ Bases:
easybuild.tools.configobj.ConfigObjErrorAn error parsing in unrepr mode.
-
exception
easybuild.tools.configobj.UnknownType¶ Bases:
Exception
-
easybuild.tools.configobj.flatten_errors(cfg, res, levels=None, results=None)¶ An example function that will turn a nested dictionary of results (as returned by
ConfigObj.validate) into a flat list.cfgis the ConfigObj instance being checked,resis the results dictionary returned byvalidate.(This is a recursive function, so you shouldn’t use the
levelsorresultsarguments - they are used by the function.)Returns a list of keys that failed. Each member of the list is a tuple:
([list of sections...], key, result)
If
validatewas called withpreserve_errors=False(the default) thenresultwill always beFalse.list of sections is a flattened list of sections that the key was found in.
If the section was missing (or a section was expected and a scalar provided - or vice-versa) then key will be
None.If the value (or section) was missing then
resultwill beFalse.If
validatewas called withpreserve_errors=Trueand a value was present, but failed the check, thenresultwill be the exception object returned. You can use this as a string that describes the failure.For example The value “3” is of the wrong type.
-
easybuild.tools.configobj.get_extra_values(conf, _prepend=())¶ Find all the values and sections not in the configspec from a validated ConfigObj.
get_extra_valuesreturns a list of tuples where each tuple represents either an extra section, or an extra value.The tuples contain two values, a tuple representing the section the value is in and the name of the extra values. For extra values in the top level section the first member will be an empty tuple. For values in the ‘foo’ section the first member will be
('foo',). For members in the ‘bar’ subsection of the ‘foo’ section the first member will be('foo', 'bar').NOTE: If you call
get_extra_valueson a ConfigObj instance that hasn’t been validated it will return an empty list.