oceanprotocol-job-details 0.2.7__py3-none-any.whl → 0.3.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
+ from .helpers import create_container, load_job_details
1
2
  from .ocean import JobDetails
2
3
 
3
-
4
- __all__ = [JobDetails]
4
+ __all__ = [JobDetails, load_job_details, create_container] # type: ignore
@@ -1,21 +1,22 @@
1
+ from typing import Generic, TypeVar
1
2
  from dependency_injector import containers, providers
3
+ from pydantic import BaseModel
2
4
 
3
5
  from oceanprotocol_job_details.loaders.impl.ddo import DDOLoader
4
6
  from oceanprotocol_job_details.loaders.impl.files import FilesLoader
5
7
  from oceanprotocol_job_details.loaders.impl.job_details import JobDetailsLoader
6
- from oceanprotocol_job_details.paths import Paths
8
+ from oceanprotocol_job_details.domain import Paths
7
9
 
8
10
 
9
- class Container(containers.DeclarativeContainer):
11
+ InputParametersT = TypeVar("InputParametersT", bound=BaseModel)
10
12
 
13
+
14
+ class Container(containers.DeclarativeContainer, Generic[InputParametersT]):
11
15
  config = providers.Configuration()
12
16
 
13
- paths = providers.Singleton(
14
- Paths,
15
- base_dir=config.base_dir,
16
- )
17
+ paths = providers.Singleton(Paths, base_dir=config.base_dir)
17
18
 
