Package lxml :: Module ElementInclude
[hide private]
[frames] | no frames]

Source Code for Module lxml.ElementInclude

  1  # 
  2  # ElementTree 
  3  # $Id: ElementInclude.py 1862 2004-06-18 07:31:02Z Fredrik $ 
  4  # 
  5  # limited xinclude support for element trees 
  6  # 
  7  # history: 
  8  # 2003-08-15 fl   created 
  9  # 2003-11-14 fl   fixed default loader 
 10  # 
 11  # Copyright (c) 2003-2004 by Fredrik Lundh.  All rights reserved. 
 12  # 
 13  # fredrik@pythonware.com 
 14  # http://www.pythonware.com 
 15  # 
 16  # -------------------------------------------------------------------- 
 17  # The ElementTree toolkit is 
 18  # 
 19  # Copyright (c) 1999-2004 by Fredrik Lundh 
 20  # 
 21  # By obtaining, using, and/or copying this software and/or its 
 22  # associated documentation, you agree that you have read, understood, 
 23  # and will comply with the following terms and conditions: 
 24  # 
 25  # Permission to use, copy, modify, and distribute this software and 
 26  # its associated documentation for any purpose and without fee is 
 27  # hereby granted, provided that the above copyright notice appears in 
 28  # all copies, and that both that copyright notice and this permission 
 29  # notice appear in supporting documentation, and that the name of 
 30  # Secret Labs AB or the author not be used in advertising or publicity 
 31  # pertaining to distribution of the software without specific, written 
 32  # prior permission. 
 33  # 
 34  # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD 
 35  # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- 
 36  # ABILITY AND FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR 
 37  # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
 38  # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
 39  # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 
 40  # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 41  # OF THIS SOFTWARE. 
 42  # -------------------------------------------------------------------- 
 43   
 44  """ 
 45  Limited XInclude support for the ElementTree package. 
 46   
 47  While lxml.etree has full support for XInclude (see 
 48  `etree.ElementTree.xinclude()`), this module provides a simpler, pure 
 49  Python, ElementTree compatible implementation that supports a simple 
 50  form of custom URL resolvers. 
 51  """ 
 52   
 53  from lxml import etree 
 54  try: 
 55      from urlparse import urljoin 
 56      from urllib2 import urlopen 
 57  except ImportError: 
 58      # Python 3 
 59      from urllib.parse import urljoin 
 60      from urllib.request import urlopen 
 61   
 62  XINCLUDE = "{http://www.w3.org/2001/XInclude}" 
 63   
 64  XINCLUDE_INCLUDE = XINCLUDE + "include" 
 65  XINCLUDE_FALLBACK = XINCLUDE + "fallback" 
 66  XINCLUDE_ITER_TAG = XINCLUDE + "*" 
 67   
 68  ## 
 69  # Fatal include error. 
 70   
71 -class FatalIncludeError(etree.LxmlSyntaxError):
72 pass
73 74 ## 75 # ET compatible default loader. 76 # This loader reads an included resource from disk. 77 # 78 # @param href Resource reference. 79 # @param parse Parse mode. Either "xml" or "text". 80 # @param encoding Optional text encoding. 81 # @return The expanded resource. If the parse mode is "xml", this 82 # is an ElementTree instance. If the parse mode is "text", this 83 # is a Unicode string. If the loader fails, it can return None 84 # or raise an IOError exception. 85 # @throws IOError If the loader fails to load the resource. 86
87 -def default_loader(href, parse, encoding=None):
88 file = open(href, 'rb') 89 if parse == "xml": 90 data = etree.parse(file).getroot() 91 else: 92 data = file.read() 93 if not encoding: 94 encoding = 'utf-8' 95 data = data.decode(encoding) 96 file.close() 97 return data
98 99 ## 100 # Default loader used by lxml.etree - handles custom resolvers properly 101 # 102
103 -def _lxml_default_loader(href, parse, encoding=None, parser=None):
104 if parse == "xml": 105 data = etree.parse(href, parser).getroot() 106 else: 107 if "://" in href: 108 f = urlopen(href) 109 else: 110 f = open(href, 'rb') 111 data = f.read() 112 f.close() 113 if not encoding: 114 encoding = 'utf-8' 115 data = data.decode(encoding) 116 return data
117 118 ## 119 # Wrapper for ET compatibility - drops the parser 120
121 -def _wrap_et_loader(loader):
122 def load(href, parse, encoding=None, parser=None): 123 return loader(href, parse, encoding)
124 return load 125 126 127 ## 128 # Expand XInclude directives. 129 # 130 # @param elem Root element. 131 # @param loader Optional resource loader. If omitted, it defaults 132 # to {@link default_loader}. If given, it should be a callable 133 # that implements the same interface as <b>default_loader</b>. 134 # @throws FatalIncludeError If the function fails to include a given 135 # resource, or if the tree contains malformed XInclude elements. 136 # @throws IOError If the function fails to load a given resource. 137 # @returns the node or its replacement if it was an XInclude node 138
139 -def include(elem, loader=None, base_url=None):
140 if base_url is None: 141 if hasattr(elem, 'getroot'): 142 tree = elem 143 elem = elem.getroot() 144 else: 145 tree = elem.getroottree() 146 if hasattr(tree, 'docinfo'): 147 base_url = tree.docinfo.URL 148 elif hasattr(elem, 'getroot'): 149 elem = elem.getroot() 150 _include(elem, loader, base_url=base_url)
151
152 -def _include(elem, loader=None, _parent_hrefs=None, base_url=None):
153 if loader is not None: 154 load_include = _wrap_et_loader(loader) 155 else: 156 load_include = _lxml_default_loader 157 158 if _parent_hrefs is None: 159 _parent_hrefs = set() 160 161 parser = elem.getroottree().parser 162 163 include_elements = list( 164 elem.iter(XINCLUDE_ITER_TAG)) 165 166 for e in include_elements: 167 if e.tag == XINCLUDE_INCLUDE: 168 # process xinclude directive 169 href = urljoin(base_url, e.get("href")) 170 parse = e.get("parse", "xml") 171 parent = e.getparent() 172 if parse == "xml": 173 if href in _parent_hrefs: 174 raise FatalIncludeError( 175 "recursive include of %r detected" % href 176 ) 177 _parent_hrefs.add(href) 178 node = load_include(href, parse, parser=parser) 179 if node is None: 180 raise FatalIncludeError( 181 "cannot load %r as %r" % (href, parse) 182 ) 183 node = _include(node, loader, _parent_hrefs) 184 if e.tail: 185 node.tail = (node.tail or "") + e.tail 186 if parent is None: 187 return node # replaced the root node! 188 parent.replace(e, node) 189 elif parse == "text": 190 text = load_include(href, parse, encoding=e.get("encoding")) 191 if text is None: 192 raise FatalIncludeError( 193 "cannot load %r as %r" % (href, parse) 194 ) 195 predecessor = e.getprevious() 196 if predecessor is not None: 197 predecessor.tail = (predecessor.tail or "") + text 198 elif parent is None: 199 return text # replaced the root node! 200 else: 201 parent.text = (parent.text or "") + text + (e.tail or "") 202 parent.remove(e) 203 else: 204 raise FatalIncludeError( 205 "unknown parse type in xi:include tag (%r)" % parse 206 ) 207 elif e.tag == XINCLUDE_FALLBACK: 208 parent = e.getparent() 209 if parent is not None and parent.tag != XINCLUDE_INCLUDE: 210 raise FatalIncludeError( 211 "xi:fallback tag must be child of xi:include (%r)" % e.tag 212 ) 213 else: 214 raise FatalIncludeError( 215 "Invalid element found in XInclude namespace (%r)" % e.tag 216 ) 217 return elem
218