summaryrefslogtreecommitdiff
path: root/silx/gui/hdf5/_utils.py
blob: ddf4db54bd3cdd0559d09ac3c8a5946dc32c857e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This package provides a set of helper class and function used by the
package `silx.gui.hdf5` package.
"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "20/12/2017"


import logging
from .. import qt
import silx.io.utils
from silx.utils.html import escape

_logger = logging.getLogger(__name__)


class Hdf5ContextMenuEvent(object):
    """Hold information provided to context menu callbacks."""

    def __init__(self, source, menu, hoveredObject):
        """
        Constructor

        :param QWidget source: Widget source
        :param QMenu menu: Context menu which will be displayed
        :param H5Node hoveredObject: Hovered H5 node
        """
        self.__source = source
        self.__menu = menu
        self.__hoveredObject = hoveredObject

    def source(self):
        """Source of the event

        :rtype: Hdf5TreeView
        """
        return self.__source

    def menu(self):
        """Menu which will be displayed

        :rtype: qt.QMenu
        """
        return self.__menu

    def hoveredObject(self):
        """Item content hovered by the mouse when the context menu was
        requested

        :rtype: H5Node
        """
        return self.__hoveredObject


def htmlFromDict(dictionary, title=None):
    """Generate a readable HTML from a dictionary

    :param dict dictionary: A Dictionary
    :rtype: str
    """
    result = """<html>
        <head>
        <style type="text/css">
        ul { -qt-list-indent: 0; list-style: none; }
        li > b {display: inline-block; min-width: 4em; font-weight: bold; }
        </style>
        </head>
        <body>
        """
    if title is not None:
        result += "<b>%s</b>" % escape(title)
    result += "<ul>"
    for key, value in dictionary.items():
        result += "<li><b>%s</b>: %s</li>" % (escape(key), escape(value))
    result += "</ul>"
    result += "</body></html>"
    return result


class Hdf5NodeMimeData(qt.QMimeData):
    """Mimedata class to identify an internal drag and drop of a Hdf5Node."""

    MIME_TYPE = "application/x-internal-h5py-node"

    def __init__(self, node=None):
        qt.QMimeData.__init__(self)
        self.__node = node
        self.setData(self.MIME_TYPE, "".encode(encoding='utf-8'))

    def node(self):
        return self.__node


class H5Node(object):
    """Adapter over an h5py object to provide missing informations from h5py
    nodes, like internal node path and filename (which are not provided by
    :mod:`h5py` for soft and external links).

    It also provides an abstraction to reach node type for mimicked h5py
    objects.
    """

    def __init__(self, h5py_item=None):
        """Constructor

        :param Hdf5Item h5py_item: An Hdf5Item
        """
        self.__h5py_object = h5py_item.obj
        self.__h5py_target = None
        self.__h5py_item = h5py_item

    def __getattr__(self, name):
        if hasattr(self.__h5py_object, name):
            attr = getattr(self.__h5py_object, name)
            return attr
        raise AttributeError("H5Node has no attribute %s" % name)

    def __get_target(self, obj):
        """
        Return the actual physical target of the provided object.

        Objects can contains links in the middle of the path, this function
        check each groups and remove this prefix in case of the link by the
        link of the path.

        :param obj: A valid h5py object (File, group or dataset)
        :type obj: h5py.Dataset or h5py.Group or h5py.File
        :rtype: h5py.Dataset or h5py.Group or h5py.File
        """
        elements = obj.name.split("/")
        if obj.name == "/":
            return obj
        elif obj.name.startswith("/"):
            elements.pop(0)
        path = ""
        while len(elements) > 0:
            e = elements.pop(0)
            path = path + "/" + e
            link = obj.parent.get(path, getlink=True)
            classlink = silx.io.utils.get_h5_class(link)

            if classlink == silx.io.utils.H5Type.EXTERNAL_LINK:
                subpath = "/".join(elements)
                external_obj = obj.parent.get(self.basename + "/" + subpath)
                return self.__get_target(external_obj)
            elif classlink == silx.io.utils.H5Type.SOFT_LINK:
                # Restart from this stat
                path = ""
                root_elements = link.path.split("/")
                if link.path == "/":
                    root_elements = []
                elif link.path.startswith("/"):
                    root_elements.pop(0)
                for name in reversed(root_elements):
                    elements.insert(0, name)

        return obj.file[path]

    @property
    def h5py_target(self):
        if self.__h5py_target is not None:
            return self.__h5py_target
        self.__h5py_target = self.__get_target(self.__h5py_object)
        return self.__h5py_target

    @property
    def h5py_object(self):
        """Returns the internal h5py node.

        :rtype: h5py.File or h5py.Group or h5py.Dataset
        """
        return self.__h5py_object

    @property
    def h5type(self):
        """Returns the node type, as an H5Type.

        :rtype: H5Node
        """
        return silx.io.utils.get_h5_class(self.__h5py_object)

    @property
    def ntype(self):
        """Returns the node type, as an h5py class.

        :rtype:
            :class:`h5py.File`, :class:`h5py.Group` or :class:`h5py.Dataset`
        """
        type_ = self.h5type
        return silx.io.utils.h5type_to_h5py_class(type_)

    @property
    def basename(self):
        """Returns the basename of this h5py node. It is the last identifier of
        the path.

        :rtype: str
        """
        return self.__h5py_object.name.split("/")[-1]

    @property
    def is_broken(self):
        """Returns true if the node is a broken link.

        :rtype: bool
        """
        if self.__h5py_item is None:
            raise RuntimeError("h5py_item is not defined")
        return self.__h5py_item.isBrokenObj()

    @property
    def local_name(self):
        """Returns the path from the master file root to this node.

        For links, this path is not equal to the h5py one.

        :rtype: str
        """
        if self.__h5py_item is None:
            raise RuntimeError("h5py_item is not defined")

        result = []
        item = self.__h5py_item
        while item is not None:
            # stop before the root item (item without parent)
            if item.parent.parent is None:
                name = item.obj.name
                if name != "/":
                    result.append(item.obj.name)
                break
            else:
                result.append(item.basename)
            item = item.parent
        if item is None:
            raise RuntimeError("The item does not have parent holding h5py.File")
        if result == []:
            return "/"
        if not result[-1].startswith("/"):
            result.append("")
        result.reverse()
        name = "/".join(result)
        return name

    def __get_local_file(self):
        """Returns the file of the root of this tree

        :rtype: h5py.File
        """
        item = self.__h5py_item
        while item.parent.parent is not None:
            class_ = silx.io.utils.get_h5_class(class_=item.h5pyClass)
            if class_ == silx.io.utils.H5Type.FILE:
                break
            item = item.parent

        class_ = silx.io.utils.get_h5_class(class_=item.h5pyClass)
        if class_ == silx.io.utils.H5Type.FILE:
            return item.obj
        else:
            return item.obj.file

    @property
    def local_file(self):
        """Returns the master file in which is this node.

        For path containing external links, this file is not equal to the h5py
        one.

        :rtype: h5py.File
        :raises RuntimeException: If no file are found
        """
        return self.__get_local_file()

    @property
    def local_filename(self):
        """Returns the filename from the master file of this node.

        For path containing external links, this path is not equal to the
        filename provided by h5py.

        :rtype: str
        :raises RuntimeException: If no file are found
        """
        return self.local_file.filename

    @property
    def local_basename(self):
        """Returns the basename from the master file root to this node.

        For path containing links, this basename can be different than the
        basename provided by h5py.

        :rtype: str
        """
        class_ = self.__h5py_item.h5Class
        if class_ is not None and class_ == silx.io.utils.H5Type.FILE:
            return ""
        return self.__h5py_item.basename

    @property
    def physical_file(self):
        """Returns the physical file in which is this node.

        .. versionadded:: 0.6

        :rtype: h5py.File
        :raises RuntimeError: If no file are found
        """
        class_ = silx.io.utils.get_h5_class(self.__h5py_object)
        if class_ == silx.io.utils.H5Type.EXTERNAL_LINK:
            # It means the link is broken
            raise RuntimeError("No file node found")
        if class_ == silx.io.utils.H5Type.SOFT_LINK:
            # It means the link is broken
            return self.local_file

        physical_obj = self.h5py_target
        return physical_obj.file

    @property
    def physical_name(self):
        """Returns the path from the location this h5py node is physically
        stored.

        For broken links, this filename can be different from the
        filename provided by h5py.

        :rtype: str
        """
        class_ = silx.io.utils.get_h5_class(self.__h5py_object)
        if class_ == silx.io.utils.H5Type.EXTERNAL_LINK:
            # It means the link is broken
            return self.__h5py_object.path
        if class_ == silx.io.utils.H5Type.SOFT_LINK:
            # It means the link is broken
            return self.__h5py_object.path

        physical_obj = self.h5py_target
        return physical_obj.name

    @property
    def physical_filename(self):
        """Returns the filename from the location this h5py node is physically
        stored.

        For broken links, this filename can be different from the
        filename provided by h5py.

        :rtype: str
        """
        class_ = silx.io.utils.get_h5_class(self.__h5py_object)
        if class_ == silx.io.utils.H5Type.EXTERNAL_LINK:
            # It means the link is broken
            return self.__h5py_object.filename
        if class_ == silx.io.utils.H5Type.SOFT_LINK:
            # It means the link is broken
            return self.local_file.filename

        return self.physical_file.filename

    @property
    def physical_basename(self):
        """Returns the basename from the location this h5py node is physically
        stored.

        For broken links, this basename can be different from the
        basename provided by h5py.

        :rtype: str
        """
        return self.physical_name.split("/")[-1]