Skip to content

Commit

Permalink
Cleanup: Use file references whenever appropriate
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
achilleas-k committed May 4, 2020
1 parent 2fc194e commit f76a283
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 76 deletions.
12 changes: 3 additions & 9 deletions nixio/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
53 changes: 0 additions & 53 deletions nixio/hdf5/h5group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions nixio/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down

0 comments on commit f76a283

Please sign in to comment.