Changeset 50:b69ca8f7174e in ralphm-patches for roster_item.patch


Ignore:
Timestamp:
Jun 1, 2011, 9:29:29 AM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Cleanup JIDs a bit more and adjust affected patches.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roster_item.patch

    r49 r50  
    1 diff -r c91f18811c37 wokkel/im.py
    2 --- a/wokkel/im.py      Mon May 23 18:19:44 2011 +0200
    3 +++ b/wokkel/im.py      Wed May 25 09:24:29 2011 +0200
    4 @@ -7,21 +7,27 @@
     1diff -r c103529d9f46 wokkel/im.py
     2--- a/wokkel/im.py      Wed May 25 20:24:36 2011 +0200
     3+++ b/wokkel/im.py      Wed May 25 20:26:47 2011 +0200
     4@@ -7,21 +7,25 @@
    55 XMPP IM protocol support.
    66 
     
    1212 """
    1313 
    14 +import warnings
    15 +
    1614+from twisted.internet import defer
    17  from twisted.words.protocols.jabber.jid import JID, internJID
     15 from twisted.words.protocols.jabber import jid
    1816+from twisted.words.protocols.jabber import error
    1917 from twisted.words.xish import domish
     
    3432     """
    3533     Stanza of kind presence.
    36 @@ -349,8 +355,8 @@
     34@@ -349,8 +353,8 @@
    3735 
    3836     This represents one contact from an XMPP contact list known as roster.
    3937 
    4038-    @ivar jid: The JID of the contact.
    41 -    @type jid: L{JID}
     39-    @type jid: L{jid.JID}
    4240+    @ivar entity: The JID of the contact.
    43 +    @type entity: L{JID}
     41+    @type entity: L{jid.JID}
    4442     @ivar name: The optional associated nickname for this contact.
    4543     @type name: C{unicode}
    4644     @ivar subscriptionTo: Subscription state to contact's presence. If C{True},
    47 @@ -360,47 +366,126 @@
     45@@ -360,47 +364,99 @@
    4846     @ivar subscriptionFrom: Contact's subscription state. If C{True}, the
    4947                             contact is subscribed to the presence information
     
    8785-class RosterClientProtocol(XMPPHandler):
    8886+
    89 +    def __getJID(self):
    90 +        warnings.warn("Use RosterItem.entity instead.", DeprecationWarning)
    91 +        return self.entity
    92 +
    93 +
    94 +    def __setJID(self, value):
    95 +        warnings.warn("Use RosterItem.entity instead.", DeprecationWarning)
    96 +        self.entity = value
    97 +
    98 +
    99 +    jid = property(__getJID, __setJID, doc="""
    100 +            JID of the contact. Deprecated in favour of C{entity}.""")
    101 +
    102 +
    103 +    def __getAsk(self):
    104 +        warnings.warn("Use RosterItem.pendingOut instead.", DeprecationWarning)
    105 +        return self.pendingOut
    106 +
    107 +
    108 +    def __setAsk(self, value):
    109 +        warnings.warn("Use RosterItem.pendingOut instead.", DeprecationWarning)
    110 +        self.pendingOut = value
    111 +
    112 +
    113 +    ask = property(__getAsk, __setAsk, doc="""
    114 +            Pending out subscription. Deprecated in favour of C{pendingOut}.""")
    115 +
    116 +
    11787+    def toElement(self):
    11888+        element = domish.Element((NS_ROSTER, 'item'))
     
    146116+    @classmethod
    147117+    def fromElement(Class, element):
    148 +        entity = internJID(element['jid'])
     118+        entity = jid.internJID(element['jid'])
    149119+        item = Class(entity)
    150120+        subscription = element.getAttribute('subscription')
     
    178148+    iqHandlers = {XPATH_ROSTER_SET: "_onRosterSet"}
    179149+
     150+
    180151     def connectionInitialized(self):
    181152-        ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
     
    184155-
    185156-    def _parseRosterItem(self, element):
    186 -        jid = internJID(element['jid'])
    187 -        item = RosterItem(jid)
     157-        entity = jid.internJID(element['jid'])
     158-        item = RosterItem(entity)
    188159-        item.name = element.getAttribute('name')
    189160-        subscription = element.getAttribute('subscription')
     
    200171 
    201172     def getRoster(self):
    202 @@ -415,8 +500,8 @@
     173@@ -415,8 +471,8 @@
    203174             roster = {}
    204175             for element in domish.generateElementsQNamed(result.query.children,
     
    211182             return roster
    212183 
    213 @@ -437,32 +522,37 @@
     184@@ -437,32 +493,36 @@
    214185         """
    215186         iq = IQ(self.xmlstream, 'set')
     
    228199-           iq.hasAttribute('from') and iq['from'] != self.xmlstream:
    229200-            return
    230 -
    231 -        iq.handled = True
    232201+        def eb(failure):
    233202+            failure.trap(RosterPushIgnored)
    234203+            raise error.StanzaError('service-unavailable')
    235204 
    236          itemElement = iq.query.item
     205-        iq.handled = True
    237206+        item = RosterItem.fromElement(iq.query.item)
    238207 
    239 -        if unicode(itemElement['subscription']) == 'remove':
    240 -            self.onRosterRemove(internJID(itemElement['jid']))
     208-        itemElement = iq.query.item
    241209+        if item.remove:
    242210+            d = defer.maybeDeferred(self.onRosterRemove, item.entity)
    243          else:
     211+        else:
     212+            d = defer.maybeDeferred(self.onRosterSet, item)
     213 
     214-        if unicode(itemElement['subscription']) == 'remove':
     215-            self.onRosterRemove(jid.internJID(itemElement['jid']))
     216-        else:
    244217-            item = self._parseRosterItem(iq.query.item)
    245218-            self.onRosterSet(item)
    246 +            d = defer.maybeDeferred(self.onRosterSet, item)
    247 +
    248219+        d.addErrback(eb)
    249220+        return d
     
    261232         @type item: L{RosterItem}
    262233         """
    263 @@ -472,6 +562,10 @@
     234@@ -472,6 +532,10 @@
    264235         """
    265236         Called when a roster push for the removal of an item was received.
     
    270241+
    271242         @param entity: The entity for which the roster item has been removed.
    272          @type entity: L{JID}
     243         @type entity: L{jid.JID}
    273244         """
    274 diff -r c91f18811c37 wokkel/test/test_im.py
    275 --- a/wokkel/test/test_im.py    Mon May 23 18:19:44 2011 +0200
    276 +++ b/wokkel/test/test_im.py    Wed May 25 09:24:29 2011 +0200
     245diff -r c103529d9f46 wokkel/test/test_im.py
     246--- a/wokkel/test/test_im.py    Wed May 25 20:24:36 2011 +0200
     247+++ b/wokkel/test/test_im.py    Wed May 25 20:26:47 2011 +0200
    277248@@ -7,13 +7,14 @@
    278249 
     
    291262 NS_XML = 'http://www.w3.org/XML/1998/namespace'
    292263 NS_ROSTER = 'jabber:iq:roster'
    293 @@ -389,23 +390,356 @@
     264@@ -389,23 +390,305 @@
    294265 
    295266 
     
    300271+    Tests for L{im.RosterItem}.
    301272+    """
    302 +
    303 +    def test_jidDeprecationGet(self):
    304 +        """
    305 +        Getting the jid attribute works as entity and warns deprecation.
    306 +        """
    307 +        item = im.RosterItem(JID('user@example.org'))
    308 +        entity = self.assertWarns(DeprecationWarning,
    309 +                                  "Use RosterItem.entity instead.",
    310 +                                  im.__file__,
    311 +                                  getattr, item, 'jid')
    312 +        self.assertIdentical(entity, item.entity)
    313 +
    314 +
    315 +    def test_jidDeprecationSet(self):
    316 +        """
    317 +        Setting the jid attribute works as entity and warns deprecation.
    318 +        """
    319 +        item = im.RosterItem(JID('user@example.org'))
    320 +        entity = self.assertWarns(DeprecationWarning,
    321 +                                  "Use RosterItem.entity instead.",
    322 +                                  im.__file__,
    323 +                                  setattr, item, 'jid',
    324 +                                  JID('other@example.org'))
    325 +        self.assertEquals(JID('other@example.org'), item.entity)
    326 +
    327 +
    328 +    def test_askDeprecationGet(self):
    329 +        """
    330 +        Getting the ask attribute works as entity and warns deprecation.
    331 +        """
    332 +        item = im.RosterItem(JID('user@example.org'))
    333 +        item.pendingOut = True
    334 +        ask = self.assertWarns(DeprecationWarning,
    335 +                               "Use RosterItem.pendingOut instead.",
    336 +                               im.__file__,
    337 +                               getattr, item, 'ask')
    338 +        self.assertTrue(ask)
    339 +
    340 +
    341 +    def test_askDeprecationSet(self):
    342 +        """
    343 +        Setting the ask attribute works as entity and warns deprecation.
    344 +        """
    345 +        item = im.RosterItem(JID('user@example.org'))
    346 +        entity = self.assertWarns(DeprecationWarning,
    347 +                                  "Use RosterItem.pendingOut instead.",
    348 +                                  im.__file__,
    349 +                                  setattr, item, 'ask',
    350 +                                  True)
    351 +        self.assertTrue(item.pendingOut)
    352 +
    353273+
    354274+    def test_toElement(self):
     
    653573         # Inspect outgoing iq request
    654574 
    655 @@ -419,10 +753,117 @@
     575@@ -419,10 +702,117 @@
    656576         self.assertEquals(1, len(children))
    657577         child = children[0]
Note: See TracChangeset for help on using the changeset viewer.