oceanprotocol-job-details 0.2.7__py3-none-any.whl → 0.3.9__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,23 @@
1
+ from types import NoneType
2
+ from typing import Generic, TypeVar
1
3
  from dependency_injector import containers, providers
4
+ from pydantic import BaseModel
2
5
 
3
6
  from oceanprotocol_job_details.loaders.impl.ddo import DDOLoader
4
7
  from oceanprotocol_job_details.loaders.impl.files import FilesLoader
5
8
  from oceanprotocol_job_details.loaders.impl.job_details import JobDetailsLoader
6
- from oceanprotocol_job_details.paths import Paths
9
+ from oceanprotocol_job_details.domain import Paths
7
10
 
8
11
 
9
- class Container(containers.DeclarativeContainer):
12
+ InputParametersT = TypeVar("InputParametersT", BaseModel, None)
10
13
 
14
+
15
+ class Container(containers.DeclarativeContainer, Generic[InputParametersT]):
11
16
  config = providers.Configuration()
12
17
 
13
- paths = providers.Singleton(
14
- Paths,
15
- base_dir=config.base_dir,
16
- )
18
+ paths = providers.Singleton(Paths, base_dir=config.base_dir)
17
19
 
18
- file_loader = providers.Factory(
20
+ file_loader = providers.Singleton(
19
21
  FilesLoader,
20
22
  dids=config.dids,
21
23
  transformation_did=config.transformation_did,
@@ -23,26 +25,16 @@ class Container(containers.DeclarativeContainer):
23
25
  logger=config.logger,
24
26
  )
25
27
 
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,
28
+ files = providers.Factory(lambda loader: loader.load(), loader=file_loader)
29
+ ddo_loader = providers.Factory(DDOLoader, files=files)
30
+ ddos = providers.Factory(lambda loader: loader.load(), loader=ddo_loader)
31
+
32
+ job_details_loader: providers.Factory[JobDetailsLoader[InputParametersT]] = (
33
+ providers.Factory(
34
+ JobDetailsLoader,
35
+ files=files,
36
+ secret=config.secret,
37
+ paths=paths,
38
+ ddos=ddos,
39
+ )
48
40
  )
@@ -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", BaseModel, None)
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] | None = None,
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,29 @@
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 types import NoneType
3
+ from typing import Generic, Type, TypeVar, final
7
4
 
8
- if TYPE_CHECKING:
9
- from oceanprotocol_job_details.ocean import DDO, Files, JobDetails
5
+ from pydantic import BaseModel
10
6
 
7
+ from oceanprotocol_job_details.domain import DDO, Files, Paths
8
+ from oceanprotocol_job_details.ocean import JobDetails
11
9
 
12
- T = TypeVar("T")
10
+ T = TypeVar("T", BaseModel, None)
13
11
 
14
12
 
15
13
  @final
16
14
  @dataclass(frozen=True)
17
15
  class JobDetailsLoader(Generic[T]):
18
-
19
- _type: Type[T] = field(repr=False)
20
-
16
+ input_type: Type[T] = field(repr=False)
21
17
  files: Files
22
18
  secret: str
23
19
  paths: Paths
24
20
  ddos: list[DDO]
25
21
 
26
22
  def load(self) -> JobDetails[T]:
27
- from oceanprotocol_job_details.ocean import JobDetails
28
-
29
- return JobDetails(
23
+ return JobDetails[T](
30
24
  files=self.files,
31
25
  secret=self.secret,
32
26
  ddos=self.ddos,
33
27
  paths=self.paths,
34
- _type=self._type,
28
+ input_type=self.input_type,
35
29
  )
@@ -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,48 @@
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 types import NoneType
7
+ from typing import Generator, Generic, Tuple, Type, TypeVar, final
19
8
 
20
- import orjson
21
- from dataclasses_json import config as dc_config
22
- from dataclasses_json import dataclass_json
9
+ import aiofiles
10
+ from pydantic import BaseModel, ConfigDict, Secret
23
11
 
24
- from oceanprotocol_job_details.di import Container
25
- from oceanprotocol_job_details.paths import Paths
12
+ from oceanprotocol_job_details.domain import DDO, Files, Paths
26
13
 
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
14
+ InputParametersT = TypeVar("InputParametersT", BaseModel, None)
219
15
 
220
16
 
221
17
  @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]):
