2
2
from typing import List
3
3
from diffgram .file .file import File
4
4
from diffgram .job .job import Job
5
+ from uuid import uuid4
5
6
6
7
7
8
class CompoundChildFile :
@@ -21,6 +22,8 @@ class CompoundChildFile:
21
22
assume_new_instances_machine_made : bool
22
23
convert_names_to_label_files : bool
23
24
video_split_duration : int
25
+ local_id : str
26
+ ordinal : int
24
27
25
28
def __init__ (self ,
26
29
child_file_type : str ,
@@ -38,7 +41,8 @@ def __init__(self,
38
41
frame_packet_map : dict = None ,
39
42
assume_new_instances_machine_made : bool = None ,
40
43
convert_names_to_label_files : bool = None ,
41
- video_split_duration : int = None ):
44
+ video_split_duration : int = None ,
45
+ ordinal : int = 0 ):
42
46
self .child_file_type = child_file_type
43
47
self .path = path
44
48
self .url = url
@@ -55,6 +59,11 @@ def __init__(self,
55
59
self .assume_new_instances_machine_made = assume_new_instances_machine_made
56
60
self .convert_names_to_label_files = convert_names_to_label_files
57
61
self .video_split_duration = video_split_duration
62
+ self .local_id = str (uuid4 ())
63
+ self .ordinal = ordinal
64
+
65
+ def set_ordinal (self , value : int ):
66
+ self .ordinal = value
58
67
59
68
60
69
class CompoundFile :
@@ -68,6 +77,12 @@ def __init__(self, project: Project, name: str, directory_id: int):
68
77
self .directory_id = directory_id
69
78
self .child_files_to_upload = []
70
79
80
+ def __refresh_compound_file_from_data_dict (self , data : dict ):
81
+ if not data :
82
+ return
83
+ for key in data :
84
+ setattr (self , key , data [key ])
85
+
71
86
def remove_compound_file (self , child_file : CompoundChildFile ) -> List [CompoundChildFile ]:
72
87
self .child_files_to_upload .remove (child_file )
73
88
return self .child_files_to_upload
@@ -83,6 +98,7 @@ def __create_compound_parent_file(self):
83
98
self .project .handle_errors (response )
84
99
data = response .json ()
85
100
self .parent_file_data = data .get ('file' )
101
+ self .__refresh_compound_file_from_data_dict (data .get ('file' ))
86
102
return data .get ('file' )
87
103
88
104
def __create_child_file (self , child_file : CompoundChildFile ):
@@ -124,7 +140,8 @@ def add_child_from_local(self,
124
140
instance_list : list = None ,
125
141
frame_packet_map : dict = None ,
126
142
assume_new_instances_machine_made : bool = True ,
127
- convert_names_to_label_files : bool = True ):
143
+ convert_names_to_label_files : bool = True ,
144
+ ordinal : int = 0 ):
128
145
new_child_file = CompoundChildFile (
129
146
child_file_type = "from_local" ,
130
147
path = path ,
@@ -144,7 +161,8 @@ def add_child_file_from_url(self,
144
161
job_id : int = None ,
145
162
video_split_duration : int = None ,
146
163
instance_list : list = None ,
147
- frame_packet_map : dict = None ):
164
+ frame_packet_map : dict = None ,
165
+ ordinal : int = 0 ):
148
166
new_child_file = CompoundChildFile (
149
167
child_file_type = "from_url" ,
150
168
url = url ,
@@ -166,7 +184,8 @@ def add_child_from_blob_path(self,
166
184
media_type : str = 'image' ,
167
185
instance_list : list = None ,
168
186
file_name : str = None ,
169
- frame_packet_map : dict = None
187
+ frame_packet_map : dict = None ,
188
+ ordinal : int = 0
170
189
):
171
190
new_child_file = CompoundChildFile (
172
191
child_file_type = "from_blob_path" ,
@@ -183,7 +202,10 @@ def add_child_from_blob_path(self,
183
202
return new_child_file
184
203
185
204
def upload (self ):
205
+ if len (self .child_files_to_upload ) == 0 :
206
+ raise AssertionError ('Need to add at least one child file to the compound file before calling upload()' )
186
207
parent_file_data : dict = self .__create_compound_parent_file ()
208
+
187
209
for child_file in self .child_files_to_upload :
188
210
self .__create_child_file (child_file )
189
211
return parent_file_data
0 commit comments