Skip to content

config

CONFIG: ServerConfig = ServerConfig() module-attribute

This singleton loads the config from a hierarchy of sources (see customise_sources) and makes it importable in the server code.

DEFAULT_CONFIG_FILE_PATH: str = str(Path.home().joinpath('.optimade.json')) module-attribute

Default configuration file path.

This variable is used as the fallback value if the environment variable OPTIMADE_CONFIG_FILE is not set.

Note

It is set to: pathlib.Path.home()/.optimade.json

For Unix-based systems (Linux) this will be equivalent to ~/.optimade.json.

ConfigFileSettingsSource

Bases: PydanticBaseSettingsSource

Configuration file settings source.

Based on the example in the pydantic documentation, this class defines loading ServerConfig settings from a configuration file.

The file must be of either type JSON or YML/YAML.

Source code in optimade/server/config.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class ConfigFileSettingsSource(PydanticBaseSettingsSource):
    """Configuration file settings source.

    Based on the example in the
    [pydantic documentation](https://docs.pydantic.dev/latest/concepts/pydantic_settings/#customise-settings-sources),
    this class defines loading ServerConfig settings from a configuration file.

    The file must be of either type JSON or YML/YAML.
    """

    def get_field_value(
        self, field: FieldInfo, field_name: str
    ) -> tuple[Any, str, bool]:
        """Must be defined according to parent abstract class.

        It does not make sense to use it for this class, since 'extra' is set to
        'allow'. We will instead just parse and take every key/field specified in the
        config file.
        """
        raise NotImplementedError

    def parse_config_file(self) -> dict[str, Any]:
        """Parse the config file and return a dictionary of its content."""
        encoding = self.config.get("env_file_encoding")
        config_file = Path(os.getenv("OPTIMADE_CONFIG_FILE", DEFAULT_CONFIG_FILE_PATH))

        parsed_config_file = {}
        if config_file.is_file():
            config_file_content = config_file.read_text(encoding=encoding)

            try:
                parsed_config_file = json.loads(config_file_content)
            except json.JSONDecodeError as json_exc:
                try:
                    # This can essentially also load JSON files, as JSON is a subset of
                    # YAML v1, but I suspect it is not as rigorous
                    parsed_config_file = yaml.safe_load(config_file_content)
                except yaml.YAMLError as yaml_exc:
                    warnings.warn(
                        f"Unable to parse config file {config_file} as JSON or "
                        "YAML, using the default settings instead..\n"
                        f"Errors:\n  JSON:\n{json_exc}.\n\n  YAML:\n{yaml_exc}"
                    )
        else:
            warnings.warn(
                f"Unable to find config file at {config_file}, using the default "
                "settings instead."
            )

        if parsed_config_file is None:
            # This can happen if the yaml loading doesn't succeed properly, e.g., if
            # the file is empty.
            warnings.warn(
                f"Unable to load any settings from {config_file}, using the default "
                "settings instead."
            )
            parsed_config_file = {}

        if not isinstance(parsed_config_file, dict):
            warnings.warn(
                f"Unable to parse config file {config_file} as a dictionary, using "
                "the default settings instead."
            )
            parsed_config_file = {}

        return parsed_config_file

    def __call__(self) -> dict[str, Any]:
        return self.parse_config_file()

__call__()

Source code in optimade/server/config.py
139
140
def __call__(self) -> dict[str, Any]:
    return self.parse_config_file()

get_field_value(field, field_name)

Must be defined according to parent abstract class.

It does not make sense to use it for this class, since 'extra' is set to 'allow'. We will instead just parse and take every key/field specified in the config file.

Source code in optimade/server/config.py
82
83
84
85
86
87
88
89
90
91
def get_field_value(
    self, field: FieldInfo, field_name: str
) -> tuple[Any, str, bool]:
    """Must be defined according to parent abstract class.

    It does not make sense to use it for this class, since 'extra' is set to
    'allow'. We will instead just parse and take every key/field specified in the
    config file.
    """
    raise NotImplementedError

parse_config_file()

Parse the config file and return a dictionary of its content.

Source code in optimade/server/config.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def parse_config_file(self) -> dict[str, Any]:
    """Parse the config file and return a dictionary of its content."""
    encoding = self.config.get("env_file_encoding")
    config_file = Path(os.getenv("OPTIMADE_CONFIG_FILE", DEFAULT_CONFIG_FILE_PATH))

    parsed_config_file = {}
    if config_file.is_file():
        config_file_content = config_file.read_text(encoding=encoding)

        try:
            parsed_config_file = json.loads(config_file_content)
        except json.JSONDecodeError as json_exc:
            try:
                # This can essentially also load JSON files, as JSON is a subset of
                # YAML v1, but I suspect it is not as rigorous
                parsed_config_file = yaml.safe_load(config_file_content)
            except yaml.YAMLError as yaml_exc:
                warnings.warn(
                    f"Unable to parse config file {config_file} as JSON or "
                    "YAML, using the default settings instead..\n"
                    f"Errors:\n  JSON:\n{json_exc}.\n\n  YAML:\n{yaml_exc}"
                )
    else:
        warnings.warn(
            f"Unable to find config file at {config_file}, using the default "
            "settings instead."
        )

    if parsed_config_file is None:
        # This can happen if the yaml loading doesn't succeed properly, e.g., if
        # the file is empty.
        warnings.warn(
            f"Unable to load any settings from {config_file}, using the default "
            "settings instead."
        )
        parsed_config_file = {}

    if not isinstance(parsed_config_file, dict):
        warnings.warn(
            f"Unable to parse config file {config_file} as a dictionary, using "
            "the default settings instead."
        )
        parsed_config_file = {}

    return parsed_config_file

LogLevel

Bases: Enum

Replication of logging LogLevels

  • notset
  • debug
  • info
  • warning
  • error
  • critical
Source code in optimade/server/config.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class LogLevel(Enum):
    """Replication of logging LogLevels

    - `notset`
    - `debug`
    - `info`
    - `warning`
    - `error`
    - `critical`

    """

    NOTSET = "notset"
    DEBUG = "debug"
    INFO = "info"
    WARNING = "warning"
    ERROR = "error"
    CRITICAL = "critical"

CRITICAL = 'critical' class-attribute instance-attribute

DEBUG = 'debug' class-attribute instance-attribute

ERROR = 'error' class-attribute instance-attribute

INFO = 'info' class-attribute instance-attribute

NOTSET = 'notset' class-attribute instance-attribute

WARNING = 'warning' class-attribute instance-attribute

ServerConfig

Bases: BaseSettings

This class stores server config parameters in a way that can be easily extended for new config file types.

