Coverage for nlp_webserver/settings.py: 88%
17 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1r"""
2crate_anon/nlp_webserver/settings.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26Settings for CRATE's implementation of an NLPRP server.
28"""
30import os
31from typing import Any, Dict, Optional
33from pyramid.paster import get_appsettings
34from pyramid.config import Configurator
36from crate_anon.common.constants import EnvVar
37from crate_anon.nlp_webserver.constants import (
38 NLP_WEBSERVER_CONFIG_ENVVAR,
39 NlpServerConfigKeys,
40)
42SETTINGS_PATH = os.getenv(NLP_WEBSERVER_CONFIG_ENVVAR)
44_DOCGEN_DUMMY_SETTINGS = {
45 v: ""
46 for k, v in NlpServerConfigKeys.__dict__.items()
47 if not k.startswith("_") and k != "SQLALCHEMY_PREFIX"
48}
49_DOCGEN_DUMMY_SETTINGS[NlpServerConfigKeys.SQLALCHEMY_URL] = "sqlite://"
50_DOCGEN_DUMMY_SETTINGS[NlpServerConfigKeys.SQLALCHEMY_ECHO] = "false"
51_DOCGEN_DUMMY_SETTINGS[NlpServerConfigKeys.ENCRYPTION_KEY] = (
52 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa="
53)
55if EnvVar.GENERATING_CRATE_DOCS in os.environ:
56 # Prevent errors whilst building docs, using dummy settings.
57 SETTINGS = _DOCGEN_DUMMY_SETTINGS
58 CONFIG = None # type: Optional[Configurator]
59else:
60 # Real settings.
61 assert (
62 SETTINGS_PATH
63 ), f"Missing environment variable {NLP_WEBSERVER_CONFIG_ENVVAR}"
64 SETTINGS = get_appsettings(SETTINGS_PATH) # type: Dict[str, Any]
65 CONFIG = Configurator(settings=SETTINGS)