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

@@ -6,8 +6,7 @@
See the file LICENSE for copying permission.
"""
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
class Error(ElementBase):
@@ -40,13 +39,13 @@ class Error(ElementBase):
should be treated.
Methods:
setup -- Overrides ElementBase.setup.
getCondition -- Retrieve the name of the condition element.
setCondition -- Add a condition element.
delCondition -- Remove the condition element.
getText -- Retrieve the contents of the <text> element.
setText -- Set the contents of the <text> element.
delText -- Remove the <text> element.
setup -- Overrides ElementBase.setup.
get_condition -- Retrieve the name of the condition element.
set_condition -- Add a condition element.
del_condition -- Remove the condition element.
get_text -- Retrieve the contents of the <text> element.
set_text -- Set the contents of the <text> element.
del_text -- Remove the <text> element.
"""
namespace = 'jabber:client'
@@ -78,6 +77,15 @@ class Error(ElementBase):
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.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 we had to generate XML then set default values.
self['type'] = 'cancel'
@@ -85,14 +93,14 @@ class Error(ElementBase):
if self.parent is not None:
self.parent()['type'] = 'error'
def getCondition(self):
def get_condition(self):
"""Return the condition element's name."""
for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag:
return child.tag.split('}', 1)[-1]
return ''
def setCondition(self, value):
def set_condition(self, value):
"""
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)))
return self
def delCondition(self):
def del_condition(self):
"""Remove the condition element."""
for child in self.xml.getchildren():
if "{%s}" % self.condition_ns in child.tag:
@@ -113,21 +121,21 @@ class Error(ElementBase):
self.xml.remove(child)
return self
def getText(self):
def get_text(self):
"""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.
Arguments:
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
def delText(self):
def del_text(self):
"""Remove the <text> element."""
self._delSub('{%s}text' % self.condition_ns)
self._del_sub('{%s}text' % self.condition_ns)
return self

View File

@@ -7,8 +7,7 @@
"""
from sleekxmpp.stanza import Message
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
class HTMLIM(ElementBase):
@@ -36,9 +35,10 @@ class HTMLIM(ElementBase):
body -- The contents of the HTML body tag.
Methods:
getBody -- Return the HTML body contents.
setBody -- Set the HTML body contents.
delBody -- Remove the HTML body contents.
setup -- Overrides ElementBase.setup.
get_body -- Return 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'
@@ -46,7 +46,24 @@ class HTMLIM(ElementBase):
interfaces = set(('body',))
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.
@@ -64,17 +81,17 @@ class HTMLIM(ElementBase):
else:
self.xml.append(html)
def getBody(self):
def get_body(self):
"""Return the contents of the HTML body."""
html = self.xml.find('{http://www.w3.org/1999/xhtml}body')
if html is None:
return ''
return html
def delBody(self):
def del_body(self):
"""Remove the HTML body contents."""
if self.parent is not None:
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.rootstanza import RootStanza
from sleekxmpp.xmlstream import RESPONSE_TIMEOUT
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
from sleekxmpp.xmlstream import RESPONSE_TIMEOUT, StanzaBase, ET
from sleekxmpp.xmlstream.handler import Waiter
from sleekxmpp.xmlstream.matcher import MatcherId
@@ -53,14 +52,14 @@ class Iq(RootStanza):
types -- May be one of: get, set, result, or error.
Methods:
__init__ -- Overrides StanzaBase.__init__.
unhandled -- Send error if there are no handlers.
setPayload -- Overrides StanzaBase.setPayload.
setQuery -- Add or modify a <query> element.
getQuery -- Return the namespace of the <query> element.
delQuery -- Remove the <query> element.
reply -- Overrides StanzaBase.reply
send -- Overrides StanzaBase.send
__init__ -- Overrides StanzaBase.__init__.
unhandled -- Send error if there are no handlers.
set_payload -- Overrides StanzaBase.set_payload.
set_query -- Add or modify a <query> element.
get_query -- Return the namespace of the <query> element.
del_query -- Remove the <query> element.
reply -- Overrides StanzaBase.reply
send -- Overrides StanzaBase.send
"""
namespace = 'jabber:client'
@@ -76,6 +75,13 @@ class Iq(RootStanza):
Overrides StanzaBase.__init__.
"""
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.stream is not None:
self['id'] = self.stream.getNewId()
@@ -94,7 +100,7 @@ class Iq(RootStanza):
self['error']['text'] = 'No handlers registered for this request.'
self.send()
def setPayload(self, value):
def set_payload(self, value):
"""
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
"""
self.clear()
StanzaBase.setPayload(self, value)
StanzaBase.set_payload(self, value)
return self
def setQuery(self, value):
def set_query(self, value):
"""
Add or modify a <query> element.
@@ -121,7 +127,7 @@ class Iq(RootStanza):
self.xml.append(query)
return self
def getQuery(self):
def get_query(self):
"""Return the namespace of the <query> element."""
for child in self.xml.getchildren():
if child.tag.endswith('query'):
@@ -131,7 +137,7 @@ class Iq(RootStanza):
return ns
return ''
def delQuery(self):
def del_query(self):
"""Remove the <query> element."""
for child in self.xml.getchildren():
if child.tag.endswith('query'):

