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
|
# 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.
#
# ###########################################################################*/
"""Mock for silx.gui.hdf5 module"""
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "12/04/2017"
import numpy
try:
import h5py
except ImportError:
h5py = None
class Node(object):
def __init__(self, basename, parent, h5py_class):
self.basename = basename
self.h5py_class = h5py_class
self.attrs = {}
self.parent = parent
if parent is not None:
self.parent._add(self)
@property
def name(self):
if self.parent is None:
return self.basename
if self.parent.name == "":
return self.basename
return self.parent.name + "/" + self.basename
@property
def file(self):
if self.parent is None:
return self
return self.parent.file
class Group(Node):
"""Mock an h5py Group"""
def __init__(self, name, parent, h5py_class=h5py.Group):
super(Group, self).__init__(name, parent, h5py_class)
self.__items = {}
def _add(self, node):
self.__items[node.basename] = node
def __getitem__(self, key):
return self.__items[key]
def __iter__(self):
for k in self.__items:
yield k
def __len__(self):
return len(self.__items)
def get(self, name, getclass=False, getlink=False):
result = self.__items[name]
if getclass:
return result.h5py_class
return result
def create_dataset(self, name, data):
return Dataset(name, self, data)
def create_group(self, name):
return Group(name, self)
def create_NXentry(self, name):
group = Group(name, self)
group.attrs["NX_class"] = "NXentry"
return group
class File(Group):
"""Mock an h5py File"""
def __init__(self, filename):
super(File, self).__init__("", None, h5py.File)
self.filename = filename
class Dataset(Node):
"""Mock an h5py Dataset"""
def __init__(self, name, parent, value):
super(Dataset, self).__init__(name, parent, h5py.Dataset)
self.__value = value
self.shape = self.__value.shape
self.dtype = self.__value.dtype
self.size = self.__value.size
self.compression = None
self.compression_opts = None
def __getitem__(self, key):
if not isinstance(self.__value, numpy.ndarray):
if key == tuple():
return self.__value
elif key == Ellipsis:
return numpy.array(self.__value)
else:
raise ValueError("Bad key")
return self.__value[key]
|