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