source: wokkel/shim.py @ 201:4266ba97b007

Last change on this file since 201:4266ba97b007 was 201:4266ba97b007, checked in by Ralph Meijer <ralphm@…>, 4 years ago

imported patch py3-shim.patch

  • Property exe set to *
File size: 1.3 KB
RevLine 
[27]1# -*- test-case-name: wokkel.test.test_shim -*-
2#
[96]3# Copyright (c) Ralph Meijer.
[27]4# See LICENSE for details.
5
6"""
7XMPP Stanza Headers and Internet Metadata.
8
9This protocol is specified in
[165]10U{XEP-0131<http://xmpp.org/extensions/xep-0131.html>}.
[27]11"""
12
[201]13from __future__ import division, absolute_import
14
15from twisted.python.compat import unicode
[27]16from twisted.words.xish import domish
17
18NS_SHIM = "http://jabber.org/protocol/shim"
19
20class Headers(domish.Element):
21    def __init__(self, headers):
22        domish.Element.__init__(self, (NS_SHIM, 'headers'))
23        for name, value in headers:
24            self.addElement('header', content=value)['name'] = name
25
26def extractHeaders(stanza):
27    """
28    Extract SHIM headers from stanza.
29
30    @param stanza: The stanza to extract headers from.
31    @type stanza: L{Element<twisted.words.xish.domish.Element>}
32    @return: Headers as a mapping from header name to a list of values.
33    @rtype: C{dict}
34    """
35    headers = {}
36
37    for element in domish.generateElementsQNamed(stanza.children,
38                                                 'headers', NS_SHIM):
39        for header in domish.generateElementsQNamed(element.children,
40                                                    'header', NS_SHIM):
41            headers.setdefault(header['name'], []).append(unicode(header))
42
43    return headers
Note: See TracBrowser for help on using the repository browser.