def walkToObject(root_object, object_key): if isinstance(object_key, basestring): path = [safeDataKey(key) for key in object_key.split('.')] else: path = [safeDataKey(key) for key in object_key] try: _object = root_object[path[0]] except KeyError: raise KeyError, WALK_ERRMSG % (dottedPath(root_object), path[0]) if len(path) == 1: return _object else: return walkToObject(_object, path[1:])
def _setObjectAttribute_(self, _object, attr_name, attr_value): """ Returns a dictionary of attr_name/attr_value pairs for all setable attributes of the dataset . """ try: _object.attrs[safeDataKey(attr_name)] = safestring(attr_value) except Exception as e: errmsg = "Could not set attribute '%s' to '%s' for object '%s'" errmsg = errmsg % (attr_name, str(attr_value), _object.name) e.args = (errmsg, ) + e.args
def _deleteObjectAttribute_(self, _object, attr_name): """ Deletes an attribute of an object. """ try: del _object.attrs[safeDataKey(attr_name)] except Exception as e: errmsg = "Could not delete attribute '%s' of object '%s'" errmsg = errmsg % (attr_name, _object.name) e.args = (errmsg, ) + e.args raise
def _getObjectAttribute_(self, _object, attr_name, default=BOGUS_VALUE): """ Returns a the value of a single attribute of an object """ try: return _object.attrs[safeDataKey(attr_name)] except Exception as e: if default != BOGUS_VALUE: return default errmsg = "ERROR retrieving attribute '%s' from object '%s'" e.args = (errmsg % (attr_name, _object.name), ) + e.args raise e
def _getDatasetAttribute_(self, parent, dataset_name, attr_name, default=BOGUS_VALUE): """ Returns a the value of a single attribute of the dataset indicated by dataset_key. """ dataset = self._getDataset_(parent, dataset_name) try: return dataset.attrs[safeDataKey(attr_name)] except Exception as e: if default != BOGUS_VALUE: return default errmsg = "ERROR retrieving attribute '%s' from dataset '%s'" e.args = (errmsg % (attr_name, dataset_name), ) + e.args raise e
def hdfObjectKey(self, dot_path): return safeDataKey(dot_path).replace('.', '/')
def _updateDataset_(self, parent, dataset_name, numpy_array, attributes, **kwargs): """ Update a dataset in the data file. If the dataset does not exist, it is created. Returns a pointer to the dataset. """ dataset = self._getDataset_(parent, dataset_name) array_dimensions = len(numpy_array.shape) dataset_dimensions = len(dataset.shape) if array_dimensions > dataset_dimensions: errmsg = 'Cannot insert array of shape %s into dataset of shape %s' raise IndexError, errmsg % (str( numpy_array.shape), str(dataset.shape), indx) if 'indexes' in kwargs: index_strings = [] for indx in kwargs['indexes']: if isinstance(indx, (tuple, list)): index_strings.append(':'.join([str(it) for it in indx])) else: index_strings.append(str(indx)) subset = ','.join(index_strings) update_string = 'dataset[%s] = numpy_array' % subset eval(update_string) elif 'index' in kwargs: indx = kwargs['index'] from_indexes = ','.join([':' for dim in range(array_dimensions)]) from_array = 'numpy_array[%s]' % from_indexes if array_dimensions < dataset_dimensions: indexes = ','.join( [':' for dim in range(dataset_dimensions - 1)]) to_array = 'dataset[%d,%s]' % (int(indx), indexes) update_string = 'dataset[%s] = numpy_array[%s]' % subset eval(update_string) else: errmsg = 'Cannot insert %s array into %s dataset as index %d.' raise IndexError, errmsg % (str( numpy_array.shape), str(dataset.shape), indx) else: if numpy_array.shape == dataset.shape: indexes = ','.join( [':' for dim in range(array_dimensions - 1)]) update_string = 'dataset[%s] = numpy_array[%s]' % (indexes, indexes) eval(update_string) elif numpy_array.shape < dataset.shape: errmsg = 'Not enough information to insert %s array into %s dataset.' raise IndexError, errmsg % (str( numpy_array.shape), str(dataset.shape)) for attr_name, attr_value in attributes.items(): try: dataset.attrs[safeDataKey(attr_name)] = safevalue(attr_value) except Exception as e: errmsg = "Could not set attribute '%s' to '%s' for dataset '%s'" ds_name = dataset.name if ds_name.startswith('/'): ds_name = ds_name[1:] errmsg = errmsg % (attr_name, str(attr_value), ds_name) e.args = (errmsg, ) + e.args raise dataset.attrs['updated'] = self._timestamp_() return dataset