2016-04-04 23:37:51 +08:00
|
|
|
import logging
|
2016-04-06 22:15:51 +08:00
|
|
|
import os
|
2016-05-26 00:46:21 +08:00
|
|
|
from datetime import datetime
|
2016-04-04 23:37:51 +08:00
|
|
|
|
2016-04-05 15:51:04 +08:00
|
|
|
import h5py
|
2023-09-17 06:52:13 +08:00
|
|
|
from PyQt6 import QtCore, QtWidgets, QtGui
|
2016-04-04 23:37:51 +08:00
|
|
|
|
2019-11-10 15:55:17 +08:00
|
|
|
from sipyco import pyon
|
|
|
|
|
2016-05-26 00:46:21 +08:00
|
|
|
|
2016-04-04 23:37:51 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-04-18 23:10:40 +08:00
|
|
|
def open_h5(info):
|
|
|
|
if not (info.isFile() and info.isReadable() and
|
|
|
|
info.suffix() == "h5"):
|
|
|
|
return
|
|
|
|
try:
|
2016-05-04 00:07:43 +08:00
|
|
|
return h5py.File(info.filePath(), "r")
|
2016-06-15 22:54:06 +08:00
|
|
|
except OSError: # e.g. file being written (see #470)
|
2016-06-16 17:50:09 +08:00
|
|
|
logger.debug("OSError when opening HDF5 file %s", info.filePath(),
|
|
|
|
exc_info=True)
|
2016-04-18 23:10:40 +08:00
|
|
|
except:
|
|
|
|
logger.warning("unable to read HDF5 file %s", info.filePath(),
|
|
|
|
exc_info=True)
|
|
|
|
|
|
|
|
|
2016-04-17 16:34:10 +08:00
|
|
|
class ThumbnailIconProvider(QtWidgets.QFileIconProvider):
|
2016-04-06 02:01:25 +08:00
|
|
|
def icon(self, info):
|
2016-04-06 18:16:40 +08:00
|
|
|
icon = self.hdf5_thumbnail(info)
|
|
|
|
if icon is None:
|
|
|
|
icon = QtWidgets.QFileIconProvider.icon(self, info)
|
|
|
|
return icon
|
|
|
|
|
|
|
|
def hdf5_thumbnail(self, info):
|
2016-04-18 23:10:40 +08:00
|
|
|
f = open_h5(info)
|
|
|
|
if not f:
|
2016-04-08 11:44:37 +08:00
|
|
|
return
|
|
|
|
with f:
|
|
|
|
try:
|
|
|
|
t = f["datasets/thumbnail"]
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
try:
|
2020-12-17 10:51:22 +08:00
|
|
|
img = QtGui.QImage.fromData(t[()])
|
2016-04-08 11:44:37 +08:00
|
|
|
except:
|
2016-04-17 16:34:10 +08:00
|
|
|
logger.warning("unable to read thumbnail from %s",
|
|
|
|
info.filePath(), exc_info=True)
|
2016-04-06 18:16:40 +08:00
|
|
|
return
|
|
|
|
pix = QtGui.QPixmap.fromImage(img)
|
|
|
|
return QtGui.QIcon(pix)
|
|
|
|
|
|
|
|
|
2016-04-17 16:34:10 +08:00
|
|
|
class DirsOnlyProxy(QtCore.QSortFilterProxyModel):
|
|
|
|
def filterAcceptsRow(self, row, parent):
|
|
|
|
idx = self.sourceModel().index(row, 0, parent)
|
|
|
|
if not self.sourceModel().fileInfo(idx).isDir():
|
|
|
|
return False
|
|
|
|
return QtCore.QSortFilterProxyModel.filterAcceptsRow(self, row, parent)
|
|
|
|
|
|
|
|
|
2016-04-21 00:11:04 +08:00
|
|
|
class ZoomIconView(QtWidgets.QListView):
|
|
|
|
zoom_step = 2**.25
|
|
|
|
aspect = 2/3
|
2016-04-21 01:16:19 +08:00
|
|
|
default_size = 25
|
|
|
|
min_size = 10
|
|
|
|
max_size = 1000
|
2016-04-21 00:11:04 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
QtWidgets.QListView.__init__(self)
|
|
|
|
self._char_width = QtGui.QFontMetrics(self.font()).averageCharWidth()
|
2023-09-17 06:52:13 +08:00
|
|
|
self.setViewMode(self.ViewMode.IconMode)
|
2016-04-21 00:11:04 +08:00
|
|
|
w = self._char_width*self.default_size
|
2022-05-29 08:43:25 +08:00
|
|
|
self.setIconSize(QtCore.QSize(w, int(w*self.aspect)))
|
2023-09-17 06:52:13 +08:00
|
|
|
self.setFlow(self.Flow.LeftToRight)
|
|
|
|
self.setResizeMode(self.ResizeMode.Adjust)
|
2016-04-21 00:11:04 +08:00
|
|
|
self.setWrapping(True)
|
|
|
|
|
|
|
|
def wheelEvent(self, ev):
|
2023-09-17 06:52:13 +08:00
|
|
|
if ev.modifiers() & QtCore.Qt.KeyboardModifier.ControlModifier:
|
2016-04-21 00:11:04 +08:00
|
|
|
a = self._char_width*self.min_size
|
|
|
|
b = self._char_width*self.max_size
|
2016-04-21 01:16:19 +08:00
|
|
|
w = self.iconSize().width()*self.zoom_step**(
|
|
|
|
ev.angleDelta().y()/120.)
|
2016-04-21 00:11:04 +08:00
|
|
|
if a <= w <= b:
|
2024-07-10 15:47:32 +08:00
|
|
|
self.setIconSize(QtCore.QSize(int(w), int(w*self.aspect)))
|
2016-04-21 00:11:04 +08:00
|
|
|
else:
|
|
|
|
QtWidgets.QListView.wheelEvent(self, ev)
|
|
|
|
|
|
|
|
|
2023-09-17 06:52:13 +08:00
|
|
|
class Hdf5FileSystemModel(QtGui.QFileSystemModel):
|
2016-05-26 00:46:21 +08:00
|
|
|
def __init__(self):
|
2023-09-17 06:52:13 +08:00
|
|
|
QtGui.QFileSystemModel.__init__(self)
|
|
|
|
self.setFilter(QtCore.QDir.Filter.Drives | QtCore.QDir.Filter.NoDotAndDotDot |
|
|
|
|
QtCore.QDir.Filter.AllDirs | QtCore.QDir.Filter.Files)
|
2016-05-26 00:46:21 +08:00
|
|
|
self.setNameFilterDisables(False)
|
|
|
|
self.setIconProvider(ThumbnailIconProvider())
|
|
|
|
|
|
|
|
def data(self, idx, role):
|
2023-09-17 06:52:13 +08:00
|
|
|
if role == QtCore.Qt.ItemDataRole.ToolTipRole:
|
2016-05-26 00:46:21 +08:00
|
|
|
info = self.fileInfo(idx)
|
|
|
|
h5 = open_h5(info)
|
|
|
|
if h5 is not None:
|
|
|
|
try:
|
2022-12-02 16:30:58 +08:00
|
|
|
expid = pyon.decode(h5["expid"][()]) if "expid" in h5 else dict()
|
|
|
|
start_time = datetime.fromtimestamp(h5["start_time"][()]) if "start_time" in h5 else "<none>"
|
2016-05-26 04:21:07 +08:00
|
|
|
v = ("artiq_version: {}\nrepo_rev: {}\nfile: {}\n"
|
|
|
|
"class_name: {}\nrid: {}\nstart_time: {}").format(
|
2022-12-07 12:11:31 +08:00
|
|
|
h5["artiq_version"].asstr()[()] if "artiq_version" in h5 else "<none>",
|
2022-12-02 16:30:58 +08:00
|
|
|
expid.get("repo_rev", "<none>"),
|
|
|
|
expid.get("file", "<none>"), expid.get("class_name", "<none>"),
|
|
|
|
h5["rid"][()] if "rid" in h5 else "<none>", start_time)
|
2016-05-26 00:46:21 +08:00
|
|
|
return v
|
|
|
|
except:
|
|
|
|
logger.warning("unable to read metadata from %s",
|
|
|
|
info.filePath(), exc_info=True)
|
2023-09-17 06:52:13 +08:00
|
|
|
return QtGui.QFileSystemModel.data(self, idx, role)
|
2016-05-26 00:46:21 +08:00
|
|
|
|
|
|
|
|
2016-04-14 17:55:44 +08:00
|
|
|
class FilesDock(QtWidgets.QDockWidget):
|
2016-06-21 19:14:22 +08:00
|
|
|
dataset_activated = QtCore.pyqtSignal(str)
|
|
|
|
dataset_changed = QtCore.pyqtSignal(str)
|
2016-07-10 00:29:01 +08:00
|
|
|
metadata_changed = QtCore.pyqtSignal(dict)
|
2016-06-21 19:14:22 +08:00
|
|
|
|
2016-07-31 23:24:08 +08:00
|
|
|
def __init__(self, datasets, browse_root=""):
|
2016-04-14 17:55:44 +08:00
|
|
|
QtWidgets.QDockWidget.__init__(self, "Files")
|
|
|
|
self.setObjectName("Files")
|
2023-09-17 06:52:13 +08:00
|
|
|
self.setFeatures(self.DockWidgetFeature.DockWidgetMovable | self.DockWidgetFeature.DockWidgetFloatable)
|
2016-04-10 16:22:24 +08:00
|
|
|
|
|
|
|
self.splitter = QtWidgets.QSplitter()
|
|
|
|
self.setWidget(self.splitter)
|
2016-04-05 15:51:04 +08:00
|
|
|
|
|
|
|
self.datasets = datasets
|
|
|
|
|
2016-05-26 00:46:21 +08:00
|
|
|
self.model = Hdf5FileSystemModel()
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
self.rt = QtWidgets.QTreeView()
|
2016-04-17 16:34:10 +08:00
|
|
|
rt_model = DirsOnlyProxy()
|
|
|
|
rt_model.setDynamicSortFilter(True)
|
|
|
|
rt_model.setSourceModel(self.model)
|
|
|
|
self.rt.setModel(rt_model)
|
|
|
|
self.model.directoryLoaded.connect(
|
|
|
|
lambda: self.rt.resizeColumnToContents(0))
|
|
|
|
self.rt.setAnimated(False)
|
2016-04-21 17:54:57 +08:00
|
|
|
if browse_root != "":
|
|
|
|
browse_root = os.path.abspath(browse_root)
|
2016-04-17 16:34:10 +08:00
|
|
|
self.rt.setRootIndex(rt_model.mapFromSource(
|
2016-04-21 17:54:57 +08:00
|
|
|
self.model.setRootPath(browse_root)))
|
|
|
|
self.rt.setHeaderHidden(True)
|
2023-09-17 06:52:13 +08:00
|
|
|
self.rt.setSelectionBehavior(self.rt.SelectionBehavior.SelectRows)
|
|
|
|
self.rt.setSelectionMode(self.rt.SelectionMode.SingleSelection)
|
2016-04-06 15:45:13 +08:00
|
|
|
self.rt.selectionModel().currentChanged.connect(
|
2016-04-06 18:16:40 +08:00
|
|
|
self.tree_current_changed)
|
2016-04-20 19:20:33 +08:00
|
|
|
self.rt.setRootIsDecorated(False)
|
2016-04-17 16:34:10 +08:00
|
|
|
for i in range(1, 4):
|
|
|
|
self.rt.hideColumn(i)
|
2016-04-10 16:22:24 +08:00
|
|
|
self.splitter.addWidget(self.rt)
|
2016-04-05 16:05:53 +08:00
|
|
|
|
2016-04-21 00:11:04 +08:00
|
|
|
self.rl = ZoomIconView()
|
2016-04-17 16:34:10 +08:00
|
|
|
self.rl.setModel(self.model)
|
|
|
|
self.rl.selectionModel().currentChanged.connect(
|
|
|
|
self.list_current_changed)
|
2016-05-16 17:45:13 +08:00
|
|
|
self.rl.activated.connect(self.list_activated)
|
2016-04-10 16:22:24 +08:00
|
|
|
self.splitter.addWidget(self.rl)
|
2016-04-05 17:17:02 +08:00
|
|
|
|
2016-04-06 18:16:40 +08:00
|
|
|
def tree_current_changed(self, current, previous):
|
2016-04-17 16:34:10 +08:00
|
|
|
idx = self.rt.model().mapToSource(current)
|
|
|
|
self.rl.setRootIndex(idx)
|
2016-04-06 18:16:40 +08:00
|
|
|
|
|
|
|
def list_current_changed(self, current, previous):
|
2016-04-17 16:34:10 +08:00
|
|
|
info = self.model.fileInfo(current)
|
2016-04-18 23:10:40 +08:00
|
|
|
f = open_h5(info)
|
|
|
|
if not f:
|
2016-04-08 11:54:49 +08:00
|
|
|
return
|
2016-06-12 13:11:36 +08:00
|
|
|
logger.debug("loading datasets from %s", info.filePath())
|
2016-04-08 11:44:37 +08:00
|
|
|
with f:
|
2016-07-10 00:29:01 +08:00
|
|
|
try:
|
2022-12-02 16:30:58 +08:00
|
|
|
expid = pyon.decode(f["expid"][()]) if "expid" in f else dict()
|
|
|
|
start_time = datetime.fromtimestamp(f["start_time"][()]) if "start_time" in f else "<none>"
|
2016-07-10 00:29:01 +08:00
|
|
|
v = {
|
2022-12-07 12:11:31 +08:00
|
|
|
"artiq_version": f["artiq_version"].asstr()[()] if "artiq_version" in f else "<none>",
|
2022-12-02 16:30:58 +08:00
|
|
|
"repo_rev": expid.get("repo_rev", "<none>"),
|
2022-03-20 12:58:55 +08:00
|
|
|
"file": expid.get("file", "<none>"),
|
2022-12-02 16:30:58 +08:00
|
|
|
"class_name": expid.get("class_name", "<none>"),
|
|
|
|
"rid": f["rid"][()] if "rid" in f else "<none>",
|
2016-07-10 00:29:01 +08:00
|
|
|
"start_time": start_time,
|
|
|
|
}
|
|
|
|
self.metadata_changed.emit(v)
|
|
|
|
except:
|
|
|
|
logger.warning("unable to read metadata from %s",
|
|
|
|
info.filePath(), exc_info=True)
|
2022-04-06 11:47:49 +08:00
|
|
|
|
|
|
|
rd = {}
|
2016-10-18 17:08:36 +08:00
|
|
|
if "archive" in f:
|
2022-04-06 11:47:49 +08:00
|
|
|
def visitor(k, v):
|
|
|
|
if isinstance(v, h5py.Dataset):
|
2023-07-10 10:52:47 +08:00
|
|
|
# v.attrs is a non-serializable h5py.AttributeManager, need to convert to dict
|
|
|
|
# See https://docs.h5py.org/en/stable/high/attr.html#h5py.AttributeManager
|
|
|
|
rd[k] = (True, v[()], dict(v.attrs))
|
2022-04-06 11:47:49 +08:00
|
|
|
|
|
|
|
f["archive"].visititems(visitor)
|
|
|
|
|
2016-07-10 00:29:01 +08:00
|
|
|
if "datasets" in f:
|
2022-04-06 11:47:49 +08:00
|
|
|
def visitor(k, v):
|
|
|
|
if isinstance(v, h5py.Dataset):
|
|
|
|
if k in rd:
|
|
|
|
logger.warning("dataset '%s' is both in archive "
|
|
|
|
"and outputs", k)
|
2023-07-10 10:52:47 +08:00
|
|
|
# v.attrs is a non-serializable h5py.AttributeManager, need to convert to dict
|
|
|
|
# See https://docs.h5py.org/en/stable/high/attr.html#h5py.AttributeManager
|
|
|
|
rd[k] = (True, v[()], dict(v.attrs))
|
2022-04-06 11:47:49 +08:00
|
|
|
|
|
|
|
f["datasets"].visititems(visitor)
|
2022-04-06 11:44:01 +08:00
|
|
|
|
|
|
|
self.datasets.init(rd)
|
|
|
|
|
2016-06-21 19:14:22 +08:00
|
|
|
self.dataset_changed.emit(info.filePath())
|
2016-04-04 23:37:51 +08:00
|
|
|
|
2016-05-16 17:45:13 +08:00
|
|
|
def list_activated(self, idx):
|
2016-06-21 19:14:22 +08:00
|
|
|
info = self.model.fileInfo(idx)
|
|
|
|
if not info.isDir():
|
|
|
|
self.dataset_activated.emit(info.filePath())
|
2016-05-16 17:45:13 +08:00
|
|
|
return
|
|
|
|
self.rl.setRootIndex(idx)
|
|
|
|
idx = self.rt.model().mapFromSource(idx)
|
|
|
|
self.rt.expand(idx)
|
|
|
|
self.rt.setCurrentIndex(idx)
|
|
|
|
|
2016-07-10 00:29:01 +08:00
|
|
|
def select(self, path):
|
|
|
|
f = os.path.abspath(path)
|
|
|
|
if os.path.isdir(f):
|
|
|
|
self.select_dir(f)
|
|
|
|
else:
|
|
|
|
self.select_file(f)
|
|
|
|
|
2016-04-17 16:34:10 +08:00
|
|
|
def select_dir(self, path):
|
|
|
|
if not os.path.exists(path):
|
2016-07-31 23:24:08 +08:00
|
|
|
logger.warning("directory does not exist %s", path)
|
2016-04-17 16:34:10 +08:00
|
|
|
return
|
|
|
|
idx = self.model.index(path)
|
2016-04-20 16:37:02 +08:00
|
|
|
if not idx.isValid():
|
2016-07-31 23:24:08 +08:00
|
|
|
logger.warning("directory invalid %s", path)
|
2016-04-20 16:37:02 +08:00
|
|
|
return
|
2016-04-17 16:34:10 +08:00
|
|
|
self.rl.setRootIndex(idx)
|
|
|
|
|
2016-04-21 01:16:19 +08:00
|
|
|
# ugly, see Spyder: late indexing, late scroll
|
2016-04-17 16:34:10 +08:00
|
|
|
def scroll_when_loaded(p):
|
|
|
|
if p != path:
|
|
|
|
return
|
|
|
|
self.model.directoryLoaded.disconnect(scroll_when_loaded)
|
|
|
|
QtCore.QTimer.singleShot(
|
2016-04-27 19:21:15 +08:00
|
|
|
100,
|
|
|
|
lambda: self.rt.scrollTo(
|
2016-04-21 01:16:19 +08:00
|
|
|
self.rt.model().mapFromSource(self.model.index(path)),
|
2023-09-17 06:52:13 +08:00
|
|
|
self.rt.ScrollHint.PositionAtCenter)
|
2016-04-27 19:21:15 +08:00
|
|
|
)
|
2016-04-17 16:34:10 +08:00
|
|
|
self.model.directoryLoaded.connect(scroll_when_loaded)
|
2016-04-20 03:59:02 +08:00
|
|
|
idx = self.rt.model().mapFromSource(idx)
|
|
|
|
self.rt.expand(idx)
|
2016-04-20 19:20:33 +08:00
|
|
|
self.rt.setCurrentIndex(idx)
|
2016-04-17 16:34:10 +08:00
|
|
|
|
|
|
|
def select_file(self, path):
|
|
|
|
if not os.path.exists(path):
|
2016-07-31 23:24:08 +08:00
|
|
|
logger.warning("file does not exist %s", path)
|
2016-04-17 16:34:10 +08:00
|
|
|
return
|
|
|
|
self.select_dir(os.path.dirname(path))
|
2016-04-20 16:37:02 +08:00
|
|
|
idx = self.model.index(path)
|
|
|
|
if not idx.isValid():
|
2016-07-31 23:24:08 +08:00
|
|
|
logger.warning("file invalid %s", path)
|
2016-04-20 16:37:02 +08:00
|
|
|
return
|
|
|
|
self.rl.setCurrentIndex(idx)
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
def save_state(self):
|
2016-07-31 23:24:08 +08:00
|
|
|
state = {
|
2016-04-20 16:37:02 +08:00
|
|
|
"dir": self.model.filePath(self.rl.rootIndex()),
|
2016-04-10 16:22:24 +08:00
|
|
|
"splitter": bytes(self.splitter.saveState()),
|
2016-04-04 23:37:51 +08:00
|
|
|
}
|
2016-07-31 23:24:08 +08:00
|
|
|
idx = self.rl.currentIndex()
|
|
|
|
if idx.isValid():
|
|
|
|
state["file"] = self.model.filePath(idx)
|
|
|
|
else:
|
|
|
|
state["file"] = None
|
|
|
|
return state
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
def restore_state(self, state):
|
2016-04-20 19:57:12 +08:00
|
|
|
self.splitter.restoreState(QtCore.QByteArray(state["splitter"]))
|
2016-07-31 23:24:08 +08:00
|
|
|
self.select_dir(state["dir"])
|
|
|
|
if state["file"] is not None:
|
|
|
|
self.select_file(state["file"])
|