Extract method loader()

This commit is contained in:
Unrud 2020-01-14 22:43:48 +01:00
parent dcca9bb6f3
commit 8543f3ea1d
5 changed files with 47 additions and 62 deletions

View File

@ -28,27 +28,14 @@ Take a look at the class ``BaseAuth`` if you want to implement your own.
"""
from importlib import import_module
from radicale.log import logger
from radicale import utils
INTERNAL_TYPES = ("none", "remote_user", "http_x_remote_user", "htpasswd")
def load(configuration):
"""Load the authentication manager chosen in configuration."""
auth_type = configuration.get("auth", "type")
if auth_type in INTERNAL_TYPES:
module = "radicale.auth.%s" % auth_type
else:
module = auth_type
try:
class_ = import_module(module).Auth
except Exception as e:
raise RuntimeError("Failed to load authentication module %r: %s" %
(module, e)) from e
logger.info("Authentication type is %r", auth_type)
return class_(configuration)
"""Load the authentication module chosen in configuration."""
return utils.loader(INTERNAL_TYPES, "auth", "Auth", configuration)
class BaseAuth:

View File

@ -30,27 +30,14 @@ Take a look at the class ``BaseRights`` if you want to implement your own.
"""
from importlib import import_module
from radicale.log import logger
from radicale import utils
INTERNAL_TYPES = ("authenticated", "owner_write", "owner_only", "from_file")
def load(configuration):
"""Load the rights manager chosen in configuration."""
rights_type = configuration.get("rights", "type")
if rights_type in INTERNAL_TYPES:
module = "radicale.rights.%s" % rights_type
else:
module = rights_type
try:
class_ = import_module(module).Rights
except Exception as e:
raise RuntimeError("Failed to load rights module %r: %s" %
(module, e)) from e
logger.info("Rights type is %r", rights_type)
return class_(configuration)
"""Load the rights module chosen in configuration."""
return utils.loader(INTERNAL_TYPES, "rights", "Rights", configuration)
def intersect_permissions(a, b="RrWw"):

View File

@ -26,13 +26,12 @@ Take a look at the class ``BaseCollection`` if you want to implement your own.
import contextlib
import json
from hashlib import md5
from importlib import import_module
import pkg_resources
import vobject
from radicale import utils
from radicale.item import filter as radicale_filter
from radicale.log import logger
INTERNAL_TYPES = ("multifilesystem",)
@ -42,19 +41,8 @@ CACHE_VERSION = (";".join(pkg_resources.get_distribution(pkg).version
def load(configuration):
"""Load the storage manager chosen in configuration."""
storage_type = configuration.get("storage", "type")
if storage_type in INTERNAL_TYPES:
module = "radicale.storage.%s" % storage_type
else:
module = storage_type
try:
class_ = import_module(module).Storage
except Exception as e:
raise RuntimeError("Failed to load storage module %r: %s" %
(module, e)) from e
logger.info("Storage type is %r", storage_type)
return class_(configuration)
"""Load the storage module chosen in configuration."""
return utils.loader(INTERNAL_TYPES, "storage", "Storage", configuration)
class ComponentExistsError(ValueError):

36
radicale/utils.py Normal file
View File

@ -0,0 +1,36 @@
# This file is part of Radicale Server - Calendar Server
# Copyright © 2014 Jean-Marc Martins
# Copyright © 2012-2017 Guillaume Ayoub
# Copyright © 2017-2018 Unrud <unrud@outlook.com>
#
# 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 <http://www.gnu.org/licenses/>.
from importlib import import_module
from radicale.log import logger
def loader(internal_types, module_name, class_name, configuration):
type_ = configuration.get(module_name, "type")
if type_ in internal_types:
module = "radicale.%s.%s" % (module_name, type_)
else:
module = type_
try:
class_ = getattr(import_module(module), class_name)
except Exception as e:
raise RuntimeError("Failed to load %s module %r: %s" %
(module_name, module, e)) from e
logger.info("%s type is %r", module_name, module)
return class_(configuration)

View File

@ -21,27 +21,14 @@ Take a look at the class ``BaseWeb`` if you want to implement your own.
"""
from importlib import import_module
from radicale.log import logger
from radicale import utils
INTERNAL_TYPES = ("none", "internal")
def load(configuration):
"""Load the web module chosen in configuration."""
web_type = configuration.get("web", "type")
if web_type in INTERNAL_TYPES:
module = "radicale.web.%s" % web_type
else:
module = web_type
try:
class_ = import_module(module).Web
except Exception as e:
raise RuntimeError("Failed to load web module %r: %s" %
(module, e)) from e
logger.info("Web type is %r", web_type)
return class_(configuration)
return utils.loader(INTERNAL_TYPES, "web", "Web", configuration)
class BaseWeb: