-
Notifications
You must be signed in to change notification settings - Fork 2
/
h5tojson.py
254 lines (204 loc) · 8.14 KB
/
h5tojson.py
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
#!/usr/bin/env python3
##############################################################################
# Copyright by The HDF Group. #
# All rights reserved. #
# #
# This file is part of H5Serv (HDF5 REST Server) Service, Libraries and #
# Utilities. The full HDF5 REST Server copyright notice, including #
# terms governing use, modification, and redistribution, is contained in #
# the file COPYING, which can be found at the root of the source code #
# distribution tree. If you do not have access to this file, you may #
# request a copy from [email protected]. #
##############################################################################
import sys
import json
import argparse
import os.path as op
import tempfile
import logging
import logging.handlers
from h5json import Hdf5db
from h5json import hdf5dtype
"""
DumpJson - return json representation of all objects within the given file
"""
class DumpJson:
def __init__(self, db, app_logger=None, options=None):
self.options = options
self.db = db
if app_logger:
self.log = app_logger
else:
self.log = logging.getLogger()
self.log.setLevel(logging.ERROR)
self.json = {}
def dumpAttribute(self, col_name, uuid, attr_name):
self.log.info("dumpAttribute: [" + attr_name + "]")
item = self.db.getAttributeItem(col_name, uuid, attr_name)
response = { 'name': attr_name }
typeItem = item['type']
response['type'] = hdf5dtype.getTypeResponse(typeItem)
response['shape'] = item['shape']
# if self.options and not self.options.D:
if 'value' not in item:
self.log.warning("no value key in attribute: " + attr_name)
else:
response['value'] = item['value'] # dump values unless header -D was passed
return response
def dumpAttributes(self, col_name, uuid):
attr_list = self.db.getAttributeItems(col_name, uuid)
self.log.info("dumpAttributes: " + uuid)
items = []
for attr in attr_list:
item = self.dumpAttribute(col_name, uuid, attr['name'])
items.append(item)
return items
def dumpLink(self, uuid, name):
item = self.db.getLinkItemByUuid(uuid, name)
for key in ('ctime', 'mtime', 'href'):
if key in item:
del item[key]
return item
def dumpLinks(self, uuid):
link_list = self.db.getLinkItems(uuid)
items = []
for link in link_list:
item = self.dumpLink(uuid, link['title'])
items.append(item)
return items
def dumpGroup(self, uuid):
item = self.db.getGroupItemByUuid(uuid)
if 'alias' in item:
alias = item['alias']
if alias:
self.log.info("dumpGroup alias: [" + alias[0] + "]")
for key in ('ctime', 'mtime', 'linkCount', 'attributeCount', 'id'):
if key in item:
del item[key]
attributes = self.dumpAttributes('groups', uuid)
if attributes:
item['attributes'] = attributes
links = self.dumpLinks(uuid)
if links:
item['links'] = links
return item
def dumpGroups(self):
groups = {}
item = self.dumpGroup(self.root_uuid)
groups[self.root_uuid] = item
uuids = self.db.getCollection("groups")
for uuid in uuids:
item = self.dumpGroup(uuid)
groups[uuid] = item
self.json['groups'] = groups
def dumpDataset(self, uuid):
response = { }
self.log.info("dumpDataset: " + uuid)
item = self.db.getDatasetItemByUuid(uuid)
if 'alias' in item:
alias = item['alias']
if alias:
self.log.info("dumpDataset alias: [" + alias[0] + "]")
response['alias'] = item['alias']
typeItem = item['type']
response['type'] = hdf5dtype.getTypeResponse(typeItem)
shapeItem = item['shape']
shape_rsp = {}
num_elements = 1
shape_rsp['class'] = shapeItem['class']
if 'dims' in shapeItem:
shape_rsp['dims'] = shapeItem['dims']
for dim in shapeItem['dims']:
num_elements *= dim
if 'maxdims' in shapeItem:
maxdims = []
for dim in shapeItem['maxdims']:
if dim == 0:
maxdims.append('H5S_UNLIMITED')
else:
maxdims.append(dim)
shape_rsp['maxdims'] = maxdims
response['shape'] = shape_rsp
if 'creationProperties' in item:
response['creationProperties'] = item['creationProperties']
attributes = self.dumpAttributes('datasets', uuid)
if attributes:
response['attributes'] = attributes
if self.options and not (self.options.D or self.options.d):
if num_elements > 0:
value = self.db.getDatasetValuesByUuid(uuid)
response['value'] = value # dump values unless header flag was passed
else:
response['value'] = [] # empty list
return response
def dumpDatasets(self):
uuids = self.db.getCollection("datasets")
if uuids:
datasets = {}
for uuid in uuids:
item = self.dumpDataset(uuid)
datasets[uuid] = item
self.json['datasets'] = datasets
def dumpDatatype(self, uuid):
response = { }
item = self.db.getCommittedTypeItemByUuid(uuid)
response['alias'] = item['alias']
typeItem = item['type']
response['type'] = hdf5dtype.getTypeResponse(typeItem)
attributes = self.dumpAttributes('datatypes', uuid)
if attributes:
response['attributes'] = attributes
return response
def dumpDatatypes(self):
uuids = self.db.getCollection("datatypes")
if uuids:
datatypes = {}
for uuid in uuids:
item = self.dumpDatatype(uuid)
datatypes[uuid] = item
self.json['datatypes'] = datatypes
def dumpFile(self):
self.root_uuid = self.db.getUUIDByPath('/')
db_version_info = self.db.getVersionInfo()
self.json['apiVersion'] = db_version_info['hdf5-json-version']
self.json['root'] = self.root_uuid
self.dumpGroups()
self.dumpDatasets()
self.dumpDatatypes()
# print(json.dumps(self.json, sort_keys=True, indent=4))
return self.json
"""
Generate a temporary filename to avoid problems with trying to create a dbfile
in a read-only directory. (See: https://github.com/HDFGroup/h5serv/issues/37)
"""
def getTempFileName():
f = tempfile.NamedTemporaryFile(delete=False)
f.close()
return f.name
def main():
parser = argparse.ArgumentParser(usage='%(prog)s [-h] [-D|-d] <hdf5_file>')
parser.add_argument('-D', action='store_true', help='surpress all data output')
parser.add_argument('-d', action='store_true', help='surpress data output for' +
' datasets (but not attribute values)')
parser.add_argument('filename', nargs='+', help='HDF5 to be converted to json')
args = parser.parse_args()
# create logger
log = logging.getLogger("h5serv")
# log.setLevel(logging.WARN)
log.setLevel(logging.INFO)
# add log handler
handler = logging.FileHandler('./h5tojson.log')
# add handler to logger
log.addHandler(handler)
filename = args.filename[0]
if not op.isfile(filename):
sys.exit("Cannot find file: %s" % filename)
log.info("h5tojson " + filename)
dbFilename = getTempFileName()
log.info("Using dbFile: " + dbFilename)
with Hdf5db(filename, dbFilePath=dbFilename, readonly=True, app_logger=log) as db:
dumper = DumpJson(db, app_logger=log, options=args)
data = dumper.dumpFile()
print(json.dumps(data, sort_keys=True, indent=4))
if __name__ == '__main__':
main()