ElementInclude.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ##
  46. import copy
  47. import ElementTree
  48. XINCLUDE = "{http://www.w3.org/2001/XInclude}"
  49. XINCLUDE_INCLUDE = XINCLUDE + "include"
  50. XINCLUDE_FALLBACK = XINCLUDE + "fallback"
  51. ##
  52. # Fatal include error.
  53. class FatalIncludeError(SyntaxError):
  54. pass
  55. ##
  56. # Default loader. This loader reads an included resource from disk.
  57. #
  58. # @param href Resource reference.
  59. # @param parse Parse mode. Either "xml" or "text".
  60. # @param encoding Optional text encoding.
  61. # @return The expanded resource. If the parse mode is "xml", this
  62. # is an ElementTree instance. If the parse mode is "text", this
  63. # is a Unicode string. If the loader fails, it can return None
  64. # or raise an IOError exception.
  65. # @throws IOError If the loader fails to load the resource.
  66. def default_loader(href, parse, encoding=None):
  67. file = open(href)
  68. if parse == "xml":
  69. data = ElementTree.parse(file).getroot()
  70. else:
  71. data = file.read()
  72. if encoding:
  73. data = data.decode(encoding)
  74. file.close()
  75. return data
  76. ##
  77. # Expand XInclude directives.
  78. #
  79. # @param elem Root element.
  80. # @param loader Optional resource loader. If omitted, it defaults
  81. # to {@link default_loader}. If given, it should be a callable
  82. # that implements the same interface as <b>default_loader</b>.
  83. # @throws FatalIncludeError If the function fails to include a given
  84. # resource, or if the tree contains malformed XInclude elements.
  85. # @throws IOError If the function fails to load a given resource.
  86. def include(elem, loader=None):
  87. if loader is None:
  88. loader = default_loader
  89. # look for xinclude elements
  90. i = 0
  91. while i < len(elem):
  92. e = elem[i]
  93. if e.tag == XINCLUDE_INCLUDE:
  94. # process xinclude directive
  95. href = e.get("href")
  96. parse = e.get("parse", "xml")
  97. if parse == "xml":
  98. node = loader(href, parse)
  99. if node is None:
  100. raise FatalIncludeError(
  101. "cannot load %r as %r" % (href, parse)
  102. )
  103. node = copy.copy(node)
  104. if e.tail:
  105. node.tail = (node.tail or "") + e.tail
  106. elem[i] = node
  107. elif parse == "text":
  108. text = loader(href, parse, e.get("encoding"))
  109. if text is None:
  110. raise FatalIncludeError(
  111. "cannot load %r as %r" % (href, parse)
  112. )
  113. if i:
  114. node = elem[i-1]
  115. node.tail = (node.tail or "") + text
  116. else:
  117. elem.text = (elem.text or "") + text + (e.tail or "")
  118. del elem[i]
  119. continue
  120. else:
  121. raise FatalIncludeError(
  122. "unknown parse type in xi:include tag (%r)" % parse
  123. )
  124. elif e.tag == XINCLUDE_FALLBACK:
  125. raise FatalIncludeError(
  126. "xi:fallback tag must be child of xi:include (%r)" % e.tag
  127. )
  128. else:
  129. include(e, loader)
  130. i = i + 1