def create(self, path: Path, source_id: int): if path.suffix != '.zip': logging.critical('Path provided does not point to a zip file') return None asset = self.filter_by(filename=str(path.name)).first() # check if asset already exists in db if asset is None: sku = FileHelpers.get_sku(path) product_name = FileHelpers.parse_product_name(path) path_raw = str(path) filename = path.name size_raw = FileHelpers.get_file_size(path) installed_raw = False # todo check if asset is already installed imported_raw = False asset = Asset(source_id=source_id, sku=sku, product_name=product_name, path_raw=path_raw, filename=filename, size_raw=size_raw, installed_raw=installed_raw, imported_raw=imported_raw) session: Session = sessionmaker(bind=self.engine, expire_on_commit=False)() SQLHelpers.commit(session, asset) return asset
def populate(self, current_node, current_data, source_id: int): node_list = [] session: Session = sessionmaker(bind=self.db.engine)() path = Path(current_data['path']) sub_list = [x for x in path.iterdir()] for sub_path in sub_list: if sub_path.is_dir(): folder: Folder = self.db.folders.create(sub_path, source_id) next_data = { 'id': folder.id, 'type': 'folder', 'path': folder.path } next_node = self.AppendItem(current_node, sub_path.name, data=next_data, image=0) node_list.append(next_node) temp_list = self.populate(next_node, next_data, source_id) node_list += temp_list elif sub_path.suffix == '.zip': self.zip_count += 1 self.size += FileHelpers.get_file_size(sub_path) asset: Asset = self.db.assets.create(sub_path, source_id) next_data = {'id': asset.id, 'type': 'asset', 'path': sub_path} next_node = self.AppendItem(current_node, asset.product_name, data=next_data, image=1) node_list.append(next_node) self.SortChildren(current_node) return node_list