[8] | 1 | # -*- test-case-name: wokkel.test.test_compat -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2001-2007 Twisted Matrix Laboratories. |
---|
| 4 | # See LICENSE for details. |
---|
| 5 | |
---|
| 6 | from twisted.words.xish import domish |
---|
| 7 | |
---|
| 8 | def toResponse(stanza, stanzaType=None): |
---|
| 9 | """ |
---|
| 10 | Create a response stanza from another stanza. |
---|
| 11 | |
---|
| 12 | This takes the addressing and id attributes from a stanza to create a (new, |
---|
| 13 | empty) response stanza. The addressing attributes are swapped and the id |
---|
| 14 | copied. Optionally, the stanza type of the response can be specified. |
---|
| 15 | |
---|
| 16 | @param stanza: the original stanza |
---|
| 17 | @type stanza: L{domish.Element} |
---|
| 18 | @param stanzaType: optional response stanza type |
---|
| 19 | @type stanzaType: C{str} |
---|
| 20 | @return: the response stanza. |
---|
| 21 | @rtype: L{domish.Element} |
---|
| 22 | """ |
---|
| 23 | |
---|
| 24 | toAddr = stanza.getAttribute('from') |
---|
| 25 | fromAddr = stanza.getAttribute('to') |
---|
| 26 | stanzaID = stanza.getAttribute('id') |
---|
| 27 | |
---|
| 28 | response = domish.Element((None, stanza.name)) |
---|
| 29 | if toAddr: |
---|
| 30 | response['to'] = toAddr |
---|
| 31 | if fromAddr: |
---|
| 32 | response['from'] = fromAddr |
---|
| 33 | if stanzaID: |
---|
| 34 | response['id'] = stanzaID |
---|
| 35 | if type: |
---|
| 36 | response['type'] = stanzaType |
---|
| 37 | |
---|
| 38 | return response |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | class XmlStreamFactoryMixin(object): |
---|
| 42 | """ |
---|
| 43 | XmlStream factory mixin that takes care of event handlers. |
---|
| 44 | |
---|
| 45 | To make sure certain event observers are set up before incoming data is |
---|
| 46 | processed, you can set up bootstrap event observers using C{addBootstrap}. |
---|
| 47 | |
---|
| 48 | The C{event} and C{fn} parameters correspond with the C{event} and |
---|
| 49 | C{observerfn} arguments to L{utility.EventDispatcher.addObserver}. |
---|
| 50 | """ |
---|
| 51 | |
---|
| 52 | def __init__(self, *args, **kwargs): |
---|
| 53 | self.bootstraps = [] |
---|
| 54 | self.args = args |
---|
| 55 | self.kwargs = kwargs |
---|
| 56 | |
---|
| 57 | def buildProtocol(self, addr): |
---|
| 58 | """ |
---|
| 59 | Create an instance of XmlStream. |
---|
| 60 | |
---|
| 61 | The returned instance will have bootstrap event observers registered |
---|
| 62 | and will proceed to handle input on an incoming connection. |
---|
| 63 | """ |
---|
| 64 | xs = self.protocol(*self.args, **self.kwargs) |
---|
| 65 | xs.factory = self |
---|
| 66 | for event, fn in self.bootstraps: |
---|
| 67 | xs.addObserver(event, fn) |
---|
| 68 | return xs |
---|
| 69 | |
---|
| 70 | def addBootstrap(self, event, fn): |
---|
| 71 | """ |
---|
| 72 | Add a bootstrap event handler. |
---|
| 73 | """ |
---|
| 74 | self.bootstraps.append((event, fn)) |
---|
| 75 | |
---|
| 76 | def removeBootstrap(self, event, fn): |
---|
| 77 | """ |
---|
| 78 | Remove a bootstrap event handler. |
---|
| 79 | """ |
---|
| 80 | self.bootstraps.remove((event, fn)) |
---|