18
+ class JobDetails(BaseModel, Generic[InputParametersT]): # type: ignore[explicit-any]
231
19
  files: Files
232
- """The input filepaths"""
233
-
234
20
  ddos: list[DDO]
235
- """list of paths to the DDOs"""
236
-
237
21
  paths: Paths
238
- """Configuration paths"""
239
-
240
- # Store the type explicitly to avoid issues
241
- _type: Type[InputParemetersT] = field(repr=False)
242
-
243
- secret: str | None = None
244
- """Shh it's a secret"""
22
+ input_type: Type[InputParametersT] | None
23
+ secret: Secret[str] | None = None
245
24
 
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")
25
+ model_config = ConfigDict(arbitrary_types_allowed=True, frozen=True)
249
26
 
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)
27
+ def inputs(self) -> Generator[Tuple[int, Path], None, None]:
28
+ yield from (
29
+ (idx, file)
30
+ for idx, files in enumerate(self.files)
31
+ for file in files.input_files
32
+ )
254
33
 
255
34
  @cached_property
256
- def input_parameters(self) -> InputParemetersT:
257
- """Read the input parameters and return them in an instance of the dataclass InputParemetersT"""
258
-
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.
35
+ def input_parameters(self) -> InputParametersT | None:
36
+ return asyncio.run(self.ainput_parameters())
286
37
 
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.
38
+ async def ainput_parameters(self) -> InputParametersT | None:
39
+ if self.input_type is None:
40
+ return None
290
41
 
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
- )
42
+ path = self.paths.algorithm_custom_parameters
43
+ async with aiofiles.open(path) as f:
44
+ raw = await f.read()
312
45
 
313
- return container.job_details_loader(_type=_type).load()
46
+ raw = raw.strip()
47
+ assert raw is not None, f"Empty file {path}"
48
+ return self.input_type.model_validate_json(raw) # type: ignore
File without changes
@@ -0,0 +1,42 @@
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 | None) -> list[str]:
27
+ if v is None:
28
+ return []
29
+
30
+ if isinstance(v, str):
31
+ data = orjson.loads(v)
32
+ assert isinstance(data, list)
33
+ return data
34
+ return v
35
+
36
+ @model_validator(mode="after")
37
+ def validate_dids(self) -> Self:
38
+ if not self.dids:
39
+ self.dids.extend(
40
+ [f.name for f in (self.base_dir / "ddos").glob("*") if f.is_file()]
41
+ )
42
+ 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.9
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,27 @@ 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
+ ```
36
40
 
37
- As a simple library, we only need to import `JobDetails` and load it, it will:
41
+ ## Usage
38
42
 
39
- 1. Fetch the needed parameters to populate the `JobDetails` instance from the environment variables or use the passed values to the `load()` method.
40
- 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.
43
+ As a simple library, we only need to import `load_job_details` and run it. It will:
41
44
 
45
+ 1. Read from disk the needed parameters to populate the `JobDetails` from the given `base_dir`. Looking for the files corresponding to the passed DIDs in the filesystem according to the [Ocean Protocol Structure](#oceanprotocol-structure).
46
+ 2. If given a `InputParameters` type that inherits from `pydantic.BaseModel`, it will create an instance from the environment variables.
42
47
 
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
+ job_details = load_job_details({"base_dir": "...", "transformation_did": "..."})
49
54
  ```
50
55
 
51
56
  ### Custom Input Parameters
@@ -53,58 +58,40 @@ job_details = JobDetails.load()
53
58
  If our algorithm has custom input parameters and we want to load them into our algorithm, we can do it as follows:
54
59
 
