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