Part of bzrlib.util.configobj.configobj View In Hierarchy
Method | __init__ | Parse a config file or create a config file object. |
Method | __repr__ | x.__repr__() <==> repr(x) |
Method | write | Write the current ConfigObj as a file |
Method | validate | Test the ConfigObj against a configspec. |
Method | reset | Clear ConfigObj instance and restore to 'freshly created' state. |
Method | reload | Reload a ConfigObj from file. |
Method | _load | Undocumented |
Method | _initialise | Undocumented |
Method | _handle_bom | Handle any BOM, and decode if necessary. |
Method | _a_to_u | Decode ASCII strings to unicode if a self.encoding is specified. |
Method | _decode | Decode infile to unicode. Using the specified encoding. |
Method | _decode_element | Decode element to unicode if necessary. |
Method | _str | Used by stringify within validate, to turn non-string values |
Method | _parse | Actually parse the config file. |
Method | _match_depth | Given a section and a depth level, walk back through the sections |
Method | _handle_error | Handle an error according to the error settings. |
Method | _unquote | Return an unquoted version of a value |
Method | _quote | Return a safely quoted version of a value. |
Method | _get_single_quote | Undocumented |
Method | _get_triple_quote | Undocumented |
Method | _handle_value | Given a value string, unquote, remove comment, |
Method | _multiline | Extract the value, where we are in a multiline situation. |
Method | _handle_configspec | Parse the configspec. |
Method | _set_configspec | Called by validate. Handles setting the configspec on subsections |
Method | _write_line | Write an individual line, for the write method |
Method | _write_marker | Write a section marker line |
Method | _handle_comment | Deal with a comment. |
Inherited from Section:
Method | __setstate__ | Undocumented |
Method | __reduce__ | Undocumented |
Method | __getitem__ | Fetch the item and do string interpolation. |
Method | __setitem__ | Correctly set a value. |
Method | __delitem__ | Remove items from the sequence when deleting. |
Method | get | A version of get that doesn't bypass string interpolation. |
Method | update | A version of update that uses our __setitem__. |
Method | pop | 'D.pop(k[,d]) -> v, remove specified key and return the corresponding value. |
Method | popitem | Pops the first (key,val) |
Method | clear | A version of clear that also affects scalars/sections |
Method | setdefault | A version of setdefault that sets sequence if appropriate. |
Method | items | D.items() -> list of D's (key, value) pairs, as 2-tuples |
Method | keys | D.keys() -> list of D's keys |
Method | values | D.values() -> list of D's values |
Method | iteritems | D.iteritems() -> an iterator over the (key, value) items of D |
Method | iterkeys | D.iterkeys() -> an iterator over the keys of D |
Method | itervalues | D.itervalues() -> an iterator over the values of D |
Method | dict | Return a deepcopy of self as a dictionary. |
Method | merge | A recursive update - useful for merging config files. |
Method | rename | Change a keyname to another, without changing position in sequence. |
Method | walk | Walk every member and call a function on the keyword and value. |
Method | as_bool | Accepts a key as input. The corresponding value must be a string or |
Method | as_int | A convenience method which coerces the specified value to an integer. |
Method | as_float | A convenience method which coerces the specified value to a float. |
Method | as_list | A convenience method which fetches the specified value, guaranteeing |
Method | restore_default | Restore (and return) default value for the specified key. |
Method | restore_defaults | Recursively restore default values to all members |
Method | _interpolate | Undocumented |
ConfigObj(infile=None, options=None, **kwargs)
If an encoding is specified, that must be used - but the BOM should still be removed (and the BOM attribute set).
(If the encoding is wrongly specified, then a BOM for an alternative encoding won't be discovered or removed.)
If an encoding is not specified, UTF8 or UTF16 BOM will be detected and removed. The BOM attribute will be set. UTF16 will be decoded to unicode.
NOTE: This method must not be called with an empty infile.
Specifying the wrong encoding is likely to cause a UnicodeDecodeError.
infile must always be returned as a list of lines, but may be passed in as a single string.
if is a string, it also needs converting to a list.
Return a reference to the right section, or raise a SyntaxError.
Either raise the error or store it. The error will have occured at cur_index
Raise a ConfigObjError if the value cannot be safely quoted. If multiline is True (default) then use triple quotes if necessary.
If list_values=False then the value is only quoted if it contains a \n (is multiline) or '#'.
If write_empty_values is set, and the value is an empty string, it won't be quoted.
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
It uses the validator object from validate.py.
To run validate on 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 configspec attribute of a section though).
It returns True if everything passes, or a dictionary of pass/fails (True/False). If every member of a subsection passes, it will just have the value True. (It also returns False if all members fail).
In addition, it converts the values from strings to their native types if their checks pass (and stringify is set).
If preserve_errors is True (False is default) then instead of a marking a fail with a False, it will preserve the actual exception object. This can contain info about the reason for failure. For example the VdtValueTooSmallError indicates that the value supplied was too small. If a value (or section) is missing it will still be marked as False.
You must have the validate module to use preserve_errors=True.
You can then use the flatten_errors function to turn your nested results dictionary into a flattened list of failures - useful for displaying meaningful error messages.