annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/xml/etree/ElementInclude.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 #
jpayne@68 2 # ElementTree
jpayne@68 3 # $Id: ElementInclude.py 3375 2008-02-13 08:05:08Z fredrik $
jpayne@68 4 #
jpayne@68 5 # limited xinclude support for element trees
jpayne@68 6 #
jpayne@68 7 # history:
jpayne@68 8 # 2003-08-15 fl created
jpayne@68 9 # 2003-11-14 fl fixed default loader
jpayne@68 10 #
jpayne@68 11 # Copyright (c) 2003-2004 by Fredrik Lundh. All rights reserved.
jpayne@68 12 #
jpayne@68 13 # fredrik@pythonware.com
jpayne@68 14 # http://www.pythonware.com
jpayne@68 15 #
jpayne@68 16 # --------------------------------------------------------------------
jpayne@68 17 # The ElementTree toolkit is
jpayne@68 18 #
jpayne@68 19 # Copyright (c) 1999-2008 by Fredrik Lundh
jpayne@68 20 #
jpayne@68 21 # By obtaining, using, and/or copying this software and/or its
jpayne@68 22 # associated documentation, you agree that you have read, understood,
jpayne@68 23 # and will comply with the following terms and conditions:
jpayne@68 24 #
jpayne@68 25 # Permission to use, copy, modify, and distribute this software and
jpayne@68 26 # its associated documentation for any purpose and without fee is
jpayne@68 27 # hereby granted, provided that the above copyright notice appears in
jpayne@68 28 # all copies, and that both that copyright notice and this permission
jpayne@68 29 # notice appear in supporting documentation, and that the name of
jpayne@68 30 # Secret Labs AB or the author not be used in advertising or publicity
jpayne@68 31 # pertaining to distribution of the software without specific, written
jpayne@68 32 # prior permission.
jpayne@68 33 #
jpayne@68 34 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
jpayne@68 35 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
jpayne@68 36 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
jpayne@68 37 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
jpayne@68 38 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
jpayne@68 39 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
jpayne@68 40 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
jpayne@68 41 # OF THIS SOFTWARE.
jpayne@68 42 # --------------------------------------------------------------------
jpayne@68 43
jpayne@68 44 # Licensed to PSF under a Contributor Agreement.
jpayne@68 45 # See http://www.python.org/psf/license for licensing details.
jpayne@68 46
jpayne@68 47 ##
jpayne@68 48 # Limited XInclude support for the ElementTree package.
jpayne@68 49 ##
jpayne@68 50
jpayne@68 51 import copy
jpayne@68 52 from . import ElementTree
jpayne@68 53
jpayne@68 54 XINCLUDE = "{http://www.w3.org/2001/XInclude}"
jpayne@68 55
jpayne@68 56 XINCLUDE_INCLUDE = XINCLUDE + "include"
jpayne@68 57 XINCLUDE_FALLBACK = XINCLUDE + "fallback"
jpayne@68 58
jpayne@68 59 ##
jpayne@68 60 # Fatal include error.
jpayne@68 61
jpayne@68 62 class FatalIncludeError(SyntaxError):
jpayne@68 63 pass
jpayne@68 64
jpayne@68 65 ##
jpayne@68 66 # Default loader. This loader reads an included resource from disk.
jpayne@68 67 #
jpayne@68 68 # @param href Resource reference.
jpayne@68 69 # @param parse Parse mode. Either "xml" or "text".
jpayne@68 70 # @param encoding Optional text encoding (UTF-8 by default for "text").
jpayne@68 71 # @return The expanded resource. If the parse mode is "xml", this
jpayne@68 72 # is an ElementTree instance. If the parse mode is "text", this
jpayne@68 73 # is a Unicode string. If the loader fails, it can return None
jpayne@68 74 # or raise an OSError exception.
jpayne@68 75 # @throws OSError If the loader fails to load the resource.
jpayne@68 76
jpayne@68 77 def default_loader(href, parse, encoding=None):
jpayne@68 78 if parse == "xml":
jpayne@68 79 with open(href, 'rb') as file:
jpayne@68 80 data = ElementTree.parse(file).getroot()
jpayne@68 81 else:
jpayne@68 82 if not encoding:
jpayne@68 83 encoding = 'UTF-8'
jpayne@68 84 with open(href, 'r', encoding=encoding) as file:
jpayne@68 85 data = file.read()
jpayne@68 86 return data
jpayne@68 87
jpayne@68 88 ##
jpayne@68 89 # Expand XInclude directives.
jpayne@68 90 #
jpayne@68 91 # @param elem Root element.
jpayne@68 92 # @param loader Optional resource loader. If omitted, it defaults
jpayne@68 93 # to {@link default_loader}. If given, it should be a callable
jpayne@68 94 # that implements the same interface as <b>default_loader</b>.
jpayne@68 95 # @throws FatalIncludeError If the function fails to include a given
jpayne@68 96 # resource, or if the tree contains malformed XInclude elements.
jpayne@68 97 # @throws OSError If the function fails to load a given resource.
jpayne@68 98
jpayne@68 99 def include(elem, loader=None):
jpayne@68 100 if loader is None:
jpayne@68 101 loader = default_loader
jpayne@68 102 # look for xinclude elements
jpayne@68 103 i = 0
jpayne@68 104 while i < len(elem):
jpayne@68 105 e = elem[i]
jpayne@68 106 if e.tag == XINCLUDE_INCLUDE:
jpayne@68 107 # process xinclude directive
jpayne@68 108 href = e.get("href")
jpayne@68 109 parse = e.get("parse", "xml")
jpayne@68 110 if parse == "xml":
jpayne@68 111 node = loader(href, parse)
jpayne@68 112 if node is None:
jpayne@68 113 raise FatalIncludeError(
jpayne@68 114 "cannot load %r as %r" % (href, parse)
jpayne@68 115 )
jpayne@68 116 node = copy.copy(node)
jpayne@68 117 if e.tail:
jpayne@68 118 node.tail = (node.tail or "") + e.tail
jpayne@68 119 elem[i] = node
jpayne@68 120 elif parse == "text":
jpayne@68 121 text = loader(href, parse, e.get("encoding"))
jpayne@68 122 if text is None:
jpayne@68 123 raise FatalIncludeError(
jpayne@68 124 "cannot load %r as %r" % (href, parse)
jpayne@68 125 )
jpayne@68 126 if i:
jpayne@68 127 node = elem[i-1]
jpayne@68 128 node.tail = (node.tail or "") + text + (e.tail or "")
jpayne@68 129 else:
jpayne@68 130 elem.text = (elem.text or "") + text + (e.tail or "")
jpayne@68 131 del elem[i]
jpayne@68 132 continue
jpayne@68 133 else:
jpayne@68 134 raise FatalIncludeError(
jpayne@68 135 "unknown parse type in xi:include tag (%r)" % parse
jpayne@68 136 )
jpayne@68 137 elif e.tag == XINCLUDE_FALLBACK:
jpayne@68 138 raise FatalIncludeError(
jpayne@68 139 "xi:fallback tag must be child of xi:include (%r)" % e.tag
jpayne@68 140 )
jpayne@68 141 else:
jpayne@68 142 include(e, loader)
jpayne@68 143 i = i + 1