ยปCore Development>Code coverage>Lib/xml/etree/ElementInclude.py

Python code coverage for Lib/xml/etree/ElementInclude.py

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