You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.5 KiB

4 years ago
  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. Limited XInclude support for the ElementTree package.
  45. While lxml.etree has full support for XInclude (see
  46. `etree.ElementTree.xinclude()`), this module provides a simpler, pure
  47. Python, ElementTree compatible implementation that supports a simple
  48. form of custom URL resolvers.
  49. """
  50. from lxml import etree
  51. try:
  52. from urlparse import urljoin
  53. from urllib2 import urlopen
  54. except ImportError:
  55. # Python 3
  56. from urllib.parse import urljoin
  57. from urllib.request import urlopen
  58. XINCLUDE = "{http://www.w3.org/2001/XInclude}"
  59. XINCLUDE_INCLUDE = XINCLUDE + "include"
  60. XINCLUDE_FALLBACK = XINCLUDE + "fallback"
  61. XINCLUDE_ITER_TAG = XINCLUDE + "*"
  62. ##
  63. # Fatal include error.
  64. class FatalIncludeError(etree.LxmlSyntaxError):
  65. pass
  66. ##
  67. # ET compatible default loader.
  68. # This loader reads an included resource from disk.
  69. #
  70. # @param href Resource reference.
  71. # @param parse Parse mode. Either "xml" or "text".
  72. # @param encoding Optional text encoding.
  73. # @return The expanded resource. If the parse mode is "xml", this
  74. # is an ElementTree instance. If the parse mode is "text", this
  75. # is a Unicode string. If the loader fails, it can return None
  76. # or raise an IOError exception.
  77. # @throws IOError If the loader fails to load the resource.
  78. def default_loader(href, parse, encoding=None):
  79. file = open(href, 'rb')
  80. if parse == "xml":
  81. data = etree.parse(file).getroot()
  82. else:
  83. data = file.read()
  84. if not encoding:
  85. encoding = 'utf-8'
  86. data = data.decode(encoding)
  87. file.close()
  88. return data
  89. ##
  90. # Default loader used by lxml.etree - handles custom resolvers properly
  91. #
  92. def _lxml_default_loader(href, parse, encoding=None, parser=None):
  93. if parse == "xml":
  94. data = etree.parse(href, parser).getroot()
  95. else:
  96. if "://" in href:
  97. f = urlopen(href)
  98. else:
  99. f = open(href, 'rb')
  100. data = f.read()
  101. f.close()
  102. if not encoding:
  103. encoding = 'utf-8'
  104. data = data.decode(encoding)
  105. return data
  106. ##
  107. # Wrapper for ET compatibility - drops the parser
  108. def _wrap_et_loader(loader):
  109. def load(href, parse, encoding=None, parser=None):
  110. return loader(href, parse, encoding)
  111. return load
  112. ##
  113. # Expand XInclude directives.
  114. #
  115. # @param elem Root element.
  116. # @param loader Optional resource loader. If omitted, it defaults
  117. # to {@link default_loader}. If given, it should be a callable
  118. # that implements the same interface as <b>default_loader</b>.
  119. # @param base_url The base URL of the original file, to resolve
  120. # relative include file references.
  121. # @throws FatalIncludeError If the function fails to include a given
  122. # resource, or if the tree contains malformed XInclude elements.
  123. # @throws IOError If the function fails to load a given resource.
  124. # @returns the node or its replacement if it was an XInclude node
  125. def include(elem, loader=None, base_url=None):
  126. if base_url is None:
  127. if hasattr(elem, 'getroot'):
  128. tree = elem
  129. elem = elem.getroot()
  130. else:
  131. tree = elem.getroottree()
  132. if hasattr(tree, 'docinfo'):
  133. base_url = tree.docinfo.URL
  134. elif hasattr(elem, 'getroot'):
  135. elem = elem.getroot()
  136. _include(elem, loader, base_url=base_url)
  137. def _include(elem, loader=None, _parent_hrefs=None, base_url=None):
  138. if loader is not None:
  139. load_include = _wrap_et_loader(loader)
  140. else:
  141. load_include = _lxml_default_loader
  142. if _parent_hrefs is None:
  143. _parent_hrefs = set()
  144. parser = elem.getroottree().parser
  145. include_elements = list(
  146. elem.iter(XINCLUDE_ITER_TAG))
  147. for e in include_elements:
  148. if e.tag == XINCLUDE_INCLUDE:
  149. # process xinclude directive
  150. href = urljoin(base_url, e.get("href"))
  151. parse = e.get("parse", "xml")
  152. parent = e.getparent()
  153. if parse == "xml":
  154. if href in _parent_hrefs:
  155. raise FatalIncludeError(
  156. "recursive include of %r detected" % href
  157. )
  158. _parent_hrefs.add(href)
  159. node = load_include(href, parse, parser=parser)
  160. if node is None:
  161. raise FatalIncludeError(
  162. "cannot load %r as %r" % (href, parse)
  163. )
  164. node = _include(node, loader, _parent_hrefs)
  165. if e.tail:
  166. node.tail = (node.tail or "") + e.tail
  167. if parent is None:
  168. return node # replaced the root node!
  169. parent.replace(e, node)
  170. elif parse == "text":
  171. text = load_include(href, parse, encoding=e.get("encoding"))
  172. if text is None:
  173. raise FatalIncludeError(
  174. "cannot load %r as %r" % (href, parse)
  175. )
  176. predecessor = e.getprevious()
  177. if predecessor is not None:
  178. predecessor.tail = (predecessor.tail or "") + text
  179. elif parent is None:
  180. return text # replaced the root node!
  181. else:
  182. parent.text = (parent.text or "") + text + (e.tail or "")
  183. parent.remove(e)
  184. else:
  185. raise FatalIncludeError(
  186. "unknown parse type in xi:include tag (%r)" % parse
  187. )
  188. elif e.tag == XINCLUDE_FALLBACK:
  189. parent = e.getparent()
  190. if parent is not None and parent.tag != XINCLUDE_INCLUDE:
  191. raise FatalIncludeError(
  192. "xi:fallback tag must be child of xi:include (%r)" % e.tag
  193. )
  194. else:
  195. raise FatalIncludeError(
  196. "Invalid element found in XInclude namespace (%r)" % e.tag
  197. )
  198. return elem