Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/config.py
# Copyright (C) 2017-2022 The Software Heritage developers | # Copyright (C) 2017-2022 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 | ||||
from typing import Any, Dict | from typing import Any, Dict, Optional | ||||
from swh.core import config | from swh.core import config | ||||
from swh.deposit import __version__ | from swh.deposit import __version__ | ||||
from swh.model.model import MetadataAuthority, MetadataAuthorityType, MetadataFetcher | from swh.model.model import MetadataAuthority, MetadataAuthorityType, MetadataFetcher | ||||
from swh.scheduler import get_scheduler | from swh.scheduler import get_scheduler | ||||
from swh.scheduler.interface import SchedulerInterface | from swh.scheduler.interface import SchedulerInterface | ||||
from swh.storage import get_storage | from swh.storage import get_storage | ||||
from swh.storage.interface import StorageInterface | from swh.storage.interface import StorageInterface | ||||
Show All 37 Lines | |||||
DEFAULT_CONFIG = { | DEFAULT_CONFIG = { | ||||
"max_upload_size": 209715200, | "max_upload_size": 209715200, | ||||
"checks": True, | "checks": True, | ||||
} | } | ||||
def setup_django_for(platform=None, config_file=None): | def setup_django_for(platform: Optional[str] = None, config_file: Optional[str] = None): | ||||
"""Setup function for command line tools (swh.deposit.create_user) to | """Setup function for command line tools (e.g. 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 a django.core.exceptions.ImproperlyConfigured | ||||
django.core.exceptions.ImproperlyConfigured error message. | error message. | ||||
Args: | Args: | ||||
platform (str): the platform the scheduling is running | platform: the platform the cli is running for | ||||
config_file (str): Extra configuration file (typically for the | config_file: Extra configuration file (typically for the production platform) | ||||
production platform) | |||||
Raises: | Raises: | ||||
ValueError in case of wrong platform inputs. | ValueError in case of wrong platform inputs | ||||
ValueError if the SWH_CONFIG_FILENAME environment variable is not set. | |||||
""" | """ | ||||
if platform is not None: | if platform is not None: | ||||
if platform not in AUTHORIZED_PLATFORMS: | if platform not in AUTHORIZED_PLATFORMS: | ||||
raise ValueError("Platform should be one of %s" % AUTHORIZED_PLATFORMS) | raise ValueError(f"Platform should be one of {AUTHORIZED_PLATFORMS}") | ||||
if "DJANGO_SETTINGS_MODULE" not in os.environ: | if "DJANGO_SETTINGS_MODULE" not in os.environ: | ||||
os.environ["DJANGO_SETTINGS_MODULE"] = "swh.deposit.settings.%s" % platform | os.environ["DJANGO_SETTINGS_MODULE"] = f"swh.deposit.settings.{platform}" | ||||
if config_file: | if config_file: | ||||
os.environ.setdefault("SWH_CONFIG_FILENAME", config_file) | os.environ.setdefault("SWH_CONFIG_FILENAME", config_file) | ||||
import django | from django import setup | ||||
django.setup() | setup() | ||||
class APIConfig: | class APIConfig: | ||||
"""API Configuration centralized class. This loads explicitly the configuration file out | """API Configuration centralized class. This loads explicitly the configuration file out | ||||
of the SWH_CONFIG_FILENAME environment variable. | of the SWH_CONFIG_FILENAME environment variable. | ||||
""" | """ | ||||
Show All 24 Lines |