ocean-runner 0.2.2__py3-none-any.whl → 0.2.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.
- ocean_runner/config.py +23 -8
- ocean_runner/runner.py +40 -11
- ocean_runner/runtime_mode.py +6 -0
- {ocean_runner-0.2.2.dist-info → ocean_runner-0.2.3.dist-info}/METADATA +1 -1
- ocean_runner-0.2.3.dist-info/RECORD +8 -0
- ocean_runner-0.2.2.dist-info/RECORD +0 -7
- {ocean_runner-0.2.2.dist-info → ocean_runner-0.2.3.dist-info}/WHEEL +0 -0
- {ocean_runner-0.2.2.dist-info → ocean_runner-0.2.3.dist-info}/licenses/LICENSE +0 -0
ocean_runner/config.py
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
|
+
import os
|
|
1
2
|
from dataclasses import asdict, dataclass, field
|
|
2
3
|
from logging import Logger
|
|
3
|
-
import os
|
|
4
4
|
from pathlib import Path
|
|
5
|
-
from typing import Callable, Iterable, TypeVar
|
|
5
|
+
from typing import Callable, Iterable, Literal, TypeVar
|
|
6
6
|
|
|
7
7
|
T = TypeVar("T")
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
@dataclass
|
|
10
|
+
@dataclass
|
|
11
11
|
class Environment:
|
|
12
12
|
"""Environment variables mock"""
|
|
13
13
|
|
|
14
|
-
base_dir: str | None = field(
|
|
14
|
+
base_dir: str | None = field(
|
|
15
|
+
default_factory=lambda: os.environ.get("BASE_DIR", None),
|
|
16
|
+
)
|
|
15
17
|
"""Base data directory, defaults to '/data'"""
|
|
16
18
|
|
|
17
|
-
dids: str = field(
|
|
19
|
+
dids: str = field(
|
|
20
|
+
default_factory=lambda: os.environ.get("DIDS"),
|
|
21
|
+
)
|
|
18
22
|
"""Datasets DID's, format: '["XXXX"]'"""
|
|
19
23
|
|
|
20
|
-
transformation_did: str = field(
|
|
24
|
+
transformation_did: str = field(
|
|
25
|
+
default_factory=lambda: os.environ.get("TRANSFORMATION_DID"),
|
|
26
|
+
)
|
|
21
27
|
"""Transformation (algorithm) DID"""
|
|
22
28
|
|
|
23
|
-
secret: str = field(
|
|
29
|
+
secret: str = field(
|
|
30
|
+
default_factory=lambda: os.environ.get("SECRET"),
|
|
31
|
+
)
|
|
24
32
|
"""Super secret secret"""
|
|
25
33
|
|
|
34
|
+
runtime: Literal["dev", "test"] = field(
|
|
35
|
+
default_factory=lambda: os.environ.get("RUNTIME", "dev").lower()
|
|
36
|
+
)
|
|
37
|
+
"""Select runtime mode"""
|
|
38
|
+
|
|
26
39
|
dict = asdict
|
|
27
40
|
|
|
28
41
|
|
|
@@ -44,5 +57,7 @@ class Config:
|
|
|
44
57
|
)
|
|
45
58
|
"""Paths that should be included so the code executes correctly"""
|
|
46
59
|
|
|
47
|
-
environment: Environment =
|
|
60
|
+
environment: Environment = field(
|
|
61
|
+
default_factory=lambda: Environment(),
|
|
62
|
+
)
|
|
48
63
|
"""Mock of environment data"""
|
ocean_runner/runner.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
from dataclasses import InitVar, asdict, dataclass, field
|
|
2
4
|
from logging import Logger
|
|
3
5
|
from pathlib import Path
|
|
@@ -6,6 +8,7 @@ from typing import Callable, Generic, Self, TypeVar
|
|
|
6
8
|
from oceanprotocol_job_details import JobDetails
|
|
7
9
|
|
|
8
10
|
from ocean_runner.config import Config
|
|
11
|
+
from ocean_runner.runtime_mode import RuntimeMode
|
|
9
12
|
|
|
10
13
|
JobDetailsT = TypeVar(
|
|
11
14
|
"JobDetailsT",
|
|
@@ -13,24 +16,37 @@ JobDetailsT = TypeVar(
|
|
|
13
16
|
ResultT = TypeVar("ResultT")
|
|
14
17
|
|
|
15
18
|
|
|
16
|
-
def default_error_callback(e: Exception) -> None:
|
|
19
|
+
def default_error_callback(_: Algorithm, e: Exception) -> None:
|
|
17
20
|
raise e
|
|
18
21
|
|
|
19
22
|
|
|
20
|
-
def default_validation(algorithm:
|
|
23
|
+
def default_validation(algorithm: Algorithm) -> None:
|
|
21
24
|
algorithm.logger.info("Validating input using default validation")
|
|
22
25
|
|
|
23
26
|
assert algorithm.job_details.ddos, "DDOs missing"
|
|
24
27
|
assert algorithm.job_details.files, "Files missing"
|
|
25
28
|
|
|
26
29
|
|
|
27
|
-
def default_save(*, result: ResultT, base: Path, algorithm:
|
|
30
|
+
def default_save(*, result: ResultT, base: Path, algorithm: Algorithm) -> None:
|
|
28
31
|
algorithm.logger.info("Saving results using default save")
|
|
29
32
|
|
|
30
33
|
with open(base / "result.txt", "w+") as f:
|
|
31
34
|
f.write(str(result))
|
|
32
35
|
|
|
33
36
|
|
|
37
|
+
def default_test_run(algorithm: Algorithm) -> int:
|
|
38
|
+
import pytest
|
|
39
|
+
|
|
40
|
+
result = pytest.main()
|
|
41
|
+
|
|
42
|
+
if result == 0:
|
|
43
|
+
algorithm.logger.info("Passed all tests")
|
|
44
|
+
else:
|
|
45
|
+
algorithm.logger.error("Some tests failed")
|
|
46
|
+
|
|
47
|
+
return result
|
|
48
|
+
|
|
49
|
+
|
|
34
50
|
@dataclass
|
|
35
51
|
class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
36
52
|
|
|
@@ -38,16 +54,18 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
|
38
54
|
|
|
39
55
|
# Load from config
|
|
40
56
|
logger: Logger = field(init=False)
|
|
41
|
-
error_callback: Callable[[Exception], None] = field(init=False)
|
|
42
57
|
|
|
43
58
|
_job_details: JobDetails[JobDetailsT] = field(init=False)
|
|
44
59
|
_result: ResultT | None = field(default=None, init=False)
|
|
60
|
+
_runtime: RuntimeMode = field(default=RuntimeMode.DEV, init=False)
|
|
61
|
+
|
|
62
|
+
error_callback = default_error_callback
|
|
45
63
|
|
|
46
64
|
def __post_init__(self, config: Config | None) -> None:
|
|
47
|
-
config = config or Config()
|
|
65
|
+
config: Config = config or Config()
|
|
48
66
|
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
if config.error_callback:
|
|
68
|
+
self.error_callback = config.error_callback
|
|
51
69
|
|
|
52
70
|
if config.logger:
|
|
53
71
|
self.logger = config.logger
|
|
@@ -71,9 +89,14 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
|
71
89
|
sys.path.extend([str(path.absolute()) for path in config.source_paths])
|
|
72
90
|
self.logger.debug(f"Added [{len(config.source_paths)}] entries to PATH")
|
|
73
91
|
|
|
92
|
+
self._runtime = RuntimeMode(config.environment.runtime) or self._runtime
|
|
93
|
+
|
|
74
94
|
self._job_details = JobDetails.load(
|
|
75
|
-
config.custom_input,
|
|
76
|
-
|
|
95
|
+
_type=config.custom_input,
|
|
96
|
+
base_dir=config.environment.base_dir,
|
|
97
|
+
dids=config.environment.dids,
|
|
98
|
+
transformation_did=config.environment.transformation_did,
|
|
99
|
+
secret=config.environment.secret,
|
|
77
100
|
)
|
|
78
101
|
|
|
79
102
|
self.logger.info("Loaded JobDetails")
|
|
@@ -103,8 +126,11 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
|
103
126
|
return self
|
|
104
127
|
|
|
105
128
|
def run(self, callable: Callable[[Self], ResultT]) -> Self:
|
|
106
|
-
self.logger.info("Running algorithm
|
|
129
|
+
self.logger.info("Running algorithm...")
|
|
107
130
|
try:
|
|
131
|
+
if self._runtime == RuntimeMode.TEST:
|
|
132
|
+
callable = default_test_run
|
|
133
|
+
|
|
108
134
|
self._result = callable(self)
|
|
109
135
|
except Exception as e:
|
|
110
136
|
self.error_callback(e)
|
|
@@ -113,7 +139,7 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
|
113
139
|
|
|
114
140
|
def save_results(
|
|
115
141
|
self,
|
|
116
|
-
callable: Callable[[ResultT, Path,
|
|
142
|
+
callable: Callable[[ResultT, Path, Algorithm], None] = default_save,
|
|
117
143
|
) -> None:
|
|
118
144
|
self.logger.info("Saving results...")
|
|
119
145
|
try:
|
|
@@ -124,3 +150,6 @@ class Algorithm(Generic[JobDetailsT, ResultT]):
|
|
|
124
150
|
)
|
|
125
151
|
except Exception as e:
|
|
126
152
|
self.error_callback(e)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
__all__ = [Algorithm]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
ocean_runner/__init__.py,sha256=awAmE6kZhuwcrD3gT7qFZArdhiuzW-EFTA6tGKhw06k,138
|
|
2
|
+
ocean_runner/config.py,sha256=zzpGxotGhGnNdHKFJqQ7fuXK5zW2IGOpzftHilcySD4,1644
|
|
3
|
+
ocean_runner/runner.py,sha256=n5odQp11yUllkOXrrwYsnQGRGXmCJCh0cQhPR81am94,4503
|
|
4
|
+
ocean_runner/runtime_mode.py,sha256=WbGTaoL3hxBWbxM8luwyOwwtQonqyIhbus0_Jd-F-3k,83
|
|
5
|
+
ocean_runner-0.2.3.dist-info/METADATA,sha256=ukIdHO2uumLlDyunACWc8UV-mPE_UsfmVEncLrqNTaQ,5781
|
|
6
|
+
ocean_runner-0.2.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
ocean_runner-0.2.3.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
|
|
8
|
+
ocean_runner-0.2.3.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
ocean_runner/__init__.py,sha256=awAmE6kZhuwcrD3gT7qFZArdhiuzW-EFTA6tGKhw06k,138
|
|
2
|
-
ocean_runner/config.py,sha256=9XSDHuNY5IspDi5ZZKmt2HX6yrI9gzeogCw-ATMCATs,1323
|
|
3
|
-
ocean_runner/runner.py,sha256=r02O2QHSc_eij1zI8soYzvjPXA8R-Khvg2XyCj58C-M,3770
|
|
4
|
-
ocean_runner-0.2.2.dist-info/METADATA,sha256=d2t6-WErsJPnmrwJr8CJvNpUrDlj3aDhm-3NC2zyS_k,5781
|
|
5
|
-
ocean_runner-0.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
6
|
-
ocean_runner-0.2.2.dist-info/licenses/LICENSE,sha256=_B25KqK4amoADWkMN150tnZFm_Fy7VvZpvIC8ZydWdI,1053
|
|
7
|
-
ocean_runner-0.2.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|