Skip to content

Commit

Permalink
Upgrade to dev core versions (#316)
Browse files Browse the repository at this point in the history
The settings strategy functions very slightly different due
to the new way session objects are handled in a pipeline.
The way it works now is when a pipeline is executed,
the configuration key in a configuration is updated with
all fields that exist currently in the session object.
The session object only exists on the side of the OTEAPI
service (or locally as a dict when using the "python" OTEClient).

So, to make this work as intended, the settings strategy still
has a settings field, but it will result in a dlite_settings field in
the session, and thereby a dlite_settings field in other strategies that rely on these settings.

This new dlite_settings fields has been added to the new
DliteConfiguration model, which all strategy-specific
configurations that relied on this feature are now based on
(except the settings configuration, naturally).

Otherwise, the strategy-specific configurations are based on
DliteResult, which is a small model that just includes the
collection_id field.
  • Loading branch information
CasperWA authored Dec 5, 2024
1 parent fdd527d commit a24efd4
Show file tree
Hide file tree
Showing 37 changed files with 976 additions and 857 deletions.
34 changes: 34 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
services:
oteapi:
image: ghcr.io/emmc-asbl/oteapi:${DOCKER_OTEAPI_VERSION:-latest}
ports:
- "5000:8080"
environment:
OTEAPI_REDIS_TYPE: redis
OTEAPI_REDIS_HOST: redis
OTEAPI_REDIS_PORT: 6379
OTEAPI_PREFIX: "${OTEAPI_PREFIX:-/api/v1}"
PATH_TO_OTEAPI_CORE:
OTEAPI_PLUGIN_PACKAGES: "-v git+https://github.com/EMMC-ASBL/oteapi-dlite@cwa/close-303-update-to-dev-core-versions"
# OTEAPI_PLUGIN_PACKAGES: "-v -e /oteapi-dlite"
depends_on:
- redis
volumes:
- "${PATH_TO_OTEAPI_CORE:-/dev/null}:/oteapi-core"
# - "${PWD}:/oteapi-dlite"
entrypoint: |
/bin/bash -c "if [ \"${PATH_TO_OTEAPI_CORE}\" != \"/dev/null\" ] && [ -n \"${PATH_TO_OTEAPI_CORE}\" ]; then \
pip install -U --force-reinstall -e /oteapi-core; fi && ./entrypoint.sh --reload --debug --log-level debug"
stop_grace_period: 1s

redis:
image: redis:latest
volumes:
- redis-oteapi:/data

volumes:
redis-oteapi:

networks:
default:
name: otenet
3 changes: 3 additions & 0 deletions docs/api_reference/models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# models

::: oteapi_dlite.models
1 change: 0 additions & 1 deletion docs/api_reference/models/.pages

This file was deleted.

5 changes: 0 additions & 5 deletions docs/api_reference/models/session.md

This file was deleted.

35 changes: 35 additions & 0 deletions oteapi_dlite/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""DLite-specific data models."""

from __future__ import annotations

from typing import Annotated, Optional

from oteapi.models import AttrDict
from pydantic import Field, JsonValue


class DLiteResult(AttrDict):
"""Class for returning values from DLite strategies."""

collection_id: Annotated[
Optional[str], Field(description="A reference to a DLite collection.")
] = None


class DLiteConfiguration(DLiteResult):
"""Data model representing recurring fields necessary in strategy-specific
configurations for DLite strategies.
Note, this data model already includes the `collection_id` field from the
`DLiteResult` data model.
"""

dlite_settings: Annotated[
dict[str, JsonValue],
Field(
description=(
"Settings used by DLite strategies within a single pipeline "
"run."
)
),
] = {} # noqa: RUF012
7 changes: 0 additions & 7 deletions oteapi_dlite/models/__init__.py

This file was deleted.

16 changes: 0 additions & 16 deletions oteapi_dlite/models/session.py

This file was deleted.

35 changes: 14 additions & 21 deletions oteapi_dlite/strategies/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@

import importlib
from collections.abc import Sequence
from typing import TYPE_CHECKING, Annotated, Optional
from typing import Annotated, Optional

import dlite
from oteapi.models import AttrDict, FunctionConfig
from pydantic import Field
from pydantic.dataclasses import dataclass

from oteapi_dlite.models import DLiteSessionUpdate
from oteapi_dlite.models import DLiteResult
from oteapi_dlite.utils import get_collection, update_collection

if TYPE_CHECKING: # pragma: no cover
from typing import Any


class DLiteConvertInputConfig(AttrDict):
"""Configuration for input instance to generic DLite converter.
Expand Down Expand Up @@ -64,7 +61,7 @@ class DLiteConvertOutputConfig(AttrDict):
] = None


