1
1
from __future__ import annotations
2
- from dataclasses import dataclass , field
3
- from dataclasses_json .cfg import config
2
+ from typing import 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
8
+
7
9
10
+ try :
11
+ from typing import Literal
12
+ except ImportError :
13
+ from typing_extensions import Literal
8
14
9
- @dataclass
10
- class Attachment (DataClassMixin ):
15
+
16
+ @define
17
+ class Attachment :
11
18
"""A class representing a CellEngine attachment.
12
19
Attachments are non-data files that are stored in an experiment.
13
20
"""
14
21
22
+ _id : str = field (on_setattr = readonly )
15
23
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
24
+ crc32c : str = field (on_setattr = readonly , repr = False )
25
+ experiment_id : str = field (on_setattr = readonly , repr = False )
26
+ md5 : str = field (on_setattr = readonly , repr = False )
27
+ size : int = field (on_setattr = readonly , repr = False )
28
+ type : Optional [Literal ["textbox" , "image" ]] = None
29
+
30
+ @property
31
+ def path (self ):
32
+ return f"experiments/{ self .experiment_id } /attachments/{ self ._id } " .rstrip (
33
+ "/None"
34
+ )
23
35
24
36
@classmethod
25
37
def get (cls , experiment_id : str , _id : str = None , name : str = None ) -> Attachment :
@@ -39,20 +51,16 @@ def upload(experiment_id: str, filepath: str, filename: str = None) -> Attachmen
39
51
return ce .APIClient ().upload_attachment (experiment_id , filepath , filename )
40
52
41
53
def update (self ):
42
- """Save changes to this Attachment to CellEngine.
54
+ """Save changes to this Attachment to CellEngine."""
55
+ res = ce .APIClient ().update (self )
56
+ self .__setstate__ (res .__getstate__ ()) # type: ignore
43
57
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__ )
58
+ @classmethod
59
+ def from_dict (cls , data : dict ):
60
+ return converter .structure (data , cls )
52
61
53
- def delete (self ):
54
- """Delete this attachment."""
55
- return ce .APIClient ().delete_entity (self .experiment_id , "attachments" , self ._id )
62
+ def to_dict (self ):
63
+ return converter .unstructure (self )
56
64
57
65
def download (self , to_file : str = None ):
58
66
"""Download the attachment.
@@ -74,3 +82,6 @@ def download(self, to_file: str = None):
74
82
f .write (res )
75
83
else :
76
84
return res
85
+
86
+ def delete (self ):
87
+ return ce .APIClient ().delete_entity (self .experiment_id , "attachments" , self ._id )
0 commit comments