6
6
import os
7
7
import requests
8
8
9
+
9
10
class FileConstructor ():
10
11
"""
11
12
@@ -87,12 +88,85 @@ def from_local(
87
88
88
89
data = response .json ()
89
90
90
- # print(data)
91
-
92
91
if data ["log" ]["success" ] is True :
93
92
file = self .file_from_response (file_dict = data ['file' ])
94
93
return file
95
94
95
+ def __build_packet_payload (self ,
96
+ url : str = None ,
97
+ media_type : str = None ,
98
+ instance_list : list = None ,
99
+ frame_packet_map : dict = None ,
100
+ video_split_duration : int = None ,
101
+ job_id : int = None ,
102
+ job : Job = None ,
103
+ type : str = None ,
104
+ connection_id : int = None ,
105
+ directory_id : int = None ,
106
+ bucket_name : str = None ,
107
+ file_name : str = None ,
108
+ blob_path : str = None ):
109
+ packet = {'media' : {}}
110
+ packet ['media' ]['url' ] = url
111
+ packet ['media' ]['type' ] = media_type
112
+
113
+ # Existing Instances
114
+ packet ['frame_packet_map' ] = frame_packet_map
115
+ packet ['type' ] = type
116
+ packet ['connection_id' ] = connection_id
117
+ packet ['directory_id' ] = directory_id
118
+ packet ['original_filename' ] = file_name
119
+ packet ['bucket_name' ] = bucket_name
120
+ packet ['raw_data_blob_path' ] = blob_path
121
+ packet ['instance_list' ] = instance_list
122
+
123
+ if job :
124
+ packet ["job_id" ] = job .id
125
+ else :
126
+ packet ["job_id" ] = job_id
127
+
128
+ if video_split_duration :
129
+ packet ["video_split_duration" ] = video_split_duration
130
+ return packet
131
+
132
+ def from_blob_path (self ,
133
+ blob_path : str ,
134
+ bucket_name : str ,
135
+ connection_id : int ,
136
+ media_type : str = 'image' ,
137
+ instance_list : list = None ,
138
+ file_name : str = None ,
139
+ frame_packet_map : dict = None ):
140
+ """
141
+ Bind a blob path in the given connection ID into Diffgram
142
+ :param blob_path:
143
+ :param bucket_name:
144
+ :param connection_id:
145
+ :param media_type:
146
+ :param instance_list:
147
+ :param frame_packet_map:
148
+ :return:
149
+ """
150
+ if self .client .default_directory :
151
+ raise Exception ("Directory not set. call set_default_directory() to set upload directory." )
152
+ directory_id = self .client .directory_id
153
+ name = file_name
154
+ if file_name is None :
155
+ name = blob_path .split ('/' )[len (blob_path .split ('/' )) - 1 ]
156
+ packet = self .__build_packet_payload (
157
+ media_type = media_type ,
158
+ instance_list = instance_list ,
159
+ frame_packet_map = frame_packet_map ,
160
+ blob_path = blob_path ,
161
+ bucket_name = bucket_name ,
162
+ connection_id = connection_id ,
163
+ file_name = name ,
164
+ directory_id = directory_id ,
165
+ type = "from_blob_path"
166
+ )
167
+ self .from_packet (packet = packet )
168
+ return True
169
+
96
170
def from_url (
97
171
self ,
98
172
url : str ,
@@ -126,27 +200,20 @@ def from_url(
126
200
127
201
"""
128
202
129
- packet = {'media' : {}}
130
- packet ['media' ]['url' ] = url
131
- packet ['media' ]['type' ] = media_type
132
-
133
- # Existing Instances
134
- packet ['frame_packet_map' ] = frame_packet_map
135
- packet ['instance_list' ] = instance_list
136
-
137
- if job :
138
- packet ["job_id" ] = job .id
139
- else :
140
- packet ["job_id" ] = job_id
141
-
142
- if video_split_duration :
143
- packet ["video_split_duration" ] = video_split_duration
144
-
203
+ packet = self .__build_packet_payload (
204
+ url = url ,
205
+ media_type = media_type ,
206
+ job = job ,
207
+ job_id = job_id ,
208
+ video_split_duration = video_split_duration ,
209
+ instance_list = instance_list ,
210
+ frame_packet_map = frame_packet_map
211
+ )
145
212
self .from_packet (packet = packet )
146
213
147
214
return True
148
215
149
- def format_packet ():
216
+ def format_packet (self ):
150
217
raise NotImplementedError
151
218
152
219
@staticmethod
@@ -168,7 +235,7 @@ def __media_packet_sanity_checks(packet) -> None:
168
235
if not media_type :
169
236
raise Exception (" 'type' key is not defined in packet['media'] use one of ['image', 'video']" )
170
237
171
- def __validate_existing_instances ():
238
+ def __validate_existing_instances (self ):
172
239
pass
173
240
174
241
def from_packet (
@@ -216,8 +283,6 @@ def from_packet(
216
283
'points': [] # Required for polygon more on this coming soon
217
284
'number': 0 # A number is optional, and only relates to video instances
218
285
}
219
-
220
-
221
286
Validates basics of packet form
222
287
and makes request to /input/packet endpoint.
223
288
@@ -325,7 +390,7 @@ def __validate_and_format_instance_list(
325
390
instance_list : list ,
326
391
assume_new_instances_machine_made : bool ,
327
392
convert_names_to_label_files : bool ,
328
- check_frame_number : bool = False ):
393
+ check_frame_number : bool = False ):
329
394
330
395
FileConstructor .sanity_check_instance_list (instance_list )
331
396
@@ -443,8 +508,6 @@ def file_list_exists(self, id_list, use_session = True):
443
508
if response_json .get ('result' ):
444
509
return response_json .get ('result' ).get ('exists' )
445
510
446
-
447
-
448
511
def get_by_id (self ,
449
512
id : int ,
450
513
with_instances : bool = False ,
@@ -477,8 +540,8 @@ def get_by_id(self,
477
540
else :
478
541
# Add Auth
479
542
response = requests .post (self .client .host + endpoint ,
480
- json = spec_dict ,
481
- auth = self .client .get_http_auth ())
543
+ json = spec_dict ,
544
+ auth = self .client .get_http_auth ())
482
545
483
546
self .client .handle_errors (response )
484
547
0 commit comments