55
60
  ```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
61
+ from pydantic import BaseModel
62
+ from oceanprotocol_job_details import load_job_details
74
63
 
75
64
 
76
- @dataclass
77
- class Foo:
65
+ class Foo(BaseModel):
78
66
  bar: str
79
67
 
80
68
 
81
- @dataclass
82
- class InputParameters:
69
+ class InputParameters(BaseModel):
83
70
  # Allows for nested types
84
71
  foo: Foo
85
72
 
86
73
 
87
- job_details = JobDetails[InputParameters].load(InputParameters)
74
+ job_details = load_job_details({"base_dir": "...", "transformation_did": "..."}, InputParameters)
88
75
 
89
76
  # Usage
77
+ job_details.input_parameters.foo
90
78
  job_details.input_parameters.foo.bar
91
79
  ```
92
80
 
93
- The values to fill the custom `InputParameters` will be parsed from the `algoCustomData.json` located next to the input data directories.
81
+ The values to fill the custom `InputParameters` will be parsed from the `algoCustomData.json` located next to the input data directories.
94
82
 
95
83
  ### Iterating Input Files the clean way
96
84
 
97
85
  ```python
98
- from oceanprotocol_job_details import JobDetails
86
+ from oceanprotocol_job_details import load_job_details
99
87
 
100
88
 
101
- job_details = JobDetails.load()
89
+ job_details = load_job_details(...)
102
90
 
103
- for idx, file_path in job_details.next_file():
91
+ for idx, file_path in job_details.inputs():
104
92
  ...
105
93
 
106
- # Or if you just want one file path
107
- _, file_path = job_details.next_file()
94
+ _, file_path = next(job_details.inputs())
108
95
  ```
109
96
 
110
97
  ## OceanProtocol Structure
@@ -122,4 +109,4 @@ data # Root /data directory
122
109
  └── outputs # Algorithm output files dir
123
110
  ```
124
111
 
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`.
112
+ > **_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,17 @@
1
+ oceanprotocol_job_details/__init__.py,sha256=nJMrZsEC5F1n9WF-v5QV095Yyc8UkhFw0AzD9o7X0IE,162
2
+ oceanprotocol_job_details/di.py,sha256=URBCcwla3pBKt4hWhRwG7s-Ib_KzoBk2-EtLzQOyAyM,1343
3
+ oceanprotocol_job_details/domain.py,sha256=2_USbeA_7VIEYS8DVb2MW6dCZasjiqIxQaGUnNUKspY,3851
4
+ oceanprotocol_job_details/helpers.py,sha256=ABm3oIRwPd-4XeCOIszCbfL2wkUJqVJJ2bqy3hR4jyw,1064
5
+ oceanprotocol_job_details/ocean.py,sha256=ocoI4OO8A5_SovXbASVucFq4W-tJr_M4C67UbS4aSF8,1473
6
+ oceanprotocol_job_details/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ oceanprotocol_job_details/settings.py,sha256=o_1Hn2vl5hMk7bAkdS7GjE4nKOAyHm7dScO2_o2sPuY,1345
8
+ oceanprotocol_job_details/loaders/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ oceanprotocol_job_details/loaders/loader.py,sha256=36X2s_0lN89kCUpItxEXfIzuBBNJySebP2B_tdWK2E0,186
10
+ oceanprotocol_job_details/loaders/impl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ oceanprotocol_job_details/loaders/impl/ddo.py,sha256=XthrQFhmP85XSVzVjBlLePtTowGR3BAsmVp3jngiQ08,668
12
+ oceanprotocol_job_details/loaders/impl/files.py,sha256=Y2vFBT2T9w9zrdpmf550-LQJxwtNPUGa0UU6bBzk9AU,1145
13
+ oceanprotocol_job_details/loaders/impl/job_details.py,sha256=QwlUaG9KozkI1wX66oDTPg4TjGkvSsi8O-TctF6eWvo,724
14
+ oceanprotocol_job_details-0.3.9.dist-info/METADATA,sha256=_6PvqdSeGHboxzX9RryS7-vnzPdnAWlozE290s0UCr4,4503
15
+ oceanprotocol_job_details-0.3.9.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
16
+ oceanprotocol_job_details-0.3.9.dist-info/licenses/LICENSE,sha256=ni3ix7P_GxK1W3VGC4fJ3o6QoCngCEpSuTJwO4nkpbw,1055
17
+ oceanprotocol_job_details-0.3.9.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,,