Skip to content

Commit 75a2f06

Browse files
committed
feat: update entrypoint repo
1 parent eef36c7 commit 75a2f06

File tree

10 files changed

+364
-300
lines changed

10 files changed

+364
-300
lines changed

src/dioptra/restapi/db/repository/entrypoints.py

Lines changed: 166 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@
2424
import dioptra.restapi.db.repository.utils as utils
2525
from dioptra.restapi.db.models import (
2626
EntryPoint,
27-
Experiment,
2827
Group,
2928
Plugin,
3029
Queue,
3130
Resource,
32-
ResourceSnapshot,
3331
Tag,
3432
)
35-
from dioptra.restapi.errors import EntityDoesNotExistError
3633

3734

3835
class EntrypointRepository:
@@ -82,7 +79,7 @@ def create(self, entrypoint: EntryPoint):
8279

8380
self.session.add(entrypoint)
8481

85-
def create_snapshot(self, entrypoint: Experiment):
82+
def create_snapshot(self, entrypoint: EntryPoint):
8683
"""
8784
Create a new entrypoint snapshot.
8885
@@ -150,7 +147,6 @@ def get_one(
150147
self,
151148
resource_id: int,
152149
deletion_policy: utils.DeletionPolicy,
153-
error_if_not_found=False,
154150
) -> EntryPoint:
155151
"""
156152
Get the latest snapshot of the given entrypoint resource; require that
@@ -173,15 +169,10 @@ def get_one(
173169
EntityDeletedError: if the entrypoint is deleted, but policy was to
174170
find a non-deleted entrypoint
175171
"""
176-
entrypoint = utils.get_one_latest_snapshot(
172+
return utils.get_one_latest_snapshot(
177173
self.session, EntryPoint, resource_id, deletion_policy
178174
)
179175

180-
if entrypoint is None and error_if_not_found:
181-
raise EntityDoesNotExistError("entry_point", entrypoint_id=resource_id)
182-
183-
return entrypoint
184-
185176
def get_one_snapshot(
186177
self,
187178
snapshot_id: int,
@@ -239,25 +230,23 @@ def get_by_name(
239230
self.session, EntryPoint, name, group, deletion_policy
240231
)
241232

242-
def get_entrypoints(
233+
def get_queues(
243234
self,
244235
entrypoint: EntryPoint | int,
245236
deletion_policy: utils.DeletionPolicy = utils.DeletionPolicy.NOT_DELETED,
246-
) -> Sequence[EntryPoint]:
237+
) -> Sequence[Queue]:
247238
"""
248-
Get the child entry points of the given entrypoint as their latest
239+
Get the child queues of the given entrypoint as their latest
249240
snapshots. If an EntryPoint object is given, the returned sequence
250241
may be shorter than len(entrypoint.children) if any of the children
251242
don't exist, or were filtered out due to deletion policy.
252243
253244
Args:
254-
entrypoint: An EntryPoint object or resource_id integer primary key
255-
value
256-
deletion_policy: Whether to look at deleted entry points,
257-
non-deleted entry points, or all entry points
245+
entrypoint: An EntryPoint object or resource_id integer primary key value
246+
deletion_policy: Whether to look at deleted, non-deleted, or all queues
258247
259248
Returns:
260-
The entry points
249+
The queues
261250
262251
Raises:
263252
EntityDoesNotExistError: if parent does not exist
@@ -266,11 +255,32 @@ def get_entrypoints(
266255

267256
return utils.get_latest_child_snapshots(
268257
self.session,
269-
EntryPoint,
258+
Queue,
270259
entrypoint,
271260
deletion_policy,
272261
)
273262

263+
def create_queues(
264+
self,
265+
entrypoint: EntryPoint,
266+
queues: Iterable[Queue | Resource | int],
267+
) -> Sequence[Queue]:
268+
"""
269+
Add the given entry points as children of the given experiment.
270+
271+
Args:
272+
entrypoint: An EntryPoint object
273+
queus: The queues to add as children
274+
275+
Returns:
276+
The list of queue children, as latest snapshots.
277+
278+
Raises:
279+
EntityDoesNotExistError: if parent or any new child does not exist
280+
EntityDeletedError: if parent or any new child is deleted
281+
"""
282+
return utils.create_resource_children(self.session, Queue, entrypoint, queues)
283+
274284
def add_queues(
275285
self,
276286
entrypoint: EntryPoint | int,
@@ -280,8 +290,8 @@ def add_queues(
280290
Add the given entry points as children of the given experiment.
281291
282292
Args:
283-
experiment: An Experiment object or resource_id integer primary key value
284-
children: The entry points to add
293+
entrypoint: An EntryPoint object or resource_id integer primary key value
294+
queus: The queues to add as children
285295
286296
Returns:
287297
The complete list of queue children, as latest snapshots (including both
@@ -294,6 +304,138 @@ def add_queues(
294304
snaps = utils.append_resource_children(self.session, Queue, entrypoint, queues)
295305
return [snap for snap in snaps if isinstance(snap, Queue)]
296306

307+
def set_queues(
308+
self,
309+
entrypoint: EntryPoint | int,
310+
queues: Iterable[Queue | Resource | int],
311+
) -> Sequence[Queue]:
312+
"""
313+
Set the child queues of the given entrypoint.
314+
This replaces all existing queues with the given resources.
315+
316+
Args:
317+
entrypoint: An EntryPoint object or resource_id integer primary key value
318+
queues: The queues to set as children
319+
320+
Returns:
321+
The child queues, as their latest snapshots
322+
323+
Raises:
324+
EntityDoesNotExistError: if experiment or any entry point not exist
325+
EntityDeletedError: if experiment or any entry point is deleted
326+
"""
327+
328+
return utils.set_resource_children(
329+
self.session,
330+
Queue,
331+
entrypoint,
332+
queues,
333+
)
334+
335+
def get_plugins(
336+
self,
337+
entrypoint: EntryPoint | int,
338+
deletion_policy: utils.DeletionPolicy = utils.DeletionPolicy.NOT_DELETED,
339+
) -> Sequence[Plugin]:
340+
"""
341+
Get the child plugins of the given entrypoint as their latest
342+
snapshots. If an EntryPoint object is given, the returned sequence
343+
may be shorter than len(entrypoint.children) if any of the children
344+
don't exist, or were filtered out due to deletion policy.
345+
346+
Args:
347+
entrypoint: An EntryPoint object or resource_id integer primary key value
348+
deletion_policy: Whether to look at deleted, non-deleted, or all plugins
349+
350+
Returns:
351+
The plugins
352+
353+
Raises:
354+
EntityDoesNotExistError: if parent does not exist
355+
EntityDeletedError: if parent is deleted
356+
"""
357+
358+
return utils.get_latest_child_snapshots(
359+
self.session,
360+
Plugin,
361+
entrypoint,
362+
deletion_policy,
363+
)
364+
365+
def create_plugins(
366+
self,
367+
entrypoint: EntryPoint,
368+
plugins: Iterable[Plugin | Resource | int],
369+
) -> Sequence[Plugin]:
370+
"""
371+
Add the plugins points as children of the given entrypoint.
372+
373+
Args:
374+
entrypoint: An EntryPoint object
375+
plugins: The plugins to add as children
376+
377+
Returns:
378+
The list of newly created plugin children, as latest snapshots.
379+
380+
Raises:
381+
EntityDoesNotExistError: if parent or any new child does not exist
382+
EntityDeletedError: if parent or any new child is deleted
383+
"""
384+
return utils.create_resource_children(self.session, Plugin, entrypoint, plugins)
385+
386+
def add_plugins(
387+
self,
388+
entrypoint: EntryPoint | int,
389+
plugins: Iterable[Plugin | Resource | int],
390+
) -> Sequence[Plugin]:
391+
"""
392+
Add the plugins points as children of the given entrypoint.
393+
394+
Args:
395+
entrypoint: An EntryPoint object or resource_id integer primary key value
396+
plugins: The plugins to add as children
397+
398+
Returns:
399+
The complete list of plugin children, as latest snapshots (including both
400+
pre-existing and new children).
401+
402+
Raises:
403+
EntityDoesNotExistError: if parent or any new child does not exist
404+
EntityDeletedError: if parent or any new child is deleted
405+
"""
406+
snaps = utils.append_resource_children(
407+
self.session, Plugin, entrypoint, plugins
408+
)
409+
return [snap for snap in snaps if isinstance(snap, Plugin)]
410+
411+
def set_plugins(
412+
self,
413+
entrypoint: EntryPoint | int,
414+
plugins: Iterable[Plugin | Resource | int],
415+
) -> Sequence[Plugin]:
416+
"""
417+
Set the child queues of the given entrypoint.
418+
This replaces all existing queues with the given resources.
419+
420+
Args:
421+
entrypoint: An EntryPoint object or resource_id integer primary key value
422+
queues: The queues to set as children
423+
424+
Returns:
425+
The child queues, as their latest snapshots
426+
427+
Raises:
428+
EntityDoesNotExistError: if experiment or any entry point not exist
429+
EntityDeletedError: if experiment or any entry point is deleted
430+
"""
431+
432+
return utils.set_resource_children(
433+
self.session,
434+
Plugin,
435+
entrypoint,
436+
plugins,
437+
)
438+
297439
def unlink_child(
298440
self,
299441
entrypoint: EntryPoint | int,
@@ -311,6 +453,7 @@ def unlink_child(
311453
Raises:
312454
EntityDoesNotExistError: if parent or child do not exist
313455
"""
456+
# TODO: need to verify correct resource type is being unlinked
314457
utils.unlink_child(self.session, entrypoint, child)
315458

316459
def unlink_queues(
@@ -336,7 +479,7 @@ def delete(self, entrypoint: EntryPoint | int) -> None:
336479
Delete an entrypoint. No-op if the entrypoint is already deleted.
337480
338481
Args:
339-
entrypoint: A Experiment object or resource_id primary key value
482+
entrypoint: A EntryPoint object or resource_id primary key value
340483
identifying an entrypoint resource
341484
342485
Raises:

src/dioptra/restapi/db/repository/experiments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def add_entrypoints(
264264
self,
265265
experiment: Experiment | int,
266266
children: Iterable[EntryPoint | int],
267-
) -> list[EntryPoint]:
267+
) -> Sequence[EntryPoint]:
268268
"""
269269
Add the given entry points as children of the given experiment.
270270

src/dioptra/restapi/db/repository/queues.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import dioptra.restapi.db.repository.utils as utils
2525
from dioptra.restapi.db.models import Group, Queue, Resource, Tag
26-
from dioptra.restapi.errors import EntityDoesNotExistError
2726

2827

2928
class QueueRepository:
@@ -167,7 +166,6 @@ def get_one(
167166
self,
168167
resource_id: int,
169168
deletion_policy: utils.DeletionPolicy,
170-
error_if_not_found=False,
171169
) -> Queue:
172170
"""
173171
Get the latest snapshot of the given queue resource; require that
@@ -177,7 +175,6 @@ def get_one(
177175
resource_id: A resource ID
178176
deletion_policy: Whether to look at deleted queues, non-deleted
179177
queues, or all queues
180-
error_if_not_found: If True, raise an error if the queue is not found.
181178
182179
Returns:
183180
A Queue object
@@ -190,15 +187,10 @@ def get_one(
190187
EntityDeletedError: if the queue is deleted, but policy was to find
191188
a non-deleted queue
192189
"""
193-
queue = utils.get_one_latest_snapshot(
190+
return utils.get_one_latest_snapshot(
194191
self.session, Queue, resource_id, deletion_policy
195192
)
196193

197-
if queue is None and error_if_not_found:
198-
raise EntityDoesNotExistError("queue", entrypoint_id=resource_id)
199-
200-
return queue
201-
202194
def get_by_name(
203195
self,
204196
name: str,

0 commit comments

Comments
 (0)