Source code in optimade/server/config.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
class ServerConfig(BaseSettings):
    """This class stores server config parameters in a way that
    can be easily extended for new config file types.
    """

    model_config = SettingsConfigDict(
        env_prefix="optimade_",
        extra="allow",
        env_file_encoding="utf-8",
        case_sensitive=False,
    )

    debug: Annotated[
        bool,
        Field(
            description="Turns on Debug Mode for the OPTIMADE Server implementation",
        ),
    ] = False

    insert_test_data: Annotated[
        bool,
        Field(
            description=(
                "Insert test data into each collection on server initialisation. If true, "
                "the configured backend will be populated with test data on server start. "
                "Should be disabled for production usage."
            ),
        ),
    ] = True

    use_real_mongo: Annotated[
        Optional[bool],
        Field(description="DEPRECATED: force usage of MongoDB over any other backend."),
    ] = None

    database_backend: Annotated[
        SupportedBackend,
        Field(
            description="Which database backend to use out of the supported backends.",
        ),
    ] = SupportedBackend.MONGOMOCK

    elastic_hosts: Annotated[
        Optional[Union[str, list[str], dict[str, Any], list[dict[str, Any]]]],
        Field(
            description="Host settings to pass through to the `Elasticsearch` class."
        ),
    ] = None

    mongo_count_timeout: Annotated[
        int,
        Field(
            description=(
                "Number of seconds to allow MongoDB to perform a full database count "
                "before falling back to `null`. This operation can require a full COLLSCAN"
                " for empty queries which can be prohibitively slow if the database does "
                "not fit into the active set, hence a timeout can drastically speed-up "
                "response times."
            ),
        ),
    ] = 5

    mongo_database: Annotated[
        str, Field(description="Mongo database for collection data")
    ] = "optimade"
    mongo_uri: Annotated[str, Field(description="URI for the Mongo server")] = (
        "localhost:27017"
    )
    links_collection: Annotated[
        str, Field(description="Mongo collection name for /links endpoint resources")
    ] = "links"
    references_collection: Annotated[
        str,
        Field(
            description="Mongo collection name for /references endpoint resources",
        ),
    ] = "references"
    structures_collection: Annotated[
        str,
        Field(
            description="Mongo collection name for /structures endpoint resources",
        ),
    ] = "structures"
    page_limit: Annotated[
        int, Field(description="Default number of resources per page")
    ] = 20
    page_limit_max: Annotated[
        int, Field(description="Max allowed number of resources per page")
    ] = 500
    default_db: Annotated[
        str,
        Field(
            description=(
                "ID of /links endpoint resource for the chosen default OPTIMADE "
                "implementation (only relevant for the index meta-database)"
            ),
        ),
    ] = "test_server"
    root_path: Annotated[
        Optional[str],
        Field(
            description=(
                "Sets the FastAPI app `root_path` parameter. This can be used to serve the"
                " API under a path prefix behind a proxy or as a sub-application of "
                "another FastAPI app. See "
                "https://fastapi.tiangolo.com/advanced/sub-applications/#technical-details-root_path"
                " for details."
            ),
        ),
    ] = None
    base_url: Annotated[
        Optional[str], Field(description="Base URL for this implementation")
    ] = None
    implementation: Annotated[
        Implementation,
        Field(
            description="Introspective information about this OPTIMADE implementation",
        ),
    ] = Implementation(
        name="OPTIMADE Python Tools",
        version=__version__,
        source_url="https://github.com/Materials-Consortia/optimade-python-tools",
        maintainer={"email": "dev@optimade.org"},
        issue_tracker=(
            "https://github.com/Materials-Consortia/optimade-python-tools/issues"
        ),
        homepage="https://optimade.org/optimade-python-tools",
    )
    index_base_url: Annotated[
        Optional[AnyHttpUrl],
        Field(
            description=(
                "An optional link to the base URL for the index meta-database of the "
                "provider."
            ),
        ),
    ] = None
    provider: Annotated[
        Provider,
        Field(
            description=(
                "General information about the provider of this OPTIMADE implementation"
            ),
        ),
    ] = Provider(
        prefix="exmpl",
        name="Example provider",
        description=(
            "Provider used for examples, not to be assigned to a real database"
        ),
        homepage="https://example.com",
    )
    provider_fields: Annotated[
        dict[
            Literal["links", "references", "structures"],
            list[Union[str, dict[Literal["name", "type", "unit", "description"], str]]],
        ],
        Field(
            description=(
                "A list of additional fields to be served with the provider's prefix "
                "attached, broken down by endpoint."
            ),
        ),
    ] = {}
    aliases: Annotated[
        dict[Literal["links", "references", "structures"], dict[str, str]],
        Field(
            description=(
                "A mapping between field names in the database with their corresponding "
                "OPTIMADE field names, broken down by endpoint."
            ),
        ),
    ] = {}
    length_aliases: Annotated[
        dict[Literal["links", "references", "structures"], dict[str, str]],
        Field(
            description=(
                "A mapping between a list property (or otherwise) and an integer property "
                "that defines the length of that list, for example elements -> nelements. "
                "The standard aliases are applied first, so this dictionary must refer to "
                "the API fields, not the database fields."
            ),
        ),
    ] = {}
    index_links_path: Annotated[
        Path,
        Field(
            description=(
                "Absolute path to a JSON file containing the MongoDB collecton of links "
                "entries (documents) to serve under the /links endpoint of the index "
                "meta-database. NB! As suggested in the previous sentence, these will "
                "only be served when using a MongoDB-based backend."
            ),
        ),
    ] = Path(__file__).parent.joinpath("index_links.json")

    is_index: Annotated[
        Optional[bool],
        Field(
            description=(
                "A runtime setting to dynamically switch between index meta-database and "
                "normal OPTIMADE servers. Used for switching behaviour of e.g., "
                "`meta->optimade_schema` values in the response. Any provided value may "
                "be overridden when used with the reference server implementation."
            ),
        ),
    ] = False

    schema_url: Annotated[
        Optional[Union[str, AnyHttpUrl]],
        Field(
            description=(
                "A URL that will be provided in the `meta->schema` field for every response"
            ),
        ),
    ] = f"https://schemas.optimade.org/openapi/v{__api_version__}/optimade.json"

    custom_landing_page: Annotated[
        Optional[Union[str, Path]],
        Field(
            description=(
                "The location of a custom landing page (Jinja template) to use for the API."
            ),
        ),
    ] = None

    index_schema_url: Annotated[
        Optional[Union[str, AnyHttpUrl]],
        Field(
            description=(
                "A URL that will be provided in the `meta->schema` field for every "
                "response from the index meta-database."
            ),
        ),
    ] = f"https://schemas.optimade.org/openapi/v{__api_version__}/optimade_index.json"

    log_level: Annotated[
        LogLevel, Field(description="Logging level for the OPTIMADE server.")
    ] = LogLevel.INFO
    log_dir: Annotated[
        Path,
        Field(
            description="Folder in which log files will be saved.",
        ),
    ] = Path("/var/log/optimade/")
    validate_query_parameters: Annotated[
        Optional[bool],
        Field(
            description=(
                "If True, the server will check whether the query parameters given in the "
                "request are correct."
            ),
        ),
    ] = True

    validate_api_response: Annotated[
        Optional[bool],
        Field(
            description=(
                "If False, data from the database will not undergo validation before being"
                " emitted by the API, and only the mapping of aliases will occur."
            ),
        ),
    ] = True

    @field_validator("implementation", mode="before")
    @classmethod
    def set_implementation_version(cls, value: Any) -> dict[str, Any]:
        """Set defaults and modify bypassed value(s)"""
        if not isinstance(value, dict):
            if isinstance(value, Implementation):
                value = value.model_dump()
            else:
                raise TypeError(
                    f"Expected a dict or Implementation instance, got {type(value)}"
                )

        res = {"version": __version__}
        res.update(value)
        return res

    @model_validator(mode="after")
    def use_real_mongo_override(self) -> "ServerConfig":
        """Overrides the `database_backend` setting with MongoDB and
        raises a deprecation warning.
        """
        use_real_mongo = self.use_real_mongo

        # Remove from model
        del self.use_real_mongo

        # Remove from set of user-defined fields
        if "use_real_mongo" in self.model_fields_set:
            self.model_fields_set.remove("use_real_mongo")

        if use_real_mongo is not None:
            warnings.warn(
                "'use_real_mongo' is deprecated, please set the appropriate 'database_backend' "
                "instead.",
                DeprecationWarning,
            )

            if use_real_mongo:
                self.database_backend = SupportedBackend.MONGODB

        return self

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> tuple[PydanticBaseSettingsSource, ...]:
        """
        **Priority of config settings sources**:

        1. Passed arguments upon initialization of
            [`ServerConfig`][optimade.server.config.ServerConfig].
        2. Environment variables, matching the syntax: `"OPTIMADE_"` or `"optimade_"` +
            `<config_name>`, e.g., `OPTIMADE_LOG_LEVEL=debug` or
            `optimade_log_dir=~/logs_dir/optimade/`.
        3. Configuration file (JSON/YAML) taken from:
            1. Environment variable `OPTIMADE_CONFIG_FILE`.
            2. Default location (see
                [DEFAULT_CONFIG_FILE_PATH][optimade.server.config.DEFAULT_CONFIG_FILE_PATH]).
        4. Settings from secret file (see
            [pydantic documentation](https://pydantic-docs.helpmanual.io/usage/settings/#secret-support)
            for more information).

        """
        return (
            init_settings,
            env_settings,
            ConfigFileSettingsSource(settings_cls),
            file_secret_settings,
        )

