Changeset View
Standalone View
swh/deposit/config.py
# 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 | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import logging | |||||
from typing import Any, Dict, Tuple | from typing import Any, Dict | ||||
from swh.core.config import SWHConfig | from swh.core import config | ||||
from swh.scheduler import get_scheduler | from swh.scheduler import get_scheduler | ||||
from swh.scheduler.interface import SchedulerInterface | |||||
# IRIs (Internationalized Resource identifier) sword 2.0 specified | # IRIs (Internationalized Resource identifier) sword 2.0 specified | ||||
EDIT_SE_IRI = "edit_se_iri" | EDIT_SE_IRI = "edit_se_iri" | ||||
EM_IRI = "em_iri" | EM_IRI = "em_iri" | ||||
CONT_FILE_IRI = "cont_file_iri" | CONT_FILE_IRI = "cont_file_iri" | ||||
SD_IRI = "servicedocument" | SD_IRI = "servicedocument" | ||||
COL_IRI = "upload" | COL_IRI = "upload" | ||||
STATE_IRI = "state_iri" | STATE_IRI = "state_iri" | ||||
Show All 23 Lines | |||||
# Revision author for deposit | # Revision author for deposit | ||||
SWH_PERSON = { | SWH_PERSON = { | ||||
"name": "Software Heritage", | "name": "Software Heritage", | ||||
"fullname": "Software Heritage", | "fullname": "Software Heritage", | ||||
"email": "robot@softwareheritage.org", | "email": "robot@softwareheritage.org", | ||||
} | } | ||||
DEFAULT_CONFIG = { | |||||
"max_upload_size": 209715200, | |||||
"checks": True, | |||||
} | |||||
def setup_django_for(platform=None, config_file=None): | def setup_django_for(platform=None, config_file=None): | ||||
"""Setup function for command line tools (swh.deposit.create_user) to | """Setup function for command line tools (swh.deposit.create_user) to | ||||
initialize the needed db access. | initialize the needed db access. | ||||
Note: | Note: | ||||
Do not import any django related module prior to this function | Do not import any django related module prior to this function | ||||
call. Otherwise, this will raise an | call. Otherwise, this will raise an | ||||
django.core.exceptions.ImproperlyConfigured error message. | django.core.exceptions.ImproperlyConfigured error message. | ||||
Show All 16 Lines | def setup_django_for(platform=None, config_file=None): | ||||
if config_file: | if config_file: | ||||
os.environ.setdefault("SWH_CONFIG_FILENAME", config_file) | os.environ.setdefault("SWH_CONFIG_FILENAME", config_file) | ||||
import django | import django | ||||
django.setup() | django.setup() | ||||
class APIConfig(SWHConfig): | class APIConfig: | ||||
"""Mixin intended to enrich views with SWH configuration. | """API Configuration centralized class. This loads explicitly the configuration file out | ||||
of the SWH_CONFIG_FILENAME environment variable. | |||||
""" | """ | ||||
CONFIG_BASE_FILENAME = "deposit/server" | def __init__(self): | ||||
tenma: config is not optional be cause at least DEFAULT_CONFIG always exists | |||||
Done Inline Actionsyeah, but it is at that moment i declare it. ardumont: yeah, but it is at that moment i declare it.
If i remove it, mypy will complain. | |||||
Not Done Inline Actionsreally? There should be a way because every attribute would need optional that way, there must be a standard way tenma: really? There should be a way because every attribute would need optional that way, there must… | |||||
Not Done Inline Actionsdid you try to drop the null initializations? that should do the trick. You're not required to initialise when you declare, and it is not recommended for instance variable (vs. class variables) tenma: did you try to drop the null initializations? that should do the trick. You're not required to… | |||||
Done Inline ActionsThis sounds sensible, I'll do that ;) ardumont: This sounds sensible, I'll do that ;)
Thanks. | |||||
Done Inline Actionswell, something in the test must change as well then because that's not happy about it then: AttributeError: <class 'swh.deposit.config.APIConfig'> has no attribute 'config' i'll check further soon. ardumont: well, something in the test must change as well then because that's not happy about it then… | |||||
Not Done Inline Actionsno more super() things needed tenma: no more super() things needed | |||||
Done Inline Actionsyeah, i'll check. Not really sure because in the deposit, class also inherits from other classes. ardumont: yeah, i'll check.
Not really sure because in the deposit, class also inherits from other… | |||||
Not Done Inline ActionsI meant, for this class. tenma: I meant, for this class. | |||||
config_file = os.environ["SWH_CONFIG_FILENAME"] | |||||
DEFAULT_CONFIG = { | conf = config.read_raw_config(config.config_basepath(config_file)) | ||||
"max_upload_size": ("int", 209715200), | self.config: Dict[str, Any] = config.merge_configs(DEFAULT_CONFIG, conf) | ||||
"checks": ("bool", True), | self.scheduler: SchedulerInterface = get_scheduler(**self.config["scheduler"]) | ||||
Done Inline Actionsself.scheduler: SchedulerInterface... exactly why i want to stop that monkeypatch thing... ardumont: `self.scheduler: SchedulerInterface`...
exactly why i want to stop that monkeypatch thing...
| |||||
"scheduler": ( | |||||
"dict", | |||||
{"cls": "remote", "args": {"url": "http://localhost:5008/"}}, | |||||
), | |||||
} | |||||
ADDITIONAL_CONFIG = {} # type: Dict[str, Tuple[str, Any]] | |||||
def __init__(self, **config): | |||||
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 is not optional be cause at least DEFAULT_CONFIG always exists