source: ralphm-patches/copy_xmppim.patch @ 49:537d1413b661

Last change on this file since 49:537d1413b661 was 49:537d1413b661, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Save work after moving stuff to keep from wokkel.xmppim to wokkel.im.

File size: 18.0 KB
RevLine 
[49]1# HG changeset patch
2# Parent 2c8dc93fbef4f2d5b7115c7afdd1b7fb799d47c3
3
4diff --git a/wokkel/xmppim.py b/wokkel/im.py
5copy from wokkel/xmppim.py
6copy to wokkel/im.py
7--- a/wokkel/xmppim.py
8+++ b/wokkel/im.py
9@@ -22,234 +22,6 @@
10 NS_XML = 'http://www.w3.org/XML/1998/namespace'
11 NS_ROSTER = 'jabber:iq:roster'
12 
13-class Presence(domish.Element):
14-    def __init__(self, to=None, type=None):
15-        domish.Element.__init__(self, (None, "presence"))
16-        if type:
17-            self["type"] = type
18-
19-        if to is not None:
20-            self["to"] = to.full()
21-
22-class AvailablePresence(Presence):
23-    def __init__(self, to=None, show=None, statuses=None, priority=0):
24-        Presence.__init__(self, to, type=None)
25-
26-        if show in ['away', 'xa', 'chat', 'dnd']:
27-            self.addElement('show', content=show)
28-
29-        if statuses is not None:
30-            for lang, status in statuses.iteritems():
31-                s = self.addElement('status', content=status)
32-                if lang:
33-                    s[(NS_XML, "lang")] = lang
34-
35-        if priority != 0:
36-            self.addElement('priority', content=unicode(int(priority)))
37-
38-class UnavailablePresence(Presence):
39-    def __init__(self, to=None, statuses=None):
40-        Presence.__init__(self, to, type='unavailable')
41-
42-        if statuses is not None:
43-            for lang, status in statuses.iteritems():
44-                s = self.addElement('status', content=status)
45-                if lang:
46-                    s[(NS_XML, "lang")] = lang
47-
48-class PresenceClientProtocol(XMPPHandler):
49-
50-    def connectionInitialized(self):
51-        self.xmlstream.addObserver('/presence', self._onPresence)
52-
53-    def _getStatuses(self, presence):
54-        statuses = {}
55-        for element in presence.elements():
56-            if element.name == 'status':
57-                lang = element.getAttribute((NS_XML, 'lang'))
58-                text = unicode(element)
59-                statuses[lang] = text
60-        return statuses
61-
62-    def _onPresence(self, presence):
63-        type = presence.getAttribute("type", "available")
64-        try:
65-            handler = getattr(self, '_onPresence%s' % (type.capitalize()))
66-        except AttributeError:
67-            return
68-        else:
69-            handler(presence)
70-
71-    def _onPresenceAvailable(self, presence):
72-        entity = JID(presence["from"])
73-
74-        show = unicode(presence.show or '')
75-        if show not in ['away', 'xa', 'chat', 'dnd']:
76-            show = None
77-
78-        statuses = self._getStatuses(presence)
79-
80-        try:
81-            priority = int(unicode(presence.priority or '')) or 0
82-        except ValueError:
83-            priority = 0
84-
85-        self.availableReceived(entity, show, statuses, priority)
86-
87-    def _onPresenceUnavailable(self, presence):
88-        entity = JID(presence["from"])
89-
90-        statuses = self._getStatuses(presence)
91-
92-        self.unavailableReceived(entity, statuses)
93-
94-    def _onPresenceSubscribed(self, presence):
95-        self.subscribedReceived(JID(presence["from"]))
96-
97-    def _onPresenceUnsubscribed(self, presence):
98-        self.unsubscribedReceived(JID(presence["from"]))
99-
100-    def _onPresenceSubscribe(self, presence):
101-        self.subscribeReceived(JID(presence["from"]))
102-
103-    def _onPresenceUnsubscribe(self, presence):
104-        self.unsubscribeReceived(JID(presence["from"]))
105-
106-
107-    def availableReceived(self, entity, show=None, statuses=None, priority=0):
108-        """
109-        Available presence was received.
110-
111-        @param entity: entity from which the presence was received.
112-        @type entity: {JID}
113-        @param show: detailed presence information. One of C{'away'}, C{'xa'},
114-                     C{'chat'}, C{'dnd'} or C{None}.
115-        @type show: C{str} or C{NoneType}
116-        @param statuses: dictionary of natural language descriptions of the
117-                         availability status, keyed by the language
118-                         descriptor. A status without a language
119-                         specified, is keyed with C{None}.
120-        @type statuses: C{dict}
121-        @param priority: priority level of the resource.
122-        @type priority: C{int}
123-        """
124-
125-    def unavailableReceived(self, entity, statuses=None):
126-        """
127-        Unavailable presence was received.
128-
129-        @param entity: entity from which the presence was received.
130-        @type entity: {JID}
131-        @param statuses: dictionary of natural language descriptions of the
132-                         availability status, keyed by the language
133-                         descriptor. A status without a language
134-                         specified, is keyed with C{None}.
135-        @type statuses: C{dict}
136-        """
137-
138-    def subscribedReceived(self, entity):
139-        """
140-        Subscription approval confirmation was received.
141-
142-        @param entity: entity from which the confirmation was received.
143-        @type entity: {JID}
144-        """
145-
146-    def unsubscribedReceived(self, entity):
147-        """
148-        Unsubscription confirmation was received.
149-
150-        @param entity: entity from which the confirmation was received.
151-        @type entity: {JID}
152-        """
153-
154-    def subscribeReceived(self, entity):
155-        """
156-        Subscription request was received.
157-
158-        @param entity: entity from which the request was received.
159-        @type entity: {JID}
160-        """
161-
162-    def unsubscribeReceived(self, entity):
163-        """
164-        Unsubscription request was received.
165-
166-        @param entity: entity from which the request was received.
167-        @type entity: {JID}
168-        """
169-
170-    def available(self, entity=None, show=None, statuses=None, priority=0):
171-        """
172-        Send available presence.
173-
174-        @param entity: optional entity to which the presence should be sent.
175-        @type entity: {JID}
176-        @param show: optional detailed presence information. One of C{'away'},
177-                     C{'xa'}, C{'chat'}, C{'dnd'}.
178-        @type show: C{str}
179-        @param statuses: dictionary of natural language descriptions of the
180-                         availability status, keyed by the language
181-                         descriptor. A status without a language
182-                         specified, is keyed with C{None}.
183-        @type statuses: C{dict}
184-        @param priority: priority level of the resource.
185-        @type priority: C{int}
186-        """
187-        self.send(AvailablePresence(entity, show, statuses, priority))
188-
189-    def unavailable(self, entity=None, statuses=None):
190-        """
191-        Send unavailable presence.
192-
193-        @param entity: optional entity to which the presence should be sent.
194-        @type entity: {JID}
195-        @param statuses: dictionary of natural language descriptions of the
196-                         availability status, keyed by the language
197-                         descriptor. A status without a language
198-                         specified, is keyed with C{None}.
199-        @type statuses: C{dict}
200-        """
201-        self.send(UnavailablePresence(entity, statuses))
202-
203-    def subscribe(self, entity):
204-        """
205-        Send subscription request
206-
207-        @param entity: entity to subscribe to.
208-        @type entity: {JID}
209-        """
210-        self.send(Presence(to=entity, type='subscribe'))
211-
212-    def unsubscribe(self, entity):
213-        """
214-        Send unsubscription request
215-
216-        @param entity: entity to unsubscribe from.
217-        @type entity: {JID}
218-        """
219-        self.send(Presence(to=entity, type='unsubscribe'))
220-
221-    def subscribed(self, entity):
222-        """
223-        Send subscription confirmation.
224-
225-        @param entity: entity that subscribed.
226-        @type entity: {JID}
227-        """
228-        self.send(Presence(to=entity, type='subscribed'))
229-
230-    def unsubscribed(self, entity):
231-        """
232-        Send unsubscription confirmation.
233-
234-        @param entity: entity that unsubscribed.
235-        @type entity: {JID}
236-        """
237-        self.send(Presence(to=entity, type='unsubscribed'))
238-
239-
240-
241 class BasePresence(Stanza):
242     """
243     Stanza of kind presence.
244@@ -605,6 +377,7 @@
245         self.groups = set()
246 
247 
248+
249 class RosterClientProtocol(XMPPHandler):
250     """
251     Client side XMPP roster protocol.
252@@ -614,6 +387,7 @@
253         ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
254         self.xmlstream.addObserver(ROSTER_SET, self._onRosterSet)
255 
256+
257     def _parseRosterItem(self, element):
258         jid = JID(element['jid'])
259         item = RosterItem(jid)
260@@ -628,6 +402,7 @@
261 
262         return item
263 
264+
265     def getRoster(self):
266         """
267         Retrieve contact list.
268@@ -683,6 +458,7 @@
269             item = self._parseRosterItem(iq.query.item)
270             self.onRosterSet(item)
271 
272+
273     def onRosterSet(self, item):
274         """
275         Called when a roster push for a new or update item was received.
276@@ -691,6 +467,7 @@
277         @type item: L{RosterItem}
278         """
279 
280+
281     def onRosterRemove(self, entity):
282         """
283         Called when a roster push for the removal of an item was received.
284@@ -698,32 +475,3 @@
285         @param entity: The entity for which the roster item has been removed.
286         @type entity: L{JID}
287         """
288-
289-class MessageProtocol(XMPPHandler):
290-    """
291-    Generic XMPP subprotocol handler for incoming message stanzas.
292-    """
293-
294-    messageTypes = None, 'normal', 'chat', 'headline', 'groupchat'
295-
296-    def connectionInitialized(self):
297-        self.xmlstream.addObserver("/message", self._onMessage)
298-
299-    def _onMessage(self, message):
300-        if message.handled:
301-            return
302-
303-        messageType = message.getAttribute("type")
304-
305-        if messageType == 'error':
306-            return
307-
308-        if messageType not in self.messageTypes:
309-            message["type"] = 'normal'
310-
311-        self.onMessage(message)
312-
313-    def onMessage(self, message):
314-        """
315-        Called when a message stanza was received.
316-        """
317diff --git a/wokkel/test/test_xmppim.py b/wokkel/test/test_im.py
318copy from wokkel/test/test_xmppim.py
319copy to wokkel/test/test_im.py
320--- a/wokkel/test/test_xmppim.py
321+++ b/wokkel/test/test_im.py
322@@ -2,7 +2,7 @@
323 # See LICENSE for details
324 
325 """
326-Tests for L{wokkel.xmppim}.
327+Tests for L{wokkel.im}.
328 """
329 
330 from twisted.internet import defer
331@@ -11,74 +11,13 @@
332 from twisted.words.protocols.jabber.xmlstream import toResponse
333 from twisted.words.xish import domish, utility
334 
335-from wokkel import xmppim
336+from wokkel import im
337 from wokkel.generic import ErrorStanza, parseXml
338 from wokkel.test.helpers import XmlStreamStub
339 
340 NS_XML = 'http://www.w3.org/XML/1998/namespace'
341 NS_ROSTER = 'jabber:iq:roster'
342 
343-class PresenceClientProtocolTest(unittest.TestCase):
344-    def setUp(self):
345-        self.output = []
346-        self.protocol = xmppim.PresenceClientProtocol()
347-        self.protocol.parent = self
348-
349-    def send(self, obj):
350-        self.output.append(obj)
351-
352-    def test_unavailableDirected(self):
353-        """
354-        Test sending of directed unavailable presence broadcast.
355-        """
356-
357-        self.protocol.unavailable(JID('user@example.com'))
358-        presence = self.output[-1]
359-        self.assertEquals("presence", presence.name)
360-        self.assertEquals(None, presence.uri)
361-        self.assertEquals("user@example.com", presence.getAttribute('to'))
362-        self.assertEquals("unavailable", presence.getAttribute('type'))
363-
364-    def test_unavailableWithStatus(self):
365-        """
366-        Test sending of directed unavailable presence broadcast with status.
367-        """
368-
369-        self.protocol.unavailable(JID('user@example.com'),
370-                                  {None: 'Disconnected'})
371-        presence = self.output[-1]
372-        self.assertEquals("presence", presence.name)
373-        self.assertEquals(None, presence.uri)
374-        self.assertEquals("user@example.com", presence.getAttribute('to'))
375-        self.assertEquals("unavailable", presence.getAttribute('type'))
376-        self.assertEquals("Disconnected", unicode(presence.status))
377-
378-    def test_unavailableBroadcast(self):
379-        """
380-        Test sending of unavailable presence broadcast.
381-        """
382-
383-        self.protocol.unavailable(None)
384-        presence = self.output[-1]
385-        self.assertEquals("presence", presence.name)
386-        self.assertEquals(None, presence.uri)
387-        self.assertEquals(None, presence.getAttribute('to'))
388-        self.assertEquals("unavailable", presence.getAttribute('type'))
389-
390-    def test_unavailableBroadcastNoEntityParameter(self):
391-        """
392-        Test sending of unavailable presence broadcast by not passing entity.
393-        """
394-
395-        self.protocol.unavailable()
396-        presence = self.output[-1]
397-        self.assertEquals("presence", presence.name)
398-        self.assertEquals(None, presence.uri)
399-        self.assertEquals(None, presence.getAttribute('to'))
400-        self.assertEquals("unavailable", presence.getAttribute('type'))
401-
402-
403-
404 class AvailabilityPresenceTest(unittest.TestCase):
405 
406     def test_fromElement(self):
407@@ -89,7 +28,7 @@
408                  </presence>
409               """
410 
411-        presence = xmppim.AvailabilityPresence.fromElement(parseXml(xml))
412+        presence = im.AvailabilityPresence.fromElement(parseXml(xml))
413         self.assertEquals(JID('user@example.org'), presence.sender)
414         self.assertEquals(JID('user@example.com'), presence.recipient)
415         self.assertTrue(presence.available)
416@@ -98,14 +37,15 @@
417         self.assertEquals(50, presence.priority)
418 
419 
420+
421 class PresenceProtocolTest(unittest.TestCase):
422     """
423-    Tests for L{xmppim.PresenceProtocol}
424+    Tests for L{im.PresenceProtocol}
425     """
426 
427     def setUp(self):
428         self.output = []
429-        self.protocol = xmppim.PresenceProtocol()
430+        self.protocol = im.PresenceProtocol()
431         self.protocol.parent = self
432         self.protocol.xmlstream = utility.EventDispatcher()
433         self.protocol.connectionInitialized()
434@@ -122,7 +62,7 @@
435         xml = """<presence type="error"/>"""
436 
437         def errorReceived(error):
438-            xmppim.PresenceProtocol.errorReceived(self.protocol, error)
439+            im.PresenceProtocol.errorReceived(self.protocol, error)
440             try:
441                 self.assertIsInstance(error, ErrorStanza)
442             except:
443@@ -143,9 +83,9 @@
444         xml = """<presence/>"""
445 
446         def availableReceived(presence):
447-            xmppim.PresenceProtocol.availableReceived(self.protocol, presence)
448+            im.PresenceProtocol.availableReceived(self.protocol, presence)
449             try:
450-                self.assertIsInstance(presence, xmppim.AvailabilityPresence)
451+                self.assertIsInstance(presence, im.AvailabilityPresence)
452             except:
453                 d.errback()
454             else:
455@@ -164,9 +104,9 @@
456         xml = """<presence type='unavailable'/>"""
457 
458         def unavailableReceived(presence):
459-            xmppim.PresenceProtocol.unavailableReceived(self.protocol, presence)
460+            im.PresenceProtocol.unavailableReceived(self.protocol, presence)
461             try:
462-                self.assertIsInstance(presence, xmppim.AvailabilityPresence)
463+                self.assertIsInstance(presence, im.AvailabilityPresence)
464             except:
465                 d.errback()
466             else:
467@@ -185,9 +125,9 @@
468         xml = """<presence type='subscribe'/>"""
469 
470         def subscribeReceived(presence):
471-            xmppim.PresenceProtocol.subscribeReceived(self.protocol, presence)
472+            im.PresenceProtocol.subscribeReceived(self.protocol, presence)
473             try:
474-                self.assertIsInstance(presence, xmppim.SubscriptionPresence)
475+                self.assertIsInstance(presence, im.SubscriptionPresence)
476             except:
477                 d.errback()
478             else:
479@@ -206,9 +146,9 @@
480         xml = """<presence type='unsubscribe'/>"""
481 
482         def unsubscribeReceived(presence):
483-            xmppim.PresenceProtocol.unsubscribeReceived(self.protocol, presence)
484+            im.PresenceProtocol.unsubscribeReceived(self.protocol, presence)
485             try:
486-                self.assertIsInstance(presence, xmppim.SubscriptionPresence)
487+                self.assertIsInstance(presence, im.SubscriptionPresence)
488             except:
489                 d.errback()
490             else:
491@@ -227,9 +167,9 @@
492         xml = """<presence type='subscribed'/>"""
493 
494         def subscribedReceived(presence):
495-            xmppim.PresenceProtocol.subscribedReceived(self.protocol, presence)
496+            im.PresenceProtocol.subscribedReceived(self.protocol, presence)
497             try:
498-                self.assertIsInstance(presence, xmppim.SubscriptionPresence)
499+                self.assertIsInstance(presence, im.SubscriptionPresence)
500             except:
501                 d.errback()
502             else:
503@@ -248,10 +188,10 @@
504         xml = """<presence type='unsubscribed'/>"""
505 
506         def unsubscribedReceived(presence):
507-            xmppim.PresenceProtocol.unsubscribedReceived(self.protocol,
508+            im.PresenceProtocol.unsubscribedReceived(self.protocol,
509                                                          presence)
510             try:
511-                self.assertIsInstance(presence, xmppim.SubscriptionPresence)
512+                self.assertIsInstance(presence, im.SubscriptionPresence)
513             except:
514                 d.errback()
515             else:
516@@ -270,9 +210,9 @@
517         xml = """<presence type='probe'/>"""
518 
519         def probeReceived(presence):
520-            xmppim.PresenceProtocol.probeReceived(self.protocol, presence)
521+            im.PresenceProtocol.probeReceived(self.protocol, presence)
522             try:
523-                self.assertIsInstance(presence, xmppim.ProbePresence)
524+                self.assertIsInstance(presence, im.ProbePresence)
525             except:
526                 d.errback()
527             else:
528@@ -451,12 +391,12 @@
529 
530 class RosterClientProtocolTest(unittest.TestCase):
531     """
532-    Tests for L{xmppim.RosterClientProtocol}.
533+    Tests for L{im.RosterClientProtocol}.
534     """
535 
536     def setUp(self):
537         self.stub = XmlStreamStub()
538-        self.protocol = xmppim.RosterClientProtocol()
539+        self.protocol = im.RosterClientProtocol()
540         self.protocol.xmlstream = self.stub.xmlstream
541         self.protocol.connectionInitialized()
542 
Note: See TracBrowser for help on using the repository browser.