mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Add memory storage
This commit is contained in:
parent
34e2c8e9dd
commit
d3c527618f
4 changed files with 49 additions and 9 deletions
|
|
@ -1,6 +1,9 @@
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NotFoundError(Error):
|
||||||
|
pass
|
||||||
|
|
||||||
class AlreadyExistingError(Error):
|
class AlreadyExistingError(Error):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,9 @@ class Storage(object):
|
||||||
|
|
||||||
def update(self, obj, etag):
|
def update(self, obj, etag):
|
||||||
'''
|
'''
|
||||||
Update the object, raise error if the etag on the server doesn't match
|
Update the object, raise :exc:`vdirsyncer.exceptions.WrongEtagError` if
|
||||||
the given etag.
|
the etag on the server doesn't match the given etag, raise
|
||||||
|
:exc:`vdirsyncer.exceptions.NotFoundError` if the item doesn't exist.
|
||||||
|
|
||||||
:returns: etag on the server
|
:returns: etag on the server
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
from vdirsyncer.storage.base import Storage, Item
|
from vdirsyncer.storage.base import Storage, Item
|
||||||
from vdirsyncer.exceptions import AlreadyExistingError
|
import vdirsyncer.exceptions as exceptions
|
||||||
|
|
||||||
class FilesystemStorage(Storage):
|
class FilesystemStorage(Storage):
|
||||||
def __init__(self, path, **kwargs):
|
def __init__(self, path, **kwargs):
|
||||||
|
|
@ -10,6 +10,9 @@ class FilesystemStorage(Storage):
|
||||||
def _get_etag(self, href):
|
def _get_etag(self, href):
|
||||||
return os.path.getmtime(href)
|
return os.path.getmtime(href)
|
||||||
|
|
||||||
|
def _get_href(self, obj):
|
||||||
|
return os.path.join(self.path, obj.uid + b'b' + self.fileext)
|
||||||
|
|
||||||
def _get_hrefs(self):
|
def _get_hrefs(self):
|
||||||
for fname in os.listdir(self.path):
|
for fname in os.listdir(self.path):
|
||||||
href = os.path.join(self.path, fname)
|
href = os.path.join(self.path, fname)
|
||||||
|
|
@ -28,13 +31,10 @@ class FilesystemStorage(Storage):
|
||||||
def item_exists(self, href):
|
def item_exists(self, href):
|
||||||
return os.path.isfile(path)
|
return os.path.isfile(path)
|
||||||
|
|
||||||
def _get_href(self, obj):
|
|
||||||
return os.path.join(self.path, obj.uid + b'b' + self.fileext)
|
|
||||||
|
|
||||||
def upload(self, obj):
|
def upload(self, obj):
|
||||||
href = self._get_href(obj)
|
href = self._get_href(obj)
|
||||||
if os.path.exists(href):
|
if os.path.exists(href):
|
||||||
raise AlreadyExistingError(href)
|
raise exceptions.AlreadyExistingError(href)
|
||||||
with open(href, 'wb+') as f:
|
with open(href, 'wb+') as f:
|
||||||
f.write(obj.raw)
|
f.write(obj.raw)
|
||||||
return href, self._get_etag(href)
|
return href, self._get_etag(href)
|
||||||
|
|
@ -43,8 +43,10 @@ class FilesystemStorage(Storage):
|
||||||
href = self._get_href(obj)
|
href = self._get_href(obj)
|
||||||
actual_etag = self._get_etag(href)
|
actual_etag = self._get_etag(href)
|
||||||
if etag != actual_etag:
|
if etag != actual_etag:
|
||||||
raise WrongEtagError(etag, actual_etag)
|
raise exceptions.WrongEtagError(etag, actual_etag)
|
||||||
|
if not os.path.exists(href):
|
||||||
|
raise exceptions.NotFoundError(href)
|
||||||
with open(href, 'wb') as f:
|
with open(href, 'wb') as f:
|
||||||
f.write(obj.raw)
|
f.write(obj.raw)
|
||||||
|
|
||||||
return self._get_etag(href)
|
return self._get_etag(href)
|
||||||
|
|
|
||||||
34
vdirsyncer/storage/memory.py
Normal file
34
vdirsyncer/storage/memory.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import datetime
|
||||||
|
from vdirsyncer.storage.base import Item, Storage
|
||||||
|
import vdirsyncer.exceptions as exceptions
|
||||||
|
|
||||||
|
class MemoryStorage(Storage):
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
self.items = {} # href => (etag, object)
|
||||||
|
super(MemoryStorage, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
def list_items(self):
|
||||||
|
for href, (etag, obj) in self.items.items():
|
||||||
|
yield href, etag
|
||||||
|
|
||||||
|
def get_items(self, hrefs):
|
||||||
|
for href in hrefs:
|
||||||
|
etag, obj = self.items[href]
|
||||||
|
return obj, href, etag
|
||||||
|
|
||||||
|
def item_exists(self, href):
|
||||||
|
return href in self.items
|
||||||
|
|
||||||
|
def upload(self, obj):
|
||||||
|
if obj.uid in self.items:
|
||||||
|
raise exceptions.AlreadyExistingError(obj)
|
||||||
|
etag = datetime.datetime.now()
|
||||||
|
self.items[obj.uid] = (etag, obj)
|
||||||
|
return obj.uid, etag
|
||||||
|
|
||||||
|
def update(self, obj, etag):
|
||||||
|
if obj.uid not in self.items:
|
||||||
|
raise exceptions.NotFoundError(obj)
|
||||||
|
etag = datetime.datetime.now()
|
||||||
|
self.items[obj.uid] = (etag, obj)
|
||||||
|
return obj.uid, etag
|
||||||
Loading…
Reference in a new issue