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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
##############################################################################
# Copyright by The HDF Group. #
# All rights reserved. #
# #
# This file is part of the HDF Compass Viewer. The full HDF Compass #
# copyright notice, including terms governing use, modification, and #
# 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 help@hdfgroup.org. #
##############################################################################
"""
Implementation of compass_model classes for HDF5 REST API.
"""
from itertools import groupby
import sys
import os.path as op
import posixpath as pp
import json
import requests
import numpy as np
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
from hdf_compass import compass_model
from hdf_compass.utils import url2path
from hdf_compass.hdf5rest_model import hdf5dtype
def get_json(endpoint, domain=None, uri=None):
# try to do a GET from the domain
req = endpoint
if uri is not None:
req += uri
headers = {}
if domain is not None:
headers['host'] = domain
logger.debug("GET: " + req)
rsp = requests.get(req, headers=headers, verify=False)
logger.debug("RSP: " + str(rsp.status_code) + ':' + rsp.text)
if rsp.status_code != 200:
raise IOError(rsp.reason)
rsp_json = json.loads(rsp.text)
return rsp_json
def sort_key(name):
""" Sorting key for names in an HDF5 group.
We provide "natural" sort order; e.g. "7" comes before "12".
"""
return [(int(''.join(g)) if k else ''.join(g)) for k, g in groupby(name, key=unicode.isdigit)]
class HDF5RestStore(compass_model.Store):
"""
Data store implementation for an HDF Service endpoint
Keys are valid h5paths to the object:
e.g.:
/g1
/g1/dset1.1
/g1/dtype
Values are URI's to the resource
/groups/<uuid>
/datasets/<uuid>
/datatypes/<uuid>
"""
@staticmethod
def plugin_name():
return "HDF5 Rest"
@staticmethod
def plugin_description():
return "A plugin used to access HDF Services."
def __contains__(self, key):
if key in self.f:
return True
try:
pkey = self.get_parent(key)
except KeyError:
return False
if pkey not in self.f:
# assumes that we've loaded any parents, before querying for children
return False
pkey_uri = self.f[pkey]
if not pkey_uri.startswith("/groups/"):
# if the parent is not a group, this is not a valid key
return False
# do a get on the link name
linkname = pp.basename(key)
contains = False
try:
link_json = self.get(pkey_uri + "/links/" + linkname)
if link_json["class"] == "H5L_TYPE_HARD":
logger.debug("add key to store:" + key)
self.f[key] = '/' + link_json["collection"] + '/' + link_json["id"]
contains = True
else:
pass # todo support soft/external links
except IOError:
# invalid link
# todo - verify it is a 404
logger.debug("invalid key:"+key)
return contains
@property
def url(self):
return self._url
@property
def display_name(self):
if self.domain:
return self.domain
else:
return self.endpoint
@property
def root(self):
return self['/']
@property
def valid(self):
return '/' in self.f
@staticmethod
def can_handle(url):
logger.debug("hdf5rest can_handle: " + url)
try:
flag = True
rsp_json = get_json(url)
for key in ("root", "created", "hrefs", "lastModified"):
if key not in rsp_json:
flag = False
break
logger.debug("able to handle %s? %r" % (url, flag))
return flag
except Exception:
logger.debug("able to handle %s? no" % url)
return False
return True
def __init__(self, url):
if not self.can_handle(url):
raise ValueError(url)
self._url = url
# extract domain if there's a "host" query param
queryParam = "host="
nindex = url.find('?' + queryParam)
if nindex < 0:
nindex = url.find('&' + queryParam)
if nindex > 0:
domain = url[(nindex + len(queryParam) + 1):]
# trim any additional query params
nindex = domain.find('&')
if nindex > 0:
domain = domain[:nindex]
self._domain = domain
else:
self._domain = None
nindex = url.find('?')
if nindex < 0:
self._endpoint = url
else:
self._endpoint = url[:(nindex)]
if self._endpoint.endswith('/'):
# trim any trailing '/'
self._endpoint = self._endpoint[:-1]
self.cache = {} # http cache
rsp = self.get('/')
self.f = {}
self.f['/'] = "/groups/" + rsp['root']
@property
def endpoint(self):
return self._endpoint
@property
def domain(self):
return self._domain
def get(self, uri):
if uri in self.cache:
rsp = self.cache[uri]
else:
rsp = get_json(self.endpoint, domain=self.domain, uri=uri)
self.cache[uri] = rsp
return rsp
def close(self):
self.f = {} # clear the key store
def get_parent(self, key):
# HDFCompass requires the parent of the root container be None
if key == "" or key == "/":
return None
pkey = pp.dirname(key)
if pkey == "":
pkey = "/"
if pkey not in self.f:
# is it possible to get to a key without traversing the parents?
# if so, we should query the server for the given path
raise KeyError("parent not found: " + pkey)
return self[pkey]
class HDF5RestGroup(compass_model.Container):
""" Represents an HDF5 group, to be displayed in the browser view. """
class_kind = "HDF5 Group"
@staticmethod
def can_handle(store, key):
return key in store and store.f[key].startswith("/groups/")
def get_names(self):
# Lazily build the list of names; this helps when browsing big files
if self._xnames is None:
rsp = self.store.get(self._uri + "/links")
self._xnames = []
links = rsp["links"]
logger.debug("got %d links for key: %s" % (len(links), self._key))
for link in links:
name = link["title"]
self._xnames.append(name)
link_key = pp.join(self.key, name)
if link_key not in self.store.f:
if link["class"] == "H5L_TYPE_HARD":
logger.debug("add key to store:" + link_key)
self.store.f[link_key] = '/' + link["collection"] + '/' + link["id"]
else:
pass # todo support soft/external links
# Natural sort is expensive
if len(self._xnames) < 1000:
self._xnames.sort(key=sort_key)
return self._xnames
def __init__(self, store, key):
self._store = store
self._key = key
self._uri = store.f[key]
self._xnames = None
rsp = store.get(self._uri)
self._count = rsp["linkCount"]
logger.debug("new group node: " + self._key)
self.get_names()
@property
def key(self):
return self._key
@property
def store(self):
return self._store
@property
def display_name(self):
name = pp.basename(self.key)
if name == "":
name = '/'
return name
@property
def display_title(self):
return "%s %s" % (self.store.display_name, self.key)
@property
def description(self):
return 'Group "%s" (%d members)' % (self.display_name, len(self))
def __len__(self):
return self._count
def __iter__(self):
for name in self._xnames:
yield self.store[pp.join(self.key, name)]
def __getitem__(self, idx):
name = self._xnames[idx]
return self.store[pp.join(self.key, name)]
class HDF5RestDataset(compass_model.Array):
""" Represents an HDF5 dataset. """
class_kind = "HDF5 Dataset"
@staticmethod
def can_handle(store, key):
return key in store and store.f[key].startswith("/datasets/")
def __init__(self, store, key):
self._store = store
self._key = key
self._uri = store.f[key]
rsp = store.get(self._uri)
shape_json = rsp["shape"]
if shape_json["class"] == "H5S_SCALAR":
self._shape = ()
elif shape_json["class"] == "H5S_SIMPLE":
self._shape = shape_json["dims"]
else:
raise IOError("Unexpected shape class: " + shape_json["class"])
type_json = rsp["type"]
self._dtype = hdf5dtype.createDataType(type_json)
@property
def key(self):
return self._key
@property
def store(self):
return self._store
@property
def display_name(self):
return pp.basename(self.key)
@property
def description(self):
return 'Dataset "%s"' % (self.display_name,)
@property
def shape(self):
return self._shape
@property
def dtype(self):
return self._dtype
def __getitem__(self, args):
logger.debug("getitem: " + str(args))
req = self._uri + "/value"
rank = len(self._shape)
if rank > 0:
sel_query = '['
for dim in range(rank):
if dim < len(args):
s = args[dim]
else:
s = slice(0, self._shape[dim])
sel_query += str(s.start)
sel_query += ':'
if s.stop > self._shape[dim]:
sel_query += str(self._shape[dim])
else:
sel_query += str(s.stop)
sel_query += ','
sel_query = sel_query[:-1] # trim trailing comma
sel_query += ']'
req += "?select=" + sel_query
rsp = self.store.get(req)
value = rsp["value"]
arr = np.array(value, dtype=self._dtype)
return arr
def is_plottable(self):
if self.dtype.kind == 'S':
logger.debug("Not plottable since ASCII String (characters: %d)" % self.dtype.itemsize)
return False
if self.dtype.kind == 'U':
logger.debug("Not plottable since Unicode String (characters: %d)" % self.dtype.itemsize)
return False
return True
class HDF5RestKV(compass_model.KeyValue):
""" A KeyValue node used for HDF5 attributes. """
class_kind = "HDF5 Attributes"
@staticmethod
def can_handle(store, key):
canhandle = False
if key in store:
uri = store.f[key]
if uri.startswith("/groups/"):
canhandle = True
elif uri.startswith("/datasets/"):
canhandle = True
elif uri.startswith("/datatypes/"):
canhandle = True
return canhandle
def __init__(self, store, key):
self._store = store
self._key = key
self._uri = store.f[key]
rsp = store.get(self._uri + "/attributes")
attributes = rsp["attributes"]
names = []
for attr in attributes:
names.append(attr["name"])
self._names = names
@property
def key(self):
return self._key
@property
def store(self):
return self._store
@property
def display_name(self):
n = pp.basename(self.key)
return n if n != '' else '/'
@property
def description(self):
return self.display_name
@property
def keys(self):
return self._names[:]
def __getitem__(self, name):
rsp = self._store.get(self._uri + "/attributes/" + name)
type_json = rsp["type"]
value_json = rsp["value"]
arr_dtype = hdf5dtype.createDataType(type_json)
arr = np.array(value_json, dtype=arr_dtype)
return arr
# Register handlers
HDF5RestStore.push(HDF5RestKV)
HDF5RestStore.push(HDF5RestDataset)
#HDF5RestStore.push(HDF5Text)
HDF5RestStore.push(HDF5RestGroup)
#HDF5RestStore.push(HDF5Image)
compass_model.push(HDF5RestStore)
|