class DLiteConvertStrategyConfig(AttrDict):
class DLiteConvertStrategyConfig(DLiteResult):
"""Configuration for generic DLite converter."""

function_name: Annotated[
Expand Down Expand Up @@ -145,35 +142,31 @@ class DLiteConvertStrategy:
"""

convert_config: DLiteConvertConfig
function_config: DLiteConvertConfig

def initialize(
self,
session: Optional[dict[str, Any]] = None,
) -> DLiteSessionUpdate:
def initialize(self) -> DLiteResult:
"""Initialize."""
return DLiteSessionUpdate(collection_id=get_collection(session).uuid)
return DLiteResult(
collection_id=get_collection(
self.function_config.configuration.collection_id
).uuid
)

def get(
self, session: Optional[dict[str, Any]] = None
) -> DLiteSessionUpdate:
def get(self) -> DLiteResult:
"""Execute the strategy.
This method will be called through the strategy-specific endpoint
of the OTE-API Services.
Parameters:
session: A session-specific dictionary context.
Returns:
SessionUpdate instance.
"""
config = self.convert_config.configuration
config = self.function_config.configuration
module = importlib.import_module(config.module_name, config.package)
function = getattr(module, config.function_name)
kwargs = config.kwargs

coll = get_collection(session)
coll = get_collection(config.collection_id)

instances = []
for i, input_config in enumerate(config.inputs):
Expand Down Expand Up @@ -201,4 +194,4 @@ def get(
coll.add(output_config.label, inst)

update_collection(coll)
return DLiteSessionUpdate(collection_id=coll.uuid)
return DLiteResult(collection_id=coll.uuid)
32 changes: 14 additions & 18 deletions oteapi_dlite/strategies/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Annotated, Optional
from typing import Annotated, Optional

from dlite.utils import get_referred_instances
from oteapi.models import AttrDict, FilterConfig
from oteapi.models import FilterConfig
from pydantic import Field
from pydantic.dataclasses import dataclass

from oteapi_dlite.models import DLiteSessionUpdate
from oteapi_dlite.models import DLiteResult
from oteapi_dlite.utils import get_collection, update_collection

if TYPE_CHECKING: # pragma: no cover
from typing import Any


class DLiteQueryConfig(AttrDict):
class DLiteQueryConfig(DLiteResult):
"""Configuration for the DLite filter strategy.
First the `remove_label` and `remove_datamodel` configurations are
Expand Down Expand Up @@ -97,22 +94,21 @@ class DLiteFilterStrategy:
**Registers strategies**:
- `("filterType", "dlite/filter")`
- `("filterType", "application/vnd.dlite-filter")`
"""

filter_config: DLiteFilterConfig

def initialize(
self,
session: Optional[dict[str, Any]] = None,
) -> DLiteSessionUpdate:
def initialize(self) -> DLiteResult:
"""Initialize."""
return DLiteSessionUpdate(collection_id=get_collection(session).uuid)
return DLiteResult(
collection_id=get_collection(
self.filter_config.configuration.collection_id
).uuid
)

def get(
self, session: Optional[dict[str, Any]] = None
) -> DLiteSessionUpdate:
def get(self) -> DLiteResult:
"""Execute the strategy."""
config = self.filter_config.configuration

Expand All @@ -122,7 +118,7 @@ def get(
)

instdict = {} # Map instance labels to [uuid, metaURI]
coll = get_collection(session)
coll = get_collection(config.collection_id)
for s, _, o in coll.get_relations(p="_has-uuid"):
instdict[s] = [o]
for s, _, o in coll.get_relations(p="_has-meta"):
Expand Down Expand Up @@ -170,4 +166,4 @@ def get(
coll.remove(label)

update_collection(coll)
return DLiteSessionUpdate(collection_id=get_collection(session).uuid)
return DLiteResult(collection_id=coll.uuid)
Loading

0 comments on commit a24efd4

Please sign in to comment.