/usr/lib/python2.7/dist-packages/zeep/loader.py is in python-zeep 2.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import os.path
from defusedxml.lxml import fromstring
from lxml import etree
from six.moves.urllib.parse import urljoin, urlparse
from zeep.exceptions import XMLSyntaxError
class ImportResolver(etree.Resolver):
"""Custom lxml resolve to use the transport object"""
def __init__(self, transport):
self.transport = transport
def resolve(self, url, pubid, context):
if urlparse(url).scheme in ('http', 'https'):
content = self.transport.load(url)
return self.resolve_string(content, context)
def parse_xml(content, transport, base_url=None, strict=True,
xml_huge_tree=False):
"""Parse an XML string and return the root Element.
:param content: The XML string
:type content: str
:param transport: The transport instance to load imported documents
:type transport: zeep.transports.Transport
:param base_url: The base url of the document, used to make relative
lookups absolute.
:type base_url: str
:param strict: boolean to indicate if the lxml should be parsed a 'strict'.
If false then the recover mode is enabled which tries to parse invalid
XML as best as it can.
:param xml_huge_tree: boolean to indicate if lxml should process very
large XML content.
:type strict: boolean
:returns: The document root
:rtype: lxml.etree._Element
"""
recover = not strict
parser = etree.XMLParser(remove_comments=True, resolve_entities=False,
recover=recover, huge_tree=xml_huge_tree)
parser.resolvers.add(ImportResolver(transport))
try:
return fromstring(content, parser=parser, base_url=base_url)
except etree.XMLSyntaxError as exc:
raise XMLSyntaxError(
"Invalid XML content received (%s)" % exc.msg,
content=content
)
def load_external(url, transport, base_url=None, strict=True):
"""Load an external XML document.
:param url:
:param transport:
:param base_url:
:param strict: boolean to indicate if the lxml should be parsed a 'strict'.
If false then the recover mode is enabled which tries to parse invalid
XML as best as it can.
:type strict: boolean
"""
if hasattr(url, 'read'):
content = url.read()
else:
if base_url:
url = absolute_location(url, base_url)
content = transport.load(url)
return parse_xml(content, transport, base_url, strict=strict)
def absolute_location(location, base):
"""Make an url absolute (if it is optional) via the passed base url.
:param location: The (relative) url
:type location: str
:param base: The base location
:type base: str
:returns: An absolute URL
:rtype: str
"""
if location == base:
return location
if urlparse(location).scheme in ('http', 'https', 'file'):
return location
if base and urlparse(base).scheme in ('http', 'https', 'file'):
return urljoin(base, location)
else:
if os.path.isabs(location):
return location
if base:
return os.path.realpath(
os.path.join(os.path.dirname(base), location))
return location
def is_relative_path(value):
"""Check if the given value is a relative path
:param value: The value
:type value: str
:returns: Boolean indicating if the url is relative. If it is absolute then
False is returned.
:rtype: boolean
"""
if urlparse(value).scheme in ('http', 'https', 'file'):
return False
return not os.path.isabs(value)
|