forked from openml/openml-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversions.py
More file actions
262 lines (209 loc) 路 7.82 KB
/
Copy pathversions.py
File metadata and controls
262 lines (209 loc) 路 7.82 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from __future__ import annotations
from collections.abc import Mapping
from typing import Any, cast
import xmltodict
from openml.enums import APIVersion, ResourceType
from openml.exceptions import (
OpenMLServerException,
)
from .base import ResourceAPI
_LEGAL_RESOURCES_DELETE = [
ResourceType.DATASET,
ResourceType.TASK,
ResourceType.FLOW,
ResourceType.STUDY,
ResourceType.RUN,
ResourceType.USER,
]
_LEGAL_RESOURCES_TAG = [
ResourceType.DATASET,
ResourceType.TASK,
ResourceType.FLOW,
ResourceType.STUDY,
ResourceType.SETUP,
ResourceType.RUN,
]
class ResourceV1API(ResourceAPI):
"""
Version 1 implementation of the OpenML resource API.
This class provides XML-based implementations for publishing,
deleting, tagging, and untagging resources using the V1 API
endpoints. Responses are parsed using ``xmltodict``.
Notes
-----
V1 endpoints expect and return XML. Error handling follows the
legacy OpenML server behavior and maps specific error codes to
more descriptive exceptions where appropriate.
"""
api_version: APIVersion = APIVersion.V1
def publish(self, path: str, files: Mapping[str, Any] | None) -> int:
"""
Publish a new resource using the V1 API.
Parameters
----------
path : str
API endpoint path for the upload.
files : Mapping of str to Any or None
Files to upload as part of the request payload.
Returns
-------
int
Identifier of the newly created resource.
Raises
------
ValueError
If the server response does not contain a valid resource ID.
OpenMLServerException
If the server returns an error during upload.
"""
response = self._http.post(path, files=files)
parsed_response = xmltodict.parse(response.content)
return self._extract_id_from_upload(parsed_response)
def delete(self, resource_id: int) -> bool:
"""
Delete a resource using the V1 API.
Parameters
----------
resource_id : int
Identifier of the resource to delete.
Returns
-------
bool
``True`` if the server confirms successful deletion.
Raises
------
ValueError
If the resource type is not supported for deletion.
OpenMLNotAuthorizedError
If the user is not permitted to delete the resource.
OpenMLServerError
If deletion fails for an unknown reason.
OpenMLServerException
For other server-side errors.
"""
if self.resource_type not in _LEGAL_RESOURCES_DELETE:
raise ValueError(f"Can't delete a {self.resource_type.value}")
endpoint_name = self._get_endpoint_name()
path = f"{endpoint_name}/{resource_id}"
try:
response = self._http.delete(path)
result = xmltodict.parse(response.content)
return f"oml:{endpoint_name}_delete" in result
except OpenMLServerException as e:
self._handle_delete_exception(endpoint_name, e)
raise
def tag(self, resource_id: int, tag: str) -> list[str]:
"""
Add a tag to a resource using the V1 API.
Parameters
----------
resource_id : int
Identifier of the resource to tag.
tag : str
Tag to associate with the resource.
Returns
-------
list of str
Updated list of tags assigned to the resource.
Raises
------
ValueError
If the resource type does not support tagging.
OpenMLServerException
If the server returns an error.
"""
if self.resource_type not in _LEGAL_RESOURCES_TAG:
raise ValueError(f"Can't tag a {self.resource_type.value}")
endpoint_name = self._get_endpoint_name()
path = f"{endpoint_name}/tag"
data = {f"{endpoint_name}_id": resource_id, "tag": tag}
response = self._http.post(path, data=data)
parsed_response = xmltodict.parse(response.content, force_list={"oml:tag"})
result = parsed_response[f"oml:{endpoint_name}_tag"]
tags: list[str] = result.get("oml:tag", [])
return tags
def untag(self, resource_id: int, tag: str) -> list[str]:
"""
Remove a tag from a resource using the V1 API.
Parameters
----------
resource_id : int
Identifier of the resource to untag.
tag : str
Tag to remove from the resource.
Returns
-------
list of str
Updated list of tags assigned to the resource.
Raises
------
ValueError
If the resource type does not support tagging.
OpenMLServerException
If the server returns an error.
"""
if self.resource_type not in _LEGAL_RESOURCES_TAG:
raise ValueError(f"Can't untag a {self.resource_type.value}")
endpoint_name = self._get_endpoint_name()
path = f"{endpoint_name}/untag"
data = {f"{endpoint_name}_id": resource_id, "tag": tag}
response = self._http.post(path, data=data)
parsed_response = xmltodict.parse(response.content, force_list={"oml:tag"})
result = parsed_response[f"oml:{endpoint_name}_untag"]
tags: list[str] = result.get("oml:tag", [])
return tags
def _get_endpoint_name(self) -> str:
if self.resource_type == ResourceType.DATASET:
return "data"
return cast("str", self.resource_type.value)
def _extract_id_from_upload(self, parsed: Mapping[str, Any]) -> int:
"""
Extract the resource identifier from an XML upload response.
Parameters
----------
parsed : Mapping of str to Any
Parsed XML response as returned by ``xmltodict.parse``.
Returns
-------
int
Extracted resource identifier.
Raises
------
ValueError
If the response structure is unexpected or no identifier
can be found.
"""
# reads id from upload response
# actual parsed dict: {"oml:upload_flow": {"@xmlns:oml": "...", "oml:id": "42"}}
# xmltodict always gives exactly one root key
((_, root_value),) = parsed.items()
if not isinstance(root_value, Mapping):
raise ValueError("Unexpected XML structure")
# Look for oml:id directly in the root value
if "oml:id" in root_value:
id_value = root_value["oml:id"]
if isinstance(id_value, (str, int)):
return int(id_value)
# Fallback: check all values for numeric/string IDs
for v in root_value.values():
if isinstance(v, (str, int)):
return int(v)
raise ValueError("No ID found in upload response")
class ResourceV2API(ResourceAPI):
"""
Version 2 implementation of the OpenML resource API.
This class represents the V2 API for resources. Operations such as
publishing, deleting, tagging, and untagging are currently not
supported and will raise ``OpenMLNotSupportedError``.
"""
api_version: APIVersion = APIVersion.V2
def publish(self, path: str, files: Mapping[str, Any] | None) -> int: # noqa: ARG002
self._not_supported(method="publish")
def delete(self, resource_id: int) -> bool: # noqa: ARG002
self._not_supported(method="delete")
def tag(self, resource_id: int, tag: str) -> list[str]: # noqa: ARG002
self._not_supported(method="tag")
def untag(self, resource_id: int, tag: str) -> list[str]: # noqa: ARG002
self._not_supported(method="untag")
def _get_endpoint_name(self) -> str:
return cast("str", self.resource_type.value)