Underscore names by default.

Stanza objects now accept the use of underscored names.

The CamelCase versions are still available for backwards compatibility,
but are discouraged.

The property stanza.values now maps to the old getStanzaValues and
setStanzaValues, in addition to _set_stanza_values and
_get_stanza_values.
This commit is contained in:
Lance Stout 2010-10-17 21:38:22 -04:00
parent faec86b3be
commit 4375ac7d8b
23 changed files with 452 additions and 298 deletions

View file

@ -21,7 +21,7 @@ from sleekxmpp.stanza.nick import Nick
from sleekxmpp.stanza.htmlim import HTMLIM from sleekxmpp.stanza.htmlim import HTMLIM
from sleekxmpp.xmlstream import XMLStream, JID, tostring from sleekxmpp.xmlstream import XMLStream, JID, tostring
from sleekxmpp.xmlstream.stanzabase import ET, registerStanzaPlugin from sleekxmpp.xmlstream import ET, register_stanza_plugin
from sleekxmpp.xmlstream.matcher import * from sleekxmpp.xmlstream.matcher import *
from sleekxmpp.xmlstream.handler import * from sleekxmpp.xmlstream.handler import *
@ -142,9 +142,9 @@ class BaseXMPP(XMLStream):
self.registerStanza(Presence) self.registerStanza(Presence)
# Initialize a few default stanza plugins. # Initialize a few default stanza plugins.
registerStanzaPlugin(Iq, Roster) register_stanza_plugin(Iq, Roster)
registerStanzaPlugin(Message, Nick) register_stanza_plugin(Message, Nick)
registerStanzaPlugin(Message, HTMLIM) register_stanza_plugin(Message, HTMLIM)
def process(self, *args, **kwargs): def process(self, *args, **kwargs):
""" """
@ -256,8 +256,8 @@ class BaseXMPP(XMLStream):
Defaults to 0. Defaults to 0.
ifrom -- The from JID to use for this stanza. ifrom -- The from JID to use for this stanza.
""" """
return self.Iq().setStanzaValues({'id': str(id), return self.Iq()._set_stanza_values({'id': str(id),
'from': ifrom}) 'from': ifrom})
def make_iq_get(self, queryxmlns=None): def make_iq_get(self, queryxmlns=None):
""" """
@ -268,8 +268,8 @@ class BaseXMPP(XMLStream):
Arguments: Arguments:
queryxmlns -- The namespace of the query to use. queryxmlns -- The namespace of the query to use.
""" """
return self.Iq().setStanzaValues({'type': 'get', return self.Iq()._set_stanza_values({'type': 'get',
'query': queryxmlns}) 'query': queryxmlns})
def make_iq_result(self, id): def make_iq_result(self, id):
""" """
@ -278,8 +278,8 @@ class BaseXMPP(XMLStream):
Arguments: Arguments:
id -- An ideally unique ID value. May use self.new_id(). id -- An ideally unique ID value. May use self.new_id().
""" """
return self.Iq().setStanzaValues({'id': id, return self.Iq()._set_stanza_values({'id': id,
'type': 'result'}) 'type': 'result'})
def make_iq_set(self, sub=None): def make_iq_set(self, sub=None):
""" """
@ -291,7 +291,7 @@ class BaseXMPP(XMLStream):
Arguments: Arguments:
sub -- A stanza or XML object to use as the Iq's payload. sub -- A stanza or XML object to use as the Iq's payload.
""" """
iq = self.Iq().setStanzaValues({'type': 'set'}) iq = self.Iq()._set_stanza_values({'type': 'set'})
if sub != None: if sub != None:
iq.append(sub) iq.append(sub)
return iq return iq
@ -309,10 +309,10 @@ class BaseXMPP(XMLStream):
Defaults to 'feature-not-implemented'. Defaults to 'feature-not-implemented'.
text -- A message describing the cause of the error. text -- A message describing the cause of the error.
""" """
iq = self.Iq().setStanzaValues({'id': id}) iq = self.Iq()._set_stanza_values({'id': id})
iq['error'].setStanzaValues({'type': type, iq['error']._set_stanza_values({'type': type,
'condition': condition, 'condition': condition,
'text': text}) 'text': text})
return iq return iq
def make_iq_query(self, iq=None, xmlns=''): def make_iq_query(self, iq=None, xmlns=''):

View file

@ -19,9 +19,9 @@ from sleekxmpp import stanza
from sleekxmpp.basexmpp import BaseXMPP from sleekxmpp.basexmpp import BaseXMPP
from sleekxmpp.stanza import Message, Presence, Iq from sleekxmpp.stanza import Message, Presence, Iq
from sleekxmpp.xmlstream import XMLStream, RestartStream from sleekxmpp.xmlstream import XMLStream, RestartStream
from sleekxmpp.xmlstream import StanzaBase, ET
from sleekxmpp.xmlstream.matcher import * from sleekxmpp.xmlstream.matcher import *
from sleekxmpp.xmlstream.handler import * from sleekxmpp.xmlstream.handler import *
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
# Flag indicating if DNS SRV records are available for use. # Flag indicating if DNS SRV records are available for use.
SRV_SUPPORT = True SRV_SUPPORT = True
@ -203,7 +203,7 @@ class ClientXMPP(BaseXMPP):
to 'remove', the entry will be deleted. to 'remove', the entry will be deleted.
groups -- The roster groups that contain this item. groups -- The roster groups that contain this item.
""" """
iq = self.Iq().setStanzaValues({'type': 'set'}) iq = self.Iq()._set_stanza_values({'type': 'set'})
iq['roster']['items'] = {jid: {'name': name, iq['roster']['items'] = {jid: {'name': name,
'subscription': subscription, 'subscription': subscription,
'groups': groups}} 'groups': groups}}
@ -222,7 +222,7 @@ class ClientXMPP(BaseXMPP):
def get_roster(self): def get_roster(self):
"""Request the roster from the server.""" """Request the roster from the server."""
iq = self.Iq().setStanzaValues({'type': 'get'}).enable('roster') iq = self.Iq()._set_stanza_values({'type': 'get'}).enable('roster')
iq.send() iq.send()
self._handle_roster(iq, request=True) self._handle_roster(iq, request=True)

View file

@ -17,9 +17,9 @@ from sleekxmpp import plugins
from sleekxmpp import stanza from sleekxmpp import stanza
from sleekxmpp.basexmpp import BaseXMPP, SRV_SUPPORT from sleekxmpp.basexmpp import BaseXMPP, SRV_SUPPORT
from sleekxmpp.xmlstream import XMLStream, RestartStream from sleekxmpp.xmlstream import XMLStream, RestartStream
from sleekxmpp.xmlstream import StanzaBase, ET
from sleekxmpp.xmlstream.matcher import * from sleekxmpp.xmlstream.matcher import *
from sleekxmpp.xmlstream.handler import * from sleekxmpp.xmlstream.handler import *
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
class ComponentXMPP(BaseXMPP): class ComponentXMPP(BaseXMPP):

View file

