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