Cleaned up names.

This commit is contained in:
Lance Stout 2010-10-27 08:09:50 -04:00
parent 673545c7e4
commit 69d430dd75
7 changed files with 36 additions and 37 deletions

View file

@ -15,7 +15,7 @@ import logging
import sleekxmpp import sleekxmpp
from sleekxmpp import plugins from sleekxmpp import plugins
from sleekxmpp.roster import MultiRoster import sleekxmpp.roster as roster
from sleekxmpp.stanza import Message, Presence, Iq, Error from sleekxmpp.stanza import Message, Presence, Iq, Error
from sleekxmpp.stanza.roster import Roster from sleekxmpp.stanza.roster import Roster
from sleekxmpp.stanza.nick import Nick from sleekxmpp.stanza.nick import Nick
@ -111,9 +111,8 @@ class BaseXMPP(XMLStream):
self.boundjid = JID(jid) self.boundjid = JID(jid)
self.plugin = {} self.plugin = {}
self.rosters = MultiRoster(self) self.roster = roster.Roster(self)
self.rosters.add(self.boundjid.bare) self.roster.add(self.boundjid.bare)
self.roster = {}
self.is_component = False self.is_component = False
self.auto_authorize = True self.auto_authorize = True
@ -537,14 +536,14 @@ class BaseXMPP(XMLStream):
self.event('message', msg) self.event('message', msg)
def _handle_available(self, presence): def _handle_available(self, presence):
self.rosters[presence['to'].bare][presence['from'].bare].handle_available(presence) self.roster[presence['to'].bare][presence['from'].bare].handle_available(presence)
def _handle_unavailable(self, presence): def _handle_unavailable(self, presence):
self.rosters[presence['to'].bare][presence['from'].bare].handle_unavailable(presence) self.roster[presence['to'].bare][presence['from'].bare].handle_unavailable(presence)
def _handle_new_subscription(self, stanza): def _handle_new_subscription(self, stanza):
roster = self.rosters[stanza['to'].bare] roster = self.roster[stanza['to'].bare]
item = self.rosters[stanza['to'].bare][stanza['from'].bare] item = self.roster[stanza['to'].bare][stanza['from'].bare]
if item['whitelisted']: if item['whitelisted']:
item.authorize() item.authorize()
elif roster.auto_authorize: elif roster.auto_authorize:
@ -555,22 +554,22 @@ class BaseXMPP(XMLStream):
item.unauthorize() item.unauthorize()
def _handle_removed_subscription(self, presence): def _handle_removed_subscription(self, presence):
self.rosters[presence['to'].bare][presence['from'].bare].unauthorize() self.roster[presence['to'].bare][presence['from'].bare].unauthorize()
def _handle_subscribe(self, stanza): def _handle_subscribe(self, stanza):
self.rosters[stanza['to'].bare][stanza['from'].bare].handle_subscribe(stanza) self.roster[stanza['to'].bare][stanza['from'].bare].handle_subscribe(stanza)
def _handle_subscribed(self, stanza): def _handle_subscribed(self, stanza):
self.rosters[stanza['to'].bare][stanza['from'].bare].handle_subscribed(stanza) self.roster[stanza['to'].bare][stanza['from'].bare].handle_subscribed(stanza)
def _handle_unsubscribe(self, stanza): def _handle_unsubscribe(self, stanza):
self.rosters[stanza['to'].bare][stanza['from'].bare].handle_unsubscribe(stanza) self.roster[stanza['to'].bare][stanza['from'].bare].handle_unsubscribe(stanza)
def _handle_unsubscribed(self, stanza): def _handle_unsubscribed(self, stanza):
self.rosters[stanza['to'].bare][stanza['from'].bare].handle_unsubscribed(stanza) self.roster[stanza['to'].bare][stanza['from'].bare].handle_unsubscribed(stanza)
def _handle_probe(self, stanza): def _handle_probe(self, stanza):
self.rosteritems[stanza['to'].bare][stanza['from'].bare].handle_probe(stanza) self.roster[stanza['to'].bare][stanza['from'].bare].handle_probe(stanza)
def _handle_presence(self, presence): def _handle_presence(self, presence):
""" """

View file

@ -421,7 +421,7 @@ class ClientXMPP(BaseXMPP):
if iq['type'] == 'set' or (iq['type'] == 'result' and request): if iq['type'] == 'set' or (iq['type'] == 'result' and request):
for jid in iq['roster']['items']: for jid in iq['roster']['items']:
item = iq['roster']['items'][jid] item = iq['roster']['items'][jid]
roster = self.rosters[iq['to'].bare] roster = self.roster[iq['to'].bare]
roster[jid]['name'] = item['name'] roster[jid]['name'] = item['name']
roster[jid]['groups'] = item['groups'] roster[jid]['groups'] = item['groups']
roster[jid]['from'] = item['subscription'] in ['from', 'both'] roster[jid]['from'] = item['subscription'] in ['from', 'both']

View file