@ -6,8 +6,7 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
class Error(ElementBase): class Error(ElementBase):
@ -40,13 +39,13 @@ class Error(ElementBase):
should be treated. should be treated.
Methods: Methods:
setup -- Overrides ElementBase.setup. setup -- Overrides ElementBase.setup.
getCondition -- Retrieve the name of the condition element. get_condition -- Retrieve the name of the condition element.
setCondition -- Add a condition element. set_condition -- Add a condition element.
delCondition -- Remove the condition element. del_condition -- Remove the condition element.
getText -- Retrieve the contents of the <text> element. get_text -- Retrieve the contents of the <text> element.
setText -- Set the contents of the <text> element. set_text -- Set the contents of the <text> element.
delText -- Remove the <text> element. del_text -- Remove the <text> element.
""" """
namespace = 'jabber:client' namespace = 'jabber:client'
@ -78,6 +77,15 @@ class Error(ElementBase):
Arguments: Arguments:
xml -- Use an existing XML object for the stanza's values. xml -- Use an existing XML object for the stanza's values.
""" """
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.getCondition = self.get_condition
self.setCondition = self.set_condition
self.delCondition = self.del_condition
self.getText = self.get_text
self.setText = self.set_text
self.delText = self.del_text
if ElementBase.setup(self, xml): if ElementBase.setup(self, xml):
#If we had to generate XML then set default values. #If we had to generate XML then set default values.
self['type'] = 'cancel' self['type'] = 'cancel'
@ -85,14 +93,14 @@ class Error(ElementBase):
if self.parent is not None: if self.parent is not None:
self.parent()['type'] = 'error' self.parent()['type'] = 'error'
def getCondition(self): def get_condition(self):
"""Return the condition element's name.""" """Return the condition element's name."""
for child in self.xml.getchildren(): for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
return child.tag.split('}', 1)[-1] return child.tag.split('}', 1)[-1]
return '' return ''
def setCondition(self, value): def set_condition(self, value):
""" """
Set the tag name of the condition element. Set the tag name of the condition element.
@ -104,7 +112,7 @@ class Error(ElementBase):
self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value))) self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
return self return self
def delCondition(self): def del_condition(self):
"""Remove the condition element.""" """Remove the condition element."""
for child in self.xml.getchildren(): for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
@ -113,21 +121,21 @@ class Error(ElementBase):
self.xml.remove(child) self.xml.remove(child)
return self return self
def getText(self): def get_text(self):
"""Retrieve the contents of the <text> element.""" """Retrieve the contents of the <text> element."""
return self._getSubText('{%s}text' % self.condition_ns) return self._get_sub_text('{%s}text' % self.condition_ns)
def setText(self, value): def set_text(self, value):
""" """
Set the contents of the <text> element. Set the contents of the <text> element.
Arguments: Arguments:
value -- The new contents for the <text> element. value -- The new contents for the <text> element.
""" """
self._setSubText('{%s}text' % self.condition_ns, text=value) self._set_sub_text('{%s}text' % self.condition_ns, text=value)
return self return self
def delText(self): def del_text(self):
"""Remove the <text> element.""" """Remove the <text> element."""
self._delSub('{%s}text' % self.condition_ns) self._del_sub('{%s}text' % self.condition_ns)
return self return self

View file

@ -7,8 +7,7 @@
""" """
from sleekxmpp.stanza import Message from sleekxmpp.stanza import Message
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
class HTMLIM(ElementBase): class HTMLIM(ElementBase):
@ -36,9 +35,10 @@ class HTMLIM(ElementBase):
body -- The contents of the HTML body tag. body -- The contents of the HTML body tag.
Methods: Methods:
getBody -- Return the HTML body contents. setup -- Overrides ElementBase.setup.
setBody -- Set the HTML body contents. get_body -- Return the HTML body contents.
delBody -- Remove the HTML body contents. set_body -- Set the HTML body contents.
del_body -- Remove the HTML body contents.
""" """
namespace = 'http://jabber.org/protocol/xhtml-im' namespace = 'http://jabber.org/protocol/xhtml-im'
@ -46,7 +46,24 @@ class HTMLIM(ElementBase):
interfaces = set(('body',)) interfaces = set(('body',))
plugin_attrib = name plugin_attrib = name
def setBody(self, html): def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides StanzaBase.setup.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setBody = self.set_body
self.getBody = self.get_body
self.delBody = self.del_body
return ElementBase.setup(self, xml)
def set_body(self, html):
""" """
Set the contents of the HTML body. Set the contents of the HTML body.
@ -64,17 +81,17 @@ class HTMLIM(ElementBase):
else: else:
self.xml.append(html) self.xml.append(html)
def getBody(self): def get_body(self):
"""Return the contents of the HTML body.""" """Return the contents of the HTML body."""
html = self.xml.find('{http://www.w3.org/1999/xhtml}body') html = self.xml.find('{http://www.w3.org/1999/xhtml}body')
if html is None: if html is None:
return '' return ''
return html return html
def delBody(self): def del_body(self):
"""Remove the HTML body contents.""" """Remove the HTML body contents."""
if self.parent is not None: if self.parent is not None:
self.parent().xml.remove(self.xml) self.parent().xml.remove(self.xml)
registerStanzaPlugin(Message, HTMLIM) register_stanza_plugin(Message, HTMLIM)

View file

@ -8,8 +8,7 @@
from sleekxmpp.stanza import Error from sleekxmpp.stanza import Error
from sleekxmpp.stanza.rootstanza import RootStanza from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream import RESPONSE_TIMEOUT from sleekxmpp.xmlstream import RESPONSE_TIMEOUT, StanzaBase, ET
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
from sleekxmpp.xmlstream.handler import Waiter from sleekxmpp.xmlstream.handler import Waiter
from sleekxmpp.xmlstream.matcher import MatcherId from sleekxmpp.xmlstream.matcher import MatcherId
@ -53,14 +52,14 @@ class Iq(RootStanza):
types -- May be one of: get, set, result, or error. types -- May be one of: get, set, result, or error.
Methods: Methods:
__init__ -- Overrides StanzaBase.__init__. __init__ -- Overrides StanzaBase.__init__.
unhandled -- Send error if there are no handlers. unhandled -- Send error if there are no handlers.
setPayload -- Overrides StanzaBase.setPayload. set_payload -- Overrides StanzaBase.set_payload.
setQuery -- Add or modify a <query> element. set_query -- Add or modify a <query> element.
getQuery -- Return the namespace of the <query> element. get_query -- Return the namespace of the <query> element.
delQuery -- Remove the <query> element. del_query -- Remove the <query> element.
reply -- Overrides StanzaBase.reply reply -- Overrides StanzaBase.reply
send -- Overrides StanzaBase.send send -- Overrides StanzaBase.send
""" """
namespace = 'jabber:client' namespace = 'jabber:client'
@ -76,6 +75,13 @@ class Iq(RootStanza):
Overrides StanzaBase.__init__. Overrides StanzaBase.__init__.
""" """
StanzaBase.__init__(self, *args, **kwargs) StanzaBase.__init__(self, *args, **kwargs)
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setPayload = self.set_payload
self.getQuery = self.get_query
self.setQuery = self.set_query
self.delQuery = self.del_query
if self['id'] == '': if self['id'] == '':
if self.stream is not None: if self.stream is not None:
self['id'] = self.stream.getNewId() self['id'] = self.stream.getNewId()
@ -94,7 +100,7 @@ class Iq(RootStanza):
self['error']['text'] = 'No handlers registered for this request.' self['error']['text'] = 'No handlers registered for this request.'
self.send() self.send()
def setPayload(self, value): def set_payload(self, value):
""" """
Set the XML contents of the <iq> stanza. Set the XML contents of the <iq> stanza.
@ -102,10 +108,10 @@ class Iq(RootStanza):
value -- An XML object to use as the <iq> stanza's contents value -- An XML object to use as the <iq> stanza's contents
""" """
self.clear() self.clear()
StanzaBase.setPayload(self, value) StanzaBase.set_payload(self, value)
return self return self
def setQuery(self, value): def set_query(self, value):
""" """
Add or modify a <query> element. Add or modify a <query> element.
@ -121,7 +127,7 @@ class Iq(RootStanza):
self.xml.append(query) self.xml.append(query)
return self return self
def getQuery(self): def get_query(self):
"""Return the namespace of the <query> element.""" """Return the namespace of the <query> element."""
for child in self.xml.getchildren(): for child in self.xml.getchildren():
if child.tag.endswith('query'): if child.tag.endswith('query'):
@ -131,7 +137,7 @@ class Iq(RootStanza):
return ns return ns
return '' return ''
def delQuery(self): def del_query(self):
"""Remove the <query> element.""" """Remove the <query> element."""
for child in self.xml.getchildren(): for child in self.xml.getchildren():
if child.tag.endswith('query'): if child.tag.endswith('query'):

View file

@ -8,7 +8,7 @@
from sleekxmpp.stanza import Error from sleekxmpp.stanza import Error
from sleekxmpp.stanza.rootstanza import RootStanza from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET from sleekxmpp.xmlstream import StanzaBase, ET
class Message(RootStanza): class Message(RootStanza):
@ -42,16 +42,17 @@ class Message(RootStanza):
types -- May be one of: normal, chat, headline, groupchat, or error. types -- May be one of: normal, chat, headline, groupchat, or error.
Methods: Methods:
chat -- Set the message type to 'chat'. setup -- Overrides StanzaBase.setup.
normal -- Set the message type to 'normal'. chat -- Set the message type to 'chat'.
reply -- Overrides StanzaBase.reply normal -- Set the message type to 'normal'.
getType -- Overrides StanzaBase interface reply -- Overrides StanzaBase.reply
getMucroom -- Return the name of the MUC room of the message. get_type -- Overrides StanzaBase interface
setMucroom -- Dummy method to prevent assignment. get_mucroom -- Return the name of the MUC room of the message.
delMucroom -- Dummy method to prevent deletion. set_mucroom -- Dummy method to prevent assignment.
getMucnick -- Return the MUC nickname of the message's sender. del_mucroom -- Dummy method to prevent deletion.
setMucnick -- Dummy method to prevent assignment. get_mucnick -- Return the MUC nickname of the message's sender.
delMucnick -- Dummy method to prevent deletion. set_mucnick -- Dummy method to prevent assignment.
del_mucnick -- Dummy method to prevent deletion.
""" """
namespace = 'jabber:client' namespace = 'jabber:client'
@ -62,7 +63,28 @@ class Message(RootStanza):
plugin_attrib = name plugin_attrib = name
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat')) types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
def getType(self): def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides StanzaBase.setup.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.getType = self.get_type
self.getMucroom = self.get_mucroom
self.setMucroom = self.set_mucroom
self.delMucroom = self.del_mucroom
self.getMucnick = self.get_mucnick
self.setMucnick = self.set_mucnick
self.delMucnick = self.del_mucnick
return StanzaBase.setup(self, xml)
def get_type(self):
""" """
Return the message type. Return the message type.
@ -70,7 +92,7 @@ class Message(RootStanza):
Returns 'normal' if no type attribute is present. Returns 'normal' if no type attribute is present.
""" """
return self._getAttr('type', 'normal') return self._get_attr('type', 'normal')
def chat(self): def chat(self):
"""Set the message type to 'chat'.""" """Set the message type to 'chat'."""
@ -104,7 +126,7 @@ class Message(RootStanza):
self['body'] = body self['body'] = body
return self return self
def getMucroom(self): def get_mucroom(self):
""" """
Return the name of the MUC room where the message originated. Return the name of the MUC room where the message originated.
@ -115,7 +137,7 @@ class Message(RootStanza):
else: else:
return '' return ''
def getMucnick(self): def get_mucnick(self):
""" """
Return the nickname of the MUC user that sent the message. Return the nickname of the MUC user that sent the message.
@ -126,18 +148,18 @@ class Message(RootStanza):
else: else:
return '' return ''
def setMucroom(self, value): def set_mucroom(self, value):
"""Dummy method to prevent modification.""" """Dummy method to prevent modification."""
pass pass
def delMucroom(self): def del_mucroom(self):
"""Dummy method to prevent deletion.""" """Dummy method to prevent deletion."""
pass pass
def setMucnick(self, value): def set_mucnick(self, value):
"""Dummy method to prevent modification.""" """Dummy method to prevent modification."""
pass pass
def delMucnick(self): def del_mucnick(self):
"""Dummy method to prevent deletion.""" """Dummy method to prevent deletion."""
pass pass

View file

@ -7,8 +7,7 @@
""" """
from sleekxmpp.stanza import Message, Presence from sleekxmpp.stanza import Message, Presence
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
class Nick(ElementBase): class Nick(ElementBase):
@ -39,9 +38,10 @@ class Nick(ElementBase):
nick -- A global, friendly or informal name chosen by a user. nick -- A global, friendly or informal name chosen by a user.
Methods: Methods:
getNick -- Return the nickname in the <nick> element. setup -- Overrides ElementBase.setup.
setNick -- Add a <nick> element with the given nickname. get_nick -- Return the nickname in the <nick> element.
delNick -- Remove the <nick> element. set_nick -- Add a <nick> element with the given nickname.
del_nick -- Remove the <nick> element.
""" """
namespace = 'http://jabber.org/nick/nick' namespace = 'http://jabber.org/nick/nick'
@ -49,7 +49,24 @@ class Nick(ElementBase):
plugin_attrib = name plugin_attrib = name
interfaces = set(('nick',)) interfaces = set(('nick',))
def setNick(self, nick): def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides StanzaBase.setup.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setNick = self.set_nick
self.getNick = self.get_nick
self.delNick = self.del_nick
return ElementBase.setup(self, xml)
def set_nick(self, nick):
""" """
Add a <nick> element with the given nickname. Add a <nick> element with the given nickname.
@ -58,15 +75,15 @@ class Nick(ElementBase):
""" """
self.xml.text = nick self.xml.text = nick
def getNick(self): def get_nick(self):
"""Return the nickname in the <nick> element.""" """Return the nickname in the <nick> element."""
return self.xml.text return self.xml.text
def delNick(self): def del_nick(self):
"""Remove the <nick> element.""" """Remove the <nick> element."""
if self.parent is not None: if self.parent is not None:
self.parent().xml.remove(self.xml) self.parent().xml.remove(self.xml)
registerStanzaPlugin(Message, Nick) register_stanza_plugin(Message, Nick)
registerStanzaPlugin(Presence, Nick) register_stanza_plugin(Presence, Nick)

View file

@ -8,7 +8,7 @@
from sleekxmpp.stanza import Error from sleekxmpp.stanza import Error
from sleekxmpp.stanza.rootstanza import RootStanza from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET from sleekxmpp.xmlstream import StanzaBase, ET
class Presence(RootStanza): class Presence(RootStanza):
@ -52,12 +52,13 @@ class Presence(RootStanza):
showtypes -- One of: away, chat, dnd, and xa. showtypes -- One of: away, chat, dnd, and xa.
Methods: Methods:
reply -- Overrides StanzaBase.reply setup -- Overrides StanzaBase.setup
setShow -- Set the value of the <show> element. reply -- Overrides StanzaBase.reply
getType -- Get the value of the type attribute or <show> element. set_show -- Set the value of the <show> element.
setType -- Set the value of the type attribute or <show> element. get_type -- Get the value of the type attribute or <show> element.
getPriority -- Get the value of the <priority> element. set_type -- Set the value of the type attribute or <show> element.
setPriority -- Set the value of the <priority> element. get_priority -- Get the value of the <priority> element.
set_priority -- Set the value of the <priority> element.
""" """
namespace = 'jabber:client' namespace = 'jabber:client'
@ -71,7 +72,27 @@ class Presence(RootStanza):
'subscribed', 'unsubscribe', 'unsubscribed')) 'subscribed', 'unsubscribe', 'unsubscribed'))
showtypes = set(('dnd', 'chat', 'xa', 'away')) showtypes = set(('dnd', 'chat', 'xa', 'away'))
def setShow(self, show): def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides ElementBase.setup.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setShow = self.set_show
self.getType = self.get_type
self.setType = self.set_type
self.delType = self.get_type
self.getPriority = self.get_priority
self.setPriority = self.set_priority
return StanzaBase.setup(self, xml)
def set_show(self, show):
""" """
Set the value of the <show> element. Set the value of the <show> element.
@ -79,12 +100,24 @@ class Presence(RootStanza):
show -- Must be one of: away, chat, dnd, or xa. show -- Must be one of: away, chat, dnd, or xa.
""" """
if show is None: if show is None:
self._delSub('show') self._del_sub('show')
elif show in self.showtypes: elif show in self.showtypes:
self._setSubText('show', text=show) self._set_sub_text('show', text=show)
return self return self
def setType(self, value): def get_type(self):
"""
Return the value of the <presence> stanza's type attribute, or
the value of the <show> element.
"""
out = self._get_attr('type')
if not out:
out = self['show']
if not out or out is None:
out = 'available'
return out
def set_type(self, value):
""" """
Set the type attribute's value, and the <show> element Set the type attribute's value, and the <show> element
if applicable. if applicable.
@ -96,19 +129,19 @@ class Presence(RootStanza):
self['show'] = None self['show'] = None
if value == 'available': if value == 'available':
value = '' value = ''
self._setAttr('type', value) self._set_attr('type', value)
elif value in self.showtypes: elif value in self.showtypes:
self['show'] = value self['show'] = value
return self return self
def delType(self): def del_type(self):
""" """
Remove both the type attribute and the <show> element. Remove both the type attribute and the <show> element.
""" """
self._delAttr('type') self._del_attr('type')
self._delSub('show') self._del_sub('show')
def setPriority(self, value): def set_priority(self, value):
""" """
Set the entity's priority value. Some server use priority to Set the entity's priority value. Some server use priority to
determine message routing behavior. determine message routing behavior.
@ -119,13 +152,13 @@ class Presence(RootStanza):
Arguments: Arguments:
value -- An integer value greater than or equal to 0. value -- An integer value greater than or equal to 0.
""" """
self._setSubText('priority', text=str(value)) self._set_sub_text('priority', text=str(value))
def getPriority(self): def get_priority(self):
""" """
Return the value of the <presence> element as an integer. Return the value of the <presence> element as an integer.
""" """
p = self._getSubText('priority') p = self._get_sub_text('priority')
if not p: if not p:
p = 0 p = 0
try: try:
@ -134,18 +167,6 @@ class Presence(RootStanza):
# The priority is not a number: we consider it 0 as a default # The priority is not a number: we consider it 0 as a default
return 0 return 0
def getType(self):
"""
Return the value of the <presence> stanza's type attribute, or
the value of the <show> element.
"""
out = self._getAttr('type')
if not out:
out = self['show']
if not out or out is None:
out = 'available'
return out
def reply(self): def reply(self):
""" """
Set the appropriate presence reply type. Set the appropriate presence reply type.

