From f76a283d5cf63c15e396463366cd78115710e7cb Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 4 May 2020 22:11:03 +0200 Subject: [PATCH] Cleanup: Use file references whenever appropriate - Removed hacky parent traversal for getting root objects like the top level metadata container. - Removed H5Group utility functions that are now obsolete. - Containers that need to do a search from the root object to delete objects now use the file reference. --- nixio/container.py | 12 +++------- nixio/hdf5/h5group.py | 53 ------------------------------------------- nixio/section.py | 15 +----------- 3 files changed, 4 insertions(+), 76 deletions(-) diff --git a/nixio/container.py b/nixio/container.py index ac4febb9..0804a81a 100644 --- a/nixio/container.py +++ b/nixio/container.py @@ -57,10 +57,7 @@ def __delitem__(self, item): self._itemclass.__name__) ) - root = self._backend.h5root - if not root: - root = self._parent._h5group - root.delete_all([item.id]) + self._file._h5group.delete_all([item.id]) def __iter__(self): for group in self._backend: @@ -122,8 +119,7 @@ def __delitem__(self, item): # the root block secids = [s.id for s in item.find_sections()] - root = self._backend.file - root.delete_all(secids) + self._file._h5group.delete_all(secids) class SourceContainer(Container): @@ -146,9 +142,7 @@ def __delitem__(self, item): # the root block srcids = [s.id for s in item.find_sources()] srcids.append(item.id) - - root = self._backend.h5root - root.delete_all(srcids) + self._file._h5group.delete_all(srcids) class LinkContainer(Container): diff --git a/nixio/hdf5/h5group.py b/nixio/hdf5/h5group.py index b7d6c703..28195042 100644 --- a/nixio/hdf5/h5group.py +++ b/nixio/hdf5/h5group.py @@ -12,11 +12,8 @@ from .h5dataset import H5DataSet from ..datatype import DataType -from ..block import Block -from ..section import Section from .. import util -from ..exceptions import InvalidEntity class H5Group(object): @@ -297,56 +294,6 @@ def change_id(_, igrp): g.visititems(change_id) return g - @property - def file(self): - """ - An H5Group object which represents the file root. - - :return: H5Group at '/' - """ - return H5Group(self.group.file, "/", create=False) - - @property - def h5root(self): - """ - Returns the H5Group of the Block or top-level Section which contains - this object. Returns None if requested on the file root '/' or the - /data or /metadata groups. - - :return: Top level object containing this group (H5Group) - """ - pathparts = self.group.name.split("/") - if len(pathparts) == 3: - return self - if self.group.name == "/": - return None - if len(pathparts) == 2: - return None - - return self.parent.h5root - - @property - def root(self): - """ - Returns the Block or top-level Section which contains this object. - Returns None if requested on the file root '/' or the /data or - /metadata groups. - - :return: Top level object containing this group (Block or Section) - """ - h5root = self.h5root - if h5root is None: - return None - topgroup = self.group.name.split("/")[1] - if topgroup == "data": - cls = Block - return cls(h5root.parent, h5root) - elif topgroup == "metadata": - cls = Section - return cls(h5root.parent, h5root) - else: - raise InvalidEntity - @property def parent(self): return self.create_from_h5obj(self._parent) diff --git a/nixio/section.py b/nixio/section.py index 85d1cdad..d3a14ab5 100644 --- a/nixio/section.py +++ b/nixio/section.py @@ -293,9 +293,8 @@ def parent(self): """ if self._sec_parent is not None: return self._sec_parent - rootmd = self._h5group.file.open_group("metadata") # BFS - sections = [Section(self.file, None, sg) for sg in rootmd] + sections = list(self.file.sections) if self in sections: # Top-level section return None @@ -309,18 +308,6 @@ def parent(self): return None - @property - def file(self): - """ - Root file object. - - :type: File - """ - par = self._parent - while isinstance(par, Entity): - par = par._parent - return par - @property def referring_objects(self): objs = []