aliases: Annotated[dict[Literal['links', 'references', 'structures'], dict[str, str]], Field(description='A mapping between field names in the database with their corresponding OPTIMADE field names, broken down by endpoint.')] = {} class-attribute instance-attribute

base_url: Annotated[Optional[str], Field(description='Base URL for this implementation')] = None class-attribute instance-attribute

custom_landing_page: Annotated[Optional[Union[str, Path]], Field(description='The location of a custom landing page (Jinja template) to use for the API.')] = None class-attribute instance-attribute

database_backend: Annotated[SupportedBackend, Field(description='Which database backend to use out of the supported backends.')] = SupportedBackend.MONGOMOCK class-attribute instance-attribute

debug: Annotated[bool, Field(description='Turns on Debug Mode for the OPTIMADE Server implementation')] = False class-attribute instance-attribute

default_db: Annotated[str, Field(description='ID of /links endpoint resource for the chosen default OPTIMADE implementation (only relevant for the index meta-database)')] = 'test_server' class-attribute instance-attribute

elastic_hosts: Annotated[Optional[Union[str, list[str], dict[str, Any], list[dict[str, Any]]]], Field(description='Host settings to pass through to the `Elasticsearch` class.')] = None class-attribute instance-attribute

implementation: Annotated[Implementation, Field(description='Introspective information about this OPTIMADE implementation')] = Implementation(name='OPTIMADE Python Tools', version=__version__, source_url='https://github.com/Materials-Consortia/optimade-python-tools', maintainer={'email': 'dev@optimade.org'}, issue_tracker='https://github.com/Materials-Consortia/optimade-python-tools/issues', homepage='https://optimade.org/optimade-python-tools') class-attribute instance-attribute

