ElementBase: Remove deprecated find() and findall() methods.

This commit is contained in:
Emmanuel Gil Peyrot 2016-09-20 16:45:29 +09:00
parent b8e091233e
commit 3a9b45e4f2
12 changed files with 46 additions and 75 deletions

View file

@ -29,7 +29,7 @@ class Mechanisms(ElementBase):
""" """
""" """
results = [] results = []
mechs = self.findall('{%s}mechanism' % self.namespace) mechs = self.xml.findall('{%s}mechanism' % self.namespace)
if mechs: if mechs:
for mech in mechs: for mech in mechs:
results.append(mech.text) results.append(mech.text)
@ -47,7 +47,7 @@ class Mechanisms(ElementBase):
def del_mechanisms(self): def del_mechanisms(self):
""" """
""" """
mechs = self.findall('{%s}mechanism' % self.namespace) mechs = self.xml.findall('{%s}mechanism' % self.namespace)
if mechs: if mechs:
for mech in mechs: for mech in mechs:
self.xml.remove(mech) self.xml.remove(mech)

View file

@ -25,7 +25,7 @@ def fault2xml(fault):
def xml2fault(params): def xml2fault(params):
vals = [] vals = []
for value in params.findall('{%s}value' % _namespace): for value in params.xml.findall('{%s}value' % _namespace):
vals.append(_xml2py(value)) vals.append(_xml2py(value))
fault = dict() fault = dict()
fault['code'] = vals[0]['faultCode'] fault['code'] = vals[0]['faultCode']
@ -92,39 +92,40 @@ def _py2xml(*args):
def xml2py(params): def xml2py(params):
namespace = 'jabber:iq:rpc' namespace = 'jabber:iq:rpc'
vals = [] vals = []
for param in params.findall('{%s}param' % namespace): for param in params.xml.findall('{%s}param' % namespace):
vals.append(_xml2py(param.find('{%s}value' % namespace))) vals.append(_xml2py(param.find('{%s}value' % namespace)))
return vals return vals
def _xml2py(value): def _xml2py(value):
namespace = 'jabber:iq:rpc' namespace = 'jabber:iq:rpc'
if value.find('{%s}nil' % namespace) is not None: find_value = value.xml.find
if find_value('{%s}nil' % namespace) is not None:
return None return None
if value.find('{%s}i4' % namespace) is not None: if find_value('{%s}i4' % namespace) is not None:
return int(value.find('{%s}i4' % namespace).text) return int(find_value('{%s}i4' % namespace).text)
if value.find('{%s}int' % namespace) is not None: if find_value('{%s}int' % namespace) is not None:
return int(value.find('{%s}int' % namespace).text) return int(find_value('{%s}int' % namespace).text)
if value.find('{%s}boolean' % namespace) is not None: if find_value('{%s}boolean' % namespace) is not None:
return bool(int(value.find('{%s}boolean' % namespace).text)) return bool(int(find_value('{%s}boolean' % namespace).text))
if value.find('{%s}string' % namespace) is not None: if find_value('{%s}string' % namespace) is not None:
return value.find('{%s}string' % namespace).text return find_value('{%s}string' % namespace).text
if value.find('{%s}double' % namespace) is not None: if find_value('{%s}double' % namespace) is not None:
return float(value.find('{%s}double' % namespace).text) return float(find_value('{%s}double' % namespace).text)
if value.find('{%s}base64' % namespace) is not None: if find_value('{%s}base64' % namespace) is not None:
return rpcbase64(value.find('{%s}base64' % namespace).text.encode()) return rpcbase64(find_value('{%s}base64' % namespace).text.encode())
if value.find('{%s}Base64' % namespace) is not None: if find_value('{%s}Base64' % namespace) is not None:
# Older versions of XEP-0009 used Base64 # Older versions of XEP-0009 used Base64
return rpcbase64(value.find('{%s}Base64' % namespace).text.encode()) return rpcbase64(find_value('{%s}Base64' % namespace).text.encode())
if value.find('{%s}dateTime.iso8601' % namespace) is not None: if find_value('{%s}dateTime.iso8601' % namespace) is not None:
return rpctime(value.find('{%s}dateTime.iso8601' % namespace).text) return rpctime(find_value('{%s}dateTime.iso8601' % namespace).text)
if value.find('{%s}struct' % namespace) is not None: if find_value('{%s}struct' % namespace) is not None:
struct = {} struct = {}
for member in value.find('{%s}struct' % namespace).findall('{%s}member' % namespace): for member in find_value('{%s}struct' % namespace).findall('{%s}member' % namespace):
struct[member.find('{%s}name' % namespace).text] = _xml2py(member.find('{%s}value' % namespace)) struct[member.find('{%s}name' % namespace).text] = _xml2py(member.find('{%s}value' % namespace))
return struct return struct
if value.find('{%s}array' % namespace) is not None: if find_value('{%s}array' % namespace) is not None:
array = [] array = []
for val in value.find('{%s}array' % namespace).find('{%s}data' % namespace).findall('{%s}value' % namespace): for val in find_value('{%s}array' % namespace).find('{%s}data' % namespace).findall('{%s}value' % namespace):
array.append(_xml2py(val)) array.append(_xml2py(val))
return array return array
raise ValueError() raise ValueError()

View file

@ -163,7 +163,7 @@ class ACL:
@classmethod @classmethod
def _next_token(cls, expression, index): def _next_token(cls, expression, index):
new_index = expression.find('*', index) new_index = expression.xml.find('*', index)
if new_index == 0: if new_index == 0:
return '' return ''
else: else:
@ -182,7 +182,7 @@ class ACL:
#! print "[TOKEN] '%s'" % token #! print "[TOKEN] '%s'" % token
size = len(token) size = len(token)
if size > 0: if size > 0:
token_index = value.find(token, position) token_index = value.xml.find(token, position)
if token_index == -1: if token_index == -1:
return False return False
else: else:

View file

@ -137,7 +137,7 @@ class DiscoInfo(ElementBase):
identity = (category, itype, lang) identity = (category, itype, lang)
if identity in self._identities: if identity in self._identities:
self._identities.remove(identity) self._identities.remove(identity)
for id_xml in self.findall('{%s}identity' % self.namespace): for id_xml in self.xml.findall('{%s}identity' % self.namespace):
id = (id_xml.attrib['category'], id = (id_xml.attrib['category'],
id_xml.attrib['type'], id_xml.attrib['type'],
id_xml.attrib.get('{%s}lang' % self.xml_ns, None)) id_xml.attrib.get('{%s}lang' % self.xml_ns, None))
@ -163,7 +163,7 @@ class DiscoInfo(ElementBase):
identities = set() identities = set()
else: else:
identities = [] identities = []
for id_xml in self.findall('{%s}identity' % self.namespace): for id_xml in self.xml.findall('{%s}identity' % self.namespace):
xml_lang = id_xml.attrib.get('{%s}lang' % self.xml_ns, None) xml_lang = id_xml.attrib.get('{%s}lang' % self.xml_ns, None)
if lang is None or xml_lang == lang: if lang is None or xml_lang == lang:
id = (id_xml.attrib['category'], id = (id_xml.attrib['category'],
@ -205,7 +205,7 @@ class DiscoInfo(ElementBase):
Arguments: Arguments:
lang -- Optional, standard xml:lang value. lang -- Optional, standard xml:lang value.
""" """
for id_xml in self.findall('{%s}identity' % self.namespace): for id_xml in self.xml.findall('{%s}identity' % self.namespace):
if lang is None: if lang is None:
self.xml.remove(id_xml) self.xml.remove(id_xml)
elif id_xml.attrib.get('{%s}lang' % self.xml_ns, None) == lang: elif id_xml.attrib.get('{%s}lang' % self.xml_ns, None) == lang:
@ -239,7 +239,7 @@ class DiscoInfo(ElementBase):
""" """
if feature in self._features: if feature in self._features:
self._features.remove(feature) self._features.remove(feature)
for feature_xml in self.findall('{%s}feature' % self.namespace): for feature_xml in self.xml.findall('{%s}feature' % self.namespace):
if feature_xml.attrib['var'] == feature: if feature_xml.attrib['var'] == feature:
self.xml.remove(feature_xml) self.xml.remove(feature_xml)
return True return True
@ -251,7 +251,7 @@ class DiscoInfo(ElementBase):
features = set() features = set()
else: else:
features = [] features = []
for feature_xml in self.findall('{%s}feature' % self.namespace): for feature_xml in self.xml.findall('{%s}feature' % self.namespace):
if dedupe: if dedupe:
features.add(feature_xml.attrib['var']) features.add(feature_xml.attrib['var'])
else: else:
@ -272,5 +272,5 @@ class DiscoInfo(ElementBase):
def del_features(self): def del_features(self):
"""Remove all features.""" """Remove all features."""
self._features = set() self._features = set()
for feature_xml in self.findall('{%s}feature' % self.namespace): for feature_xml in self.xml.findall('{%s}feature' % self.namespace):
self.xml.remove(feature_xml) self.xml.remove(feature_xml)

View file

@ -95,7 +95,7 @@ class DiscoItems(ElementBase):
node -- Optional extra identifying information. node -- Optional extra identifying information.
""" """
if (jid, node) in self._items: if (jid, node) in self._items:
for item_xml in self.findall('{%s}item' % self.namespace): for item_xml in self.xml.findall('{%s}item' % self.namespace):
item = (item_xml.attrib['jid'], item = (item_xml.attrib['jid'],
item_xml.attrib.get('node', None)) item_xml.attrib.get('node', None))
if item == (jid, node): if item == (jid, node):

View file

@ -100,7 +100,7 @@ class Command(ElementBase):
self.del_actions() self.del_actions()
if values: if values:
self._set_sub_text('{%s}actions' % self.namespace, '', True) self._set_sub_text('{%s}actions' % self.namespace, '', True)
actions = self.find('{%s}actions' % self.namespace) actions = self.xml.find('{%s}actions' % self.namespace)
for val in values: for val in values:
if val in self.next_actions: if val in self.next_actions:
action = ET.Element('{%s}%s' % (self.namespace, val)) action = ET.Element('{%s}%s' % (self.namespace, val))
@ -111,7 +111,7 @@ class Command(ElementBase):
Return the set of allowable next actions. Return the set of allowable next actions.
""" """
actions = set() actions = set()
actions_xml = self.find('{%s}actions' % self.namespace) actions_xml = self.xml.find('{%s}actions' % self.namespace)
if actions_xml is not None: if actions_xml is not None:
for action in self.next_actions: for action in self.next_actions:
action_xml = actions_xml.find('{%s}%s' % (self.namespace, action_xml = actions_xml.find('{%s}%s' % (self.namespace,

View file

@ -70,7 +70,7 @@ class Set(ElementBase):
'count', 'index', 'last', 'max')) 'count', 'index', 'last', 'max'))
def set_first_index(self, val): def set_first_index(self, val):
fi = self.find("{%s}first" % (self.namespace)) fi = self.xml.find("{%s}first" % (self.namespace))
if fi is not None: if fi is not None:
if val: if val:
fi.attrib['index'] = val fi.attrib['index'] = val
@ -82,7 +82,7 @@ class Set(ElementBase):
self.xml.append(fi) self.xml.append(fi)
def get_first_index(self): def get_first_index(self):
fi = self.find("{%s}first" % (self.namespace)) fi = self.xml.find("{%s}first" % (self.namespace))
if fi is not None: if fi is not None:
return fi.attrib.get('index', '') return fi.attrib.get('index', '')

View file

@ -50,7 +50,7 @@ class ChatState(ElementBase):
def get_chat_state(self): def get_chat_state(self):
parent = self.parent() parent = self.parent()
for state in self.states: for state in self.states:
state_xml = parent.find('{%s}%s' % (self.namespace, state)) state_xml = parent.xml.find('{%s}%s' % (self.namespace, state))
if state_xml is not None: if state_xml is not None:
self.xml = state_xml self.xml = state_xml
return state return state
@ -68,7 +68,7 @@ class ChatState(ElementBase):
def del_chat_state(self): def del_chat_state(self):
parent = self.parent() parent = self.parent()
for state in self.states: for state in self.states:
state_xml = parent.find('{%s}%s' % (self.namespace, state)) state_xml = parent.xml.find('{%s}%s' % (self.namespace, state))
if state_xml is not None: if state_xml is not None:
self.xml = ET.Element('') self.xml = ET.Element('')
parent.xml.remove(state_xml) parent.xml.remove(state_xml)

View file

@ -32,7 +32,7 @@ class Request(ElementBase):
def get_request_receipt(self): def get_request_receipt(self):
parent = self.parent() parent = self.parent()
if parent.find("{%s}request" % self.namespace) is not None: if parent.xml.find("{%s}request" % self.namespace) is not None:
return True return True
else: else:
return False return False
@ -63,7 +63,7 @@ class Received(ElementBase):
def get_receipt(self): def get_receipt(self):
parent = self.parent() parent = self.parent()
xml = parent.find("{%s}received" % self.namespace) xml = parent.xml.find("{%s}received" % self.namespace)
if xml is not None: if xml is not None:
return xml.attrib.get('id', '') return xml.attrib.get('id', '')
return '' return ''

View file

@ -99,7 +99,7 @@ class StreamManagement(ElementBase):
interfaces = set(['required', 'optional']) interfaces = set(['required', 'optional'])
def get_required(self): def get_required(self):
return self.find('{%s}required' % self.namespace) is not None return self.xml.find('{%s}required' % self.namespace) is not None
def set_required(self, val): def set_required(self, val):
self.del_required() self.del_required()
@ -110,7 +110,7 @@ class StreamManagement(ElementBase):
self._del_sub('required') self._del_sub('required')
def get_optional(self): def get_optional(self):
return self.find('{%s}optional' % self.namespace) is not None return self.xml.find('{%s}optional' % self.namespace) is not None
def set_optional(self, val): def set_optional(self, val):
self.del_optional() self.del_optional()

View file

@ -540,7 +540,7 @@ class XEP_0325(BasePlugin):
fields = [f['name'] for f in iq['setResponse']['datas']] fields = [f['name'] for f in iq['setResponse']['datas']]
error_msg = None error_msg = None
if not iq['setResponse'].find('error') is None and not iq['setResponse']['error']['text'] == "": if not iq['setResponse'].xml.find('error') is None and not iq['setResponse']['error']['text'] == "":
error_msg = iq['setResponse']['error']['text'] error_msg = iq['setResponse']['error']['text']
callback = self.sessions[seqnr]["callback"] callback = self.sessions[seqnr]["callback"]

View file

@ -1123,36 +1123,6 @@ class ElementBase(object):
# Everything matched. # Everything matched.
return True return True
def find(self, xpath):
"""Find an XML object in this stanza given an XPath expression.
Exposes ElementTree interface for backwards compatibility.
.. note::
Matching on attribute values is not supported in Python 2.6
or Python 3.1
:param string xpath: An XPath expression matching a single
desired element.
"""
return self.xml.find(xpath)
def findall(self, xpath):
"""Find multiple XML objects in this stanza given an XPath expression.
Exposes ElementTree interface for backwards compatibility.
.. note::
Matching on attribute values is not supported in Python 2.6
or Python 3.1.
:param string xpath: An XPath expression matching multiple
desired elements.
"""
return self.xml.findall(xpath)
def get(self, key, default=None): def get(self, key, default=None):
"""Return the value of a stanza interface. """Return the value of a stanza interface.