2021-03-01 08:49:49 +00:00
|
|
|
import json
|
2018-11-27 11:33:16 +00:00
|
|
|
import os
|
2021-03-01 08:49:49 +00:00
|
|
|
from datetime import datetime
|
2018-11-30 00:40:18 +00:00
|
|
|
|
2018-11-22 10:25:22 +00:00
|
|
|
from PyQt5 import uic
|
2020-03-23 06:20:09 +00:00
|
|
|
from PyQt5.QtCore import Qt
|
2020-11-20 00:46:09 +00:00
|
|
|
from PyQt5.QtWidgets import QHeaderView
|
2018-11-22 10:25:22 +00:00
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
from vorta.utils import get_asset, get_dict_from_list, nested_dict
|
2020-09-05 14:08:36 +00:00
|
|
|
from vorta.views.partials.tree_view import TreeModel
|
2018-11-22 10:25:22 +00:00
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
uifile = get_asset("UI/extractdialog.ui")
|
2018-11-22 10:25:22 +00:00
|
|
|
ExtractDialogUI, ExtractDialogBase = uic.loadUiType(uifile)
|
|
|
|
|
2018-11-22 19:31:10 +00:00
|
|
|
|
2018-11-22 10:25:22 +00:00
|
|
|
class ExtractDialog(ExtractDialogBase, ExtractDialogUI):
|
2018-11-27 11:33:16 +00:00
|
|
|
def __init__(self, fs_data, archive):
|
2018-11-22 10:25:22 +00:00
|
|
|
super().__init__()
|
|
|
|
self.setupUi(self)
|
2018-11-27 11:33:16 +00:00
|
|
|
|
2019-03-04 14:47:15 +00:00
|
|
|
nested_file_list = nested_dict()
|
2020-03-23 06:20:09 +00:00
|
|
|
self.selected = set()
|
2019-03-04 14:47:15 +00:00
|
|
|
|
2021-03-12 14:47:11 +00:00
|
|
|
def parse_json_line(data):
|
2021-03-01 08:49:49 +00:00
|
|
|
size = data["size"]
|
|
|
|
# python >= 3.7
|
|
|
|
# modified = datetime.fromisoformat(data["mtime"]).ctime()
|
|
|
|
# python < 3.7
|
|
|
|
try:
|
|
|
|
modified = datetime.strptime(data["mtime"], "%Y-%m-%dT%H:%M:%S.%f").ctime()
|
|
|
|
except ValueError:
|
|
|
|
modified = datetime.strptime(data["mtime"], "%Y-%m-%dT%H:%M:%S").ctime()
|
|
|
|
dirpath, name = os.path.split(data["path"])
|
2018-11-30 00:40:18 +00:00
|
|
|
# add to nested dict of folders to find nested dirs.
|
2021-03-01 08:49:49 +00:00
|
|
|
d = get_dict_from_list(nested_file_list, dirpath.split("/"))
|
2018-11-30 00:40:18 +00:00
|
|
|
if name not in d:
|
|
|
|
d[name] = {}
|
2021-05-30 15:21:27 +00:00
|
|
|
return size, modified, name, dirpath, data["type"]
|
2018-11-27 11:33:16 +00:00
|
|
|
|
2021-03-12 14:47:11 +00:00
|
|
|
# handle case of a single line of result, which will already be a dict
|
|
|
|
lines = [fs_data] if isinstance(fs_data, dict) else \
|
|
|
|
[json.loads(line) for line in fs_data.split('\n') if line]
|
|
|
|
|
|
|
|
files_with_attributes = [parse_json_line(line) for line in lines]
|
2018-11-22 10:25:22 +00:00
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
model = ExtractTree(files_with_attributes, nested_file_list, self.selected)
|
2018-11-27 11:33:16 +00:00
|
|
|
|
|
|
|
view = self.treeView
|
|
|
|
view.setAlternatingRowColors(True)
|
|
|
|
view.setUniformRowHeights(True) # Allows for scrolling optimizations.
|
|
|
|
view.setModel(model)
|
|
|
|
header = view.header()
|
2018-11-22 10:25:22 +00:00
|
|
|
header.setStretchLastSection(False)
|
|
|
|
header.setSectionResizeMode(1, QHeaderView.ResizeToContents)
|
|
|
|
header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
|
|
|
|
header.setSectionResizeMode(0, QHeaderView.Stretch)
|
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
self.archiveNameLabel.setText(f"{archive.name}, {archive.time}")
|
2018-11-27 11:33:16 +00:00
|
|
|
self.cancelButton.clicked.connect(self.close)
|
|
|
|
self.extractButton.clicked.connect(self.accept)
|
2018-12-05 09:05:47 +00:00
|
|
|
|
|
|
|
|
2020-03-23 06:20:09 +00:00
|
|
|
class ExtractTree(TreeModel):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
files_with_attributes,
|
|
|
|
nested_file_list,
|
|
|
|
selected_files_folders,
|
|
|
|
parent=None,
|
|
|
|
):
|
|
|
|
super().__init__(
|
|
|
|
files_with_attributes, nested_file_list, selected_files_folders, parent
|
|
|
|
)
|
2018-11-27 11:33:16 +00:00
|
|
|
|
|
|
|
def data(self, index, role):
|
|
|
|
if not index.isValid():
|
|
|
|
return None
|
|
|
|
|
|
|
|
item = index.internalPointer()
|
|
|
|
|
|
|
|
if role == Qt.DisplayRole:
|
|
|
|
return item.data(index.column())
|
|
|
|
elif role == Qt.CheckStateRole and index.column() == 0:
|
|
|
|
return item.getCheckedState()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def flags(self, index):
|
|
|
|
if not index.isValid():
|
|
|
|
return Qt.NoItemFlags
|
|
|
|
|
|
|
|
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
|