diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import hashlib +import logging from typing import Sequence, Type @@ -58,6 +59,9 @@ ACCEPT_ARCHIVE_CONTENT_TYPES = ["application/zip", "application/x-tar"] +logger = logging.getLogger(__name__) + + class AuthenticatedAPIView(APIView): """Mixin intended as a based API view to enforce the basic authentication check diff --git a/swh/deposit/config.py b/swh/deposit/config.py --- a/swh/deposit/config.py +++ b/swh/deposit/config.py @@ -1,15 +1,16 @@ -# Copyright (C) 2017-2018 The Software Heritage developers +# Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os -import logging -from typing import Any, Dict, Tuple +from typing import Any, Dict, Optional -from swh.core.config import SWHConfig +from swh.core import config from swh.scheduler import get_scheduler +from swh.scheduler.interface import SchedulerInterface + # IRIs (Internationalized Resource identifier) sword 2.0 specified EDIT_SE_IRI = "edit_se_iri" @@ -49,6 +50,13 @@ } +DEFAULT_CONFIG = { + "max_upload_size": 209715200, + "checks": True, + "scheduler": {"cls": "remote", "args": {"url": "http://localhost:5008/"}}, +} + + def setup_django_for(platform=None, config_file=None): """Setup function for command line tools (swh.deposit.create_user) to initialize the needed db access. @@ -81,30 +89,20 @@ django.setup() -class APIConfig(SWHConfig): - """Mixin intended to enrich views with SWH configuration. +class APIConfig: + """API Configuration centralized class - """ - - CONFIG_BASE_FILENAME = "deposit/server" + This loads explicitely the configuration file out of the SWH_CONFIG_FILENAME + environment variable. - DEFAULT_CONFIG = { - "max_upload_size": ("int", 209715200), - "checks": ("bool", True), - "scheduler": ( - "dict", - {"cls": "remote", "args": {"url": "http://localhost:5008/"}}, - ), - } + """ - ADDITIONAL_CONFIG = {} # type: Dict[str, Tuple[str, Any]] + config: Optional[Dict[str, Any]] = None + scheduler: Optional[SchedulerInterface] = None - def __init__(self, **config): + def __init__(self): super().__init__() - self.config = self.parse_config_file( - additional_configs=[self.ADDITIONAL_CONFIG] - ) - self.config.update(config) - self.log = logging.getLogger("swh.deposit") - if self.config.get("scheduler"): - self.scheduler = get_scheduler(**self.config["scheduler"]) + config_file = os.environ["SWH_CONFIG_FILENAME"] + conf = config.read_raw_config(config.config_basepath(config_file)) + self.config = config.merge_configs(DEFAULT_CONFIG, conf) + self.scheduler = get_scheduler(**self.config["scheduler"]) diff --git a/swh/deposit/tests/conftest.py b/swh/deposit/tests/conftest.py --- a/swh/deposit/tests/conftest.py +++ b/swh/deposit/tests/conftest.py @@ -78,15 +78,12 @@ def deposit_autoconfig(monkeypatch, deposit_config, swh_scheduler_config): """Enforce config for deposit classes inherited from APIConfig.""" - def mock_parse_config(*args, **kw): - config = deposit_config.copy() - config["scheduler"] = { - "cls": "local", - "args": swh_scheduler_config, - } - return config - - monkeypatch.setattr(APIConfig, "parse_config_file", mock_parse_config) + # add the scheduler to configuration + config = deposit_config.copy() + config["scheduler"] = { + "cls": "local", + "args": swh_scheduler_config, + } scheduler = get_scheduler("local", swh_scheduler_config) task_type = { @@ -96,6 +93,13 @@ } scheduler.create_task_type(task_type) + def noop(self): + pass + + monkeypatch.setattr(APIConfig, "__init__", noop) + monkeypatch.setattr(APIConfig, "config", config) + monkeypatch.setattr(APIConfig, "scheduler", scheduler) + @pytest.fixture(scope="session") def django_db_setup(request, django_db_blocker, postgresql_proc):