mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
API cleanup, bug fixes
This commit is contained in:
parent
89aeb27afb
commit
e1cb484d2d
6 changed files with 23 additions and 14 deletions
|
|
@ -38,17 +38,18 @@ class Storage(object):
|
||||||
def get(self, uid):
|
def get(self, uid):
|
||||||
'''
|
'''
|
||||||
:param uid: uid to fetch
|
:param uid: uid to fetch
|
||||||
:returns: (object, uid, etag)
|
:returns: (object, etag)
|
||||||
'''
|
'''
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def get_multi(self, uids):
|
def get_multi(self, uids):
|
||||||
'''
|
'''
|
||||||
:param uids: list of uids to fetch
|
:param uids: list of uids to fetch
|
||||||
:returns: iterable of (object, uid, etag)
|
:returns: iterable of (uid, obj, etag)
|
||||||
'''
|
'''
|
||||||
for uid in uids:
|
for uid in uids:
|
||||||
yield self.get(uid)
|
obj, etag = self.get(uid)
|
||||||
|
yield uid, obj, etag
|
||||||
|
|
||||||
def has(self, uid):
|
def has(self, uid):
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ class FilesystemStorage(Storage):
|
||||||
def get(self, uid):
|
def get(self, uid):
|
||||||
fpath = self._get_filepath(uid)
|
fpath = self._get_filepath(uid)
|
||||||
with open(fpath, 'rb') as f:
|
with open(fpath, 'rb') as f:
|
||||||
return Item(f.read()), uid, os.path.getmtime(fpath)
|
return Item(f.read()), os.path.getmtime(fpath)
|
||||||
|
|
||||||
def has(self, uid):
|
def has(self, uid):
|
||||||
return os.path.isfile(self._get_filepath(uid))
|
return os.path.isfile(self._get_filepath(uid))
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ class MemoryStorage(Storage):
|
||||||
|
|
||||||
def get(self, uid):
|
def get(self, uid):
|
||||||
etag, obj = self.items[uid]
|
etag, obj = self.items[uid]
|
||||||
return obj, uid, etag
|
return obj, etag
|
||||||
|
|
||||||
def has(self, uid):
|
def has(self, uid):
|
||||||
return uid in self.items
|
return uid in self.items
|
||||||
|
|
|
||||||
|
|
@ -36,9 +36,9 @@ def sync(storage_a, storage_b, status):
|
||||||
get_actions(list_a, list_b, status)
|
get_actions(list_a, list_b, status)
|
||||||
|
|
||||||
def prefetch():
|
def prefetch():
|
||||||
for item, uid, etag in storage_a.get_multi(prefetch_from_a):
|
for uid, item, etag in storage_a.get_multi(prefetch_from_a):
|
||||||
items_a[uid] = (item, etag)
|
items_a[uid] = (item, etag)
|
||||||
for item, uid, etag in storage_b.get_multi(prefetch_from_b):
|
for uid, item, etag in storage_b.get_multi(prefetch_from_b):
|
||||||
items_b[uid] = (item, etag)
|
items_b[uid] = (item, etag)
|
||||||
prefetch()
|
prefetch()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,7 @@ class StorageTests(object):
|
||||||
assert a == b
|
assert a == b
|
||||||
for i in b:
|
for i in b:
|
||||||
assert s.has(i)
|
assert s.has(i)
|
||||||
item, uid, etag = s.get(i)
|
item, etag = s.get(i)
|
||||||
assert uid == i
|
|
||||||
assert item.raw == 'UID:{}'.format(i)
|
assert item.raw == 'UID:{}'.format(i)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,10 @@ class SyncTests(TestCase):
|
||||||
item = Item('UID:1')
|
item = Item('UID:1')
|
||||||
a.upload(item)
|
a.upload(item)
|
||||||
sync(a, b, status)
|
sync(a, b, status)
|
||||||
obj_a, uid_a, etag_a = a.get('1')
|
obj_a, etag_a = a.get('1')
|
||||||
obj_b, uid_b, etag_b = b.get('1')
|
obj_b, etag_b = b.get('1')
|
||||||
|
assert only(status) == '1'
|
||||||
assert obj_a.raw == obj_b.raw == item.raw
|
assert obj_a.raw == obj_b.raw == item.raw
|
||||||
assert uid_a == uid_b == only(status) == '1'
|
|
||||||
|
|
||||||
# creation and deletion
|
# creation and deletion
|
||||||
item2 = Item('UID:2')
|
item2 = Item('UID:2')
|
||||||
|
|
@ -48,7 +48,7 @@ class SyncTests(TestCase):
|
||||||
assert list(status) == ['2']
|
assert list(status) == ['2']
|
||||||
assert next(a.list())[0] == '2'
|
assert next(a.list())[0] == '2'
|
||||||
assert next(b.list())[0] == '2'
|
assert next(b.list())[0] == '2'
|
||||||
obj2_a, uid2_a, etag2_a = a.get('2')
|
obj2_a, etag2_a = a.get('2')
|
||||||
assert obj2_a.raw == item2.raw
|
assert obj2_a.raw == item2.raw
|
||||||
|
|
||||||
new_item2 = Item('UID:2\nHUEHUEHUE:PRECISELY')
|
new_item2 = Item('UID:2\nHUEHUEHUE:PRECISELY')
|
||||||
|
|
@ -59,7 +59,7 @@ class SyncTests(TestCase):
|
||||||
assert list(status) == list(old_status)
|
assert list(status) == list(old_status)
|
||||||
assert next(a.list())[0] == '2'
|
assert next(a.list())[0] == '2'
|
||||||
assert next(b.list())[0] == '2'
|
assert next(b.list())[0] == '2'
|
||||||
obj, uid, etag = b.get('2')
|
obj, etag = b.get('2')
|
||||||
assert obj.raw == new_item2.raw
|
assert obj.raw == new_item2.raw
|
||||||
|
|
||||||
def test_irrelevant_status(self):
|
def test_irrelevant_status(self):
|
||||||
|
|
@ -68,3 +68,12 @@ class SyncTests(TestCase):
|
||||||
status = {'1': ('UID:1', 1234)}
|
status = {'1': ('UID:1', 1234)}
|
||||||
sync(a, b, status)
|
sync(a, b, status)
|
||||||
assert not status
|
assert not status
|
||||||
|
|
||||||
|
def test_new_item(self):
|
||||||
|
a = MemoryStorage()
|
||||||
|
b = MemoryStorage()
|
||||||
|
status = {}
|
||||||
|
a.upload(Item('UID:1'))
|
||||||
|
sync(a, b, status)
|
||||||
|
obj, etag = b.get('1')
|
||||||
|
assert obj.raw == 'UID:1'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue