jpayne@69: # jpayne@69: # ElementTree jpayne@69: # $Id: ElementInclude.py 3375 2008-02-13 08:05:08Z fredrik $ jpayne@69: # jpayne@69: # limited xinclude support for element trees jpayne@69: # jpayne@69: # history: jpayne@69: # 2003-08-15 fl created jpayne@69: # 2003-11-14 fl fixed default loader jpayne@69: # jpayne@69: # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved. jpayne@69: # jpayne@69: # fredrik@pythonware.com jpayne@69: # http://www.pythonware.com jpayne@69: # jpayne@69: # -------------------------------------------------------------------- jpayne@69: # The ElementTree toolkit is jpayne@69: # jpayne@69: # Copyright (c) 1999-2008 by Fredrik Lundh jpayne@69: # jpayne@69: # By obtaining, using, and/or copying this software and/or its jpayne@69: # associated documentation, you agree that you have read, understood, jpayne@69: # and will comply with the following terms and conditions: jpayne@69: # jpayne@69: # Permission to use, copy, modify, and distribute this software and jpayne@69: # its associated documentation for any purpose and without fee is jpayne@69: # hereby granted, provided that the above copyright notice appears in jpayne@69: # all copies, and that both that copyright notice and this permission jpayne@69: # notice appear in supporting documentation, and that the name of jpayne@69: # Secret Labs AB or the author not be used in advertising or publicity jpayne@69: # pertaining to distribution of the software without specific, written jpayne@69: # prior permission. jpayne@69: # jpayne@69: # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD jpayne@69: # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- jpayne@69: # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR jpayne@69: # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY jpayne@69: # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, jpayne@69: # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS jpayne@69: # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE jpayne@69: # OF THIS SOFTWARE. jpayne@69: # -------------------------------------------------------------------- jpayne@69: jpayne@69: # Licensed to PSF under a Contributor Agreement. jpayne@69: # See http://www.python.org/psf/license for licensing details. jpayne@69: jpayne@69: ## jpayne@69: # Limited XInclude support for the ElementTree package. jpayne@69: ## jpayne@69: jpayne@69: import copy jpayne@69: from . import ElementTree jpayne@69: jpayne@69: XINCLUDE = "{http://www.w3.org/2001/XInclude}" jpayne@69: jpayne@69: XINCLUDE_INCLUDE = XINCLUDE + "include" jpayne@69: XINCLUDE_FALLBACK = XINCLUDE + "fallback" jpayne@69: jpayne@69: ## jpayne@69: # Fatal include error. jpayne@69: jpayne@69: class FatalIncludeError(SyntaxError): jpayne@69: pass jpayne@69: jpayne@69: ## jpayne@69: # Default loader. This loader reads an included resource from disk. jpayne@69: # jpayne@69: # @param href Resource reference. jpayne@69: # @param parse Parse mode. Either "xml" or "text". jpayne@69: # @param encoding Optional text encoding (UTF-8 by default for "text"). jpayne@69: # @return The expanded resource. If the parse mode is "xml", this jpayne@69: # is an ElementTree instance. If the parse mode is "text", this jpayne@69: # is a Unicode string. If the loader fails, it can return None jpayne@69: # or raise an OSError exception. jpayne@69: # @throws OSError If the loader fails to load the resource. jpayne@69: jpayne@69: def default_loader(href, parse, encoding=None): jpayne@69: if parse == "xml": jpayne@69: with open(href, 'rb') as file: jpayne@69: data = ElementTree.parse(file).getroot() jpayne@69: else: jpayne@69: if not encoding: jpayne@69: encoding = 'UTF-8' jpayne@69: with open(href, 'r', encoding=encoding) as file: jpayne@69: data = file.read() jpayne@69: return data jpayne@69: jpayne@69: ## jpayne@69: # Expand XInclude directives. jpayne@69: # jpayne@69: # @param elem Root element. jpayne@69: # @param loader Optional resource loader. If omitted, it defaults jpayne@69: # to {@link default_loader}. If given, it should be a callable jpayne@69: # that implements the same interface as default_loader. jpayne@69: # @throws FatalIncludeError If the function fails to include a given jpayne@69: # resource, or if the tree contains malformed XInclude elements. jpayne@69: # @throws OSError If the function fails to load a given resource. jpayne@69: jpayne@69: def include(elem, loader=None): jpayne@69: if loader is None: jpayne@69: loader = default_loader jpayne@69: # look for xinclude elements jpayne@69: i = 0 jpayne@69: while i < len(elem): jpayne@69: e = elem[i] jpayne@69: if e.tag == XINCLUDE_INCLUDE: jpayne@69: # process xinclude directive jpayne@69: href = e.get("href") jpayne@69: parse = e.get("parse", "xml") jpayne@69: if parse == "xml": jpayne@69: node = loader(href, parse) jpayne@69: if node is None: jpayne@69: raise FatalIncludeError( jpayne@69: "cannot load %r as %r" % (href, parse) jpayne@69: ) jpayne@69: node = copy.copy(node) jpayne@69: if e.tail: jpayne@69: node.tail = (node.tail or "") + e.tail jpayne@69: elem[i] = node jpayne@69: elif parse == "text": jpayne@69: text = loader(href, parse, e.get("encoding")) jpayne@69: if text is None: jpayne@69: raise FatalIncludeError( jpayne@69: "cannot load %r as %r" % (href, parse) jpayne@69: ) jpayne@69: if i: jpayne@69: node = elem[i-1] jpayne@69: node.tail = (node.tail or "") + text + (e.tail or "") jpayne@69: else: jpayne@69: elem.text = (elem.text or "") + text + (e.tail or "") jpayne@69: del elem[i] jpayne@69: continue jpayne@69: else: jpayne@69: raise FatalIncludeError( jpayne@69: "unknown parse type in xi:include tag (%r)" % parse jpayne@69: ) jpayne@69: elif e.tag == XINCLUDE_FALLBACK: jpayne@69: raise FatalIncludeError( jpayne@69: "xi:fallback tag must be child of xi:include (%r)" % e.tag jpayne@69: ) jpayne@69: else: jpayne@69: include(e, loader) jpayne@69: i = i + 1