Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to improve compatibility of _Storage with dict #119

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
20 changes: 20 additions & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,23 @@ def test_clear(self):
storage.clear()
with open(filename) as inp:
self.assertEqual(inp.read(), '{}')

def test_dict_compatibility(self):
"""Test that the _Storage class fully implements dict()"""
filename = '/tmp/testdict1.json'
storage1 = _Storage(filename, file_format='json')
storage2 = _Storage(filename.replace('1','2'), file_format='json')
storage1['key'] = 'abc'
self.assertEqual(storage1, storage1, '%s is equal to itself' % dict(storage1))
self.assertNotEqual(storage1, storage2, '%s & %s are not equal' % (dict(storage1), dict(storage2)))
self.assertGreater(storage1, storage2, '%s > %s' % (dict(storage1), dict(storage2)))
self.assertLess(storage2, storage1, '%s < %s' % (dict(storage1), dict(storage2)))
self.assertEqual(len(storage1),1)
self.assertEqual(len(storage2),0)
self.assertTrue(bool(storage1), 'non-empty objects test True')
self.assertFalse(bool(storage2), 'empty objects test False')
self.assertTrue(storage1.has_key('key'))
self.assertFalse(storage1.has_key('non-key'))



22 changes: 19 additions & 3 deletions xbmcswift2/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def raw_dict(self):
class _Storage(collections.MutableMapping, _PersistentDictMixin):
'''Storage that acts like a dict but also can persist to disk.

:param filename: An absolute filepath to reprsent the storage on disk. The
:param filename: An absolute filepath to represent the storage on disk. The
storage will loaded from this file if it already exists,
otherwise the file will be created.
:param file_format: 'pickle', 'json' or 'csv'. pickle is the default. Be
aware that json and csv have limited support for python
objets.
objects.

.. warning:: Currently there are no limitations on the size of the storage.
Please be sure to call :meth:`~xbmcswift2._Storage.clear`
Expand All @@ -135,13 +135,29 @@ def __iter__(self):
return iter(self._items)

def __len__(self):
return self._items.__len__
return self._items.__len__()

def viewkeys(self):
return self._items.viewkeys()

def viewitems(self):
return dict(self).viewitems()

def viewvalues(self):
return dict(self).viewvalues()

# without defining this, cmp() still works, but it looks like all _Storage()
# objects are equal
# I don't compare the underlying dicts because of the timestamps
def __cmp__(self, other):
return cmp(dict(self), dict(other))

def raw_dict(self):
'''Returns the wrapped dict'''
return self._items

initial_update = collections.MutableMapping.update
has_key = collections.MutableMapping.__contains__

def clear(self):
super(_Storage, self).clear()
Expand Down