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