index_base_url: Annotated[Optional[AnyHttpUrl], Field(description='An optional link to the base URL for the index meta-database of the provider.')] = None class-attribute instance-attribute

index_schema_url: Annotated[Optional[Union[str, AnyHttpUrl]], Field(description='A URL that will be provided in the `meta->schema` field for every response from the index meta-database.')] = f'https://schemas.optimade.org/openapi/v{__api_version__}/optimade_index.json' class-attribute instance-attribute

insert_test_data: Annotated[bool, Field(description='Insert test data into each collection on server initialisation. If true, the configured backend will be populated with test data on server start. Should be disabled for production usage.')] = True class-attribute instance-attribute

is_index: Annotated[Optional[bool], Field(description='A runtime setting to dynamically switch between index meta-database and normal OPTIMADE servers. Used for switching behaviour of e.g., `meta->optimade_schema` values in the response. Any provided value may be overridden when used with the reference server implementation.')] = False class-attribute instance-attribute

length_aliases: Annotated[dict[Literal['links', 'references', 'structures'], dict[str, str]], Field(description='A mapping between a list property (or otherwise) and an integer property that defines the length of that list, for example elements -> nelements. The standard aliases are applied first, so this dictionary must refer to the API fields, not the database fields.')] = {} class-attribute instance-attribute

