Created
May 28, 2010 05:20
-
-
Save legastero/416792 to your computer and use it in GitHub Desktop.
Code example #2 for creating custom stanza objects using the Task example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sleekxmpp | |
| from sleekxmpp.xmlstream.stanzabase import ElementBase, ET, JID | |
| from sleekxmpp.stanza.iq import Iq | |
| class Param(ElementBase): | |
| namespace = 'example:task' | |
| name = 'param' | |
| plugin_attrib = 'param' | |
| interfaces = set(('name', 'value')) | |
| sub_interfaces = interfaces | |
| class Task(ElementBase): | |
| namespace = 'example:task' | |
| name = 'task' | |
| plugin_attrib = 'task' | |
| interfaces = set(('id', 'command', 'cleanup', 'params')) | |
| sub_interfaces = set(('command', 'cleanup')) | |
| subitem = (Param,) | |
| def getParams(self): | |
| params = {} | |
| for par in self.xml.findall('{%s}param' % Param.namespace): | |
| param = Param(par) | |
| params[param['name']] = param['value'] | |
| return params | |
| def setParams(self, params): | |
| # params is a dictonary | |
| for name in params: | |
| self.addParam(name, params[name]) | |
| def delParams(self): | |
| params = self.xml.findall('{%s}param' % Param.namespace) | |
| for param in params: | |
| self.xml.remove(param) | |
| def addParam(self, name, value): | |
| # Use Param(None, self) to link the param object | |
| # with the task object. | |
| param_obj = Param(None, self) | |
| param_obj['name'] = name | |
| param_obj['value'] = value | |
| def delParam(self, name): | |
| # Get all <param /> elements | |
| params = self.xml.findall('{%s}param' % Param.namespace) | |
| for parXML in params: | |
| # Create a stanza object to test against | |
| param = Param(parXML) | |
| # Remove <param /> element if name matches | |
| if param['name'] == name: | |
| self.xml.remove(parXML) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment