anyconfig.backend.xml

XML files parser backend, should be available always.

Changed in version 0.1.0: Added XML dump support.

  • Format to support: XML, e.g. http://www.w3.org/TR/xml11/
  • Requirements: one of the followings
    • lxml2.etree if available
    • xml.etree.ElementTree in standard lib if python >= 2.5
    • elementtree.ElementTree (otherwise)
  • Limitations:
    • ‘<prefix>attrs’, ‘<prefix>text’ and ‘<prefix>children’ are used as special parameter to keep XML structure of original data. You have to cusomize <prefix> (default: ‘@’) if any config parameters conflict with some of them.
    • Some data or structures of original XML file may be lost if make it backed to XML file; XML file - (anyconfig.load) -> config - (anyconfig.dump) -> XML file
    • XML specific features (namespace, etc.) may not be processed correctly.
  • Special Options: None supported
anyconfig.backend.xml.etree_to_container(root, cls, pprefix='@')

Convert XML ElementTree to a collection of container objects.

Parameters:
  • root – etree root object or None
  • cls – Container class
  • pprefix – Special parameter name prefix
anyconfig.backend.xml.container_to_etree(obj, cls, parent=None, pprefix='@')

Convert a container object to XML ElementTree.

Parameters:
  • obj – Container instance to convert to
  • cls – Container class
  • parent – XML ElementTree parent node object or None
  • pprefix – Special parameter name prefix
anyconfig.backend.xml.etree_write(tree, stream)

Write XML ElementTree root content into stream.

Parameters:
  • tree – XML ElementTree object
  • stream – File or file-like object can write to
class anyconfig.backend.xml.Parser

Bases: anyconfig.backend.base.ToStreamDumper

Parser for XML files.

_type = 'xml'
_extensions = ['xml']
_open_flags = ('rb', 'wb')
load_from_string(content, **kwargs)

Load config from XML snippet (a string content).

Parameters:
  • content – XML snippet (a string)
  • kwargs – optional keyword parameters passed to
Returns:

self.container object holding config parameters

load_from_path(filepath, **kwargs)
Parameters:
  • filepath – XML file path
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

load_from_stream(stream, **kwargs)
Parameters:
  • stream – XML file or file-like object
  • kwargs – optional keyword parameters to be sanitized :: dict
Returns:

self.container object holding config parameters

dump_to_string(cnf, **kwargs)
Parameters:
  • cnf – Configuration data to dump :: self.container
  • kwargs – optional keyword parameters
Returns:

string represents the configuration

dump_to_stream(cnf, stream, **kwargs)
Parameters:
  • cnf – Configuration data to dump :: self.container
  • stream – Config file or file like object write to
  • kwargs – optional keyword parameters
__module__ = 'anyconfig.backend.xml'

Previous topic

anyconfig.backend.yaml

Next topic

anyconfig.mergeabledict

This Page