log_dir: Annotated[Path, Field(description='Folder in which log files will be saved.')] = Path('/var/log/optimade/') class-attribute instance-attribute

log_level: Annotated[LogLevel, Field(description='Logging level for the OPTIMADE server.')] = LogLevel.INFO class-attribute instance-attribute

model_config = SettingsConfigDict(env_prefix='optimade_', extra='allow', env_file_encoding='utf-8', case_sensitive=False) class-attribute instance-attribute

mongo_count_timeout: Annotated[int, Field(description='Number of seconds to allow MongoDB to perform a full database count before falling back to `null`. This operation can require a full COLLSCAN for empty queries which can be prohibitively slow if the database does not fit into the active set, hence a timeout can drastically speed-up response times.')] = 5 class-attribute instance-attribute

mongo_database: Annotated[str, Field(description='Mongo database for collection data')] = 'optimade' class-attribute instance-attribute

mongo_uri: Annotated[str, Field(description='URI for the Mongo server')] = 'localhost:27017' class-attribute instance-attribute

page_limit: Annotated[int, Field(description='Default number of resources per page')] = 20 class-attribute instance-attribute

page_limit_max: Annotated[int, Field(description='Max allowed number of resources per page')] = 500 class-attribute instance-attribute

provider: Annotated[Provider, Field(description='General information about the provider of this OPTIMADE implementation')] = Provider(prefix='exmpl', name='Example provider', description='Provider used for examples, not to be assigned to a real database', homepage='https://example.com') class-attribute instance-attribute

provider_fields: Annotated[dict[Literal['links', 'references', 'structures'], list[Union[str, dict[Literal['name', 'type', 'unit', 'description'], str]]]], Field(description="A list of additional fields to be served with the provider's prefix attached, broken down by endpoint.")] = {} class-attribute instance-attribute

references_collection: Annotated[str, Field(description='Mongo collection name for /references endpoint resources')] = 'references' class-attribute instance-attribute

root_path: Annotated[Optional[str], Field(description='Sets the FastAPI app `root_path` parameter. This can be used to serve the API under a path prefix behind a proxy or as a sub-application of another FastAPI app. See https://fastapi.tiangolo.com/advanced/sub-applications/#technical-details-root_path for details.')] = None class-attribute instance-attribute

schema_url: Annotated[Optional[Union[str, AnyHttpUrl]], Field(description='A URL that will be provided in the `meta->schema` field for every response')] = f'https://schemas.optimade.org/openapi/v{__api_version__}/optimade.json' class-attribute instance-attribute

structures_collection: Annotated[str, Field(description='Mongo collection name for /structures endpoint resources')] = 'structures' class-attribute instance-attribute

use_real_mongo: Annotated[Optional[bool], Field(description='DEPRECATED: force usage of MongoDB over any other backend.')] = None class-attribute instance-attribute

validate_api_response: Annotated[Optional[bool], Field(description='If False, data from the database will not undergo validation before being emitted by the API, and only the mapping of aliases will occur.')] = True class-attribute instance-attribute

validate_query_parameters: Annotated[Optional[bool], Field(description='If True, the server will check whether the query parameters given in the request are correct.')] = True class-attribute instance-attribute

set_implementation_version(value) classmethod

Set defaults and modify bypassed value(s)

Source code in optimade/server/config.py
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
@field_validator("implementation", mode="before")
@classmethod
def set_implementation_version(cls, value: Any) -> dict[str, Any]:
    """Set defaults and modify bypassed value(s)"""
    if not isinstance(value, dict):
        if isinstance(value, Implementation):
            value = value.model_dump()
        else:
            raise TypeError(
                f"Expected a dict or Implementation instance, got {type(value)}"
            )

    res = {"version": __version__}
    res.update(value)
    return res

settings_customise_sources(settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings) classmethod

Priority of config settings sources:

  1. Passed arguments upon initialization of ServerConfig.
  2. Environment variables, matching the syntax: "OPTIMADE_" or "optimade_" + <config_name>, e.g., OPTIMADE_LOG_LEVEL=debug or optimade_log_dir=~/logs_dir/optimade/.
  3. Configuration file (JSON/YAML) taken from:
    1. Environment variable OPTIMADE_CONFIG_FILE.
    2. Default location (see DEFAULT_CONFIG_FILE_PATH).
  4. Settings from secret file (see pydantic documentation for more information).
Source code in optimade/server/config.py
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
@classmethod
def settings_customise_sources(
    cls,
    settings_cls: type[BaseSettings],
    init_settings: PydanticBaseSettingsSource,
    env_settings: PydanticBaseSettingsSource,
    dotenv_settings: PydanticBaseSettingsSource,
    file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
    """
    **Priority of config settings sources**:

    1. Passed arguments upon initialization of
        [`ServerConfig`][optimade.server.config.ServerConfig].
    2. Environment variables, matching the syntax: `"OPTIMADE_"` or `"optimade_"` +
        `<config_name>`, e.g., `OPTIMADE_LOG_LEVEL=debug` or
        `optimade_log_dir=~/logs_dir/optimade/`.
    3. Configuration file (JSON/YAML) taken from:
        1. Environment variable `OPTIMADE_CONFIG_FILE`.
        2. Default location (see
            [DEFAULT_CONFIG_FILE_PATH][optimade.server.config.DEFAULT_CONFIG_FILE_PATH]).
    4. Settings from secret file (see
        [pydantic documentation](https://pydantic-docs.helpmanual.io/usage/settings/#secret-support)
        for more information).

    """
    return (
        init_settings,
        env_settings,
        ConfigFileSettingsSource(settings_cls),
        file_secret_settings,
    )

use_real_mongo_override()

Overrides the database_backend setting with MongoDB and raises a deprecation warning.

Source code in optimade/server/config.py
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
@model_validator(mode="after")
def use_real_mongo_override(self) -> "ServerConfig":
    """Overrides the `database_backend` setting with MongoDB and
    raises a deprecation warning.
    """
    use_real_mongo = self.use_real_mongo

    # Remove from model
    del self.use_real_mongo

    # Remove from set of user-defined fields
    if "use_real_mongo" in self.model_fields_set:
        self.model_fields_set.remove("use_real_mongo")

    if use_real_mongo is not None:
        warnings.warn(
            "'use_real_mongo' is deprecated, please set the appropriate 'database_backend' "
            "instead.",
            DeprecationWarning,
        )

        if use_real_mongo:
            self.database_backend = SupportedBackend.MONGODB

    return self

SupportedBackend

Bases: Enum

Enumeration of supported database backends

  • elastic: Elasticsearch.
  • mongodb: MongoDB.
  • mongomock: Also MongoDB, but instead of using the pymongo driver to connect to a live Mongo database instance, this will use the mongomock driver, creating an in-memory database, which is mainly used for testing.
Source code in optimade/server/config.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class SupportedBackend(Enum):
    """Enumeration of supported database backends

    - `elastic`: [Elasticsearch](https://www.elastic.co/).
    - `mongodb`: [MongoDB](https://www.mongodb.com/).
    - `mongomock`: Also MongoDB, but instead of using the
        [`pymongo`](https://pymongo.readthedocs.io/) driver to connect to a live Mongo
        database instance, this will use the
        [`mongomock`](https://github.com/mongomock/mongomock) driver, creating an
        in-memory database, which is mainly used for testing.

    """

    ELASTIC = "elastic"
    MONGODB = "mongodb"
    MONGOMOCK = "mongomock"

ELASTIC = 'elastic' class-attribute instance-attribute

MONGODB = 'mongodb' class-attribute instance-attribute

MONGOMOCK = 'mongomock' class-attribute instance-attribute