Class p.u.c.ConfigObj(Section):

Part of pida.utils.configobj View In Hierarchy

An object to read, create, and write config files.

Split Table into Classes Show Methods in One Table

Line # Kind Name Docs
1129 Method __init__ Parse or create a config file object.
1261 Method __repr__ Undocumented
1266 Method _handle_bom Handle any BOM, and decode if necessary.
1367 Method _a_to_u Decode ASCII strings to unicode if a self.encoding is specified.
1374 Method _decode Decode infile to unicode. Using the specified encoding.
1392 Method _decode_element Decode element to unicode if necessary.
1400 Method _str Used by stringify within validate, to turn non-string values
1410 Method _parse Actually parse the config file.
1580 Method _match_depth Given a section and a depth level, walk back through the sections
1598 Method _handle_error Handle an error according to the error settings.
1616 Method _unquote Return an unquoted version of a value
1622 Method _quote Return a safely quoted version of a value.
1698 Method _handle_value Given a value string, unquote, remove comment,
1743 Method _multiline Extract the value, where we are in a multiline situation.
1777 Method _handle_configspec Parse the configspec.
1796 Method _set_configspec_value Used to recursively set configspec values.
1826 Method _handle_repeat Dynamically assign configspec for repeated section.
1857 Method _write_line Write an individual line, for the write method
1871 Method _write_marker Write a section marker line
1880 Method _handle_comment Deal with a comment.
1891 Method write Write the current ConfigObj as a file
1992 Method validate Test the ConfigObj against a configspec.

Inherited from Section:

Line # Kind Name Docs
508 Method _interpolate Undocumented
530 Method __getitem__ Fetch the item and do string interpolation.
537 Method __setitem__ Correctly set a value.
595 Method __delitem__ Remove items from the sequence when deleting.
605 Method get A version of get that doesn't bypass string interpolation.
612 Method update A version of update that uses our __setitem__.
619 Method pop Undocumented
634 Method popitem Pops the first (key,val)
644 Method clear A version of clear that also affects scalars/sections
659 Method setdefault A version of setdefault that sets sequence if appropriate.
667 Method items Undocumented
671 Method keys Undocumented
675 Method values Undocumented
679 Method iteritems Undocumented
683 Method iterkeys Undocumented
689 Method itervalues Undocumented
701 Method dict Return a deepcopy of self as a dictionary.
728 Method merge A recursive update - useful for merging config files.
754 Method rename Change a keyname to another, without changing position in sequence.
783 Method walk Walk every member and call a function on the keyword and value.
867 Method decode Decode all strings and values to unicode, using the specified encoding.
910 Method encode Encode all strings and values from unicode,
935 Method istrue A deprecated version of as_bool.
941 Method as_bool Accepts a key as input. The corresponding value must be a string or
983 Method as_int A convenience method which coerces the specified value to an integer.
1005 Method as_float A convenience method which coerces the specified value to a float.
Line # Kind Name Docs
595 Method __delitem__ Remove items from the sequence when deleting.
530 Method __getitem__ Fetch the item and do string interpolation.
1129 Method __init__ Parse or create a config file object.
1261 Method __repr__ Undocumented
537 Method __setitem__ Correctly set a value.
1367 Method _a_to_u Decode ASCII strings to unicode if a self.encoding is specified.
1374 Method _decode Decode infile to unicode. Using the specified encoding.
1392 Method _decode_element Decode element to unicode if necessary.
1266 Method _handle_bom Handle any BOM, and decode if necessary.
1880 Method _handle_comment Deal with a comment.
1777 Method _handle_configspec Parse the configspec.
1598 Method _handle_error Handle an error according to the error settings.
1826 Method _handle_repeat Dynamically assign configspec for repeated section.
1698 Method _handle_value Given a value string, unquote, remove comment,
508 Method _interpolate Undocumented
1580 Method _match_depth Given a section and a depth level, walk back through the sections
1743 Method _multiline Extract the value, where we are in a multiline situation.
1410 Method _parse Actually parse the config file.
1622 Method _quote Return a safely quoted version of a value.
1796 Method _set_configspec_value Used to recursively set configspec values.
1400 Method _str Used by stringify within validate, to turn non-string values
1616 Method _unquote Return an unquoted version of a value
1857 Method _write_line Write an individual line, for the write method
1871 Method _write_marker Write a section marker line
941 Method as_bool Accepts a key as input. The corresponding value must be a string or
1005 Method as_float A convenience method which coerces the specified value to a float.
983 Method as_int A convenience method which coerces the specified value to an integer.
644 Method clear A version of clear that also affects scalars/sections
867 Method decode Decode all strings and values to unicode, using the specified encoding.
701 Method dict Return a deepcopy of self as a dictionary.
910 Method encode Encode all strings and values from unicode,
605 Method get A version of get that doesn't bypass string interpolation.
935 Method istrue A deprecated version of as_bool.
667 Method items Undocumented
679 Method iteritems Undocumented
683 Method iterkeys Undocumented
689 Method itervalues Undocumented
671 Method keys Undocumented
728 Method merge A recursive update - useful for merging config files.
619 Method pop Undocumented
634 Method popitem Pops the first (key,val)
754 Method rename Change a keyname to another, without changing position in sequence.
659 Method setdefault A version of setdefault that sets sequence if appropriate.
612 Method update A version of update that uses our __setitem__.
1992 Method validate Test the ConfigObj against a configspec.
675 Method values Undocumented
783 Method walk Walk every member and call a function on the keyword and value.
1891 Method write Write the current ConfigObj as a file
def __init__(self, infile=None, options=None, **kwargs):
Parse or create a config file object.

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

def __repr__(self):
Undocumented
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 ``
`` (is multiline).
        
        If ``write_empty_values`` is set, and the value is an empty string, it
        won't be quoted.
        
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_value(self, configspec, section):
Used to recursively set configspec values.
def _handle_repeat(self, section, configspec):
Dynamically assign configspec for repeated section.
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 indeicates 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.

API Documentation for PIDA, generated by pydoctor.