]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Lib/xml/etree/ElementInclude.py
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 4/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / xml / etree / ElementInclude.py
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Lib/xml/etree/ElementInclude.py b/AppPkg/Applications/Python/Python-2.7.10/Lib/xml/etree/ElementInclude.py
new file mode 100644 (file)
index 0000000..2bfac34
--- /dev/null
@@ -0,0 +1,142 @@
+#\r
+# ElementTree\r
+# $Id: ElementInclude.py 3375 2008-02-13 08:05:08Z fredrik $\r
+#\r
+# limited xinclude support for element trees\r
+#\r
+# history:\r
+# 2003-08-15 fl   created\r
+# 2003-11-14 fl   fixed default loader\r
+#\r
+# Copyright (c) 2003-2004 by Fredrik Lundh.  All rights reserved.\r
+#\r
+# fredrik@pythonware.com\r
+# http://www.pythonware.com\r
+#\r
+# --------------------------------------------------------------------\r
+# The ElementTree toolkit is\r
+#\r
+# Copyright (c) 1999-2008 by Fredrik Lundh\r
+#\r
+# By obtaining, using, and/or copying this software and/or its\r
+# associated documentation, you agree that you have read, understood,\r
+# and will comply with the following terms and conditions:\r
+#\r
+# Permission to use, copy, modify, and distribute this software and\r
+# its associated documentation for any purpose and without fee is\r
+# hereby granted, provided that the above copyright notice appears in\r
+# all copies, and that both that copyright notice and this permission\r
+# notice appear in supporting documentation, and that the name of\r
+# Secret Labs AB or the author not be used in advertising or publicity\r
+# pertaining to distribution of the software without specific, written\r
+# prior permission.\r
+#\r
+# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD\r
+# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-\r
+# ABILITY AND FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR\r
+# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY\r
+# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\r
+# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
+# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE\r
+# OF THIS SOFTWARE.\r
+# --------------------------------------------------------------------\r
+\r
+# Licensed to PSF under a Contributor Agreement.\r
+# See http://www.python.org/psf/license for licensing details.\r
+\r
+##\r
+# Limited XInclude support for the ElementTree package.\r
+##\r
+\r
+import copy\r
+from . import ElementTree\r
+\r
+XINCLUDE = "{http://www.w3.org/2001/XInclude}"\r
+\r
+XINCLUDE_INCLUDE = XINCLUDE + "include"\r
+XINCLUDE_FALLBACK = XINCLUDE + "fallback"\r
+\r
+##\r
+# Fatal include error.\r
+\r
+class FatalIncludeError(SyntaxError):\r
+    pass\r
+\r
+##\r
+# Default loader.  This loader reads an included resource from disk.\r
+#\r
+# @param href Resource reference.\r
+# @param parse Parse mode.  Either "xml" or "text".\r
+# @param encoding Optional text encoding.\r
+# @return The expanded resource.  If the parse mode is "xml", this\r
+#    is an ElementTree instance.  If the parse mode is "text", this\r
+#    is a Unicode string.  If the loader fails, it can return None\r
+#    or raise an IOError exception.\r
+# @throws IOError If the loader fails to load the resource.\r
+\r
+def default_loader(href, parse, encoding=None):\r
+    with open(href) as file:\r
+        if parse == "xml":\r
+            data = ElementTree.parse(file).getroot()\r
+        else:\r
+            data = file.read()\r
+            if encoding:\r
+                data = data.decode(encoding)\r
+    return data\r
+\r
+##\r
+# Expand XInclude directives.\r
+#\r
+# @param elem Root element.\r
+# @param loader Optional resource loader.  If omitted, it defaults\r
+#     to {@link default_loader}.  If given, it should be a callable\r
+#     that implements the same interface as <b>default_loader</b>.\r
+# @throws FatalIncludeError If the function fails to include a given\r
+#     resource, or if the tree contains malformed XInclude elements.\r
+# @throws IOError If the function fails to load a given resource.\r
+\r
+def include(elem, loader=None):\r
+    if loader is None:\r
+        loader = default_loader\r
+    # look for xinclude elements\r
+    i = 0\r
+    while i < len(elem):\r
+        e = elem[i]\r
+        if e.tag == XINCLUDE_INCLUDE:\r
+            # process xinclude directive\r
+            href = e.get("href")\r
+            parse = e.get("parse", "xml")\r
+            if parse == "xml":\r
+                node = loader(href, parse)\r
+                if node is None:\r
+                    raise FatalIncludeError(\r
+                        "cannot load %r as %r" % (href, parse)\r
+                        )\r
+                node = copy.copy(node)\r
+                if e.tail:\r
+                    node.tail = (node.tail or "") + e.tail\r
+                elem[i] = node\r
+            elif parse == "text":\r
+                text = loader(href, parse, e.get("encoding"))\r
+                if text is None:\r
+                    raise FatalIncludeError(\r
+                        "cannot load %r as %r" % (href, parse)\r
+                        )\r
+                if i:\r
+                    node = elem[i-1]\r
+                    node.tail = (node.tail or "") + text + (e.tail or "")\r
+                else:\r
+                    elem.text = (elem.text or "") + text + (e.tail or "")\r
+                del elem[i]\r
+                continue\r
+            else:\r
+                raise FatalIncludeError(\r
+                    "unknown parse type in xi:include tag (%r)" % parse\r
+                )\r
+        elif e.tag == XINCLUDE_FALLBACK:\r
+            raise FatalIncludeError(\r
+                "xi:fallback tag must be child of xi:include (%r)" % e.tag\r
+                )\r
+        else:\r
+            include(e, loader)\r
+        i = i + 1\r