]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/xml/etree/ElementInclude.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / xml / etree / ElementInclude.py
CommitLineData
4710c53d 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 file = open(href)\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 file.close()\r
86 return data\r
87\r
88##\r
89# Expand XInclude directives.\r
90#\r
91# @param elem Root element.\r
92# @param loader Optional resource loader. If omitted, it defaults\r
93# to {@link default_loader}. If given, it should be a callable\r
94# that implements the same interface as <b>default_loader</b>.\r
95# @throws FatalIncludeError If the function fails to include a given\r
96# resource, or if the tree contains malformed XInclude elements.\r
97# @throws IOError If the function fails to load a given resource.\r
98\r
99def include(elem, loader=None):\r
100 if loader is None:\r
101 loader = default_loader\r
102 # look for xinclude elements\r
103 i = 0\r
104 while i < len(elem):\r
105 e = elem[i]\r
106 if e.tag == XINCLUDE_INCLUDE:\r
107 # process xinclude directive\r
108 href = e.get("href")\r
109 parse = e.get("parse", "xml")\r
110 if parse == "xml":\r
111 node = loader(href, parse)\r
112 if node is None:\r
113 raise FatalIncludeError(\r
114 "cannot load %r as %r" % (href, parse)\r
115 )\r
116 node = copy.copy(node)\r
117 if e.tail:\r
118 node.tail = (node.tail or "") + e.tail\r
119 elem[i] = node\r
120 elif parse == "text":\r
121 text = loader(href, parse, e.get("encoding"))\r
122 if text is None:\r
123 raise FatalIncludeError(\r
124 "cannot load %r as %r" % (href, parse)\r
125 )\r
126 if i:\r
127 node = elem[i-1]\r
128 node.tail = (node.tail or "") + text + (e.tail or "")\r
129 else:\r
130 elem.text = (elem.text or "") + text + (e.tail or "")\r
131 del elem[i]\r
132 continue\r
133 else:\r
134 raise FatalIncludeError(\r
135 "unknown parse type in xi:include tag (%r)" % parse\r
136 )\r
137 elif e.tag == XINCLUDE_FALLBACK:\r
138 raise FatalIncludeError(\r
139 "xi:fallback tag must be child of xi:include (%r)" % e.tag\r
140 )\r
141 else:\r
142 include(e, loader)\r
143 i = i + 1\r