Back to index

python3.2  3.2.2
Classes | Functions | Variables
xml.etree.ElementInclude Namespace Reference

Classes

class  FatalIncludeError
 Fatal include error. More...

Functions

def default_loader
 Default loader.
def include
 Expand XInclude directives.

Variables

string XINCLUDE = "{http://www.w3.org/2001/XInclude}"
string XINCLUDE_INCLUDE = "include"
string XINCLUDE_FALLBACK = "fallback"

Class Documentation

class xml::etree::ElementInclude::FatalIncludeError

Fatal include error.

Definition at line 62 of file ElementInclude.py.


Function Documentation

def xml.etree.ElementInclude.default_loader (   href,
  parse,
  encoding = None 
)

Default loader.

This loader reads an included resource from disk.

Parameters:
hrefResource reference.
parseParse mode. Either "xml" or "text".
encodingOptional text encoding (UTF-8 by default for "text").
Returns:
The expanded resource. If the parse mode is "xml", this is an ElementTree instance. If the parse mode is "text", this is a Unicode string. If the loader fails, it can return None or raise an IOError exception.
Exceptions:
IOErrorIf the loader fails to load the resource.

Definition at line 77 of file ElementInclude.py.

00077 
00078 def default_loader(href, parse, encoding=None):
00079     if parse == "xml":
00080         file = open(href, 'rb')
00081         data = ElementTree.parse(file).getroot()
00082     else:
00083         if not encoding:
00084             encoding = 'UTF-8'
00085         file = open(href, 'r', encoding=encoding)
00086         data = file.read()
00087     file.close()
00088     return data

def xml.etree.ElementInclude.include (   elem,
  loader = None 
)

Expand XInclude directives.

Parameters:
elemRoot element.
loaderOptional resource loader. If omitted, it defaults to default_loader. If given, it should be a callable that implements the same interface as default_loader.
Exceptions:
FatalIncludeErrorIf the function fails to include a given resource, or if the tree contains malformed XInclude elements.
IOErrorIf the function fails to load a given resource.

Definition at line 100 of file ElementInclude.py.

00100 
00101 def include(elem, loader=None):
00102     if loader is None:
00103         loader = default_loader
00104     # look for xinclude elements
00105     i = 0
00106     while i < len(elem):
00107         e = elem[i]
00108         if e.tag == XINCLUDE_INCLUDE:
00109             # process xinclude directive
00110             href = e.get("href")
00111             parse = e.get("parse", "xml")
00112             if parse == "xml":
00113                 node = loader(href, parse)
00114                 if node is None:
00115                     raise FatalIncludeError(
00116                         "cannot load %r as %r" % (href, parse)
00117                         )
00118                 node = copy.copy(node)
00119                 if e.tail:
00120                     node.tail = (node.tail or "") + e.tail
00121                 elem[i] = node
00122             elif parse == "text":
00123                 text = loader(href, parse, e.get("encoding"))
00124                 if text is None:
00125                     raise FatalIncludeError(
00126                         "cannot load %r as %r" % (href, parse)
00127                         )
00128                 if i:
00129                     node = elem[i-1]
00130                     node.tail = (node.tail or "") + text + (e.tail or "")
00131                 else:
00132                     elem.text = (elem.text or "") + text + (e.tail or "")
00133                 del elem[i]
00134                 continue
00135             else:
00136                 raise FatalIncludeError(
00137                     "unknown parse type in xi:include tag (%r)" % parse
00138                 )
00139         elif e.tag == XINCLUDE_FALLBACK:
00140             raise FatalIncludeError(
00141                 "xi:fallback tag must be child of xi:include (%r)" % e.tag
00142                 )
00143         else:
00144             include(e, loader)
00145         i = i + 1

Here is the call graph for this function:


Variable Documentation

string xml.etree.ElementInclude.XINCLUDE = "{http://www.w3.org/2001/XInclude}"

Definition at line 54 of file ElementInclude.py.

Definition at line 57 of file ElementInclude.py.

Definition at line 56 of file ElementInclude.py.