Changeset 50:b69ca8f7174e in ralphm-patches for roster_item.patch
- Timestamp:
- Jun 1, 2011, 9:29:29 AM (11 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
roster_item.patch
r49 r50 1 diff -r c 91f18811c37wokkel/im.py2 --- a/wokkel/im.py Mon May 23 18:19:442011 +02003 +++ b/wokkel/im.py Wed May 25 09:24:292011 +02004 @@ -7,21 +7,2 7@@1 diff -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 @@ 5 5 XMPP IM protocol support. 6 6 … … 12 12 """ 13 13 14 +import warnings15 +16 14 +from twisted.internet import defer 17 from twisted.words.protocols.jabber .jid import JID, internJID15 from twisted.words.protocols.jabber import jid 18 16 +from twisted.words.protocols.jabber import error 19 17 from twisted.words.xish import domish … … 34 32 """ 35 33 Stanza of kind presence. 36 @@ -349,8 +35 5,8 @@34 @@ -349,8 +353,8 @@ 37 35 38 36 This represents one contact from an XMPP contact list known as roster. 39 37 40 38 - @ivar jid: The JID of the contact. 41 - @type jid: L{ JID}39 - @type jid: L{jid.JID} 42 40 + @ivar entity: The JID of the contact. 43 + @type entity: L{ JID}41 + @type entity: L{jid.JID} 44 42 @ivar name: The optional associated nickname for this contact. 45 43 @type name: C{unicode} 46 44 @ivar subscriptionTo: Subscription state to contact's presence. If C{True}, 47 @@ -360,47 +36 6,126@@45 @@ -360,47 +364,99 @@ 48 46 @ivar subscriptionFrom: Contact's subscription state. If C{True}, the 49 47 contact is subscribed to the presence information … … 87 85 -class RosterClientProtocol(XMPPHandler): 88 86 + 89 + def __getJID(self):90 + warnings.warn("Use RosterItem.entity instead.", DeprecationWarning)91 + return self.entity92 +93 +94 + def __setJID(self, value):95 + warnings.warn("Use RosterItem.entity instead.", DeprecationWarning)96 + self.entity = value97 +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.pendingOut106 +107 +108 + def __setAsk(self, value):109 + warnings.warn("Use RosterItem.pendingOut instead.", DeprecationWarning)110 + self.pendingOut = value111 +112 +113 + ask = property(__getAsk, __setAsk, doc="""114 + Pending out subscription. Deprecated in favour of C{pendingOut}.""")115 +116 +117 87 + def toElement(self): 118 88 + element = domish.Element((NS_ROSTER, 'item')) … … 146 116 + @classmethod 147 117 + def fromElement(Class, element): 148 + entity = internJID(element['jid'])118 + entity = jid.internJID(element['jid']) 149 119 + item = Class(entity) 150 120 + subscription = element.getAttribute('subscription') … … 178 148 + iqHandlers = {XPATH_ROSTER_SET: "_onRosterSet"} 179 149 + 150 + 180 151 def connectionInitialized(self): 181 152 - ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER … … 184 155 - 185 156 - def _parseRosterItem(self, element): 186 - jid =internJID(element['jid'])187 - item = RosterItem( jid)157 - entity = jid.internJID(element['jid']) 158 - item = RosterItem(entity) 188 159 - item.name = element.getAttribute('name') 189 160 - subscription = element.getAttribute('subscription') … … 200 171 201 172 def getRoster(self): 202 @@ -415,8 + 500,8 @@173 @@ -415,8 +471,8 @@ 203 174 roster = {} 204 175 for element in domish.generateElementsQNamed(result.query.children, … … 211 182 return roster 212 183 213 @@ -437,32 + 522,37@@184 @@ -437,32 +493,36 @@ 214 185 """ 215 186 iq = IQ(self.xmlstream, 'set') … … 228 199 - iq.hasAttribute('from') and iq['from'] != self.xmlstream: 229 200 - return 230 -231 - iq.handled = True232 201 + def eb(failure): 233 202 + failure.trap(RosterPushIgnored) 234 203 + raise error.StanzaError('service-unavailable') 235 204 236 itemElement = iq.query.item 205 - iq.handled = True 237 206 + item = RosterItem.fromElement(iq.query.item) 238 207 239 - if unicode(itemElement['subscription']) == 'remove': 240 - self.onRosterRemove(internJID(itemElement['jid'])) 208 - itemElement = iq.query.item 241 209 + if item.remove: 242 210 + 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: 244 217 - item = self._parseRosterItem(iq.query.item) 245 218 - self.onRosterSet(item) 246 + d = defer.maybeDeferred(self.onRosterSet, item)247 +248 219 + d.addErrback(eb) 249 220 + return d … … 261 232 @type item: L{RosterItem} 262 233 """ 263 @@ -472,6 +5 62,10 @@234 @@ -472,6 +532,10 @@ 264 235 """ 265 236 Called when a roster push for the removal of an item was received. … … 270 241 + 271 242 @param entity: The entity for which the roster item has been removed. 272 @type entity: L{ JID}243 @type entity: L{jid.JID} 273 244 """ 274 diff -r c 91f18811c37wokkel/test/test_im.py275 --- a/wokkel/test/test_im.py Mon May 23 18:19:442011 +0200276 +++ b/wokkel/test/test_im.py Wed May 25 09:24:292011 +0200245 diff -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 277 248 @@ -7,13 +7,14 @@ 278 249 … … 291 262 NS_XML = 'http://www.w3.org/XML/1998/namespace' 292 263 NS_ROSTER = 'jabber:iq:roster' 293 @@ -389,23 +390,3 56@@264 @@ -389,23 +390,305 @@ 294 265 295 266 … … 300 271 + Tests for L{im.RosterItem}. 301 272 + """ 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 = True334 + 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 +353 273 + 354 274 + def test_toElement(self): … … 653 573 # Inspect outgoing iq request 654 574 655 @@ -419,10 +7 53,117 @@575 @@ -419,10 +702,117 @@ 656 576 self.assertEquals(1, len(children)) 657 577 child = children[0]
Note: See TracChangeset
for help on using the changeset viewer.