66import os
77import requests
88
9+
910class FileConstructor ():
1011 """
1112
@@ -87,12 +88,85 @@ def from_local(
8788
8889 data = response .json ()
8990
90- # print(data)
91-
9291 if data ["log" ]["success" ] is True :
9392 file = self .file_from_response (file_dict = data ['file' ])
9493 return file
9594
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+
96170 def from_url (
97171 self ,
98172 url : str ,
@@ -126,27 +200,20 @@ def from_url(
126200
127201 """
128202
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+ )
145212 self .from_packet (packet = packet )
146213
147214 return True
148215
149- def format_packet ():
216+ def format_packet (self ):
150217 raise NotImplementedError
151218
152219 @staticmethod
@@ -168,7 +235,7 @@ def __media_packet_sanity_checks(packet) -> None:
168235 if not media_type :
169236 raise Exception (" 'type' key is not defined in packet['media'] use one of ['image', 'video']" )
170237
171- def __validate_existing_instances ():
238+ def __validate_existing_instances (self ):
172239 pass
173240
174241 def from_packet (
@@ -216,8 +283,6 @@ def from_packet(
216283 'points': [] # Required for polygon more on this coming soon
217284 'number': 0 # A number is optional, and only relates to video instances
218285 }
219-
220-
221286 Validates basics of packet form
222287 and makes request to /input/packet endpoint.
223288
@@ -325,7 +390,7 @@ def __validate_and_format_instance_list(
325390 instance_list : list ,
326391 assume_new_instances_machine_made : bool ,
327392 convert_names_to_label_files : bool ,
328- check_frame_number : bool = False ):
393+ check_frame_number : bool = False ):
329394
330395 FileConstructor .sanity_check_instance_list (instance_list )
331396
@@ -443,8 +508,6 @@ def file_list_exists(self, id_list, use_session = True):
443508 if response_json .get ('result' ):
444509 return response_json .get ('result' ).get ('exists' )
445510
446-
447-
448511 def get_by_id (self ,
449512 id : int ,
450513 with_instances : bool = False ,
@@ -477,8 +540,8 @@ def get_by_id(self,
477540 else :
478541 # Add Auth
479542 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 ())
482545
483546 self .client .handle_errors (response )
484547
0 commit comments