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