18
- file_loader = providers.Factory(
19
+ file_loader = providers.Singleton(
19
20
  FilesLoader,
20
21
  dids=config.dids,
21
22
  transformation_did=config.transformation_did,
@@ -23,26 +24,16 @@ class Container(containers.DeclarativeContainer):
23
24
  logger=config.logger,
24
25
  )
25
26
 
26
- files = providers.Factory(
27
- lambda loader: loader.load(),
28
- loader=file_loader,
29
- )
30
-
31
- # DDOLoader depends on Files loaded from FilesLoader
32
- ddo_loader = providers.Factory(
33
- DDOLoader,
34
- files=files,
35
- )
36
-
37
- ddos = providers.Factory(
38
- lambda loader: loader.load(),
39
- loader=ddo_loader,
40
- )
41
-
42
- job_details_loader = providers.Factory(
43
- JobDetailsLoader,
44
- files=files,
45
- secret=config.secret,
46
- paths=paths,
47
- ddos=ddos,
27
+ files = providers.Factory(lambda loader: loader.load(), loader=file_loader)
28
+ ddo_loader = providers.Factory(DDOLoader, files=files)
29
+ ddos = providers.Factory(lambda loader: loader.load(), loader=ddo_loader)
30
+
31
+ job_details_loader: providers.Factory[JobDetailsLoader[InputParametersT]] = (
32
+ providers.Factory(
33
+ JobDetailsLoader,
34
+ files=files,
35
+ secret=config.secret,
36
+ paths=paths,
37
+ ddos=ddos,
38
+ )
48
39
  )
@@ -0,0 +1,187 @@
1
+ # mypy: disable-error-code=explicit-any
2
+ from dataclasses import InitVar, dataclass, field
3
+ from pathlib import Path
4
+ from typing import Generator, List, Optional, Sequence, TypeAlias, TypeVar
5
+
6
+ from pydantic import BaseModel, ConfigDict, Field, JsonValue
7
+
8
+
9
+ class Credential(BaseModel):
10
+ type: str
11
+ values: list[str]
12
+
13
+
14
+ class Credentials(BaseModel):
15
+ allow: list[Credential]
16
+ deny: list[Credential]
17
+
18
+
19
+ class DockerContainer(BaseModel):
20
+ image: str
21
+ tag: str
22
+ entrypoint: str
23
+
24
+
25
+ class Algorithm(BaseModel):
26
+ container: DockerContainer
27
+ language: str
28
+ version: str
29
+ consumerParameters: JsonValue
30
+
31
+
32
+ class Metadata(BaseModel):
33
+ description: str
34
+ name: str
35
+ type: str
36
+ author: str
37
+ license: str
38
+ algorithm: Optional[Algorithm] = None
39
+ tags: Optional[list[str]] = None
40
+ created: Optional[str] = None
41
+ updated: Optional[str] = None
42
+ copyrightHolder: Optional[str] = None
43
+ links: Optional[list[str]] = None
44
+ contentLanguage: Optional[str] = None
45
+ categories: Optional[list[str]] = None
46
+
47
+
48
+ class ConsumerParameters(BaseModel):
49
+ name: str
50
+ type: str
51
+ label: str
52
+ required: bool
53
+ description: str
54
+ default: str
55
+ option: Optional[list[str]] = None
56
+
57
+
58
+ class Service(BaseModel):
59
+ id: str
60
+ type: str
61
+ timeout: int
62
+ files: str
63
+ datatokenAddress: str
64
+ serviceEndpoint: str
65
+ additionalInformation: Optional[str] = None
66
+ name: Optional[str] = None
67
+ description: Optional[str] = None
68
+
69
+
70
+ class Event(BaseModel):
71
+ tx: str
72
+ block: int
73
+ from_: str = Field(alias="from")
74
+ contract: str
75
+ datetime: str
76
+
77
+ model_config = ConfigDict(populate_by_name=True)
78
+
79
+
80
+ class NFT(BaseModel):
81
+ address: str
82
+ name: str
83
+ symbol: str
84
+ state: int
85
+ tokenURI: str
86
+ owner: str
87
+ created: str
88
+
89
+
90
+ class DataToken(BaseModel):
91
+ address: str
92
+ name: str
93
+ symbol: str
94
+ serviceId: str
95
+
96
+
97
+ class Price(BaseModel):
98
+ value: int
99
+
100
+
101
+ class Stats(BaseModel):
102
+ allocated: int
103
+ orders: int
104
+ price: Price
105
+
106
+
107
+ class Purgatory(BaseModel):
108
+ state: bool
109
+
110
+
111
+ class DDO(BaseModel):
112
+ id: str
113
+ context: list[str] = Field(alias="@context")
114
+ nftAddress: str
115
+ chainId: int
116
+ version: str
117
+ metadata: Metadata
118
+ services: list[Service]
119
+ credentials: Credentials
120
+ event: Event
121
+ nft: NFT
122
+ datatokens: list[DataToken]
123
+ stats: Stats
124
+ purgatory: Purgatory
125
+
126
+ model_config = ConfigDict(populate_by_name=True)
127
+
128
+
129
+ @dataclass(frozen=True)
130
+ class DIDPaths:
131
+ did: str
132
+ ddo: Path = field(repr=False)
133
+
134
+ files: InitVar[Generator[Path, None, None]]
135
+
136
+ _input: List[Path] = field(init=False, repr=False)
137
+
138
+ def __post_init__(self, files: Generator[Path, None, None]) -> None:
139
+ assert self.ddo.exists(), f"DDO {self.ddo} does not exist"
140
+
141
+ object.__setattr__(self, "_input", list(files))
142
+
143
+ @property
144
+ def input_files(self) -> List[Path]:
145
+ return self._input
146
+
147
+ def __len__(self) -> int:
148
+ return len(self._input)
149
+
150
+
151
+ Files: TypeAlias = Sequence[DIDPaths]
152
+
153
+
154
+ @dataclass(frozen=True)
155
+ class Paths:
156
+ """Configuration class for the Ocean Protocol Job Details"""
157
+
158
+ base_dir: InitVar[Path | None] = None
159
+
160
+ _base: Path = field(init=False, repr=False)
161
+
162
+ def __post_init__(self, base_dir: Path | None) -> None:
163
+ object.__setattr__(self, "_base", base_dir if base_dir else Path("/data"))
164
+
165
+ @property
166
+ def data(self) -> Path:
167
+ return self._base
168
+
169
+ @property
170
+ def inputs(self) -> Path:
171
+ return self.data / "inputs"
172
+
173
+ @property
174
+ def ddos(self) -> Path:
175
+ return self.data / "ddos"
176
+
177
+ @property
178
+ def outputs(self) -> Path:
179
+ return self.data / "outputs"
180
+
181
+ @property
182
+ def logs(self) -> Path:
183
+ return self.data / "logs"
184
+
185
+ @property
186
+ def algorithm_custom_parameters(self) -> Path:
187
+ return self.inputs / "algoCustomData.json"
@@ -0,0 +1,29 @@
1
+ from typing import Any, Dict, Type, TypeVar
2
+
3
+ from pydantic import BaseModel, JsonValue
4
+
5
+ from oceanprotocol_job_details.di import Container
6
+ from oceanprotocol_job_details.ocean import JobDetails
7
+ from oceanprotocol_job_details.settings import JobSettings
8
+
9
+ InputParametersT = TypeVar("InputParametersT", bound=BaseModel)
10
+
11
+
12
+ def create_container(config: Dict[str, Any]) -> Container[InputParametersT]: # type: ignore[explicit-any]
13
+ """Return a fully configured Container from a config dict."""
14
+ container = Container[InputParametersT]()
15
+ settings = JobSettings(**config)
16
+ container.config.from_pydantic(settings)
17
+ return container
18
+
19
+
20
+ def load_job_details(
21
+ config: Dict[str, JsonValue],
22
+ input_type: Type[InputParametersT],
23
+ ) -> JobDetails[InputParametersT]:
24
+ """
25
+ Load JobDetails for a given input_type using the config.
26
+ Returns a fully initialized JobDetails instance.
27
+ """
28
+ container: Container[InputParametersT] = create_container(config)
29
+ return container.job_details_loader(input_type=input_type).load()
@@ -2,31 +2,23 @@ from __future__ import annotations
2
2
 
3
3
  from dataclasses import InitVar, dataclass, field
4
4
  from pathlib import Path
5
- from typing import TYPE_CHECKING, final
5
+ from typing import final
6
6
 
7
- if TYPE_CHECKING:
8
- from oceanprotocol_job_details.ocean import DDO, Files
7
+ from oceanprotocol_job_details.domain import DDO, Files
9
8
 
10
9
 
11
10
  @final
12
11
  @dataclass(frozen=True)
13
12
  class DDOLoader:
14
-
15
- files: InitVar[list[Files]]
13
+ files: InitVar[Files]
16
14
  """The files to load the DDOs from"""
17
15
 
18
16
  _ddo_paths: list[Path] = field(init=False)
19
17
 
20
- def __post_init__(self, files: list[Files]) -> None:
21
- assert files, "Missing files"
18
+ def __post_init__(self, files: Files) -> None:
19
+ assert files is not None and len(files) != 0, "Missing files"
22
20
 
23
21
  object.__setattr__(self, "_ddo_paths", [f.ddo for f in files])
24
22
 
25
23
  def load(self) -> list[DDO]:
26
- from oceanprotocol_job_details.ocean import DDO
27
-
28
- ddos = []
29
- for path in self._ddo_paths:
30
- with open(path, "r") as f:
31
- ddos.append(DDO.from_json(f.read()))
32
- return ddos
24
+ return [DDO.model_validate_json(p.read_text()) for p in self._ddo_paths]
@@ -1,64 +1,44 @@
1
1
  from __future__ import annotations
2
2
 
3
- import json
4
3
  from dataclasses import InitVar, dataclass, field
5
4
  from logging import Logger
6
- from typing import TYPE_CHECKING, final
5
+ from pathlib import Path
6
+ from typing import Literal, final
7
7
 
8
- from oceanprotocol_job_details.paths import Paths
9
-
10
- if TYPE_CHECKING:
11
- from oceanprotocol_job_details.ocean import DIDPaths, Files
8
+ from oceanprotocol_job_details.domain import DIDPaths, Files, Paths
12
9
 
13
10
 
14
11
  @final
15
12
  @dataclass(frozen=True)
16
13
  class FilesLoader:
17
-
18
- dids: InitVar[str | None]
19
- """Input DIDs"""
20
-
21
- transformation_did: InitVar[str | None]
22
- """DID for the transformation algorithm"""
23
-
24
14
  paths: Paths
25
15
  """Path configurations of the project"""
26
16
 
27
- logger: Logger
17
+ logger: Logger = field(repr=False)
28
18
  """Logger to use"""
29
19
 
30
- _dids: str = field(init=False)
31
- _transformation_did: str = field(init=False)
32
-
33
- def __post_init__(
34
- self,
35
- dids: str | None,
36
- transformation_did: str | None,
37
- ) -> None:
38
- def _load_dids(dids, logger):
39
- if dids:
40
- return json.loads(dids)
20
+ dids: list[str]
21
+ """Input DIDs"""
41
22
 
42
- logger.info("Missing DIDS, Inferring DIDS from input DDOs")
43
- return [f.parts[-1] for f in self.paths.ddos.iterdir()]
23
+ transformation_did: str
24
+ """DID for the transformation algorithm"""
44
25
 
45
- object.__setattr__(self, "_transformation_did", transformation_did)
46
- object.__setattr__(self, "_dids", _load_dids(dids, self.logger))
26
+ def __post_init__(self) -> None:
27
+ assert self.dids, "Missing input DIDs"
47
28
 
48
- assert self._dids, "Missing input DIDs"
29
+ def calculate_path(self, did: str, path_type: Literal["input", "ddo"]) -> Path:
30
+ match path_type:
31
+ case "ddo":
32
+ return self.paths.ddos / did
33
+ case "input":
34
+ return self.paths.inputs / did
49
35
 
50
36
  def load(self) -> Files:
51
- from oceanprotocol_job_details.ocean import DIDPaths, Files
52
-
53
- files: list[DIDPaths] = []
54
- for did in self._dids:
55
- base = self.paths.inputs / did
56
- files.append(
57
- DIDPaths(
58
- did=did,
59
- ddo=self.paths.ddos / did,
60
- input_files=list(base.iterdir()),
61
- )
37
+ return [
38
+ DIDPaths(
39
+ did=did,
40
+ ddo=self.calculate_path(did, "ddo"),
41
+ files=self.calculate_path(did, "input").iterdir(),
62
42
  )
63
-
64
- return Files(files)
43
+ for did in self.dids
44
+ ]
@@ -1,35 +1,28 @@
1
- from __future__ import annotations
2
-
3
1
  from dataclasses import dataclass, field
4
- from typing import TYPE_CHECKING, Generic, Type, TypeVar, final
5
-
6
- from oceanprotocol_job_details.paths import Paths
2
+ from typing import Generic, Type, TypeVar, final
7
3
 
8
- if TYPE_CHECKING:
9
- from oceanprotocol_job_details.ocean import DDO, Files, JobDetails
4
+ from pydantic import BaseModel
10
5
 
6
+ from oceanprotocol_job_details.domain import DDO, Files, Paths
7
+ from oceanprotocol_job_details.ocean import JobDetails
11
8
 
12
- T = TypeVar("T")
9
+ T = TypeVar("T", bound=BaseModel)
13
10
 
14
11
 
15
12
  @final
16
13
  @dataclass(frozen=True)
17
14
  class JobDetailsLoader(Generic[T]):
18
-
19
- _type: Type[T] = field(repr=False)
20
-
15
+ input_type: Type[T] = field(repr=False)
21
16
  files: Files
22
17
  secret: str
23
18
  paths: Paths
24
19
  ddos: list[DDO]
25
20
 
26
21
  def load(self) -> JobDetails[T]:
27
- from oceanprotocol_job_details.ocean import JobDetails
28
-
29
- return JobDetails(
22
+ return JobDetails[T](
30
23
  files=self.files,
31
24
  secret=self.secret,
32
25
  ddos=self.ddos,
33
26
  paths=self.paths,
34
- _type=self._type,
27
+ input_type=self.input_type,
35
28
  )
@@ -4,10 +4,6 @@ T = TypeVar("T", covariant=True)
4
4
 
5
5
 
6
6
  class Loader(Protocol[T]):
7
-
8
7
  def load(self) -> T:
9
8
  """Load an instance of the given type"""
10
9
  ...
11
-
12
-
13
- del T
@@ -1,313 +1,51 @@
1
1
  from __future__ import annotations
2
2
 
3
- import os
4
- from dataclasses import dataclass, field
3
+ import asyncio
5
4
  from functools import cached_property
6
- from logging import Logger, getLogger
7
5
  from pathlib import Path
8
- from typing import (
9
- Any,
10
- Generator,
11
- Generic,
12
- Iterator,
13
- Optional,
14
- Sequence,
15
- Type,
16
- TypeVar,
17
- final,
18
- )
6
+ from typing import Generator, Generic, Tuple, Type, TypeVar, final
19
7
 
20
- import orjson
21
- from dataclasses_json import config as dc_config
22
- from dataclasses_json import dataclass_json
8
+ import aiofiles
9
+ from pydantic import BaseModel, ConfigDict, Secret, model_validator
23
10
 
24
- from oceanprotocol_job_details.di import Container
25
- from oceanprotocol_job_details.paths import Paths
11
+ from oceanprotocol_job_details.domain import DDO, Files, Paths
26
12
 
27
- InputParemetersT = TypeVar("InputParemetersT")
28
-
29
-
30
- @dataclass_json
31
- @dataclass
32
- class Credential:
33
- type: str
34
- values: list[str]
35
-
36
-
37
- @dataclass_json
38
- @dataclass
39
- class Credentials:
40
- allow: list[Credential]
41
- deny: list[Credential]
42
-
43
-
44
- @dataclass_json
45
- @dataclass
46
- class DockerContainer:
47
- image: str
48
- tag: str
49
- entrypoint: str
50
-
51
-
52
- @dataclass_json
53
- @dataclass
54
- class Algorithm: # type: ignore
55
- container: DockerContainer
56
- language: str
57
- version: str
58
- consumerParameters: Any # type: ignore
59
-
60
-
61
- @dataclass_json
62
- @dataclass
63
- class Metadata:
64
- description: str
65
- name: str
66
- type: str
67
- author: str
68
- license: str
69
- algorithm: Optional[Algorithm] = None
70
- tags: Optional[list[str]] = None
71
- created: Optional[str] = None
72
- updated: Optional[str] = None
73
- copyrightHolder: Optional[str] = None
74
- links: Optional[list[str]] = None
75
- contentLanguage: Optional[str] = None
76
- categories: Optional[list[str]] = None
77
-
78
-
79
- @dataclass_json
80
- @dataclass
81
- class ConsumerParameters:
82
- name: str
83
- type: str
84
- label: str
85
- required: bool
86
- description: str
87
- default: str
88
- option: Optional[list[str]] = None
89
-
90
-
91
- @dataclass_json
92
- @dataclass
93
- class Service:
94
- id: str
95
- type: str
96
- timeout: int
97
- files: str
98
- datatokenAddress: str
99
- serviceEndpoint: str
100
- additionalInformation: Optional[str] = None
101
- name: Optional[str] = None
102
- description: Optional[str] = None
103
-
104
-
105
- @dataclass_json
106
- @dataclass
107
- class Event:
108
- tx: str
109
- block: int
110
- from_: str = field(metadata=dc_config(field_name="from"))
111
- contract: str
112
- datetime: str
113
-
114
-
115
- @dataclass_json
116
- @dataclass
117
- class NFT:
118
- address: str
119
- name: str
120
- symbol: str
121
- state: int
122
- tokenURI: str
123
- owner: str
124
- created: str
125
-
126
-
127
- @dataclass_json
128
- @dataclass
129
- class DataToken:
130
- address: str
131
- name: str
132
- symbol: str
133
- serviceId: str
134
-
135
-
136
- @dataclass_json
137
- @dataclass
138
- class Price:
139
- value: int
140
-
141
-
142
- @dataclass_json
143
- @dataclass
144
- class Stats:
145
- allocated: int
146
- orders: int
147
- price: Price
148
-
149
-
150
- @dataclass_json
151
- @dataclass
152
- class Purgatory:
153
- state: bool
154
-
155
-
156
- @dataclass_json
157
- @dataclass
158
- class DDO:
159
- id: str
160
- context: list[str] = field(metadata=dc_config(field_name="@context"))
161
- nftAddress: str
162
- chainId: int
163
- version: str
164
- metadata: Metadata
165
- services: list[Service]
166
- credentials: Credentials
167
- event: Event
168
- nft: NFT
169
- datatokens: list[DataToken]
170
- stats: Stats
171
- purgatory: Purgatory
172
-
173
-
174
- @dataclass(frozen=True)
175
- class DIDPaths:
176
- did: str
177
- ddo: Path
178
- input_files: Sequence[Path]
179
-
180
- def __post_init__(self) -> None:
181
- assert self.ddo.exists(), f"DDO {self.ddo} does not exist"
182
- for input_file in self.input_files:
183
- assert input_file.exists(), f"File {input_file} does not exist"
184
-
185
- def __len__(self) -> int:
186
- return len(self.input_files)
187
-
188
-
189
- @dataclass(frozen=True)
190
- class Files:
191
- _files: Sequence[DIDPaths]
192
-
193
- @property
194
- def files(self) -> Sequence[DIDPaths]:
195
- return self._files
196
-
197
- def __getitem__(self, index: int) -> DIDPaths:
198
- return self.files[index]
199
-
200
- def __iter__(self) -> Iterator[DIDPaths]:
201
- return iter(self.files)
202
-
203
- def __len__(self) -> int:
204
- return len(self.files)
205
-
206
-
207
- def _normalize_json(value):
208
- if isinstance(value, str):
209
- try:
210
- decoded = orjson.loads(value)
211
- return _normalize_json(decoded) # recurse if nested again
212
- except orjson.JSONDecodeError:
213
- return value
214
- elif isinstance(value, dict):
215
- return {k: _normalize_json(v) for k, v in value.items()}
216
- elif isinstance(value, list):
217
- return [_normalize_json(v) for v in value]
218
- return value
13
+ InputParemetersT = TypeVar("InputParemetersT", bound=BaseModel)
219
14
 
220
15
 
221
16
  @final
222
- @dataclass_json
223
- @dataclass
224
- class _EmptyJobDetails: ...
225
-
226
-
227
- @final
228
- @dataclass_json
229
- @dataclass(frozen=True)
230
- class JobDetails(Generic[InputParemetersT]):
17
+ class JobDetails(BaseModel, Generic[InputParemetersT]): # type: ignore[explicit-any]
231
18
  files: Files
232
- """The input filepaths"""
233
-
234
19
  ddos: list[DDO]
235
- """list of paths to the DDOs"""
236
-
237
20
  paths: Paths
238
- """Configuration paths"""
21
+ input_type: Type[InputParemetersT]
22
+ secret: Secret[str] | None = None
239
23
 
240
- # Store the type explicitly to avoid issues
241
- _type: Type[InputParemetersT] = field(repr=False)
24
+ model_config = ConfigDict(arbitrary_types_allowed=True, frozen=True)
242
25
 
243
- secret: str | None = None
244
- """Shh it's a secret"""
245
-
246
- def __post_init__(self) -> None:
247
- if not hasattr(self._type, "__dataclass_fields__"):
248
- raise TypeError(f"{self._type} is not a dataclass type")
26
+ @model_validator(mode="after")
27
+ def validate_type(self) -> JobDetails[InputParemetersT]:
28
+ assert issubclass(self.input_type, BaseModel), (
29
+ f"{self.input_type} must be subtype of pydantic.BaseModel"
30
+ )
31
+ return self
249
32
 
250
- def next_path(self) -> Generator[tuple[int, Path], None, None]:
251
- for idx, did_files in enumerate(self.files):
252
- for file in did_files.input_files:
253
- yield (idx, file)
33
+ def inputs(self) -> Generator[Tuple[int, Path], None, None]:
34
+ yield from (
35
+ (idx, file)
36
+ for idx, files in enumerate(self.files)
37
+ for file in files.input_files
38
+ )
254
39
 
255
40
  @cached_property
256
41
  def input_parameters(self) -> InputParemetersT:
257
- """Read the input parameters and return them in an instance of the dataclass InputParemetersT"""
42
+ return asyncio.run(self.ainput_parameters())
258
43
 
259
- with open(self.paths.algorithm_custom_parameters, "r") as f:
260
- raw = f.read().strip()
261
- if not raw:
262
- raise ValueError(
263
- f"Custom parameters file {self.paths.algorithm_custom_parameters} is empty"
264
- )
265
- try:
266
- parsed = _normalize_json(orjson.loads(raw))
267
- return dataclass_json(self._type).from_dict(parsed) # type: ignore
268
- except Exception as e:
269
- raise ValueError(
270
- f"Failed to parse input paramers into {self._type.__name__}: {e}\n"
271
- f"Raw content: {raw}"
272
- ) from e
273
-
274
- @classmethod
275
- def load(
276
- cls,
277
- _type: Type[InputParemetersT] | None = None,
278
- *,
279
- base_dir: str | None = None,
280
- dids: str | None = None,
281
- transformation_did: str | None = None,
282
- secret: str | None = None,
283
- logger: Logger | None = None,
284
- ) -> JobDetails[InputParemetersT]:
285
- """Load a JobDetails instance that holds the runtime details.
286
-
287
- Loading it will check the following:
288
- 1. That the needed environment variables are set.
289
- 1. That the ocean protocol contains the needed data based on the passed environment variables.
290
-
291
- Those needed environment variables are:
292
- - BASE_DIR: Base directory to read the data from, parent of the ddos, inputs, outputs and logs directories.
293
- - DIDS: The DIDs of the inputs
294
- - TRANSFORMATION_DID: The DID of the transformation algorithm
295
- - SECRET (optional): A really secret secret
296
- """
297
-
298
- if _type is None:
299
- _type = _EmptyJobDetails
300
-
301
- container = Container()
302
- container.config.from_dict(
303
- {
304
- "base_dir": base_dir or os.environ.get("BASE_DIR", None),
305
- "dids": dids or os.environ.get("DIDS", None),
306
- "transformation_did": transformation_did
307
- or os.environ.get("TRANSFORMATION_DID", None),
308
- "secret": secret or os.environ.get("SECRET", None),
309
- "logger": logger or getLogger(__name__),
310
- }
311
- )
44
+ async def ainput_parameters(self) -> InputParemetersT:
45
+ path = self.paths.algorithm_custom_parameters
46
+ async with aiofiles.open(path) as f:
47
+ raw = await f.read()
312
48
 
313
- return container.job_details_loader(_type=_type).load()
49
+ raw = raw.strip()
50
+ assert raw is not None, f"Empty file {path}"
51
+ return self.input_type.model_validate_json(raw)
@@ -0,0 +1,37 @@
1
+ # mypy: disable-error-code=call-overload
2
+ from logging import Logger, getLogger
3
+ from pathlib import Path
4
+ from typing import Self
5
+
6
+ import orjson
7
+ from pydantic import Field, field_validator, model_validator
8
+ from pydantic_settings import BaseSettings, SettingsConfigDict
9
+
10
+
11
+ class JobSettings(BaseSettings): # type: ignore[explicit-any]
12
+ base_dir: Path = Field(alias="BASE_DIR")
13
+ dids: list[str] = Field(default_factory=list, alias="DIDS")
14
+ transformation_did: str = Field(alias="TRANSFORMATION_DID")
15
+ secret: str | None = Field(default=None, alias="SECRET")
16
+ logger: Logger = Field(default_factory=lambda: getLogger(__name__))
17
+
18
+ model_config = SettingsConfigDict(
19
+ extra="forbid",
20
+ populate_by_name=True,
21
+ arbitrary_types_allowed=True,
22
+ )
23
+
24
+ @field_validator("dids", mode="before")
25
+ @classmethod
26
+ def split_dids(cls, v: list[str] | str) -> list[str]:
27
+ if isinstance(v, str):
28
+ data = orjson.loads(v)
29
+ assert isinstance(data, list)
30
+ return data
31
+ return v
32
+
33
+ @model_validator(mode="after")
34
+ def validate_dids(self) -> Self:
35
+ if not self.dids:
36
+ self.dids.extend([f.name for f in (self.base_dir / "ddos").glob("*")])
37
+ return self
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oceanprotocol-job-details
3
- Version: 0.2.7
3
+ Version: 0.3.3
4
4
  Summary: A Python package to get details from OceanProtocol jobs
5
5
  Project-URL: Homepage, https://github.com/AgrospAI/oceanprotocol-job-details
6
6
  Project-URL: Issues, https://github.com/AgrospAI/oceanprotocol-job-details/issues
@@ -17,9 +17,11 @@ Classifier: License :: OSI Approved :: MIT License
17
17
  Classifier: Operating System :: OS Independent
18
18
  Classifier: Programming Language :: Python :: 3
19
19
  Requires-Python: >=3.10
20
- Requires-Dist: dataclasses-json>=0.6.7
20
+ Requires-Dist: aiofiles>=25.1.0
21
21
  Requires-Dist: dependency-injector>=4.48.2
22
22
  Requires-Dist: orjson>=3.11.3
23
+ Requires-Dist: pydantic-settings>=2.12.0
24
+ Requires-Dist: pydantic>=2.12.5
23
25
  Description-Content-Type: text/markdown
24
26
 
25
27
  A Python package to get details from OceanProtocol jobs
@@ -28,24 +30,29 @@ A Python package to get details from OceanProtocol jobs
28
30
 
29
31
  ## Installation
30
32
 
31
- ```
33
+ ```bash
32
34
  pip install oceanprotocol-job-details
33
35
  ```
34
36
 
35
- ## Usage
37
+ ```bash
38
+ uv add oceanprotocol-job-details
39
+ ```
40
+
41
+ ## Usage
36
42
 
37
- As a simple library, we only need to import `JobDetails` and load it, it will:
43
+ As a simple library, we only need to import `load_job_details` and run it. It will:
38
44
 
39
- 1. Fetch the needed parameters to populate the `JobDetails` instance from the environment variables or use the passed values to the `load()` method.
45
+ 1. Fetch the needed parameters to populate the `JobDetails` instance from the environment variables or use the passed values to the function.
40
46
  1. Look for the files corresponding to the passed DIDs in the filesystem according to the [Ocean Protocol Structure](#oceanprotocol-structure) and load them into the `JobDetails` instance.
41
47
 
42
-
43
48
  ### Minimal Example
44
49
 
45
50
  ```python
46
- from oceanprotocol_job_details import JobDetails
51
+ from oceanprotocol_job_details import load_job_details
47
52
 
48
- job_details = JobDetails.load()
53
+ class InputParameters(BaseModel): ...
54
+
55
+ job_details = load_job_details({}, InputParameters)
49
56
  ```
50
57
 
51
58
  ### Custom Input Parameters
@@ -53,58 +60,40 @@ job_details = JobDetails.load()
53
60
  If our algorithm has custom input parameters and we want to load them into our algorithm, we can do it as follows:
54
61
 
55
62
  ```python
56
- from dataclasses import dataclass
57
- from oceanprotocol_job_details import JobDetails
58
-
59
-
60
- @dataclass
61
- class InputParameters:
62
- foobar: str
63
-
64
-
65
- job_details = JobDetails[InputParameters].load(InputParameters)
66
-
67
- # Usage
68
- job_details.input_parameters.foobar
69
- ```
70
-
71
- ```python
72
- from dataclasses import dataclass
73
- from oceanprotocol_job_details import JobDetails
63
+ from pydantic import BaseModel
64
+ from oceanprotocol_job_details import load_job_details
74
65
 
75
66
 
76
- @dataclass
77
- class Foo:
67
+ class Foo(BaseModel):
78
68
  bar: str
79
69
 
80
70
 
81
- @dataclass
82
- class InputParameters:
71
+ class InputParameters(BaseModel):
83
72
  # Allows for nested types
84
73
  foo: Foo
85
74
 
86
75
 
87
- job_details = JobDetails[InputParameters].load(InputParameters)
76
+ job_details = load_job_details({}, InputParameters)
88
77
 
89
78
  # Usage
79
+ job_details.input_parameters.foo
90
80
  job_details.input_parameters.foo.bar
91
81
  ```
92
82
 
93
- The values to fill the custom `InputParameters` will be parsed from the `algoCustomData.json` located next to the input data directories.
83
+ The values to fill the custom `InputParameters` will be parsed from the `algoCustomData.json` located next to the input data directories.
94
84
 
95
85
  ### Iterating Input Files the clean way
96
86
 
97
87
  ```python
98
- from oceanprotocol_job_details import JobDetails
88
+ from oceanprotocol_job_details import load_job_details
99
89
 
100
90
 
101
- job_details = JobDetails.load()
91
+ job_details = load_job_details
102
92
 
103
- for idx, file_path in job_details.next_file():
93
+ for idx, file_path in job_details.inputs():
104
94
  ...
105
95
 
106
- # Or if you just want one file path
107
- _, file_path = job_details.next_file()
96
+ _, file_path = next(job_details.inputs())
108
97
  ```
109
98
 
110
99
  ## OceanProtocol Structure
@@ -122,4 +111,4 @@ data # Root /data directory
122
111
  └── outputs # Algorithm output files dir
123
112
  ```
124
113
 
125
- > **_Note:_** Even though it's possible that the algorithm is passed multiple datasets, right now the implementation only allows to use **one dataset** per algorithm execution, so **normally** the executing job will only have **one ddo**, **one dir** inside inputs, and **one data file** named `0`.
114
+ > **_Note:_** Even though it's possible that the algorithm is passed multiple datasets, right now the implementation only allows to use **one dataset** per algorithm execution, so **normally** the executing job will only have **one ddo**, **one dir** inside inputs, and **one data file** named `0`.
@@ -0,0 +1,16 @@
1
+ oceanprotocol_job_details/__init__.py,sha256=nJMrZsEC5F1n9WF-v5QV095Yyc8UkhFw0AzD9o7X0IE,162
2
+ oceanprotocol_job_details/di.py,sha256=lsogbmjvmPfkd0mjLvn9vYLIZebwJm5RNraWt7WE5LA,1316
3
+ oceanprotocol_job_details/domain.py,sha256=2_USbeA_7VIEYS8DVb2MW6dCZasjiqIxQaGUnNUKspY,3851
4
+ oceanprotocol_job_details/helpers.py,sha256=ubH_KjAROqYvn0mkbA0-89vpdKIhVNGZ0h9pQLfPNow,1045
5
+ oceanprotocol_job_details/ocean.py,sha256=q8rgT5ycA2ifey3XNhUW0bcJwfMp7hpKU-6EVDeKV1o,1620
6
+ oceanprotocol_job_details/settings.py,sha256=zgIYPzaXjsgcmuhT7L2ipSP-2eNaodugHZr0rn2Z420,1248
7
+ oceanprotocol_job_details/loaders/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ oceanprotocol_job_details/loaders/loader.py,sha256=36X2s_0lN89kCUpItxEXfIzuBBNJySebP2B_tdWK2E0,186
9
+ oceanprotocol_job_details/loaders/impl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ oceanprotocol_job_details/loaders/impl/ddo.py,sha256=XthrQFhmP85XSVzVjBlLePtTowGR3BAsmVp3jngiQ08,668
11
+ oceanprotocol_job_details/loaders/impl/files.py,sha256=Y2vFBT2T9w9zrdpmf550-LQJxwtNPUGa0UU6bBzk9AU,1145
12
+ oceanprotocol_job_details/loaders/impl/job_details.py,sha256=7mEdeTo-cmsWuqWPdN7btjLjo6p5Oa1_acjSLWL5tb8,697
13
+ oceanprotocol_job_details-0.3.3.dist-info/METADATA,sha256=pDXdsInFTf36wYjsqWSd_ygeCDggozjueHlhF6OyvlQ,4416
14
+ oceanprotocol_job_details-0.3.3.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
15
+ oceanprotocol_job_details-0.3.3.dist-info/licenses/LICENSE,sha256=ni3ix7P_GxK1W3VGC4fJ3o6QoCngCEpSuTJwO4nkpbw,1055
16
+ oceanprotocol_job_details-0.3.3.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,38 +0,0 @@
1
- from dataclasses import InitVar, dataclass, field
2
- from pathlib import Path
3
-
4
-
5
- @dataclass
6
- class Paths:
7
- """Configuration class for the Ocean Protocol Job Details"""
8
-
9
- base_dir: InitVar[Path | None]
10
-
11
- _base: Path = field(init=False)
12
-
13
- def __post_init__(self, base_dir: str | Path | None) -> None:
14
- self._base = Path(base_dir) if base_dir else Path("/data")
15
-
16
- @property
17
- def data(self) -> Path:
18
- return self._base
19
-
20
- @property
21
- def inputs(self) -> Path:
22
- return self.data / "inputs"
23
-
24
- @property
25
- def ddos(self) -> Path:
26
- return self.data / "ddos"
27
-
28
- @property
29
- def outputs(self) -> Path:
30
- return self.data / "outputs"
31
-
32
- @property
33
- def logs(self) -> Path:
34
- return self.data / "logs"
35
-
36
- @property
37
- def algorithm_custom_parameters(self) -> Path:
38
- return self.inputs / "algoCustomData.json"
@@ -1,14 +0,0 @@
1
- oceanprotocol_job_details/__init__.py,sha256=C67wv7fy5ZT5FtbGD-oQeSoLU-x6e2ts-820koFo034,55
2
- oceanprotocol_job_details/di.py,sha256=PE6FGLRZPLDx-J7FfutCNES7sqpZA8e6jdu4tNe-AjQ,1210
3
- oceanprotocol_job_details/ocean.py,sha256=t1BP4bdIcNgzEkXuVM7g9Bg0pEz_ljctE2CQEtSPnwA,7431
4
- oceanprotocol_job_details/paths.py,sha256=o2IwhKO8nxIp03-NXYUhhWC7XPiMjz61AACStZRcJX0,877
5
- oceanprotocol_job_details/loaders/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- oceanprotocol_job_details/loaders/loader.py,sha256=HIzsVKCuGP7ghfM7ppN3ANVybvsA64wr3h8I68mqS6A,195
7
- oceanprotocol_job_details/loaders/impl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- oceanprotocol_job_details/loaders/impl/ddo.py,sha256=SuPvQBc2MS-q0g8BQOfCXPAvMvhl7Vnbq9GHugAU--s,828
9
- oceanprotocol_job_details/loaders/impl/files.py,sha256=ZiIb-gblgIqU5HBy0mrZwooH-Qu_HSWyaJ3VtBIhJcs,1696
10
- oceanprotocol_job_details/loaders/impl/job_details.py,sha256=wf0xNAG4tESq57vqkdtMQ8BdiyS91j5f7FL8Gfwbjh4,770
11
- oceanprotocol_job_details-0.2.7.dist-info/METADATA,sha256=uuxmkYSbZFKGb01Ng_miulyrUv4cx0Djv0Obte575dk,4507
12
- oceanprotocol_job_details-0.2.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
13
- oceanprotocol_job_details-0.2.7.dist-info/licenses/LICENSE,sha256=ni3ix7P_GxK1W3VGC4fJ3o6QoCngCEpSuTJwO4nkpbw,1055
14
- oceanprotocol_job_details-0.2.7.dist-info/RECORD,,