Ignore:
Timestamp:
May 9, 2012, 2:24:28 PM (10 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Add convenience method Request.parseRequest.

The new method Request.parseRequest is called from Request.fromElement
with the child element of the iq that is passed to fromElement, providing
a convenient hook for parsing the payload of incoming requests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/generic.py

    r165 r171  
    184184    @classmethod
    185185    def fromElement(Class, element):
     186        """
     187        Create a stanza from a L{domish.Element}.
     188        """
    186189        stanza = Class()
    187190        stanza.parseElement(element)
     
    190193
    191194    def parseElement(self, element):
     195        """
     196        Parse the stanza element.
     197
     198        This is called with the stanza's element when a L{Stanza} is
     199        created using L{fromElement}. It parses the stanza's core attributes
     200        (addressing, type and id), strips the namespace from the stanza
     201        element for easier transport across streams and passes on
     202        child elements for further parsing.
     203
     204        Child element parsers are defined by providing a C{childParsers}
     205        attribute on a subclass, as a mapping from (URI, name) to the name
     206        of the handler on C{self}. C{parseElement} will accumulate
     207        C{childParsers} from its class hierarchy, iterate over the child
     208        elements and pass it to matching handlers based on the child element's
     209        URI and name. The special key of C{None} can be used to pass all
     210        child elements to.
     211        """
    192212        if element.hasAttribute('from'):
    193213            self.sender = jid.internJID(element['from'])
     
    209229                handler = handlers[child.uri, child.name]
    210230            except KeyError:
    211                 pass
    212             else:
    213                 getattr(self, handler)(child)
     231                try:
     232                    handler = handlers[None]
     233                except KeyError:
     234                    continue
     235
     236            getattr(self, handler)(child)
    214237
    215238
     
    235258
    236259
     260
    237261class Request(Stanza):
    238262    """
     
    247271    timeout = None
    248272
     273    childParsers = {None: 'parseRequest'}
     274
    249275    def __init__(self, recipient=None, sender=None, stanzaType='get'):
    250276        Stanza.__init__(self, recipient=recipient, sender=sender)
    251277        self.stanzaType = stanzaType
     278
     279
     280    def parseRequest(self, element):
     281        """
     282        Called with the request's child element for parsing.
     283
     284        When a request instance is created using L{fromElement}, this method
     285        is called with the child element of the iq. Override this method for
     286        parsing the request's payload.
     287        """
    252288
    253289
Note: See TracChangeset for help on using the changeset viewer.