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

Use optimade.__api_version__ instead of CONFIG #32

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 0 additions & 6 deletions .ci/optimade-version.json

This file was deleted.

53 changes: 0 additions & 53 deletions .ci/optimade_version_update.py

This file was deleted.

11 changes: 0 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,3 @@ repos:
.codecov.yml
)$
language: system

- id: optimade-version
name: Update OPTiMaDe shield
description: If needed, the json for the OPTiMaDe shield will be updated with new config version
entry: python ./.ci/optimade_version_update.py
files: >
(?x)^(
.ci/optimade-version.json|
aiida_optimade/config.json
)$
language: system
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| Latest release | Build status | Activity |
|:--------------:|:------------:|:--------:|
| [![PyPI](https://img.shields.io/pypi/v/aiida-optimade)](https://pypi.org/project/aiida-optimade/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/aiida-optimade)](https://pypi.org/project/aiida-optimade/) [![OPTiMaDe](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/aiidateam/aiida-optimade/master/.ci/optimade-version.json)](https://github.com/Materials-Consortia/OPTiMaDe/) | [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/aiidateam/aiida-optimade/aiida-optimade)](https://github.com/aiidateam/aiida-optimade/actions/) [![Codecov](https://img.shields.io/codecov/c/gh/aiidateam/aiida-optimade)](https://codecov.io/gh/aiidateam/aiida-optimade) | [![GitHub last commit](https://img.shields.io/github/last-commit/aiidateam/aiida-optimade)](https://github.com/aiidateam/aiida-optimade) |
| [![PyPI](https://img.shields.io/pypi/v/aiida-optimade)](https://pypi.org/project/aiida-optimade/)<br>[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/aiida-optimade)](https://pypi.org/project/aiida-optimade/)<br>[![OPTiMaDe](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/Materials-Consortia/optimade-python-tools/v0.3.0/.ci/optimade-version.json)](https://github.com/Materials-Consortia/OPTiMaDe/) | [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/aiidateam/aiida-optimade/aiida-optimade)](https://github.com/aiidateam/aiida-optimade/actions/)<br>[![Codecov](https://img.shields.io/codecov/c/gh/aiidateam/aiida-optimade)](https://codecov.io/gh/aiidateam/aiida-optimade) | [![GitHub last commit](https://img.shields.io/github/last-commit/aiidateam/aiida-optimade)](https://github.com/aiidateam/aiida-optimade) |

This is a RESTful API server created with [FastAPI](https://fastapi.tiangolo.com/) that exposes an AiiDA database according to the [OPTiMaDe specification](https://github.com/Materials-Consortia/OPTiMaDe/blob/develop/optimade.rst).

Expand Down Expand Up @@ -35,10 +35,10 @@ pip install -e aiida-optimade/
```shell
# specify AiiDA profile (will use default otherwise)
export AIIDA_PROFILE=optimade
sh run.sh
./aiida-optimade/run.sh
```

Navigate to `http://127.0.0.1:5000/optimade/info`
Navigate to `http://localhost:5000/optimade/info`

## Running via docker

Expand All @@ -48,7 +48,7 @@ Adapt `profiles/quicksetup.json` and `profiles/docker-compose.yml` appropriately
docker-compose -f profiles/docker-compose.yml up --build
```

Navigate to `http://127.0.0.1:3253/optimade/info`
Navigate to `http://localhost:3253/optimade/info`

Stop by using

Expand Down
3 changes: 1 addition & 2 deletions aiida_optimade/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"api_version": "0.10.0",
"page_limit": 100,
"page_limit": 15,
"db_page_limit": 500,
"index_base_url": null,
"provider": {
Expand Down
4 changes: 1 addition & 3 deletions aiida_optimade/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class ServerConfig(Config):
@staticmethod
def _DEFAULTS(field: str) -> Any: # pylint: disable=invalid-name
res = {
"api_version": "1.0.0",
"page_limit": 100,
"page_limit": 15,
"db_page_limit": 500,
"provider": {
"prefix": "_aiida_",
Expand Down Expand Up @@ -52,7 +51,6 @@ def load_from_json(self):
with open(self._path) as config_file:
config = json.load(config_file)

self.version = config.get("api_version", self._DEFAULTS("api_version"))
self.page_limit = int(config.get("page_limit", self._DEFAULTS("page_limit")))
self.db_page_limit = int(
config.get("db_page_limit", self._DEFAULTS("db_page_limit"))
Expand Down
7 changes: 4 additions & 3 deletions aiida_optimade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

from aiida import load_profile

from optimade import __api_version__

from aiida_optimade.common.exceptions import AiidaError
from aiida_optimade.config import CONFIG
import aiida_optimade.exceptions as exc_handlers


Expand All @@ -22,7 +23,7 @@
"[Automated Interactive Infrastructure and Database for Computational Science (AiiDA)](http://www.aiida.net) "
"aims to help researchers with managing complex workflows and making them fully reproducible."
),
version=CONFIG.version,
version=__api_version__,
docs_url="/optimade/extensions/docs",
redoc_url="/optimade/extensions/redoc",
openapi_url="/optimade/extensions/openapi.json",
Expand Down Expand Up @@ -81,7 +82,7 @@ async def backend_middleware(request: Request, call_next):
# /optimade/vMajor.Minor
# /optimade/vMajor.Minor.Patch
VALID_PREFIXES = ["/optimade"]
VERSION = [int(_) for _ in CONFIG.version.split(".")]
VERSION = [int(_) for _ in __api_version__.split(".")]
while VERSION:
if VERSION[0] or len(VERSION) >= 2:
VALID_PREFIXES.append(
Expand Down
6 changes: 4 additions & 2 deletions aiida_optimade/routers/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.requests import Request

from optimade import __api_version__

from optimade.models import (
ErrorResponse,
InfoResponse,
Expand Down Expand Up @@ -36,11 +38,11 @@ def get_info(request: Request):
meta=u.meta_values(str(request.url), 1, 1, more_data_available=False),
data=BaseInfoResource(
attributes=BaseInfoAttributes(
api_version=f"v{CONFIG.version}",
api_version=f"v{__api_version__}",
available_api_versions=[
{
"url": f"{parse_result.scheme}://{parse_result.netloc}",
"version": CONFIG.version,
"version": __api_version__,
}
],
entry_types_by_format={"json": list(ENTRY_INFO_SCHEMAS.keys())},
Expand Down
1 change: 0 additions & 1 deletion aiida_optimade/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def handle_pagination(
elif query["page_offset"] == 0 or abs(query["page_offset"]) < int(
query.get("page_limit", [CONFIG.page_limit])[0]
):
print("here now")
prev_query = query.copy()
prev_query.pop("page_offset")
urlencoded_prev = urllib.parse.urlencode(prev_query, doseq=True)
Expand Down
22 changes: 13 additions & 9 deletions aiida_optimade/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
from aiida_optimade.config import CONFIG
from optimade.validator import ImplementationValidator

# this must be changed before APP is imported
# some tests currently depend on this value remaining at 5
CONFIG.page_limit = 5 # noqa: E402

# Use specific AiiDA profile
if os.getenv("AIIDA_PROFILE", None) is None:
os.environ["AIIDA_PROFILE"] = "optimade_v1_aiida_sqla"
Expand Down Expand Up @@ -151,20 +147,28 @@ def test_structures_endpoint_data(self):
assert self.json_response["meta"]["more_data_available"]

def test_get_next_responses(self):
cursor = self.json_response["data"].copy()
total_data = self.json_response["meta"]["data_available"]
page_limit = 5

response = self.client.get(self.request_str + f"?page_limit={page_limit}")
json_response = response.json()
assert response.status_code == 200, f"Request failed: {response.json()}"

cursor = json_response["data"].copy()
assert json_response["meta"]["more_data_available"]
more_data_available = True
next_request = self.json_response["links"]["next"]
next_request = json_response["links"]["next"]

id_ = len(cursor)
while more_data_available and id_ < CONFIG.page_limit * 3:
while more_data_available and id_ < page_limit * 3:
next_response = self.client.get(next_request).json()
next_request = next_response["links"]["next"]
cursor.extend(next_response["data"])
more_data_available = next_response["meta"]["more_data_available"]
if more_data_available:
assert len(next_response["data"]) == CONFIG.page_limit
assert len(next_response["data"]) == page_limit
else:
assert len(next_response["data"]) == 1089 % CONFIG.page_limit
assert len(next_response["data"]) == total_data % page_limit
id_ += len(next_response["data"])

assert len(cursor) == id_
Expand Down
4 changes: 3 additions & 1 deletion aiida_optimade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from starlette.requests import Request
from starlette.responses import JSONResponse

from optimade import __api_version__

from optimade.models import (
ResponseMeta,
ResponseMetaQuery,
Expand All @@ -30,7 +32,7 @@ def meta_values(
query=ResponseMetaQuery(
representation=f"{parse_result.path}?{parse_result.query}"
),
api_version=f"v{CONFIG.version}",
api_version=f"v{__api_version__}",
time_stamp=datetime.utcnow(),
data_returned=data_returned,
more_data_available=more_data_available,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"aiida-core~=1.0.1",
"fastapi~=0.44",
"lark-parser~=0.7.8",
"optimade~=0.2",
"optimade~=0.3.0",
"pydantic~=1.0, <2.0.0",
"uvicorn",
],
Expand Down
30 changes: 16 additions & 14 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,33 @@ def setver(_, patch=False, new_ver=""):


@task
def set_optimade_ver(_, ver=""):
"""Update the OPTiMaDe version throughout the package"""
def optimade_req(_, ver=""):
"""Update the optimade-python-tools minimum version requirement"""

if not ver:
raise Exception("Please specify --ver='Major.Minor.Patch'")
with open("aiida_optimade/config.json", "r") as handle:

with open("setup.py", "r") as handle:
lines = [
re.sub(
'"api_version": ([^,]+),',
'"api_version": "{}",'.format(ver),
l.rstrip(),
)
for l in handle
re.sub("optimade~=([^,]+)", f'optimade~={ver}"', l.rstrip()) for l in handle
]
with open("aiida_optimade/config.json", "w") as handle:
with open("setup.py", "w") as handle:
handle.write("\n".join(lines))
handle.write("\n")

with open(".ci/optimade-version.json", "r") as handle:
with open("README.md", "r") as handle:
lines = [
re.sub('"message": .+', '"message": "v{}",'.format(ver), l.rstrip())
re.sub(
"https://raw.githubusercontent.com/Materials-Consortia/"
"optimade-python-tools/v([^,]+)/.ci/",
"https://raw.githubusercontent.com/Materials-Consortia/"
f"optimade-python-tools/v{ver}/.ci/",
l.rstrip("\n"),
)
for l in handle
]
with open(".ci/optimade-version.json", "w") as handle:
with open("README.md", "w") as handle:
handle.write("\n".join(lines))
handle.write("\n")

print("Bumped OPTiMaDe version to {}".format(ver))
print("Bumped OPTiMaDe Python Tools version requirement to {}".format(ver))