@ -1,11 +1,11 @@
import logging import logging
class MultiRoster(object): class Roster(object):
def __init__(self, xmpp, datastore=None): def __init__(self, xmpp, db=None):
self.xmpp = xmpp self.xmpp = xmpp
self.datastore = datastore self.db = db
self._rosters = {} self._rosters = {}
def __getitem__(self, key): def __getitem__(self, key):
@ -21,14 +21,14 @@ class MultiRoster(object):
def add(self, node): def add(self, node):
if node not in self._rosters: if node not in self._rosters:
self._rosters[node] = Roster(self.xmpp, node, self.datastore) self._rosters[node] = RosterNode(self.xmpp, node, self.db)
class Roster(object): class RosterNode(object):
def __init__(self, xmpp, jid, datastore=None): def __init__(self, xmpp, jid, db=None):
self.xmpp = xmpp self.xmpp = xmpp
self.jid = jid self.jid = jid
self.datastore = datastore self.db = db
self.auto_authorize = True self.auto_authorize = True
self.auto_subscribe = True self.auto_subscribe = True
self._jids = {} self._jids = {}
@ -56,7 +56,7 @@ class Roster(object):
'whitelisted': whitelisted, 'whitelisted': whitelisted,
'subscription': 'none'} 'subscription': 'none'}
self._jids[jid] = RosterItem(self.xmpp, jid, self.jid, self._jids[jid] = RosterItem(self.xmpp, jid, self.jid,
state=state, datastore=self.datastore) state=state, db=self.db)
if save: if save:
self._jids[jid].save() self._jids[jid].save()
@ -99,14 +99,13 @@ class Roster(object):
class RosterItem(object): class RosterItem(object):
def __init__(self, xmpp, jid, owner=None, def __init__(self, xmpp, jid, owner=None,
state=None, datastore=None): state=None, db=None):
self.xmpp = xmpp self.xmpp = xmpp
self.jid = jid self.jid = jid
self.owner = owner or self.xmpp.jid self.owner = owner or self.xmpp.jid
self.last_status = None self.last_status = None
self.resources = {} self.resources = {}
self.datastore = datastore self.db = db
self._state = state or { self._state = state or {
'from': False, 'from': False,
'to': False, 'to': False,
@ -116,13 +115,13 @@ class RosterItem(object):
'subscription': 'none', 'subscription': 'none',
'name': '', 'name': '',
'groups': []} 'groups': []}
self._datastore_state = {} self._db_state = {}
self.load() self.load()
def load(self): def load(self):
if self.datastore: if self.db:
item = self.datastore.load(self.owner, self.jid, item = self.db.load(self.owner, self.jid,
self._datastore_state) self._db_state)
if item: if item:
self['name'] = item['name'] self['name'] = item['name']
self['groups'] = item['groups'] self['groups'] = item['groups']
@ -136,9 +135,9 @@ class RosterItem(object):
return None return None
def save(self): def save(self):
if self.datastore: if self.db:
self.datastore.save(self.owner, self.jid, self.db.save(self.owner, self.jid,
self._state, self._datastore_state) self._state, self._db_state)
def __getitem__(self, key): def __getitem__(self, key):
if key in self._state: if key in self._state:

View file

@ -140,7 +140,7 @@ class SleekTest(unittest.TestCase):
def check_roster(self, owner, jid, name=None, subscription=None, def check_roster(self, owner, jid, name=None, subscription=None,
afrom=None, ato=None, pending_out=None, pending_in=None, afrom=None, ato=None, pending_out=None, pending_in=None,
groups=None): groups=None):
roster = self.xmpp.rosters[owner][jid] roster = self.xmpp.roster[owner][jid]
print roster._state print roster._state
if name is not None: if name is not None:
self.assertEqual(roster['name'], name, self.assertEqual(roster['name'], name,

View file

@ -49,7 +49,8 @@ class TestPresenceStanzas(SleekTest):
self.failUnless(happened == [], self.failUnless(happened == [],
"changed_status event triggered for extra unavailable presence") "changed_status event triggered for extra unavailable presence")
self.failUnless(c.roster == {}, roster = c.roster['crap@wherever']
self.failUnless(roster['bill@chadmore.com'].resources == {},
"Roster updated for superfulous unavailable presence") "Roster updated for superfulous unavailable presence")
def testNickPlugin(self): def testNickPlugin(self):

View file

@ -183,7 +183,7 @@ class TestStreamPresence(SleekTest):
presence_subscribe) presence_subscribe)
# With this setting we should reject all subscriptions. # With this setting we should reject all subscriptions.
self.xmpp.rosters['tester@localhost'].auto_authorize = False self.xmpp.roster['tester@localhost'].auto_authorize = False
self.recv(""" self.recv("""
<presence from="user@localhost" <presence from="user@localhost"

View file

@ -41,7 +41,7 @@ class TestStreamRoster(SleekTest):
# Wait for get_roster to return. # Wait for get_roster to return.
t.join() t.join()
print self.xmpp.rosters['tester@localhost']['user@localhost']._state print self.xmpp.roster['tester@localhost']['user@localhost']._state
self.check_roster('tester@localhost', 'user@localhost', self.check_roster('tester@localhost', 'user@localhost',
name='User', name='User',
subscription='from', subscription='from',