Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from config init to BaseSettings #226

Merged
merged 53 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6875ba3
replace config with BaseSettings derived class
Mar 5, 2020
04bece3
Merge branch 'master' into switch-to-basesettings
Mar 12, 2020
f1fec91
add providers and aliases
Mar 12, 2020
92b894b
add typing extensions
Mar 12, 2020
91b9fbf
fix prefixes
Mar 12, 2020
7d1de5a
reorganize tests and update environment variable value
Mar 13, 2020
2e0105e
fix prefix name striping
Mar 13, 2020
da9d82c
add better defaults
Mar 13, 2020
ba9401a
fix mappers
Mar 13, 2020
e84ead2
this appears counter to the tests
Mar 13, 2020
9c6e0cc
lint
Mar 13, 2020
32c9818
add has to optimade error
Mar 13, 2020
89154b6
fix index url
Mar 13, 2020
339d729
fix test names
Mar 13, 2020
acf1a81
add ability to have a default file
Mar 13, 2020
2b0f94a
revert back to sensible defaults
Mar 14, 2020
86bb775
enable loading test specific config
Mar 14, 2020
399f6a5
Add field descriptions
Mar 14, 2020
35bbd2f
fix lint issues
Mar 14, 2020
953e099
fix eager tests
Mar 14, 2020
bf3a002
move setup_config to whole test suite
Mar 14, 2020
16229e7
Merge branch 'master' into switch-to-basesettings
Mar 14, 2020
ff244e2
remove adhoc config update
Mar 14, 2020
d94bb25
stage test settings via specialized config file
Mar 14, 2020
3d7b701
fix dockerfile
Mar 14, 2020
de44f99
test error hashability
Mar 14, 2020
c1435c9
Merge branch 'master' into switch-to-basesettings
Mar 16, 2020
dba9f52
Fix all capitalisation of OPTIMADE (closes #232) (#233)
ml-evs Mar 16, 2020
0efce3f
remove stale files
Mar 19, 2020
b45c9a1
update tests
Mar 19, 2020
3490669
update data for example prefix
Mar 19, 2020
dae6077
update prefix to not have underscores
Mar 19, 2020
b48029c
update default_db description
Mar 19, 2020
d746541
fix prefix
Mar 19, 2020
4203473
delete another wayward ini
Mar 19, 2020
ce46705
remove mp reference
Mar 19, 2020
0ce30dc
fix bad prefix in test
Mar 19, 2020
290c848
Merge branch 'master' into switch-to-basesettings
Mar 19, 2020
c55223e
fix setup and manifest
Mar 19, 2020
08191db
add example config json
Mar 19, 2020
8bca7d9
update readme
Mar 19, 2020
55c6b9a
Merge branch 'master' into switch-to-basesettings
Mar 19, 2020
1f97877
Update INSTALL.md
Mar 19, 2020
2b9c402
Apply suggestions from code review
Mar 19, 2020
ea9d0b9
Update install.md
Mar 19, 2020
42b40a2
fix lint issues
Mar 19, 2020
290773d
use relative path
Mar 19, 2020
eb31bf3
update docs to describe index_links_path
Mar 19, 2020
392a5d8
better examples
Mar 19, 2020
eb42ac7
make config file controllable via docker args
Mar 19, 2020
09d04a0
add variable to docker-compose
Mar 20, 2020
da28f47
update install
Mar 20, 2020
76d2e37
add default config
Mar 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ EXPOSE ${PORT}

COPY .docker/run.sh ./

COPY tests/test_config.json ./
ENV OPTIMADE_CONFIG_FILE /app/test_config.json
ARG CONFIG_FILE=tests/test_config.json
COPY ${CONFIG_FILE} ./config.json
ENV OPTIMADE_CONFIG_FILE /app/config.json

CMD ["/app/run.sh"]
12 changes: 8 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
This package may be used to setup and run an [OPTIMADE index meta-database](https://github.com/Materials-Consortia/OPTIMADE/blob/develop/optimade.rst#index-meta-database).
Install the package via `pip install optimade[server]`.

The preferred method of configuring the server is to use a [JSON file](example_json.config) and setting the `optimade_config_file` environment variable to point to the location of this JSON file. The server can also be configured via environmet variables prefixed with `optimade_` and the corresponding variable names in `ServerConfig` in `optimade/server/config.py`. These environment variables are not case sensitive, so both `OPTIMADE_CONFIG_FILE` and `optimade_config_file` are valid.
This python OPTIMADE implementation can be configured in two ways:
First, the server can be configured via environment variables prefixed with `optimade_` and the corresponding variable names in `ServerConfig` in the [`config.py` file](optimade/server/config.py). These take precedence. These environment variables are not case sensitive, so both `OPTIMADE_CONFIG_FILE` and `optimade_config_file` are valid.
Second, and the preferred method of configuring the server, is to use a [JSON file](example_json.config) with the bulk of the configuration and setting the `optimade_config_file` environment variable to point to the absolute location of this JSON file. By default this points to `~/.optimade.json` which can also be used to store the configuration if you don't want to set `optimade_config_file`.

For any configuration parameters not set by the above two, the defaults in built into `ServerConfig` in `optimade.server.config` will be used.

The index meta-database is set up to populate a `mongomock` in-memory database with resources from a static `json` file containing the `child` resources you, as a database provider, want to serve under this index meta-database.
There is a built-in index meta-database set up to populate a `mongomock` in-memory database with resources from a static `json` file containing the `child` resources you, as a database provider, want to serve under this index meta-database. The location of that `json` file is controllable using the `index_links_path` property of the configuration or setting via the environment variable `optimade_index_links_path`.

Running the index meta-database is then as simple as writing `./run.sh index` in a terminal from the root of this package.
You can find it at the base URL: <http://localhost:5001/v0.10.1>.

shyamd marked this conversation as resolved.
Show resolved Hide resolved
Here is an example of how it may look to start your server:
```shell
:~$ export OPTIMADE_CONFIG_FILE=/home/optimade_server/config.json
:~$ ./optimade/optimade-python-tools/run.sh index
:~$ ./path/to/optimade/run.sh index
```

## Full development installation
Expand Down Expand Up @@ -45,7 +49,7 @@ py.test
# Install pre-commit environment (e.g., auto-formats code on `git commit`)
pre-commit install

# Optional: Install MongoDB (and set `use_real_mongodb = true`)
# Optional: Install MongoDB (and set `use_real_mongo = true`)
# Below method installs in conda environment and
# - starts server in background
# - ensures and uses ~/dbdata directory to store data
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
context: .
dockerfile: Dockerfile
args:
CONFIG_FILE: tests/test_config.json
PORT: 5000
environment:
MAIN: main
Expand All @@ -20,6 +21,7 @@ services:
context: .
dockerfile: Dockerfile
args:
CONFIG_FILE: tests/test_config.json
PORT: 5001
environment:
MAIN: main_index
Expand Down
13 changes: 5 additions & 8 deletions example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
"links_collection": "optimade_links",
"references_collection": "optimade_references",
"structures_collection": "optimade_structures",
"page_limit": 20,
"page_limit_max": 500,
"default_db": "test_server",
"base_url": null,
"default_db": "example_server",
"base_url": "https://optimade.example.org",
"implementation": {
"name": "Example implementation",
"version": "0.7.0",
"source_url": "https://github.com/Materials-Consortia/optimade-python-tools",
"maintainer": null
"maintainer": {"email": "[email protected]"}
},
"provider": {
"name": "Example provider",
"description": "Provider used for examples, not to be assigned to a real database",
"prefix": "exmpl",
"homepage": "https://example.com",
"index_base_url": "http://localhost:5001"
"index_base_url": "https://example.com/index"
},
"provider_fields": {
"structures": [
Expand All @@ -37,4 +34,4 @@
"chemical_formula_anonymous": "formula_anonymous"
}
}
}
}
73 changes: 50 additions & 23 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import json
from typing import Any, Optional, Dict, List
from typing_extensions import Literal

try:
from typing import Literal
except ImportError:
from typing_extensions import Literal
from pathlib import Path
from warnings import warn

Expand All @@ -27,7 +31,7 @@ class ServerConfig(BaseSettings):
"~/.optimade.json", description="File to load alternative defaults from"
)
debug: bool = Field(
False, description="Turns on Debug Mode for the Optimade Server implementation"
False, description="Turns on Debug Mode for the OPTIMADE Server implementation"
)
use_real_mongo: bool = Field(
False, description="Use a real Mongo server rather than MongoMock"
Expand All @@ -36,39 +40,62 @@ class ServerConfig(BaseSettings):
"optimade", description="Mongo database for collection data"
)
mongo_uri: str = Field("localhost:27017", description="URI for the Mongo server")
links_collection: str = Field("links", description="Collection name for Links")
links_collection: str = Field(
"links", description="Mongo collection name for /links endpoint resources"
)
references_collection: str = Field(
"references", description="Collection name for References"
"references",
description="Mongo collection name for /references endpoint resources",
)
structures_collection: str = Field(
"structures", description="Collection name for Structures"
"structures",
description="Mongo collection name for /structures endpoint resources",
)
page_limit: int = Field(20, description="Default number of resources per page")
page_limit_max: int = Field(
500, description="Max allowed number of resources per page"
)
page_limit: int = Field(20, description="Default items per page")
page_limit_max: int = Field(500, description="Max items per page")
default_db: str = Field(
"test_server",
description="ID of the provider's chosen default OPTIMADE API database.",
description="ID of /links endpoint resource for the chosen default OPTIMADE implementation (only relevant for the index meta-database)",
)
base_url: Optional[str] = Field(
None, description="URL for the homepage for this implementation"
None, description="Base URL for this implementation"
)
implementation: Implementation = Implementation(
name="Example implementation",
version=__version__,
source_url="https://github.com/Materials-Consortia/optimade-python-tools",
maintainer=None,
implementation: Implementation = Field(
Implementation(
name="Example implementation",
version=__version__,
source_url="https://github.com/Materials-Consortia/optimade-python-tools",
maintainer=None,
),
description="Introspective information about this OPTIMADE implementation",
)
provider: Provider = Provider(
prefix="exmpl",
name="Example provider",
description="Provider used for examples, not to be assigned to a real database",
homepage="https://example.com",
index_base_url="http://localhost:5001",
provider: Provider = Field(
Provider(
prefix="exmpl",
name="Example provider",
description="Provider used for examples, not to be assigned to a real database",
homepage="https://example.com",
index_base_url="http://localhost:5001",
),
description="General information about the provider of this OPTIMADE implementation",
)
provider_fields: Dict[
Literal["links", "references", "structures"], List[str]
] = Field(
{},
description="A list of additional fields to be served with the provider's prefix attached, broken down by endpoint.",
)
aliases: 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.",
)
provider_fields: Dict[Literal["links", "references", "structures"], List[str]] = {}
aliases: Dict[Literal["links", "references", "structures"], Dict[str, str]] = {}

index_links_path: Path = Path(__file__).parent.joinpath("index_links.json")
index_links_path: Path = Field(
Path(__file__).parent.joinpath("index_links.json"),
description="Absolute path to a JSON file containing the MongoDB collection of /links resources for the index meta-database",
)

@root_validator(pre=True)
def load_default_settings(cls, values):
Expand Down
4 changes: 1 addition & 3 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ def meta_values(
else:
url_path = parse_result.path

provider = CONFIG.provider.copy()

return ResponseMeta(
query=ResponseMetaQuery(representation=f"{url_path}?{parse_result.query}"),
api_version=f"v{__api_version__}",
time_stamp=datetime.utcnow(),
data_returned=data_returned,
more_data_available=more_data_available,
provider=provider,
provider=CONFIG.provider,
data_available=data_available,
implementation=CONFIG.implementation,
**kwargs,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"pydantic~=1.4",
"email_validator",
"requests~=2.23",
"typing-extensions~=3.7.4.1",
'typing-extensions~=3.7.4.1;python_version<"3.8"',
shyamd marked this conversation as resolved.
Show resolved Hide resolved
],
extras_require={
"all": all_deps,
Expand Down
15 changes: 3 additions & 12 deletions tests/test_config.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
{
"debug": false,
"use_real_mongo": false,
"mongo_database": "optimade",
"mongo_uri": "localhost:27017",
"links_collection": "links",
"references_collection": "references",
"structures_collection": "structures",
"page_limit": 20,
"page_limit_max": 500,
"default_db": "test_server",
"base_url": null,
"base_url": "http://localhost:5000",
"implementation": {
"name": "Example implementation",
"version": "0.7.0",
"source_url": "https://github.com/Materials-Consortia/optimade-python-tools",
"maintainer": null
"maintainer": {"email": "[email protected]"}
},
"provider": {
"name": "Example provider",
Expand All @@ -37,4 +28,4 @@
"chemical_formula_anonymous": "formula_anonymous"
}
}
}
}