Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_imssubscription_xml(self):
"""Create an IMS subscription document for this IRS"""
# Create an IMS subscription mode with a dummy private ID node.
root = ET.Element("IMSSubscription")
root.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
root.set("xsi:noNamespaceSchemaLocation", "CxDataType.xsd")
priv_elem = ET.SubElement(root, "PrivateID")
priv_elem.text = "Unspecified"
found_sip_uri = False
sp_uuids = yield self.get_associated_service_profiles()
for sp_uuid in sp_uuids:
# Add a ServiceProfile node for each profile in this IRS with iFCs.
#
# Note that the IFC XML contains a wrapping tag.
try:
ifc_xml = yield ServiceProfile(sp_uuid).get_ifc()
def remote_identity(tag):
"""
Create an XML element representing the "remote" identity (i.e. the party the
call list owner called or was called by). We choose a random URI that looks
like a DN.
"""
top_elem = ET.Element(tag)
uri_elem = ET.SubElement(top_elem, "URI")
name_elem = ET.SubElement(top_elem, "name")
number = "%0.10d" % random.randrange(10000000000)
uri_elem.text = "sip:%s@example.com" % number
name_elem.text = "Tel number %s" % number
return top_elem
Alice Adams
bob@example.com
Bob Barker
1
1
2002-05-30T09:30:10
2002-05-30T09:30:20
2002-05-30T09:35:00
"""
call_elem = ET.Element('call')
if outgoing:
call_elem.append(local_identity(uri, 'from'))
call_elem.append(remote_identity('to'))
else:
call_elem.append(local_identity(uri, 'to'))
call_elem.append(remote_identity('from'))
answered_elem = ET.SubElement(call_elem, 'answered')
answered_elem.text = '1' if answered else '0'
outgoing_elem = ET.SubElement(call_elem, 'outgoing')
outgoing_elem.text = '1' if outgoing else '0'
start_time_elem = ET.SubElement(call_elem, 'start-time')
start_time_elem.text = call_list_timestamp(timestamp)
"""Wrapper around a SOAP request.
@param reqname: the SOAP name of the request
@param kwargs : to define the tags included in the request.
@return : the full etree.Element of the response
keyword arguments can be either
- simple types (they'l be converted to value)
- `OTRSObject`, they will be serialized with their `.to_xml()`
- list of `OTRSObject`s: each `OTRSObject`s in the list
will be serialized with their `.to_xml()` (used for
dynamic fields and attachments).
- list of simple types will be converted to multiple
value elements (e.g. used for search filters)
"""
xml_req_root = etree.Element(reqname)
for k, v in kwargs.items():
if isinstance(v, OTRSObject):
e = v.to_xml()
xml_req_root.append(e)
elif isinstance(v, (list, tuple)):
for vv in v:
if isinstance(vv, OTRSObject):
e = vv.to_xml()
else:
e = etree.Element(k)
e.text = unicode(vv)
xml_req_root.append(e)
else:
e = etree.Element(k)
e.text = unicode(v)
def local_identity(uri, tag):
"""
Create an XML element representing the "local" identity (i.e. the owner of
the call list).
"""
top_elem = ET.Element(tag)
uri_elem = ET.SubElement(top_elem, "URI")
# Omit the name element (as it would often not be present in the underlying
# SIP signaling).
uri_elem.text = uri
return top_elem
try:
import http.client as httplib
import urllib.request as urllib2
except ImportError:
import httplib
import urllib2
from otrs.objects import extract_tagname
from otrs.objects import OTRSObject
from posixpath import join as urljoin
import sys
# defusedxml doesn't define these non-parsing related objects
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import tostring
etree.Element = _ElementType = Element
etree.SubElement = SubElement
etree.tostring = tostring
# Fix Python 2.x.
try:
UNICODE_EXISTS = bool(type(unicode))
except NameError:
unicode = lambda s: str(s)
class OTRSError(Exception):
"""Base class for OTRS Errors."""
def __init__(self, fd):
"""Initialize OTRS Error."""
self.code = fd.getcode()
xml_req_root = etree.Element(reqname)
for k, v in kwargs.items():
if isinstance(v, OTRSObject):
e = v.to_xml()
xml_req_root.append(e)
elif isinstance(v, (list, tuple)):
for vv in v:
if isinstance(vv, OTRSObject):
e = vv.to_xml()
else:
e = etree.Element(k)
e.text = unicode(vv)
xml_req_root.append(e)
else:
e = etree.Element(k)
e.text = unicode(v)
xml_req_root.append(e)
request = urllib2.Request(
self.endpoint, self._pack_req(xml_req_root),
{'Content-Type': 'text/xml;charset=utf-8'})
try:
if ((sys.version_info[0] == 3 and sys.version_info < (3, 4, 3)) or
(sys.version_info < (2, 7, 9))):
fd = urllib2.urlopen(request, timeout=self.timeout)
else:
try:
fd = urllib2.urlopen(request, context=self.ssl_context, timeout=self.timeout)
except TypeError:
fd = urllib2.urlopen(request, timeout=self.timeout)
from lxml import _elementpath as _dummy_elementpath # noqa
# lxml with safe defaults
from defusedxml.lxml import _etree as etree
use_lxml = True
_ElementType = etree._Element
except ImportError:
# warnings.warn("Could not import lxml") # , ImportWarning)
# Try xml module (Python 2.5 or later) with safe defaults
from defusedxml import ElementTree as etree
# defusedxml doesn't define these non-parsing related objects
from xml.etree.ElementTree import Element, SubElement, tostring
etree.Element = _ElementType = Element
etree.SubElement = SubElement
etree.tostring = tostring
# ========================================================================
# XML
# ========================================================================
def is_etree_element(obj):
return isinstance(obj, _ElementType)
def string_to_xml(text):
"""Convert XML string into etree.Element."""
try: