annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/xml/etree/ElementInclude.py @ 69:33d812a61356

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