From f4cdde9a527199b6994e070c291cc8cc974040b8 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 4 Apr 2014 13:20:18 +0200 Subject: [PATCH] Ability to handle missing UIDs. --- tests/storage/test_http.py | 63 ++++++++------------------------------ vdirsyncer/storage/http.py | 24 +++++++++------ 2 files changed, 28 insertions(+), 59 deletions(-) diff --git a/tests/storage/test_http.py b/tests/storage/test_http.py index 9f72949..12abb2e 100644 --- a/tests/storage/test_http.py +++ b/tests/storage/test_http.py @@ -18,25 +18,15 @@ class TestHttpStorage(object): def test_list(self, monkeypatch): collection_url = 'http://127.0.0.1/calendar/collection/' - responses = [ + items = [ dedent(b''' - BEGIN:VCALENDAR - VERSION:2.0 - PRODID:http://www.example.com/calendarapplication/ - METHOD:PUBLISH 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(), + dedent(b''' BEGIN:VEVENT - UID:461092315asdasd540@example.com - LOCATION:Somewhere else SUMMARY:Eine zweite Kurzinfo DESCRIPTION:Beschreibung des anderen Termines BEGIN:VALARM @@ -47,8 +37,11 @@ class TestHttpStorage(object): DURATION:PT1H END:VALARM END:VEVENT - END:VCALENDAR - ''') + ''').strip() + ] + + responses = [ + '\n'.join([b'BEGIN:VCALENDAR'] + items + [b'END:VCALENDAR']) ] def get(*a, **kw): @@ -61,40 +54,10 @@ class TestHttpStorage(object): monkeypatch.setattr('requests.get', get) s = HttpStorage(url=collection_url) - l = list(s.list()) - hrefs = set(href for href, etag in l) - href1 = u'461092315540@example.com' - href2 = u'461092315asdasd540@example.com' - assert hrefs == set((href1, href2)) + for href, etag in s.list(): + item, etag2 = s.get(href) + assert etag2 == etag + items.remove(item.raw.strip()) - item, etag = s.get(href1) - 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())) + assert not items diff --git a/vdirsyncer/storage/http.py b/vdirsyncer/storage/http.py index 97eb7da..dd36634 100644 --- a/vdirsyncer/storage/http.py +++ b/vdirsyncer/storage/http.py @@ -65,7 +65,6 @@ def prepare_verify(verify): class HttpStorageBase(Storage): _repr_attributes = ('username', 'url') - _items = None def __init__(self, url, username='', password='', collection=None, verify=True, auth=None, useragent='vdirsyncer', **kwargs): @@ -106,20 +105,27 @@ class HttpStorageBase(Storage): class HttpStorage(HttpStorageBase): + + _items = None + + def __init__(self, **kwargs): + super(HttpStorage, self).__init__(**kwargs) + self._items = {} + def list(self): - if self._items is None: - r = requests.get(self.url, **self._settings) - r.raise_for_status() - self._items = {} - for item in split_collection(r.text): - self._items[item.uid] = item + r = requests.get(self.url, **self._settings) + r.raise_for_status() + self._items.clear() + for i, item in enumerate(split_collection(r.text)): + uid = item.uid if item.uid is not None else i + self._items[uid] = item for uid, item in self._items.items(): - yield uid, hashlib.sha256(item.raw) + yield uid, hashlib.sha256(item.raw).hexdigest() def get(self, href): x = self._items[href] - return x, hashlib.sha256(x.raw) + return x, hashlib.sha256(x.raw).hexdigest() def has(self, href): return href in self._items