View file

@ -12,7 +12,7 @@ import sys
from sleekxmpp.exceptions import XMPPError from sleekxmpp.exceptions import XMPPError
from sleekxmpp.stanza import Error from sleekxmpp.stanza import Error
from sleekxmpp.xmlstream.stanzabase import ET, StanzaBase, registerStanzaPlugin from sleekxmpp.xmlstream import ET, StanzaBase, register_stanza_plugin
class RootStanza(StanzaBase): class RootStanza(StanzaBase):
@ -63,4 +63,4 @@ class RootStanza(StanzaBase):
self.send() self.send()
registerStanzaPlugin(RootStanza, Error) register_stanza_plugin(RootStanza, Error)

View file

@ -8,8 +8,7 @@
from sleekxmpp.stanza import Iq from sleekxmpp.stanza import Iq
from sleekxmpp.xmlstream import JID from sleekxmpp.xmlstream import JID
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin from sleekxmpp.xmlstream import ET, ElementBase, register_stanza_plugin
from sleekxmpp.xmlstream.stanzabase import ET, ElementBase
class Roster(ElementBase): class Roster(ElementBase):
@ -29,9 +28,9 @@ class Roster(ElementBase):
in the stanza. in the stanza.
Methods: Methods:
getItems -- Return a dictionary of roster entries. get_items -- Return a dictionary of roster entries.
setItems -- Add <item> elements. set_items -- Add <item> elements.
delItems -- Remove all <item> elements. del_items -- Remove all <item> elements.
""" """
namespace = 'jabber:iq:roster' namespace = 'jabber:iq:roster'
@ -39,7 +38,24 @@ class Roster(ElementBase):
plugin_attrib = 'roster' plugin_attrib = 'roster'
interfaces = set(('items',)) interfaces = set(('items',))
def setItems(self, items): def setup(self, xml=None):
"""
Populate the stanza object using an optional XML object.
Overrides StanzaBase.setup.
Arguments:
xml -- Use an existing XML object for the stanza's values.
"""
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setItems = self.set_items
self.getItems = self.get_items
self.delItems = self.del_items
return ElementBase.setup(self, xml)
def set_items(self, items):
""" """
Set the roster entries in the <roster> stanza. Set the roster entries in the <roster> stanza.
@ -54,7 +70,7 @@ class Roster(ElementBase):
Arguments: Arguments:
items -- A dictionary of roster entries. items -- A dictionary of roster entries.
""" """
self.delItems() self.del_items()
for jid in items: for jid in items:
ijid = str(jid) ijid = str(jid)
item = ET.Element('{jabber:iq:roster}item', {'jid': ijid}) item = ET.Element('{jabber:iq:roster}item', {'jid': ijid})
@ -72,7 +88,7 @@ class Roster(ElementBase):
self.xml.append(item) self.xml.append(item)
return self return self
def getItems(self): def get_items(self):
""" """
Return a dictionary of roster entries. Return a dictionary of roster entries.
@ -98,7 +114,7 @@ class Roster(ElementBase):
items[itemxml.get('jid')] = item items[itemxml.get('jid')] = item
return items return items
def delItems(self): def del_items(self):
""" """
Remove all <item> elements from the roster stanza. Remove all <item> elements from the roster stanza.
""" """
@ -106,4 +122,4 @@ class Roster(ElementBase):
self.xml.remove(child) self.xml.remove(child)
registerStanzaPlugin(Iq, Roster) register_stanza_plugin(Iq, Roster)

View file

@ -13,8 +13,7 @@ import sleekxmpp
from sleekxmpp import ClientXMPP, ComponentXMPP from sleekxmpp import ClientXMPP, ComponentXMPP
from sleekxmpp.stanza import Message, Iq, Presence from sleekxmpp.stanza import Message, Iq, Presence
from sleekxmpp.test import TestSocket, TestLiveSocket from sleekxmpp.test import TestSocket, TestLiveSocket
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin, ET from sleekxmpp.xmlstream import StanzaBase, ET, register_stanza_plugin
from sleekxmpp.xmlstream.stanzabase import StanzaBase
from sleekxmpp.xmlstream.tostring import tostring from sleekxmpp.xmlstream.tostring import tostring

View file

@ -9,6 +9,7 @@
from sleekxmpp.xmlstream.jid import JID from sleekxmpp.xmlstream.jid import JID
from sleekxmpp.xmlstream.scheduler import Scheduler from sleekxmpp.xmlstream.scheduler import Scheduler
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ElementBase, ET from sleekxmpp.xmlstream.stanzabase import StanzaBase, ElementBase, ET
from sleekxmpp.xmlstream.stanzabase import register_stanza_plugin
from sleekxmpp.xmlstream.tostring import tostring from sleekxmpp.xmlstream.tostring import tostring
from sleekxmpp.xmlstream.xmlstream import XMLStream, RESPONSE_TIMEOUT from sleekxmpp.xmlstream.xmlstream import XMLStream, RESPONSE_TIMEOUT
from sleekxmpp.xmlstream.xmlstream import RestartStream from sleekxmpp.xmlstream.xmlstream import RestartStream

View file

