|
| 1 | +from diffgram.core.core import Project |
| 2 | +from typing import List |
| 3 | +from diffgram.file.file import File |
| 4 | +from diffgram.job.job import Job |
| 5 | + |
| 6 | + |
| 7 | +class CompoundChildFile: |
| 8 | + path: str |
| 9 | + url: str |
| 10 | + blob_path: str |
| 11 | + file_name: str |
| 12 | + bucket_name: str |
| 13 | + child_file_type: str |
| 14 | + connection_id: int |
| 15 | + media_type: str |
| 16 | + job: Job |
| 17 | + job_id: int |
| 18 | + directory_id: int |
| 19 | + instance_list: list |
| 20 | + frame_packet_map: dict |
| 21 | + assume_new_instances_machine_made: bool |
| 22 | + convert_names_to_label_files: bool |
| 23 | + video_split_duration: int |
| 24 | + |
| 25 | + def __init__(self, |
| 26 | + child_file_type: str, |
| 27 | + path: str = None, |
| 28 | + url: str = None, |
| 29 | + blob_path: str = None, |
| 30 | + file_name: str = None, |
| 31 | + bucket_name: str = None, |
| 32 | + connection_id: int = None, |
| 33 | + media_type: str = None, |
| 34 | + job: Job = None, |
| 35 | + job_id: int = None, |
| 36 | + directory_id: int = None, |
| 37 | + instance_list: list = None, |
| 38 | + frame_packet_map: dict = None, |
| 39 | + assume_new_instances_machine_made: bool = None, |
| 40 | + convert_names_to_label_files: bool = None, |
| 41 | + video_split_duration: int = None): |
| 42 | + self.child_file_type = child_file_type |
| 43 | + self.path = path |
| 44 | + self.url = url |
| 45 | + self.blob_path = blob_path |
| 46 | + self.file_name = file_name |
| 47 | + self.bucket_name = bucket_name |
| 48 | + self.connection_id = connection_id |
| 49 | + self.media_type = media_type |
| 50 | + self.job = job |
| 51 | + self.job_id = job_id |
| 52 | + self.directory_id = directory_id |
| 53 | + self.instance_list = instance_list |
| 54 | + self.frame_packet_map = frame_packet_map |
| 55 | + self.assume_new_instances_machine_made = assume_new_instances_machine_made |
| 56 | + self.convert_names_to_label_files = convert_names_to_label_files |
| 57 | + self.video_split_duration = video_split_duration |
| 58 | + |
| 59 | + |
| 60 | +class CompoundFile: |
| 61 | + project: Project |
| 62 | + parent_file_data: dict |
| 63 | + child_files_to_upload: List[CompoundChildFile] |
| 64 | + |
| 65 | + def __init__(self, project: Project, name: str, directory_id: int): |
| 66 | + self.project = project |
| 67 | + self.name = name |
| 68 | + self.directory_id = directory_id |
| 69 | + self.child_files_to_upload = [] |
| 70 | + |
| 71 | + def remove_compound_file(self, child_file: CompoundChildFile) -> List[CompoundChildFile]: |
| 72 | + self.child_files_to_upload.remove(child_file) |
| 73 | + return self.child_files_to_upload |
| 74 | + |
| 75 | + def __create_compound_parent_file(self): |
| 76 | + url = f'/api/v1/project/{self.project.project_string_id}/file/new-compound' |
| 77 | + data = { |
| 78 | + 'name': self.name, |
| 79 | + 'directory_id': self.directory_id |
| 80 | + } |
| 81 | + response = self.project.session.post(url = self.project.host + url, |
| 82 | + json = data) |
| 83 | + self.project.handle_errors(response) |
| 84 | + data = response.json() |
| 85 | + self.parent_file_data = data.get('file') |
| 86 | + print('self,', self.parent_file_data) |
| 87 | + return data.get('file') |
| 88 | + |
| 89 | + def __create_child_file(self, child_file: CompoundChildFile): |
| 90 | + if child_file.child_file_type == 'from_local': |
| 91 | + return self.project.file.from_local( |
| 92 | + path = child_file.path, |
| 93 | + directory_id = self.directory_id, |
| 94 | + instance_list = child_file.instance_list, |
| 95 | + frame_packet_map = child_file.frame_packet_map, |
| 96 | + assume_new_instances_machine_made = child_file.assume_new_instances_machine_made, |
| 97 | + convert_names_to_label_files = child_file.convert_names_to_label_files, |
| 98 | + parent_file_id = self.parent_file_data.get('id') |
| 99 | + ) |
| 100 | + elif child_file.child_file_type == 'from_url': |
| 101 | + return self.project.file.from_url( |
| 102 | + url = child_file.path, |
| 103 | + media_type = child_file.media_type, |
| 104 | + job_id = child_file.job_id, |
| 105 | + job = child_file.job, |
| 106 | + video_split_duration = child_file.video_split_duration, |
| 107 | + instance_list = child_file.instance_list, |
| 108 | + frame_packet_map = child_file.frame_packet_map, |
| 109 | + parent_file_id = self.parent_file_data.get('id') |
| 110 | + ) |
| 111 | + elif child_file.child_file_type == 'from_blob_path': |
| 112 | + return self.project.file.from_blob_path( |
| 113 | + blob_path = child_file.blob_path, |
| 114 | + bucket_name = child_file.bucket_name, |
| 115 | + connection_id = child_file.connection_id, |
| 116 | + media_type = child_file.media_type, |
| 117 | + instance_list = child_file.instance_list, |
| 118 | + file_name = child_file.file_name, |
| 119 | + frame_packet_map = child_file.frame_packet_map, |
| 120 | + parent_file_id = self.parent_file_data.get('id') |
| 121 | + ) |
| 122 | + |
| 123 | + def add_child_from_local(self, |
| 124 | + path: str, |
| 125 | + instance_list: list = None, |
| 126 | + frame_packet_map: dict = None, |
| 127 | + assume_new_instances_machine_made: bool = True, |
| 128 | + convert_names_to_label_files: bool = True): |
| 129 | + new_child_file = CompoundChildFile( |
| 130 | + child_file_type = "from_local", |
| 131 | + path = path, |
| 132 | + directory_id = self.directory_id, |
| 133 | + instance_list = instance_list, |
| 134 | + frame_packet_map = frame_packet_map, |
| 135 | + assume_new_instances_machine_made = assume_new_instances_machine_made, |
| 136 | + convert_names_to_label_files = convert_names_to_label_files |
| 137 | + ) |
| 138 | + self.child_files_to_upload.append(new_child_file) |
| 139 | + return new_child_file |
| 140 | + |
| 141 | + def add_child_file_from_url(self, |
| 142 | + url: str, |
| 143 | + media_type: str = "image", |
| 144 | + job: Job = None, |
| 145 | + job_id: int = None, |
| 146 | + video_split_duration: int = None, |
| 147 | + instance_list: list = None, |
| 148 | + frame_packet_map: dict = None): |
| 149 | + new_child_file = CompoundChildFile( |
| 150 | + child_file_type = "from_url", |
| 151 | + url = url, |
| 152 | + media_type = media_type, |
| 153 | + job = job, |
| 154 | + directory_id = self.directory_id, |
| 155 | + job_id = job_id, |
| 156 | + video_split_duration = video_split_duration, |
| 157 | + instance_list = instance_list, |
| 158 | + frame_packet_map = frame_packet_map, |
| 159 | + ) |
| 160 | + self.child_files_to_upload.append(new_child_file) |
| 161 | + return new_child_file |
| 162 | + |
| 163 | + def add_child_from_blob_path(self, |
| 164 | + blob_path: str, |
| 165 | + bucket_name: str, |
| 166 | + connection_id: int, |
| 167 | + media_type: str = 'image', |
| 168 | + instance_list: list = None, |
| 169 | + file_name: str = None, |
| 170 | + frame_packet_map: dict = None |
| 171 | + ): |
| 172 | + new_child_file = CompoundChildFile( |
| 173 | + child_file_type = "from_blob_path", |
| 174 | + blob_path = blob_path, |
| 175 | + bucket_name = bucket_name, |
| 176 | + connection_id = connection_id, |
| 177 | + directory_id = self.directory_id, |
| 178 | + media_type = media_type, |
| 179 | + instance_list = instance_list, |
| 180 | + file_name = file_name, |
| 181 | + frame_packet_map = frame_packet_map, |
| 182 | + ) |
| 183 | + self.child_files_to_upload.append(new_child_file) |
| 184 | + return new_child_file |
| 185 | + |
| 186 | + def upload(self): |
| 187 | + parent_file_data: dict = self.__create_compound_parent_file() |
| 188 | + for child_file in self.child_files_to_upload: |
| 189 | + self.__create_child_file(child_file) |
| 190 | + return parent_file_data |
0 commit comments