b.u.c.c.ConfigObj(Section) : class documentation

Part of bzrlib.util.configobj.configobj View In Hierarchy

An object to read, create, and write config files.
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
def __init__(self, infile=None, options=None, _inspec=False, **kwargs):
Parse a config file or create a config file object.

ConfigObj(infile=None, options=None, **kwargs)

def _load(self, infile, configspec):
Undocumented
def _initialise(self, options=None):
Undocumented
def __repr__(self):
x.__repr__() <==> repr(x)
def _handle_bom(self, infile):
Handle any BOM, and decode if necessary.

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.

def _a_to_u(self, aString):
Decode ASCII strings to unicode if a self.encoding is specified.
def _decode(self, infile, encoding):
Decode infile to unicode. Using the specified encoding.

if is a string, it also needs converting to a list.

def _decode_element(self, line):
Decode element to unicode if necessary.
def _str(self, value):
Used by stringify within validate, to turn non-string values into strings.
def _parse(self, infile):
Actually parse the config file.
def _match_depth(self, sect, depth):
Given a section and a depth level, walk back through the sections parents to see if the depth level matches a previous section.

Return a reference to the right section, or raise a SyntaxError.

def _handle_error(self, text, ErrorClass, infile, cur_index):
Handle an error according to the error settings.

Either raise the error or store it. The error will have occured at cur_index

def _unquote(self, value):
Return an unquoted version of a value
def _quote(self, value, multiline=True):
Return a safely quoted version of a value.

Raise a ConfigObjError if the value cannot be safely quoted. If multiline is True (default) then use triple quotes if necessary.

  • Don't quote values that don't need it.
  • Recursively quote members of a list and return a comma joined list.
  • Multiline is False for lists.
  • Obey list syntax for empty and single member lists.

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.

def _get_single_quote(self, value):
Undocumented
def _get_triple_quote(self, value):
Undocumented
def _handle_value(self, value):
Given a value string, unquote, remove comment, handle lists. (including empty and single member lists)
def _multiline(self, value, infile, cur_index, maxline):
Extract the value, where we are in a multiline situation.
def _handle_configspec(self, configspec):
Parse the configspec.
def _set_configspec(self, section, copy):
Called by validate. Handles setting the configspec on subsections including sections to be validated by __many__
def _write_line(self, indent_string, entry, this_entry, comment):
Write an individual line, for the write method
def _write_marker(self, indent_string, depth, entry, comment):
Write a section marker line
def _handle_comment(self, comment):
Deal with a comment.
def write(self, 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
def validate(self, validator, preserve_errors=False, copy=False, section=None):
Test the ConfigObj against a configspec.

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.

def reset(self):
Clear ConfigObj instance and restore to 'freshly created' state.
def reload(self):
Reload a ConfigObj from file.

This method raises a ReloadError if the ConfigObj doesn't have a filename attribute pointing to a file.

API Documentation for Bazaar, generated by pydoctor at 2022-06-16 00:25:16.