@ -20,7 +20,7 @@ from sleekxmpp.xmlstream.tostring import tostring
XML_TYPE = type(ET.Element('xml')) XML_TYPE = type(ET.Element('xml'))
def registerStanzaPlugin(stanza, plugin): def register_stanza_plugin(stanza, plugin):
""" """
Associate a stanza object as a plugin for another stanza. Associate a stanza object as a plugin for another stanza.
@ -33,6 +33,10 @@ def registerStanzaPlugin(stanza, plugin):
stanza.plugin_tag_map[tag] = plugin stanza.plugin_tag_map[tag] = plugin
# To maintain backwards compatibility for now, preserve the camel case name.
registerStanzaPlugin = register_stanza_plugin
class ElementBase(object): class ElementBase(object):
""" """
@ -78,9 +82,9 @@ class ElementBase(object):
... plugin_attrib = "custom" ... plugin_attrib = "custom"
The plugin stanza class must be associated with its intended The plugin stanza class must be associated with its intended
container stanza by using registerStanzaPlugin as so: container stanza by using register_stanza_plugin as so:
>>> registerStanzaPlugin(Message, MessagePlugin) >>> register_stanza_plugin(Message, MessagePlugin)
The plugin may then be accessed as if it were built-in to the parent The plugin may then be accessed as if it were built-in to the parent
stanza. stanza.
@ -115,38 +119,43 @@ class ElementBase(object):
parent -- The parent stanza of this stanza. parent -- The parent stanza of this stanza.
plugins -- A map of enabled plugin names with the plugins -- A map of enabled plugin names with the
initialized plugin stanza objects. initialized plugin stanza objects.
values -- A dictionary of the stanza's interfaces
and interface values, including plugins.
Methods: Methods:
setup -- Initialize the stanza's XML contents. setup -- Initialize the stanza's XML contents.
enable -- Instantiate a stanza plugin. Alias for initPlugin. enable -- Instantiate a stanza plugin.
initPlugin -- Instantiate a stanza plugin. Alias for init_plugin.
getStanzaValues -- Return a dictionary of stanza interfaces and init_plugin -- Instantiate a stanza plugin.
their values. _get_stanza_values -- Return a dictionary of stanza interfaces and
setStanzaValues -- Set stanza interface values given a dictionary of their values.
interfaces and values. _set_stanza_values -- Set stanza interface values given a dictionary
__getitem__ -- Return the value of a stanza interface. of interfaces and values.
__setitem__ -- Set the value of a stanza interface. __getitem__ -- Return the value of a stanza interface.
__delitem__ -- Remove the value of a stanza interface. __setitem__ -- Set the value of a stanza interface.
_setAttr -- Set an attribute value of the main stanza element. __delitem__ -- Remove the value of a stanza interface.
_delAttr -- Remove an attribute from the main stanza element. _set_attr -- Set an attribute value of the main
_getAttr -- Return an attribute's value from the main stanza element.
stanza element. _del_attr -- Remove an attribute from the main
_getSubText -- Return the text contents of a subelement. stanza element.
_setSubText -- Set the text contents of a subelement. _get_attr -- Return an attribute's value from the main
_delSub -- Remove a subelement. stanza element.
match -- Compare the stanza against an XPath expression. _get_sub_text -- Return the text contents of a subelement.
find -- Return subelement matching an XPath expression. _set_sub_ext -- Set the text contents of a subelement.
findall -- Return subelements matching an XPath expression. _del_sub -- Remove a subelement.
get -- Return the value of a stanza interface, with an match -- Compare the stanza against an XPath expression.
optional default value. find -- Return subelement matching an XPath expression.
keys -- Return the set of interface names accepted by findall -- Return subelements matching an XPath expression.
the stanza. get -- Return the value of a stanza interface, with an
append -- Add XML content or a substanza to the stanza. optional default value.
appendxml -- Add XML content to the stanza. keys -- Return the set of interface names accepted by
pop -- Remove a substanza. the stanza.
next -- Return the next iterable substanza. append -- Add XML content or a substanza to the stanza.
_fix_ns -- Apply the stanza's namespace to non-namespaced appendxml -- Add XML content to the stanza.
elements in an XPath expression. pop -- Remove a substanza.
next -- Return the next iterable substanza.
_fix_ns -- Apply the stanza's namespace to non-namespaced
elements in an XPath expression.
""" """
name = 'stanza' name = 'stanza'
@ -167,6 +176,18 @@ class ElementBase(object):
xml -- Initialize the stanza with optional existing XML. xml -- Initialize the stanza with optional existing XML.
parent -- Optional stanza object that contains this stanza. parent -- Optional stanza object that contains this stanza.
""" """
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.initPlugin = self.init_plugin
self._getAttr = self._get_attr
self._setAttr = self._set_attr
self._delAttr = self._del_attr
self._getSubText = self._get_sub_text
self._setSubText = self._set_sub_text
self._delSub = self._del_sub
self.getStanzaValues = self._get_stanza_values
self.setStanzaValues = self._set_stanza_values
self.xml = xml self.xml = xml
self.plugins = {} self.plugins = {}
self.iterables = [] self.iterables = []
@ -176,6 +197,9 @@ class ElementBase(object):
else: else:
self.parent = weakref.ref(parent) self.parent = weakref.ref(parent)
ElementBase.values = property(ElementBase._get_stanza_values,
ElementBase._set_stanza_values)
if self.setup(xml): if self.setup(xml):
# If we generated our own XML, then everything is ready. # If we generated our own XML, then everything is ready.
return return
@ -227,14 +251,14 @@ class ElementBase(object):
""" """
Enable and initialize a stanza plugin. Enable and initialize a stanza plugin.
Alias for initPlugin. Alias for init_plugin.
Arguments: Arguments:
attrib -- The stanza interface for the plugin. attrib -- The stanza interface for the plugin.
""" """
return self.initPlugin(attrib) return self.init_plugin(attrib)
def initPlugin(self, attrib): def init_plugin(self, attrib):
""" """
Enable and initialize a stanza plugin. Enable and initialize a stanza plugin.
@ -246,7 +270,7 @@ class ElementBase(object):
self.plugins[attrib] = plugin_class(parent=self) self.plugins[attrib] = plugin_class(parent=self)
return self return self
def getStanzaValues(self): def _get_stanza_values(self):
""" """
Return a dictionary of the stanza's interface values. Return a dictionary of the stanza's interface values.
@ -256,18 +280,18 @@ class ElementBase(object):
for interface in self.interfaces: for interface in self.interfaces:
values[interface] = self[interface] values[interface] = self[interface]
for plugin, stanza in self.plugins.items(): for plugin, stanza in self.plugins.items():
values[plugin] = stanza.getStanzaValues() values[plugin] = stanza._get_stanza_values()
if self.iterables: if self.iterables:
iterables = [] iterables = []
for stanza in self.iterables: for stanza in self.iterables:
iterables.append(stanza.getStanzaValues()) iterables.append(stanza._get_stanza_values())
iterables[-1].update({ iterables[-1].update({
'__childtag__': "{%s}%s" % (stanza.namespace, '__childtag__': "{%s}%s" % (stanza.namespace,
stanza.name)}) stanza.name)})
values['substanzas'] = iterables values['substanzas'] = iterables
return values return values
def setStanzaValues(self, values): def _set_stanza_values(self, values):
""" """
Set multiple stanza interface values using a dictionary. Set multiple stanza interface values using a dictionary.
@ -287,15 +311,15 @@ class ElementBase(object):
subclass.name) subclass.name)
if subdict['__childtag__'] == child_tag: if subdict['__childtag__'] == child_tag:
sub = subclass(parent=self) sub = subclass(parent=self)
sub.setStanzaValues(subdict) sub._set_stanza_values(subdict)
self.iterables.append(sub) self.iterables.append(sub)
break break
elif interface in self.interfaces: elif interface in self.interfaces:
self[interface] = value self[interface] = value
elif interface in self.plugin_attrib_map: elif interface in self.plugin_attrib_map:
if interface not in self.plugins: if interface not in self.plugins:
self.initPlugin(interface) self.init_plugin(interface)
self.plugins[interface].setStanzaValues(value) self.plugins[interface]._set_stanza_values(value)
return self return self
def __getitem__(self, attrib): def __getitem__(self, attrib):
@ -307,17 +331,18 @@ class ElementBase(object):
'Message contents' 'Message contents'
Stanza interfaces are typically mapped directly to the underlying XML Stanza interfaces are typically mapped directly to the underlying XML
object, but can be overridden by the presence of a getAttrib method object, but can be overridden by the presence of a get_attrib method
(or getFoo where the interface is named foo, etc). (or get_foo where the interface is named foo, etc).
The search order for interface value retrieval for an interface The search order for interface value retrieval for an interface
named 'foo' is: named 'foo' is:
1. The list of substanzas. 1. The list of substanzas.
2. The result of calling getFoo. 2. The result of calling get_foo.
3. The contents of the foo subelement, if foo is a sub interface. 3. The result of calling getFoo.
4. The value of the foo attribute of the XML object. 4. The contents of the foo subelement, if foo is a sub interface.
5. The plugin named 'foo' 5. The value of the foo attribute of the XML object.
6. An empty string. 6. The plugin named 'foo'
7. An empty string.
Arguments: Arguments:
attrib -- The name of the requested stanza interface. attrib -- The name of the requested stanza interface.
@ -325,17 +350,20 @@ class ElementBase(object):
if attrib == 'substanzas': if attrib == 'substanzas':
return self.iterables return self.iterables
elif attrib in self.interfaces: elif attrib in self.interfaces:
get_method = "get%s" % attrib.title() get_method = "get_%s" % attrib.lower()
get_method2 = "get%s" % attrib.title()
if hasattr(self, get_method): if hasattr(self, get_method):
return getattr(self, get_method)() return getattr(self, get_method)()
elif hasattr(self, get_method2):
return getattr(self, get_method2)()
else: else:
if attrib in self.sub_interfaces: if attrib in self.sub_interfaces:
return self._getSubText(attrib) return self._get_sub_text(attrib)
else: else:
return self._getAttr(attrib) return self._get_attr(attrib)
elif attrib in self.plugin_attrib_map: elif attrib in self.plugin_attrib_map:
if attrib not in self.plugins: if attrib not in self.plugins:
self.initPlugin(attrib) self.init_plugin(attrib)
return self.plugins[attrib] return self.plugins[attrib]
else: else:
return '' return ''
@ -350,18 +378,19 @@ class ElementBase(object):
'Hi!' 'Hi!'
Stanza interfaces are typically mapped directly to the underlying XML Stanza interfaces are typically mapped directly to the underlying XML
object, but can be overridden by the presence of a setAttrib method object, but can be overridden by the presence of a set_attrib method
(or setFoo where the interface is named foo, etc). (or set_foo where the interface is named foo, etc).
The effect of interface value assignment for an interface The effect of interface value assignment for an interface
named 'foo' will be one of: named 'foo' will be one of:
1. Delete the interface's contents if the value is None. 1. Delete the interface's contents if the value is None.
2. Call setFoo, if it exists. 2. Call set_foo, if it exists.
3. Set the text of a foo element, if foo is in sub_interfaces. 3. Call setFoo, if it exists.
4. Set the value of a top level XML attribute name foo. 4. Set the text of a foo element, if foo is in sub_interfaces.
5. Attempt to pass value to a plugin named foo using the plugin's 5. Set the value of a top level XML attribute name foo.
6. Attempt to pass value to a plugin named foo using the plugin's
foo interface. foo interface.
6. Do nothing. 7. Do nothing.
Arguments: Arguments:
attrib -- The name of the stanza interface to modify. attrib -- The name of the stanza interface to modify.
@ -369,19 +398,22 @@ class ElementBase(object):
""" """
if attrib in self.interfaces: if attrib in self.interfaces:
if value is not None: if value is not None:
set_method = "set%s" % attrib.title() set_method = "set_%s" % attrib.lower()
set_method2 = "set%s" % attrib.title()
if hasattr(self, set_method): if hasattr(self, set_method):
getattr(self, set_method)(value,) getattr(self, set_method)(value,)
elif hasattr(self, set_method2):
getattr(self, set_method2)(value,)
else: else:
if attrib in self.sub_interfaces: if attrib in self.sub_interfaces:
return self._setSubText(attrib, text=value) return self._set_sub_text(attrib, text=value)
else: else:
self._setAttr(attrib, value) self._set_attr(attrib, value)
else: else:
self.__delitem__(attrib) self.__delitem__(attrib)
elif attrib in self.plugin_attrib_map: elif attrib in self.plugin_attrib_map:
if attrib not in self.plugins: if attrib not in self.plugins:
self.initPlugin(attrib) self.init_plugin(attrib)
self.plugins[attrib][attrib] = value self.plugins[attrib][attrib] = value
return self return self
@ -398,29 +430,33 @@ class ElementBase(object):
'' ''
Stanza interfaces are typically mapped directly to the underlyig XML Stanza interfaces are typically mapped directly to the underlyig XML
object, but can be overridden by the presence of a delAttrib method object, but can be overridden by the presence of a del_attrib method
(or delFoo where the interface is named foo, etc). (or del_foo where the interface is named foo, etc).
The effect of deleting a stanza interface value named foo will be The effect of deleting a stanza interface value named foo will be
one of: one of:
1. Call delFoo, if it exists. 1. Call del_foo, if it exists.
2. Delete foo element, if foo is in sub_interfaces. 2. Call delFoo, if it exists.
3. Delete top level XML attribute named foo. 3. Delete foo element, if foo is in sub_interfaces.
4. Remove the foo plugin, if it was loaded. 4. Delete top level XML attribute named foo.
5. Do nothing. 5. Remove the foo plugin, if it was loaded.
6. Do nothing.
Arguments: Arguments:
attrib -- The name of the affected stanza interface. attrib -- The name of the affected stanza interface.
""" """
if attrib in self.interfaces: if attrib in self.interfaces:
del_method = "del%s" % attrib.title() del_method = "del_%s" % attrib.lower()
del_method2 = "del%s" % attrib.title()
if hasattr(self, del_method): if hasattr(self, del_method):
getattr(self, del_method)() getattr(self, del_method)()
elif hasattr(self, del_method2):
getattr(self, del_method2)()
else: else:
if attrib in self.sub_interfaces: if attrib in self.sub_interfaces:
return self._delSub(attrib) return self._del_sub(attrib)
else: else:
self._delAttr(attrib) self._del_attr(attrib)
elif attrib in self.plugin_attrib_map: elif attrib in self.plugin_attrib_map:
if attrib in self.plugins: if attrib in self.plugins:
xml = self.plugins[attrib].xml xml = self.plugins[attrib].xml
@ -428,7 +464,7 @@ class ElementBase(object):
self.xml.remove(xml) self.xml.remove(xml)
return self return self
def _setAttr(self, name, value): def _set_attr(self, name, value):
""" """
Set the value of a top level attribute of the underlying XML object. Set the value of a top level attribute of the underlying XML object.
@ -445,7 +481,7 @@ class ElementBase(object):
else: else:
self.xml.attrib[name] = value self.xml.attrib[name] = value
def _delAttr(self, name): def _del_attr(self, name):
""" """
Remove a top level attribute of the underlying XML object. Remove a top level attribute of the underlying XML object.
@ -455,7 +491,7 @@ class ElementBase(object):
if name in self.xml.attrib: if name in self.xml.attrib:
del self.xml.attrib[name] del self.xml.attrib[name]
def _getAttr(self, name, default=''): def _get_attr(self, name, default=''):
""" """
Return the value of a top level attribute of the underlying Return the value of a top level attribute of the underlying
XML object. XML object.
@ -471,7 +507,7 @@ class ElementBase(object):
""" """
return self.xml.attrib.get(name, default) return self.xml.attrib.get(name, default)
def _getSubText(self, name, default=''): def _get_sub_text(self, name, default=''):
""" """
Return the text contents of a sub element. Return the text contents of a sub element.
@ -491,7 +527,7 @@ class ElementBase(object):
else: else:
return stanza.text return stanza.text
def _setSubText(self, name, text=None, keep=False): def _set_sub_text(self, name, text=None, keep=False):
""" """
Set the text contents of a sub element. Set the text contents of a sub element.
@ -513,7 +549,7 @@ class ElementBase(object):
element = self.xml.find(name) element = self.xml.find(name)
if not text and not keep: if not text and not keep:
return self._delSub(name) return self._del_sub(name)
if element is None: if element is None:
# We need to add the element. If the provided name was # We need to add the element. If the provided name was
@ -534,7 +570,7 @@ class ElementBase(object):
element.text = text element.text = text
return element return element
def _delSub(self, name, all=False): def _del_sub(self, name, all=False):
""" """
Remove sub elements that match the given name or XPath. Remove sub elements that match the given name or XPath.
@ -819,13 +855,13 @@ class ElementBase(object):
return False return False
# Check that this stanza is a superset of the other stanza. # Check that this stanza is a superset of the other stanza.
values = self.getStanzaValues() values = self._get_stanza_values()
for key in other.keys(): for key in other.keys():
if key not in values or values[key] != other[key]: if key not in values or values[key] != other[key]:
return False return False
# Check that the other stanza is a superset of this stanza. # Check that the other stanza is a superset of this stanza.
values = other.getStanzaValues() values = other._get_stanza_values()
for key in self.keys(): for key in self.keys():
if key not in values or values[key] != self[key]: if key not in values or values[key] != self[key]:
return False return False
@ -932,23 +968,23 @@ class StanzaBase(ElementBase):
tag -- The namespaced version of the stanza's name. tag -- The namespaced version of the stanza's name.
Methods: Methods:
setType -- Set the type of the stanza. set_type -- Set the type of the stanza.
getTo -- Return the stanza recipients JID. get_to -- Return the stanza recipients JID.
setTo -- Set the stanza recipient's JID. set_to -- Set the stanza recipient's JID.
getFrom -- Return the stanza sender's JID. get_from -- Return the stanza sender's JID.
setFrom -- Set the stanza sender's JID. set_from -- Set the stanza sender's JID.
getPayload -- Return the stanza's XML contents. get_payload -- Return the stanza's XML contents.
setPayload -- Append to the stanza's XML contents. set_payload -- Append to the stanza's XML contents.
delPayload -- Remove the stanza's XML contents. del_payload -- Remove the stanza's XML contents.
clear -- Reset the stanza's XML contents. clear -- Reset the stanza's XML contents.
reply -- Reset the stanza and modify the 'to' and 'from' reply -- Reset the stanza and modify the 'to' and 'from'
attributes to prepare for sending a reply. attributes to prepare for sending a reply.
error -- Set the stanza's type to 'error'. error -- Set the stanza's type to 'error'.
unhandled -- Callback for when the stanza is not handled by a unhandled -- Callback for when the stanza is not handled by a
stream handler. stream handler.
exception -- Callback for if an exception is raised while exception -- Callback for if an exception is raised while
handling the stanza. handling the stanza.
send -- Send the stanza using the stanza's stream. send -- Send the stanza using the stanza's stream.
""" """
name = 'stanza' name = 'stanza'
@ -970,6 +1006,17 @@ class StanzaBase(ElementBase):
sfrom -- Optional string or JID object of the sender's JID. sfrom -- Optional string or JID object of the sender's JID.
sid -- Optional ID value for the stanza. sid -- Optional ID value for the stanza.
""" """
# To comply with PEP8, method names now use underscores.
# Deprecated method names are re-mapped for backwards compatibility.
self.setType = self.set_type
self.getTo = self.get_to
self.setTo = self.set_to
self.getFrom = self.get_from
self.setFrom = self.set_from
self.getPayload = self.get_payload
self.setPayload = self.set_payload
self.delPayload = self.del_payload
self.stream = stream self.stream = stream
if stream is not None: if stream is not None:
self.namespace = stream.default_ns self.namespace = stream.default_ns
@ -982,7 +1029,7 @@ class StanzaBase(ElementBase):
self['from'] = sfrom self['from'] = sfrom
self.tag = "{%s}%s" % (self.namespace, self.name) self.tag = "{%s}%s" % (self.namespace, self.name)
def setType(self, value): def set_type(self, value):
""" """
Set the stanza's 'type' attribute. Set the stanza's 'type' attribute.
@ -995,37 +1042,37 @@ class StanzaBase(ElementBase):
self.xml.attrib['type'] = value self.xml.attrib['type'] = value
return self return self
def getTo(self): def get_to(self):
"""Return the value of the stanza's 'to' attribute.""" """Return the value of the stanza's 'to' attribute."""
return JID(self._getAttr('to')) return JID(self._get_attr('to'))
def setTo(self, value): def set_to(self, value):
""" """
Set the 'to' attribute of the stanza. Set the 'to' attribute of the stanza.
Arguments: Arguments:
value -- A string or JID object representing the recipient's JID. value -- A string or JID object representing the recipient's JID.
""" """
return self._setAttr('to', str(value)) return self._set_attr('to', str(value))
def getFrom(self): def get_from(self):
"""Return the value of the stanza's 'from' attribute.""" """Return the value of the stanza's 'from' attribute."""
return JID(self._getAttr('from')) return JID(self._get_attr('from'))
def setFrom(self, value): def set_from(self, value):
""" """
Set the 'from' attribute of the stanza. Set the 'from' attribute of the stanza.
Arguments: Arguments:
from -- A string or JID object representing the sender's JID. from -- A string or JID object representing the sender's JID.
""" """
return self._setAttr('from', str(value)) return self._set_attr('from', str(value))
def getPayload(self): def get_payload(self):
"""Return a list of XML objects contained in the stanza.""" """Return a list of XML objects contained in the stanza."""
return self.xml.getchildren() return self.xml.getchildren()
def setPayload(self, value): def set_payload(self, value):
""" """
Add XML content to the stanza. Add XML content to the stanza.
@ -1039,7 +1086,7 @@ class StanzaBase(ElementBase):
self.append(val) self.append(val)
return self return self
def delPayload(self): def del_payload(self):
"""Remove the XML contents of the stanza.""" """Remove the XML contents of the stanza."""
self.clear() self.clear()
return self return self

View file

@ -383,7 +383,7 @@ class XMLStream(object):
one that appears as a direct child of the stream's root element. one that appears as a direct child of the stream's root element.
Stanzas that appear as substanzas of a root stanza do not need to Stanzas that appear as substanzas of a root stanza do not need to
be registered here. That is done using registerStanzaPlugin() from be registered here. That is done using register_stanza_plugin() from
sleekxmpp.xmlstream.stanzabase. sleekxmpp.xmlstream.stanzabase.
Stanzas that are not registered will not be converted into Stanzas that are not registered will not be converted into

View file

@ -55,7 +55,7 @@ class TestElementBase(SleekTest):
interfaces = set(('bar', 'baz')) interfaces = set(('bar', 'baz'))
subitem = set((TestSubStanza,)) subitem = set((TestSubStanza,))
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
stanza = TestStanza() stanza = TestStanza()
stanza['bar'] = 'a' stanza['bar'] = 'a'
@ -102,8 +102,8 @@ class TestElementBase(SleekTest):
interfaces = set(('bar', 'baz')) interfaces = set(('bar', 'baz'))
subitem = set((TestSubStanza,)) subitem = set((TestSubStanza,))
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
registerStanzaPlugin(TestStanza, TestStanzaPlugin2) register_stanza_plugin(TestStanza, TestStanzaPlugin2)
stanza = TestStanza() stanza = TestStanza()
values = {'bar': 'a', values = {'bar': 'a',
@ -144,7 +144,7 @@ class TestElementBase(SleekTest):
interfaces = set(('fizz',)) interfaces = set(('fizz',))
TestStanza.subitem = (TestStanza,) TestStanza.subitem = (TestStanza,)
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
stanza = TestStanza() stanza = TestStanza()
substanza = TestStanza() substanza = TestStanza()
@ -189,7 +189,7 @@ class TestElementBase(SleekTest):
plugin_attrib = "foobar" plugin_attrib = "foobar"
interfaces = set(('foobar',)) interfaces = set(('foobar',))
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
stanza = TestStanza() stanza = TestStanza()
@ -223,7 +223,7 @@ class TestElementBase(SleekTest):
plugin_attrib = "foobar" plugin_attrib = "foobar"
interfaces = set(('foobar',)) interfaces = set(('foobar',))
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
stanza = TestStanza() stanza = TestStanza()
stanza['bar'] = 'a' stanza['bar'] = 'a'
@ -261,27 +261,27 @@ class TestElementBase(SleekTest):
<foo xmlns="foo" /> <foo xmlns="foo" />
""") """)
self.failUnless(stanza._getAttr('bar') == '', self.failUnless(stanza._get_attr('bar') == '',
"Incorrect value returned for an unset XML attribute.") "Incorrect value returned for an unset XML attribute.")
stanza._setAttr('bar', 'a') stanza._set_attr('bar', 'a')
stanza._setAttr('baz', 'b') stanza._set_attr('baz', 'b')
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
<foo xmlns="foo" bar="a" baz="b" /> <foo xmlns="foo" bar="a" baz="b" />
""") """)
self.failUnless(stanza._getAttr('bar') == 'a', self.failUnless(stanza._get_attr('bar') == 'a',
"Retrieved XML attribute value is incorrect.") "Retrieved XML attribute value is incorrect.")
stanza._setAttr('bar', None) stanza._set_attr('bar', None)
stanza._delAttr('baz') stanza._del_attr('baz')
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
<foo xmlns="foo" /> <foo xmlns="foo" />
""") """)
self.failUnless(stanza._getAttr('bar', 'c') == 'c', self.failUnless(stanza._get_attr('bar', 'c') == 'c',
"Incorrect default value returned for an unset XML attribute.") "Incorrect default value returned for an unset XML attribute.")
def testGetSubText(self): def testGetSubText(self):
@ -300,11 +300,11 @@ class TestElementBase(SleekTest):
self.xml.append(wrapper) self.xml.append(wrapper)
def getBar(self): def getBar(self):
return self._getSubText("wrapper/bar", default="not found") return self._get_sub_text("wrapper/bar", default="not found")
stanza = TestStanza() stanza = TestStanza()
self.failUnless(stanza['bar'] == 'not found', self.failUnless(stanza['bar'] == 'not found',
"Default _getSubText value incorrect.") "Default _get_sub_text value incorrect.")
stanza['bar'] = 'found' stanza['bar'] = 'found'
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
@ -315,7 +315,7 @@ class TestElementBase(SleekTest):
</foo> </foo>
""") """)
self.failUnless(stanza['bar'] == 'found', self.failUnless(stanza['bar'] == 'found',
"_getSubText value incorrect: %s." % stanza['bar']) "_get_sub_text value incorrect: %s." % stanza['bar'])
def testSubElement(self): def testSubElement(self):
"""Test setting the contents of a sub element.""" """Test setting the contents of a sub element."""
@ -326,16 +326,16 @@ class TestElementBase(SleekTest):
interfaces = set(('bar', 'baz')) interfaces = set(('bar', 'baz'))
def setBaz(self, value): def setBaz(self, value):
self._setSubText("wrapper/baz", text=value) self._set_sub_text("wrapper/baz", text=value)
def getBaz(self): def getBaz(self):
return self._getSubText("wrapper/baz") return self._get_sub_text("wrapper/baz")
def setBar(self, value): def setBar(self, value):
self._setSubText("wrapper/bar", text=value) self._set_sub_text("wrapper/bar", text=value)
def getBar(self): def getBar(self):
return self._getSubText("wrapper/bar") return self._get_sub_text("wrapper/bar")
stanza = TestStanza() stanza = TestStanza()
stanza['bar'] = 'a' stanza['bar'] = 'a'
@ -348,7 +348,7 @@ class TestElementBase(SleekTest):
</wrapper> </wrapper>
</foo> </foo>
""") """)
stanza._setSubText('wrapper/bar', text='', keep=True) stanza._set_sub_text('wrapper/bar', text='', keep=True)
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
<foo xmlns="foo"> <foo xmlns="foo">
<wrapper> <wrapper>
@ -359,7 +359,7 @@ class TestElementBase(SleekTest):
""", use_values=False) """, use_values=False)
stanza['bar'] = 'a' stanza['bar'] = 'a'
stanza._setSubText('wrapper/bar', text='') stanza._set_sub_text('wrapper/bar', text='')
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
<foo xmlns="foo"> <foo xmlns="foo">
<wrapper> <wrapper>
@ -377,22 +377,22 @@ class TestElementBase(SleekTest):
interfaces = set(('bar', 'baz')) interfaces = set(('bar', 'baz'))
def setBar(self, value): def setBar(self, value):
self._setSubText("path/to/only/bar", value); self._set_sub_text("path/to/only/bar", value);
def getBar(self): def getBar(self):
return self._getSubText("path/to/only/bar") return self._get_sub_text("path/to/only/bar")
def delBar(self): def delBar(self):
self._delSub("path/to/only/bar") self._del_sub("path/to/only/bar")
def setBaz(self, value): def setBaz(self, value):
self._setSubText("path/to/just/baz", value); self._set_sub_text("path/to/just/baz", value);
def getBaz(self): def getBaz(self):
return self._getSubText("path/to/just/baz") return self._get_sub_text("path/to/just/baz")
def delBaz(self): def delBaz(self):
self._delSub("path/to/just/baz") self._del_sub("path/to/just/baz")
stanza = TestStanza() stanza = TestStanza()
stanza['bar'] = 'a' stanza['bar'] = 'a'
@ -430,7 +430,7 @@ class TestElementBase(SleekTest):
stanza['bar'] = 'a' stanza['bar'] = 'a'
stanza['baz'] = 'b' stanza['baz'] = 'b'
stanza._delSub('path/to/only/bar', all=True) stanza._del_sub('path/to/only/bar', all=True)
self.check_stanza(TestStanza, stanza, """ self.check_stanza(TestStanza, stanza, """
<foo xmlns="foo"> <foo xmlns="foo">
@ -460,17 +460,17 @@ class TestElementBase(SleekTest):
subitem = (TestSubStanza,) subitem = (TestSubStanza,)
def setQux(self, value): def setQux(self, value):
self._setSubText('qux', text=value) self._set_sub_text('qux', text=value)
def getQux(self): def getQux(self):
return self._getSubText('qux') return self._get_sub_text('qux')
class TestStanzaPlugin(ElementBase): class TestStanzaPlugin(ElementBase):
name = "plugin" name = "plugin"
namespace = "http://test/slash/bar" namespace = "http://test/slash/bar"
interfaces = set(('attrib',)) interfaces = set(('attrib',))
registerStanzaPlugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin)
stanza = TestStanza() stanza = TestStanza()
self.failUnless(stanza.match("foo"), self.failUnless(stanza.match("foo"),
@ -549,7 +549,7 @@ class TestElementBase(SleekTest):
interfaces = set(('bar', 'baz')) interfaces = set(('bar', 'baz'))
plugin_attrib = 'qux' plugin_attrib = 'qux'
registerStanzaPlugin(TestStanza, TestStanza) register_stanza_plugin(TestStanza, TestStanza)
stanza = TestStanza() stanza = TestStanza()

View file

@ -5,9 +5,9 @@ import sleekxmpp.plugins.gmail_notify as gmail
class TestGmail(SleekTest): class TestGmail(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Iq, gmail.GmailQuery) register_stanza_plugin(Iq, gmail.GmailQuery)
registerStanzaPlugin(Iq, gmail.MailBox) register_stanza_plugin(Iq, gmail.MailBox)
registerStanzaPlugin(Iq, gmail.NewMail) register_stanza_plugin(Iq, gmail.NewMail)
def testCreateQuery(self): def testCreateQuery(self):
"""Testing querying Gmail for emails.""" """Testing querying Gmail for emails."""

View file

@ -6,7 +6,7 @@ from sleekxmpp.stanza.htmlim import HTMLIM
class TestMessageStanzas(SleekTest): class TestMessageStanzas(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Message, HTMLIM) register_stanza_plugin(Message, HTMLIM)
def testGroupchatReplyRegression(self): def testGroupchatReplyRegression(self):
"Regression groupchat reply should be to barejid" "Regression groupchat reply should be to barejid"

View file

@ -16,7 +16,7 @@ class TestPresenceStanzas(SleekTest):
p['type'] = 'available' p['type'] = 'available'
self.check_presence(p, "<presence />") self.check_presence(p, "<presence />")
self.failUnless(p['type'] == 'available', self.failUnless(p['type'] == 'available',
"Incorrect presence['type'] for type 'available'") "Incorrect presence['type'] for type 'available': %s" % p['type'])
for showtype in ['away', 'chat', 'dnd', 'xa']: for showtype in ['away', 'chat', 'dnd', 'xa']:
p['type'] = showtype p['type'] = showtype

View file

@ -5,9 +5,9 @@ import sleekxmpp.plugins.xep_0004 as xep_0004
class TestDataForms(SleekTest): class TestDataForms(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Message, xep_0004.Form) register_stanza_plugin(Message, xep_0004.Form)
registerStanzaPlugin(xep_0004.Form, xep_0004.FormField) register_stanza_plugin(xep_0004.Form, xep_0004.FormField)
registerStanzaPlugin(xep_0004.FormField, xep_0004.FieldOption) register_stanza_plugin(xep_0004.FormField, xep_0004.FieldOption)
def testMultipleInstructions(self): def testMultipleInstructions(self):
"""Testing using multiple instructions elements in a data form.""" """Testing using multiple instructions elements in a data form."""

View file

@ -5,8 +5,8 @@ import sleekxmpp.plugins.xep_0030 as xep_0030
class TestDisco(SleekTest): class TestDisco(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Iq, xep_0030.DiscoInfo) register_stanza_plugin(Iq, xep_0030.DiscoInfo)
registerStanzaPlugin(Iq, xep_0030.DiscoItems) register_stanza_plugin(Iq, xep_0030.DiscoItems)
def testCreateInfoQueryNoNode(self): def testCreateInfoQueryNoNode(self):
"""Testing disco#info query with no node.""" """Testing disco#info query with no node."""

View file

@ -5,7 +5,7 @@ import sleekxmpp.plugins.xep_0033 as xep_0033
class TestAddresses(SleekTest): class TestAddresses(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Message, xep_0033.Addresses) register_stanza_plugin(Message, xep_0033.Addresses)
def testAddAddress(self): def testAddAddress(self):
"""Testing adding extended stanza address.""" """Testing adding extended stanza address."""

View file

@ -4,11 +4,11 @@ import sleekxmpp.plugins.xep_0085 as xep_0085
class TestChatStates(SleekTest): class TestChatStates(SleekTest):
def setUp(self): def setUp(self):
registerStanzaPlugin(Message, xep_0085.Active) register_stanza_plugin(Message, xep_0085.Active)
registerStanzaPlugin(Message, xep_0085.Composing) register_stanza_plugin(Message, xep_0085.Composing)
registerStanzaPlugin(Message, xep_0085.Gone) register_stanza_plugin(Message, xep_0085.Gone)
registerStanzaPlugin(Message, xep_0085.Inactive) register_stanza_plugin(Message, xep_0085.Inactive)
registerStanzaPlugin(Message, xep_0085.Paused) register_stanza_plugin(Message, xep_0085.Paused)
def testCreateChatState(self): def testCreateChatState(self):
"""Testing creating chat states.""" """Testing creating chat states."""