Ability to handle missing UIDs.

This commit is contained in:
Markus Unterwaditzer 2014-04-04 13:20:18 +02:00
parent 50314418ec
commit f4cdde9a52
2 changed files with 28 additions and 59 deletions

View file

@ -18,25 +18,15 @@ class TestHttpStorage(object):
def test_list(self, monkeypatch): def test_list(self, monkeypatch):
collection_url = 'http://127.0.0.1/calendar/collection/' collection_url = 'http://127.0.0.1/calendar/collection/'
responses = [ items = [
dedent(b''' dedent(b'''
BEGIN:VCALENDAR
VERSION:2.0
PRODID:http://www.example.com/calendarapplication/
METHOD:PUBLISH
BEGIN:VEVENT BEGIN:VEVENT
UID:461092315540@example.com
LOCATION:Somewhere
SUMMARY:Eine Kurzinfo SUMMARY:Eine Kurzinfo
DESCRIPTION:Beschreibung des Termines DESCRIPTION:Beschreibung des Termines
CLASS:PUBLIC
DTSTART:20060910T220000Z
DTEND:20060919T215900Z
DTSTAMP:20060812T125900Z
END:VEVENT END:VEVENT
''').strip(),
dedent(b'''
BEGIN:VEVENT BEGIN:VEVENT
UID:461092315asdasd540@example.com
LOCATION:Somewhere else
SUMMARY:Eine zweite Kurzinfo SUMMARY:Eine zweite Kurzinfo
DESCRIPTION:Beschreibung des anderen Termines DESCRIPTION:Beschreibung des anderen Termines
BEGIN:VALARM BEGIN:VALARM
@ -47,8 +37,11 @@ class TestHttpStorage(object):
DURATION:PT1H DURATION:PT1H
END:VALARM END:VALARM
END:VEVENT END:VEVENT
END:VCALENDAR ''').strip()
''') ]
responses = [
'\n'.join([b'BEGIN:VCALENDAR'] + items + [b'END:VCALENDAR'])
] ]
def get(*a, **kw): def get(*a, **kw):
@ -61,40 +54,10 @@ class TestHttpStorage(object):
monkeypatch.setattr('requests.get', get) monkeypatch.setattr('requests.get', get)
s = HttpStorage(url=collection_url) s = HttpStorage(url=collection_url)
l = list(s.list())
hrefs = set(href for href, etag in l) for href, etag in s.list():
href1 = u'461092315540@example.com' item, etag2 = s.get(href)
href2 = u'461092315asdasd540@example.com' assert etag2 == etag
assert hrefs == set((href1, href2)) items.remove(item.raw.strip())
item, etag = s.get(href1) assert not items
assert_item_equals(item, Item(dedent(u'''
BEGIN:VEVENT
UID:461092315540@example.com
LOCATION:Somewhere
SUMMARY:Eine Kurzinfo
DESCRIPTION:Beschreibung des Termines
CLASS:PUBLIC
DTSTART:20060910T220000Z
DTEND:20060919T215900Z
DTSTAMP:20060812T125900Z
END:VEVENT
''').strip()))
item, etag = s.get(href2)
assert_item_equals(item, Item(dedent(u'''
BEGIN:VEVENT
UID:461092315asdasd540@example.com
LOCATION:Somewhere else
SUMMARY:Eine zweite Kurzinfo
DESCRIPTION:Beschreibung des anderen Termines
BEGIN:VALARM
ACTION:AUDIO
TRIGGER:19980403T120000
ATTACH;FMTTYPE=audio/basic:http://host.com/pub/ssbanner.aud
REPEAT:4
DURATION:PT1H
END:VALARM
END:VEVENT
''').strip()))

View file

@ -65,7 +65,6 @@ def prepare_verify(verify):
class HttpStorageBase(Storage): class HttpStorageBase(Storage):
_repr_attributes = ('username', 'url') _repr_attributes = ('username', 'url')
_items = None
def __init__(self, url, username='', password='', collection=None, def __init__(self, url, username='', password='', collection=None,
verify=True, auth=None, useragent='vdirsyncer', **kwargs): verify=True, auth=None, useragent='vdirsyncer', **kwargs):
@ -106,20 +105,27 @@ class HttpStorageBase(Storage):
class HttpStorage(HttpStorageBase): class HttpStorage(HttpStorageBase):
_items = None
def __init__(self, **kwargs):
super(HttpStorage, self).__init__(**kwargs)
self._items = {}
def list(self): def list(self):
if self._items is None: r = requests.get(self.url, **self._settings)
r = requests.get(self.url, **self._settings) r.raise_for_status()
r.raise_for_status() self._items.clear()
self._items = {} for i, item in enumerate(split_collection(r.text)):
for item in split_collection(r.text): uid = item.uid if item.uid is not None else i
self._items[item.uid] = item self._items[uid] = item
for uid, item in self._items.items(): for uid, item in self._items.items():
yield uid, hashlib.sha256(item.raw) yield uid, hashlib.sha256(item.raw).hexdigest()
def get(self, href): def get(self, href):
x = self._items[href] x = self._items[href]
return x, hashlib.sha256(x.raw) return x, hashlib.sha256(x.raw).hexdigest()
def has(self, href): def has(self, href):
return href in self._items return href in self._items