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 |
|
__call__()
¶
Source code in optimade/server/config.py
139 140 |
|
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 |
|
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 |
|
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 |
|
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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
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[str | None, Field(description='Base URL for this implementation')] = None
class-attribute
instance-attribute
¶
custom_landing_page: Annotated[str | Path | None, 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[str | list[str] | dict[str, Any] | list[dict[str, Any]] | None, 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[AnyHttpUrl | None, Field(description='An optional link to the base URL for the index meta-database of the provider.')] = None
class-attribute
instance-attribute
¶
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')
class-attribute
instance-attribute
¶
index_schema_url: Annotated[str | AnyHttpUrl | None, 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_from_jsonl: Annotated[Path | None, Field(description='The absolute path to an OPTIMADE JSONL file to use to initialize the database. A unique index will be created over the ID to avoid duplication over multiple runs.')] = None
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[bool | None, 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
¶
links_collection: Annotated[str, Field(description='Mongo collection name for /links endpoint resources')] = 'links'
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[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[str | None, 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[str | AnyHttpUrl | None, 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[bool | None, Field(description='DEPRECATED: force usage of MongoDB over any other backend.')] = None
class-attribute
instance-attribute
¶
validate_api_response: Annotated[bool | None, 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[bool | None, Field(description='If True, the server will check whether the query parameters given in the request are correct.')] = True
class-attribute
instance-attribute
¶
check_jsonl_path(value)
classmethod
¶
Check that the path to the JSONL file is valid.
Source code in optimade/server/config.py
418 419 420 421 422 423 424 425 |
|
set_implementation_version(value)
classmethod
¶
Set defaults and modify bypassed value(s)
Source code in optimade/server/config.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
|
settings_customise_sources(settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings)
classmethod
¶
Priority of config settings sources:
- Passed arguments upon initialization of
ServerConfig
. - Environment variables, matching the syntax:
"OPTIMADE_"
or"optimade_"
+<config_name>
, e.g.,OPTIMADE_LOG_LEVEL=debug
oroptimade_log_dir=~/logs_dir/optimade/
. - Configuration file (JSON/YAML) taken from:
- Environment variable
OPTIMADE_CONFIG_FILE
. - Default location (see DEFAULT_CONFIG_FILE_PATH).
- Environment variable
- Settings from secret file (see pydantic documentation for more information).
Source code in optimade/server/config.py
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
use_real_mongo_override()
¶
Overrides the database_backend
setting with MongoDB and
raises a deprecation warning.
Source code in optimade/server/config.py
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 |
|
SupportedBackend
¶
Bases: Enum
Enumeration of supported database backends
elastic
: Elasticsearch.mongodb
: MongoDB.mongomock
: Also MongoDB, but instead of using thepymongo
driver to connect to a live Mongo database instance, this will use themongomock
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 |
|