Mercurial > sat_tmp
comparison wokkel/delay.py @ 0:09e7c32a6a00
use sat.tmp.wokkel as a buffer module until the changes are integrated to wokkel
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 15 Dec 2014 12:46:58 +0100 |
parents | |
children | dc3a3f454f39 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:09e7c32a6a00 |
---|---|
1 # -*- test-case-name: wokkel.test.test_delay -*- | |
2 # | |
3 # Copyright (c) Ralph Meijer. | |
4 # See LICENSE for details. | |
5 | |
6 """ | |
7 Delayed Delivery. | |
8 | |
9 Support for comunicating Delayed Delivery information as specified by | |
10 U{XEP-0203<http://xmpp.org/extensions/xep-0203.html>} and its predecessor | |
11 U{XEP-0091<http://xmpp.org/extensions/xep-0091.html>}. | |
12 """ | |
13 | |
14 from dateutil.parser import parse | |
15 from dateutil.tz import tzutc | |
16 | |
17 from twisted.words.protocols.jabber.jid import InvalidFormat, JID | |
18 from twisted.words.xish import domish | |
19 | |
20 NS_DELAY = 'urn:xmpp:delay' | |
21 NS_JABBER_DELAY = 'jabber:x:delay' | |
22 | |
23 class Delay(object): | |
24 """ | |
25 Delayed Delivery information. | |
26 | |
27 Instances of this class represent delayed delivery information that can be | |
28 parsed from and rendered into both XEP-0203 and legacy XEP-0091 formats. | |
29 | |
30 @ivar stamp: The timestamp the stanza was originally sent. | |
31 @type stamp: L{datetime.datetime} | |
32 @ivar sender: The optional entity that originally sent the stanza or | |
33 delayed its delivery. | |
34 @type sender: L{JID} | |
35 """ | |
36 | |
37 def __init__(self, stamp, sender=None): | |
38 self.stamp = stamp | |
39 self.sender = sender | |
40 | |
41 | |
42 def toElement(self, legacy=False): | |
43 """ | |
44 Render this instance into a domish Element. | |
45 | |
46 @param legacy: If C{True}, use the legacy XEP-0091 format. | |
47 @type legacy: C{bool} | |
48 """ | |
49 if not self.stamp: | |
50 raise ValueError("stamp is required") | |
51 if self.stamp.tzinfo is None: | |
52 raise ValueError("stamp is not offset-aware") | |
53 | |
54 if legacy: | |
55 element = domish.Element((NS_JABBER_DELAY, 'x')) | |
56 stampFormat = '%Y%m%dT%H:%M:%S' | |
57 else: | |
58 element = domish.Element((NS_DELAY, 'delay')) | |
59 stampFormat = '%Y-%m-%dT%H:%M:%SZ' | |
60 | |
61 stamp = self.stamp.astimezone(tzutc()) | |
62 element['stamp'] = stamp.strftime(stampFormat) | |
63 | |
64 if self.sender: | |
65 element['from'] = self.sender.full() | |
66 | |
67 return element | |
68 | |
69 | |
70 @staticmethod | |
71 def fromElement(element): | |
72 """ | |
73 Create an instance from a domish Element. | |
74 """ | |
75 try: | |
76 stamp = parse(element[u'stamp']) | |
77 | |
78 # Assume UTC if no timezone was given | |
79 if stamp.tzinfo is None: | |
80 stamp = stamp.replace(tzinfo=tzutc()) | |
81 except (KeyError, ValueError, TypeError): | |
82 stamp = None | |
83 | |
84 try: | |
85 sender = JID(element[u'from']) | |
86 except (KeyError, InvalidFormat): | |
87 sender = None | |
88 | |
89 delay = Delay(stamp, sender) | |
90 return delay | |
91 | |
92 | |
93 | |
94 class DelayMixin(object): | |
95 """ | |
96 Mixin for parsing delayed delivery information from stanzas. | |
97 | |
98 This can be used as a mixin for subclasses of L{wokkel.generic.Stanza} | |
99 for parsing delayed delivery information. If both XEP-0203 and XEP-0091 | |
100 formats are present, the former takes precedence. | |
101 """ | |
102 | |
103 delay = None | |
104 | |
105 childParsers = { | |
106 (NS_DELAY, 'delay'): '_childParser_delay', | |
107 (NS_JABBER_DELAY, 'x'): '_childParser_legacyDelay', | |
108 } | |
109 | |
110 | |
111 def _childParser_delay(self, element): | |
112 self.delay = Delay.fromElement(element) | |
113 | |
114 | |
115 def _childParser_legacyDelay(self, element): | |
116 if not self.delay: | |
117 self.delay = Delay.fromElement(element) |