CapsCache: made docstring match the API, fixed minor bugs, added tests
This commit is contained in:
parent
00b7e9823f
commit
82b30c0791
|
@ -24,7 +24,7 @@ import helpers
|
||||||
|
|
||||||
class CapsCache(object):
|
class CapsCache(object):
|
||||||
''' This object keeps the mapping between caps data and real disco
|
''' This object keeps the mapping between caps data and real disco
|
||||||
features they represent, and provides simple way to query that info.
|
features they represent, and provides simple way to query that info.
|
||||||
It is application-wide, that is there's one object for all
|
It is application-wide, that is there's one object for all
|
||||||
connections.
|
connections.
|
||||||
Goals:
|
Goals:
|
||||||
|
@ -44,46 +44,39 @@ class CapsCache(object):
|
||||||
# object creation
|
# object creation
|
||||||
>>> cc=CapsCache(logger_object)
|
>>> cc=CapsCache(logger_object)
|
||||||
|
|
||||||
>>> caps=('http://exodus.jabberstudio.org/caps', '0.9', None) # node, ver, ext
|
>>> caps = ('sha-1', '66/0NaeaBKkwk85efJTGmU47vXI=')
|
||||||
>>> muc='http://jabber.org/protocol/muc'
|
>>> muc = 'http://jabber.org/protocol/muc'
|
||||||
>>> chatstates='http://jabber.org/protocol/chatstates'
|
>>> chatstates = 'http://jabber.org/protocol/chatstates'
|
||||||
|
|
||||||
|
# setting data
|
||||||
|
>>> cc[caps].identities = [{'category':'client', 'type':'pc'}]
|
||||||
|
>>> cc[caps].features = [muc]
|
||||||
|
|
||||||
# retrieving data
|
# retrieving data
|
||||||
>>> muc in cc[caps].features
|
>>> muc in cc[caps].features
|
||||||
True
|
True
|
||||||
>>> muc in cc[caps]
|
>>> chatstates in cc[caps].features
|
||||||
True
|
|
||||||
>>> chatstates in cc[caps]
|
|
||||||
False
|
False
|
||||||
>>> cc[caps].identities
|
>>> cc[caps].identities
|
||||||
set({'category':'client', 'type':'pc'})
|
[{'category': 'client', 'type': 'pc'}]
|
||||||
>>> x=cc[caps] # more efficient if making several queries for one set of caps
|
>>> x = cc[caps] # more efficient if making several queries for one set of caps
|
||||||
ATypicalBlackBoxObject
|
ATypicalBlackBoxObject
|
||||||
>>> muc in x
|
>>> muc in x.features
|
||||||
True
|
True
|
||||||
>>> x.node
|
|
||||||
'http://exodus.jabberstudio.org/caps'
|
|
||||||
|
|
||||||
# retrieving data (multiple exts case)
|
# retrieving data (multiple exts case)
|
||||||
>>> caps=('http://gajim.org/caps', '0.9', ('csn', 'ft'))
|
>>> caps=('http://gajim.org/caps', '0.9', ('csn', 'ft'))
|
||||||
>>> muc in cc[caps]
|
>>> muc in cc[caps].features
|
||||||
True
|
True
|
||||||
|
|
||||||
# setting data
|
|
||||||
>>> newcaps=('http://exodus.jabberstudio.org/caps', '0.9a', None)
|
|
||||||
>>> cc[newcaps].identities.add({'category':'client', 'type':'pc', 'name':'Gajim'})
|
|
||||||
>>> cc[newcaps].features+=muc # same as:
|
|
||||||
>>> cc[newcaps]+=muc
|
|
||||||
>>> cc[newcaps]['csn']+=chatstates # adding data as if ext was 'csn'
|
|
||||||
# warning: no feature removal!
|
|
||||||
'''
|
'''
|
||||||
def __init__(self, logger=None):
|
def __init__(self, logger=None):
|
||||||
''' Create a cache for entity capabilities. '''
|
''' Create a cache for entity capabilities. '''
|
||||||
# our containers:
|
# our containers:
|
||||||
# __cache is a dictionary mapping: pair of node and version maps
|
# __cache is a dictionary mapping: pair of hash method and hash maps
|
||||||
# to CapsCacheItem object
|
# to CapsCacheItem object
|
||||||
# __CacheItem is a class that stores data about particular
|
# __CacheItem is a class that stores data about particular
|
||||||
# client (node/version pair)
|
# client (hash method/hash pair)
|
||||||
self.__cache = {}
|
self.__cache = {}
|
||||||
|
|
||||||
class CacheItem(object):
|
class CacheItem(object):
|
||||||
|
@ -129,7 +122,7 @@ class CapsCache(object):
|
||||||
d['xml:lang'] = i[2]
|
d['xml:lang'] = i[2]
|
||||||
if i[3]:
|
if i[3]:
|
||||||
d['name'] = i[3]
|
d['name'] = i[3]
|
||||||
list.append(d)
|
list_.append(d)
|
||||||
return list_
|
return list_
|
||||||
def _set_identities(ciself, value):
|
def _set_identities(ciself, value):
|
||||||
ciself._identities = []
|
ciself._identities = []
|
||||||
|
@ -172,7 +165,9 @@ class CapsCache(object):
|
||||||
def __getitem__(self, caps):
|
def __getitem__(self, caps):
|
||||||
if caps in self.__cache:
|
if caps in self.__cache:
|
||||||
return self.__cache[caps]
|
return self.__cache[caps]
|
||||||
hash_method, hash = caps[0], caps[1]
|
|
||||||
|
hash_method, hash = caps
|
||||||
|
|
||||||
x = self.__CacheItem(hash_method, hash)
|
x = self.__CacheItem(hash_method, hash)
|
||||||
self.__cache[(hash_method, hash)] = x
|
self.__cache[(hash_method, hash)] = x
|
||||||
return x
|
return x
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
# tests for capabilities and the capabilities cache
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import testlib
|
||||||
|
testlib.setup_env()
|
||||||
|
|
||||||
|
from common import gajim
|
||||||
|
from common import xmpp
|
||||||
|
|
||||||
|
from common.caps import CapsCache
|
||||||
|
|
||||||
|
from mock import Mock
|
||||||
|
|
||||||
|
class MockLogger(Mock):
|
||||||
|
def __init__(self, *args):
|
||||||
|
Mock.__init__(self, *args)
|
||||||
|
|
||||||
|
class TestCapsCache(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.logger = MockLogger()
|
||||||
|
self.cc = CapsCache(self.logger)
|
||||||
|
|
||||||
|
def test_examples(self):
|
||||||
|
'''tests the examples given in common/caps.py'''
|
||||||
|
|
||||||
|
caps = ('sha-1', '66/0NaeaBKkwk85efJTGmU47vXI=')
|
||||||
|
identity = {'category': 'client', 'type': 'pc'}
|
||||||
|
|
||||||
|
muc = 'http://jabber.org/protocol/muc'
|
||||||
|
chatstates = 'http://jabber.org/protocol/chatstates'
|
||||||
|
|
||||||
|
self.cc[caps].identities = [identity]
|
||||||
|
self.cc[caps].features = [muc]
|
||||||
|
|
||||||
|
self.assert_(muc in self.cc[caps].features)
|
||||||
|
self.assert_(chatstates not in self.cc[caps].features)
|
||||||
|
|
||||||
|
id = self.cc[caps].identities
|
||||||
|
|
||||||
|
self.assertEqual(1, len(id))
|
||||||
|
|
||||||
|
id = id[0]
|
||||||
|
self.assertEqual('client', id['category'])
|
||||||
|
self.assertEqual('pc', id['type'])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue