elementtree id elementinclude py 3375 20080213 08 05 08z fredrik limited xinclude support for element trees history 20030815 fl created 20031114 fl fixed default loader c 20032004 by fredrik lundh fredrikpythonware com http www pythonware com the elementtree toolkit is c 19992008 by fredrik lundh by obtaining using andor copying this software andor its associated documentation you agree that you have read understood and will comply with the following terms and conditions permission to use copy modify and distribute this software and its associated documentation for any purpose and without fee is hereby granted provided that the above notice appears in all copies and that both that notice and this permission notice appear in supporting documentation and that the name of secret labs ab or the not be used in advertising or publicity pertaining to distribution of the software without specific written prior permission secret labs ab and the disclaims all warranties with regard to this software including all implied warranties of merchant ability and fitness in no event shall secret labs ab or the be liable for any special indirect or consequential damages or any damages whatsoever resulting from loss of use data or profits whether in an action of contract negligence or other tortious action arising out of or in connection with the use or performance of this software licensed to psf under a contributor agreement see https www python orgpsflicense for licensing details limited xinclude support for the elementtree package for security reasons the inclusion depth is limited to this readonly value by default fatal include error default loader this loader reads an included resource from disk param href resource reference param parse parse mode either xml or text param encoding optional text encoding utf8 by default for text return 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 oserror exception throws oserror if the loader fails to load the resource expand xinclude directives param elem root element param loader optional resource loader if omitted it defaults to link defaultloader if given it should be a callable that implements the same interface as bdefaultloaderb param baseurl the base url of the original file to resolve relative include file references param maxdepth the maximum number of recursive inclusions limited to reduce the risk of malicious content explosion pass a negative value to disable the limitation throws limitedrecursiveincludeerror if the link maxdepth was exceeded throws fatalincludeerror if the function fails to include a given resource or if the tree contains malformed xinclude elements throws ioerror if the function fails to load a given resource returns the node or its replacement if it was an xinclude node look for xinclude elements process xinclude directive elementtree id elementinclude py 3375 2008 02 13 08 05 08z fredrik limited xinclude support for element trees history 2003 08 15 fl created 2003 11 14 fl fixed default loader c 2003 2004 by fredrik lundh fredrik pythonware com http www pythonware com the elementtree toolkit is c 1999 2008 by fredrik lundh by obtaining using and or copying this software and or its associated documentation you agree that you have read understood and will comply with the following terms and conditions permission to use copy modify and distribute this software and its associated documentation for any purpose and without fee is hereby granted provided that the above notice appears in all copies and that both that notice and this permission notice appear in supporting documentation and that the name of secret labs ab or the not be used in advertising or publicity pertaining to distribution of the software without specific written prior permission secret labs ab and the disclaims all warranties with regard to this software including all implied warranties of merchant ability and fitness in no event shall secret labs ab or the be liable for any special indirect or consequential damages or any damages whatsoever resulting from loss of use data or profits whether in an action of contract negligence or other tortious action arising out of or in connection with the use or performance of this software licensed to psf under a contributor agreement see https www python org psf license for licensing details limited xinclude support for the elementtree package for security reasons the inclusion depth is limited to this read only value by default fatal include error default loader this loader reads an included resource from disk param href resource reference param parse parse mode either xml or text param encoding optional text encoding utf 8 by default for text return 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 oserror exception throws oserror if the loader fails to load the resource expand xinclude directives param elem root element param loader optional resource loader if omitted it defaults to link default_loader if given it should be a callable that implements the same interface as b default_loader b param base_url the base url of the original file to resolve relative include file references param max_depth the maximum number of recursive inclusions limited to reduce the risk of malicious content explosion pass a negative value to disable the limitation throws limitedrecursiveincludeerror if the link max_depth was exceeded throws fatalincludeerror if the function fails to include a given resource or if the tree contains malformed xinclude elements throws ioerror if the function fails to load a given resource returns the node or its replacement if it was an xinclude node look for xinclude elements process xinclude directive fixme this makes little sense with recursive includes
import copy
from . import ElementTree
from urllib.parse import urljoin

XINCLUDE = "{http://www.w3.org/2001/XInclude}"

XINCLUDE_INCLUDE = XINCLUDE + "include"
XINCLUDE_FALLBACK = XINCLUDE + "fallback"


DEFAULT_MAX_INCLUSION_DEPTH = 6





class FatalIncludeError(SyntaxError):
    pass


class LimitedRecursiveIncludeError(FatalIncludeError):
    pass














def default_loader(href, parse, encoding=None):
    if parse == "xml":
        with open(href, 'rb') as file:
            data = ElementTree.parse(file).getroot()
    else:
        if not encoding:
            encoding = 'UTF-8'
        with open(href, 'r', encoding=encoding) as file:
            data = file.read()
    return data



















def include(elem, loader=None, base_url=None,
            max_depth=DEFAULT_MAX_INCLUSION_DEPTH):
    if max_depth is None:
        max_depth = -1
    elif max_depth < 0:
        raise ValueError("expected non-negative depth or None for 'max_depth', got %r" % max_depth)

    if hasattr(elem, 'getroot'):
        elem = elem.getroot()
    if loader is None:
        loader = default_loader

    _include(elem, loader, base_url, max_depth, set())


def _include(elem, loader, base_url, max_depth, _parent_hrefs):
    
    i = 0
    while i < len(elem):
        e = elem[i]
        if e.tag == XINCLUDE_INCLUDE:
            
            href = e.get("href")
            if base_url:
                href = urljoin(base_url, href)
            parse = e.get("parse", "xml")
            if parse == "xml":
                if href in _parent_hrefs:
                    raise FatalIncludeError("recursive include of %s" % href)
                if max_depth == 0:
                    raise LimitedRecursiveIncludeError(
                        "maximum xinclude depth reached when including file %s" % href)
                _parent_hrefs.add(href)
                node = loader(href, parse)
                if node is None:
                    raise FatalIncludeError(
                        "cannot load %r as %r" % (href, parse)
                        )
                node = copy.copy(node)  
                _include(node, loader, href, max_depth - 1, _parent_hrefs)
                _parent_hrefs.remove(href)
                if e.tail:
                    node.tail = (node.tail or "") + e.tail
                elem[i] = node
            elif parse == "text":
                text = loader(href, parse, e.get("encoding"))
                if text is None:
                    raise FatalIncludeError(
                        "cannot load %r as %r" % (href, parse)
                        )
                if e.tail:
                    text += e.tail
                if i:
                    node = elem[i-1]
                    node.tail = (node.tail or "") + text
                else:
                    elem.text = (elem.text or "") + text
                del elem[i]
                continue
            else:
                raise FatalIncludeError(
                    "unknown parse type in xi:include tag (%r)" % parse
                )
        elif e.tag == XINCLUDE_FALLBACK:
            raise FatalIncludeError(
                "xi:fallback tag must be child of xi:include (%r)" % e.tag
                )
        else:
            _include(e, loader, base_url, max_depth, _parent_hrefs)
        i += 1