View File

@@ -8,7 +8,7 @@
from sleekxmpp.stanza import Error
from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
from sleekxmpp.xmlstream import StanzaBase, ET
class Message(RootStanza):
@@ -42,16 +42,17 @@ class Message(RootStanza):
types -- May be one of: normal, chat, headline, groupchat, or error.
Methods:
chat -- Set the message type to 'chat'.
normal -- Set the message type to 'normal'.
reply -- Overrides StanzaBase.reply
getType -- Overrides StanzaBase interface
getMucroom -- Return the name of the MUC room of the message.
setMucroom -- Dummy method to prevent assignment.
delMucroom -- Dummy method to prevent deletion.
getMucnick -- Return the MUC nickname of the message's sender.
setMucnick -- Dummy method to prevent assignment.
delMucnick -- Dummy method to prevent deletion.
setup -- Overrides StanzaBase.setup.
chat -- Set the message type to 'chat'.
normal -- Set the message type to 'normal'.
reply -- Overrides StanzaBase.reply
get_type -- Overrides StanzaBase interface
get_mucroom -- Return the name of the MUC room of the message.
set_mucroom -- Dummy method to prevent assignment.
del_mucroom -- Dummy method to prevent deletion.
get_mucnick -- Return the MUC nickname of the message's sender.
set_mucnick -- Dummy method to prevent assignment.
del_mucnick -- Dummy method to prevent deletion.
"""
namespace = 'jabber:client'
@@ -62,7 +63,28 @@ class Message(RootStanza):
plugin_attrib = name
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.
@@ -70,7 +92,7 @@ class Message(RootStanza):
Returns 'normal' if no type attribute is present.
"""
return self._getAttr('type', 'normal')
return self._get_attr('type', 'normal')
def chat(self):
"""Set the message type to 'chat'."""
@@ -104,7 +126,7 @@ class Message(RootStanza):
self['body'] = body
return self
def getMucroom(self):
def get_mucroom(self):
"""
Return the name of the MUC room where the message originated.
@@ -115,7 +137,7 @@ class Message(RootStanza):
else:
return ''
def getMucnick(self):
def get_mucnick(self):
"""
Return the nickname of the MUC user that sent the message.
@@ -126,18 +148,18 @@ class Message(RootStanza):
else:
return ''
def setMucroom(self, value):
def set_mucroom(self, value):
"""Dummy method to prevent modification."""
pass
def delMucroom(self):
def del_mucroom(self):
"""Dummy method to prevent deletion."""
pass
def setMucnick(self, value):
def set_mucnick(self, value):
"""Dummy method to prevent modification."""
pass
def delMucnick(self):
def del_mucnick(self):
"""Dummy method to prevent deletion."""
pass

View File

@@ -7,8 +7,7 @@
"""
from sleekxmpp.stanza import Message, Presence
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ElementBase, ET
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
class Nick(ElementBase):
@@ -39,9 +38,10 @@ class Nick(ElementBase):
nick -- A global, friendly or informal name chosen by a user.
Methods:
getNick -- Return the nickname in the <nick> element.
setNick -- Add a <nick> element with the given nickname.
delNick -- Remove the <nick> element.
setup -- Overrides ElementBase.setup.
get_nick -- Return the nickname in 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'
@@ -49,7 +49,24 @@ class Nick(ElementBase):
plugin_attrib = name
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.
@@ -58,15 +75,15 @@ class Nick(ElementBase):
"""
self.xml.text = nick
def getNick(self):
def get_nick(self):
"""Return the nickname in the <nick> element."""
return self.xml.text
def delNick(self):
def del_nick(self):
"""Remove the <nick> element."""
if self.parent is not None:
self.parent().xml.remove(self.xml)
registerStanzaPlugin(Message, Nick)
registerStanzaPlugin(Presence, Nick)
register_stanza_plugin(Message, Nick)
register_stanza_plugin(Presence, Nick)

