From 8543f3ea1da119fde8ef6e2f8bb5d17be97a749b Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 14 Jan 2020 22:43:48 +0100 Subject: [PATCH] Extract method loader() --- radicale/auth/__init__.py | 19 +++---------------- radicale/rights/__init__.py | 19 +++---------------- radicale/storage/__init__.py | 18 +++--------------- radicale/utils.py | 36 ++++++++++++++++++++++++++++++++++++ radicale/web/__init__.py | 17 ++--------------- 5 files changed, 47 insertions(+), 62 deletions(-) create mode 100644 radicale/utils.py diff --git a/radicale/auth/__init__.py b/radicale/auth/__init__.py index 6b2bbcc..415c91b 100644 --- a/radicale/auth/__init__.py +++ b/radicale/auth/__init__.py @@ -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: diff --git a/radicale/rights/__init__.py b/radicale/rights/__init__.py index 05d078b..f775261 100644 --- a/radicale/rights/__init__.py +++ b/radicale/rights/__init__.py @@ -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"): diff --git a/radicale/storage/__init__.py b/radicale/storage/__init__.py index 1d703d2..cea629a 100644 --- a/radicale/storage/__init__.py +++ b/radicale/storage/__init__.py @@ -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): diff --git a/radicale/utils.py b/radicale/utils.py new file mode 100644 index 0000000..cb63f5e --- /dev/null +++ b/radicale/utils.py @@ -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 +# +# 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 . + +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) diff --git a/radicale/web/__init__.py b/radicale/web/__init__.py index f1cd214..dda2260 100644 --- a/radicale/web/__init__.py +++ b/radicale/web/__init__.py @@ -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: