1
1
from __future__ import annotations
2
- from dataclasses import dataclass , field
3
- from dataclasses_json .cfg import config
2
+ from typing import Literal , Optional
3
+
4
+ from attr import define , field
4
5
5
6
import cellengine as ce
6
- from cellengine .utils . dataclass_mixin import DataClassMixin , ReadOnly
7
+ from cellengine .utils import converter , readonly
7
8
8
9
9
- @dataclass
10
- class Attachment ( DataClassMixin ) :
10
+ @define
11
+ class Attachment :
11
12
"""A class representing a CellEngine attachment.
12
13
Attachments are non-data files that are stored in an experiment.
13
14
"""
14
15
16
+ _id : str = field (on_setattr = readonly )
15
17
filename : str
16
- _id : str = field (
17
- metadata = config (field_name = "_id" ), default = ReadOnly ()
18
- ) # type: ignore
19
- crc32c : str = field (repr = False , default = ReadOnly ()) # type: ignore
20
- experiment_id : str = field (repr = False , default = ReadOnly ()) # type: ignore
21
- md5 : str = field (repr = False , default = ReadOnly ()) # type: ignore
22
- size : int = field (repr = False , default = ReadOnly ()) # type: ignore
18
+ crc32c : str = field (on_setattr = readonly , repr = False )
19
+ experiment_id : str = field (on_setattr = readonly , repr = False )
20
+ md5 : str = field (on_setattr = readonly , repr = False )
21
+ size : int = field (on_setattr = readonly , repr = False )
22
+ grid_id : Optional [str ] = None
23
+ type : Optional [Literal ["textbox" , "image" ]] = None
24
+
25
+ @property
26
+ def client (self ):
27
+ return ce .APIClient ()
28
+
29
+ @property
30
+ def path (self ):
31
+ return f"experiments/{ self .experiment_id } /attachments/{ self ._id } " .rstrip (
32
+ "/None"
33
+ )
23
34
24
35
@classmethod
25
36
def get (cls , experiment_id : str , _id : str = None , name : str = None ) -> Attachment :
@@ -39,20 +50,20 @@ def upload(experiment_id: str, filepath: str, filename: str = None) -> Attachmen
39
50
return ce .APIClient ().upload_attachment (experiment_id , filepath , filename )
40
51
41
52
def update (self ):
42
- """Save changes to this Attachment to CellEngine.
53
+ """Save changes to this Attachment to CellEngine."""
54
+ res = self .client .update (self )
55
+ self .__setstate__ (res .__getstate__ ()) # type: ignore
43
56
44
- Returns:
45
- None: Updates the Attachment on CellEngine and synchronizes the
46
- local Attachment object properties with remote state.
47
- """
48
- res = ce .APIClient ().update_entity (
49
- self .experiment_id , self ._id , "attachments" , body = self .to_dict ()
50
- )
51
- self .__dict__ .update (Attachment .from_dict (res ).__dict__ )
57
+ @classmethod
58
+ def from_dict (cls , data : dict ):
59
+ return converter .structure (data , cls )
52
60
53
- def delete (self ):
54
- """Delete this attachment."""
55
- return ce .APIClient ().delete_entity (self .experiment_id , "attachments" , self ._id )
61
+ def to_dict (self ):
62
+ return converter .unstructure (self )
63
+
64
+ def asdict (self ):
65
+ """Force use of cattrs"""
66
+ return self .to_dict ()
56
67
57
68
def download (self , to_file : str = None ):
58
69
"""Download the attachment.
@@ -74,3 +85,6 @@ def download(self, to_file: str = None):
74
85
f .write (res )
75
86
else :
76
87
return res
88
+
89
+ def delete (self ):
90
+ return self .client .delete_entity (self .experiment_id , "attachments" , self ._id )
0 commit comments