View File

@@ -8,7 +8,7 @@
from sleekxmpp.stanza import Error
from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
from sleekxmpp.xmlstream import StanzaBase, ET
class Presence(RootStanza):
@@ -52,12 +52,13 @@ class Presence(RootStanza):
showtypes -- One of: away, chat, dnd, and xa.
Methods:
reply -- Overrides StanzaBase.reply
setShow -- Set the value of the <show> element.
getType -- Get the value of the type attribute or <show> element.
setType -- Set the value of the type attribute or <show> element.
getPriority -- Get the value of the <priority> element.
setPriority -- Set the value of the <priority> element.
setup -- Overrides StanzaBase.setup
reply -- Overrides StanzaBase.reply
set_show -- Set the value of the <show> element.
get_type -- Get the value of the type attribute or <show> element.
set_type -- Set the value of the type attribute or <show> element.
get_priority -- Get the value of the <priority> element.
set_priority -- Set the value of the <priority> element.
"""
namespace = 'jabber:client'
@@ -71,7 +72,27 @@ class Presence(RootStanza):
'subscribed', 'unsubscribe', 'unsubscribed'))
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.
@@ -79,12 +100,24 @@ class Presence(RootStanza):
show -- Must be one of: away, chat, dnd, or xa.
"""
if show is None:
self._delSub('show')
self._del_sub('show')
elif show in self.showtypes:
self._setSubText('show', text=show)
self._set_sub_text('show', text=show)
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
if applicable.
@@ -96,19 +129,19 @@ class Presence(RootStanza):
self['show'] = None
if value == 'available':
value = ''
self._setAttr('type', value)
self._set_attr('type', value)
elif value in self.showtypes:
self['show'] = value
return self
def delType(self):
def del_type(self):
"""
Remove both the type attribute and the <show> element.
"""
self._delAttr('type')
self._delSub('show')
self._del_attr('type')
self._del_sub('show')
def setPriority(self, value):
def set_priority(self, value):
"""
Set the entity's priority value. Some server use priority to
determine message routing behavior.
@@ -119,13 +152,13 @@ class Presence(RootStanza):
Arguments:
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.
"""
p = self._getSubText('priority')
p = self._get_sub_text('priority')
if not p:
p = 0
try:
@@ -134,18 +167,6 @@ class Presence(RootStanza):
# The priority is not a number: we consider it 0 as a default
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):
"""
Set the appropriate presence reply type.

View File

@@ -12,7 +12,7 @@ import sys
from sleekxmpp.exceptions import XMPPError
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):
@@ -63,4 +63,4 @@ class RootStanza(StanzaBase):
self.send()
registerStanzaPlugin(RootStanza, Error)
register_stanza_plugin(RootStanza, Error)

View File

@@ -8,8 +8,7 @@
from sleekxmpp.stanza import Iq
from sleekxmpp.xmlstream import JID
from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin
from sleekxmpp.xmlstream.stanzabase import ET, ElementBase
from sleekxmpp.xmlstream import ET, ElementBase, register_stanza_plugin
class Roster(ElementBase):
@@ -29,9 +28,9 @@ class Roster(ElementBase):
in the stanza.
Methods:
getItems -- Return a dictionary of roster entries.
setItems -- Add <item> elements.
delItems -- Remove all <item> elements.
get_items -- Return a dictionary of roster entries.
set_items -- Add <item> elements.
del_items -- Remove all <item> elements.
"""
namespace = 'jabber:iq:roster'
@@ -39,7 +38,24 @@ class Roster(ElementBase):
plugin_attrib = 'roster'
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.
@@ -54,7 +70,7 @@ class Roster(ElementBase):
Arguments:
items -- A dictionary of roster entries.
"""
self.delItems()
self.del_items()
for jid in items:
ijid = str(jid)
item = ET.Element('{jabber:iq:roster}item', {'jid': ijid})
@@ -72,7 +88,7 @@ class Roster(ElementBase):
self.xml.append(item)
return self
def getItems(self):
def get_items(self):
"""
Return a dictionary of roster entries.
@@ -98,7 +114,7 @@ class Roster(ElementBase):
items[itemxml.get('jid')] = item
return items
def delItems(self):
def del_items(self):
"""
Remove all <item> elements from the roster stanza.
"""
@@ -106,4 +122,4 @@ class Roster(ElementBase):
self.xml.remove(child)
registerStanzaPlugin(Iq, Roster)
register_stanza_plugin(Iq, Roster)