-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
244 lines (199 loc) · 7.66 KB
/
base.py
File metadata and controls
244 lines (199 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
Simvue Artifact
===============
Class for defining and interacting with artifact objects.
"""
import datetime
import http
import io
import typing
import pydantic
try:
from typing import Self
except ImportError:
from typing_extensions import Self # noqa: F401
from simvue.api.url import URL
from simvue.exception import ObjectNotFoundError
from simvue.models import DATETIME_FORMAT
from simvue.api.objects.base import SimvueObject, staging_check, write_only
from simvue.api.objects.run import Run
from simvue.api.request import (
put as sv_put,
get_json_from_response,
post as sv_post,
get as sv_get,
)
Category = typing.Literal["code", "input", "output"]
UPLOAD_TIMEOUT: int = 30
DOWNLOAD_TIMEOUT: int = 30
DOWNLOAD_CHUNK_SIZE: int = 8192
class ArtifactBase(SimvueObject):
"""Connect to/create an artifact locally or on the server"""
def __init__(
self, identifier: str | None = None, _read_only: bool = True, **kwargs
) -> None:
self._label = "artifact"
self._endpoint = f"{self._label}s"
super().__init__(identifier=identifier, _read_only=_read_only, **kwargs)
# If the artifact is an online instance, need a place to store the response
# from the initial creation
self._init_data: dict[str, dict] = {}
def commit(self) -> None:
self._logger.info("Cannot call method 'commit' on write-once type 'Artifact'")
def attach_to_run(self, run_id: str, category: Category) -> None:
"""Attach this artifact to a given run"""
self._init_data["runs"][run_id] = category
if self._offline:
self._staging["runs"] = self._init_data["runs"]
super().commit()
return
_run_artifacts_url = (
URL(self._user_config.server.url)
/ f"runs/{run_id}/artifacts/{self._init_data['id']}"
)
_response = sv_put(
url=f"{_run_artifacts_url}",
headers=self._headers,
json={"category": category},
)
get_json_from_response(
expected_status=[http.HTTPStatus.OK],
scenario=f"adding artifact '{self.name}' to run '{run_id}'",
response=_response,
)
def on_reconnect(self, id_mapping: dict[str, str]) -> None:
_offline_staging = self._init_data["runs"].copy()
for id, category in _offline_staging.items():
self.attach_to_run(run_id=id_mapping[id], category=category)
def _upload(self, file: io.BytesIO) -> None:
if self._offline:
super().commit()
return
if not (_url := self._staging.get("url")):
return
_name = self._staging["name"]
_response = sv_post(
url=_url,
headers={},
params={},
is_json=False,
files={"file": file},
data=self._init_data.get("fields"),
)
self._logger.debug(
"Got status code %d when uploading artifact",
_response.status_code,
)
get_json_from_response(
expected_status=[http.HTTPStatus.OK, http.HTTPStatus.NO_CONTENT],
allow_parse_failure=True, # JSON response from S3 not parsible
scenario=f"uploading artifact '{_name}' to object storage",
response=_response,
)
# Temporarily remove read-only state
self.read_only(False)
# Update the server status to confirm file uploaded
self.uploaded = True
super().commit()
self.read_only(True)
def _get(
self, storage: str | None = None, url: str | None = None, **kwargs
) -> dict[str, typing.Any]:
return super()._get(
storage=storage or self._staging.get("server", {}).get("storage_id"),
url=url,
**kwargs,
)
@property
def checksum(self) -> str:
"""Retrieve the checksum for this artifact"""
return self._get_attribute("checksum")
@property
def storage_url(self) -> URL | None:
"""Retrieve upload URL for artifact"""
return URL(_url) if (_url := self._init_data.get("url")) else None
@property
def original_path(self) -> str:
"""Retrieve the original path of the file associated with this artifact"""
return self._get_attribute("original_path")
@property
def storage_id(self) -> str | None:
"""Retrieve the storage identifier for this artifact"""
return self._get_attribute("storage_id")
@property
def mime_type(self) -> str:
"""Retrieve the MIME type for this artifact"""
return self._get_attribute("mime_type")
@property
def size(self) -> int:
"""Retrieve the size for this artifact in bytes"""
return self._get_attribute("size")
@property
def name(self) -> str | None:
"""Retrieve name for the artifact"""
return self._get_attribute("name")
@property
def created(self) -> datetime.datetime | None:
"""Retrieve created datetime for the artifact"""
_created: str | None = self._get_attribute("created")
return (
datetime.datetime.strptime(_created, DATETIME_FORMAT) if _created else None
)
@property
@staging_check
def uploaded(self) -> bool:
"""Returns whether a file was uploaded for this artifact."""
return self._get_attribute("uploaded")
@uploaded.setter
@write_only
@pydantic.validate_call
def uploaded(self, is_uploaded: bool) -> None:
"""Set if a file was successfully uploaded for this artifact."""
self._staging["uploaded"] = is_uploaded
@property
def download_url(self) -> URL | None:
"""Retrieve the URL for downloading this artifact"""
return self._get_attribute("url")
@property
def runs(self) -> typing.Generator[str, None, None]:
"""Retrieve all runs for which this artifact is related"""
for _id, _ in Run.get(filters=[f"artifact.id == {self.id}"]):
yield _id
def get_category(self, run_id: str) -> Category:
"""Retrieve the category of this artifact with respect to a given run"""
_run_url = (
URL(self._user_config.server.url)
/ f"runs/{run_id}/artifacts/{self._identifier}"
)
_response = sv_get(url=_run_url, header=self._headers)
_json_response = get_json_from_response(
response=_response,
expected_status=[http.HTTPStatus.OK, http.HTTPStatus.NOT_FOUND],
scenario=f"Retrieval of category for artifact '{self._identifier}' with respect to run '{run_id}'",
)
if _response.status_code == http.HTTPStatus.NOT_FOUND:
raise ObjectNotFoundError(
self._label, self._identifier, extra=f"for run '{run_id}'"
)
return _json_response["category"]
@pydantic.validate_call
def download_content(self) -> typing.Generator[bytes, None, None]:
"""Stream artifact content"""
if not self.download_url:
raise ValueError(
f"Could not retrieve URL for artifact '{self._identifier}'"
)
_response = sv_get(
f"{self.download_url}", timeout=DOWNLOAD_TIMEOUT, headers=None
)
get_json_from_response(
response=_response,
allow_parse_failure=True,
expected_status=[http.HTTPStatus.OK],
scenario=f"Retrieval of file for {self._label} '{self._identifier}'",
)
_total_length: str | None = _response.headers.get("content-length")
if _total_length is None:
yield _response.content
else:
yield from _response.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE)