From 3b0328ca1ecdb0c0d35c4c4d7b1d54c1a4b1d83d Mon Sep 17 00:00:00 2001 From: Sergey Fursov Date: Sat, 28 Dec 2013 14:15:35 +0400 Subject: [PATCH] Allow attach custom storage backend --- config | 6 +++++- radicale/config.py | 1 + radicale/storage/__init__.py | 13 +++++++++---- tests/__init__.py | 19 +++++++++++++++++++ tests/custom/storage.py | 30 ++++++++++++++++++++++++++++++ tests/test_base.py | 3 ++- 6 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 tests/custom/storage.py diff --git a/config b/config index 621093e..411d49b 100644 --- a/config +++ b/config @@ -104,6 +104,7 @@ committer = Firstname Lastname [rights] # Rights backend +# Value: regex backend = "regex" # Rights management method @@ -116,9 +117,12 @@ file = ~/.config/radicale/rights [storage] # Storage backend -# Value: filesystem | multifilesystem | database +# Value: filesystem | multifilesystem | database | custom type = filesystem +# Custom storage handler +custom_handler = + # Folder for storing local collections, created if not present filesystem_folder = ~/.config/radicale/collections diff --git a/radicale/config.py b/radicale/config.py index b905c60..53a3346 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -81,6 +81,7 @@ INITIAL_CONFIG = { "file": "~/.config/radicale/rights"}, "storage": { "type": "filesystem", + "custom_handler": "", "filesystem_folder": os.path.expanduser( "~/.config/radicale/collections"), "database_url": ""}, diff --git a/radicale/storage/__init__.py b/radicale/storage/__init__.py index ca9dad3..ab698f4 100644 --- a/radicale/storage/__init__.py +++ b/radicale/storage/__init__.py @@ -23,15 +23,20 @@ This module loads the storage backend, according to the storage configuration. """ - +import sys from .. import config, ical def load(): """Load list of available storage managers.""" storage_type = config.get("storage", "type") - root_module = __import__( - "storage.%s" % storage_type, globals=globals(), level=2) - module = getattr(root_module, storage_type) + if storage_type == "custom": + storage_module = config.get("storage", "custom_handler") + __import__(storage_module) + module = sys.modules[storage_module] + else: + root_module = __import__( + "storage.%s" % storage_type, globals=globals(), level=2) + module = getattr(root_module, storage_type) ical.Collection = module.Collection return module diff --git a/tests/__init__.py b/tests/__init__.py index 3878cfe..1621bae 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -123,6 +123,25 @@ class GitMultiFileSystem(GitFileSystem, MultiFileSystem): """Base class for multifilesystem tests using Git""" +class CustomStorageSystem(BaseTest): + """Base class for custom backend tests.""" + storage_type = "custom" + + def setup(self): + """Setup function for each test.""" + self.colpath = tempfile.mkdtemp() + config.set("storage", "type", self.storage_type) + config.set("storage", "custom_handler", "tests.custom.storage") + from tests.custom import storage + storage.FOLDER = self.colpath + storage.GIT_REPOSITORY = None + self.application = radicale.Application() + + def teardown(self): + """Teardown function for each test.""" + shutil.rmtree(self.colpath) + + class AuthSystem(BaseTest): """Base class to test Radicale with Htpasswd authentication""" def setup(self): diff --git a/tests/custom/storage.py b/tests/custom/storage.py new file mode 100644 index 0000000..bc12356 --- /dev/null +++ b/tests/custom/storage.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Radicale Server - Calendar Server +# Copyright © 2012-2013 Guillaume Ayoub +# +# This library is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Radicale. If not, see . + +""" +Custom storage backend. + +Copy of filesystem storage backend for testing + +""" + +from radicale.storage import filesystem + + +class Collection(filesystem.Collection): + """Collection stored in a flat ical file.""" diff --git a/tests/test_base.py b/tests/test_base.py index 071c44d..d2c030c 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -25,6 +25,7 @@ from . import (FileSystem, MultiFileSystem, DataBaseSystem, GitFileSystem, GitMultiFileSystem) from .helpers import get_file_content import sys +from tests import CustomStorageSystem class BaseRequests(object): @@ -83,7 +84,7 @@ class BaseRequests(object): # Generate classes with different configs cl_list = [FileSystem, MultiFileSystem, DataBaseSystem, - GitFileSystem, GitMultiFileSystem] + GitFileSystem, GitMultiFileSystem, CustomStorageSystem] for cl in cl_list: classname = "Test%s" % cl.__name__ setattr(sys.modules[__name__],