diff options
Diffstat (limited to 'silx/io/commonh5.py')
-rw-r--r-- | silx/io/commonh5.py | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/silx/io/commonh5.py b/silx/io/commonh5.py index 02c4181..4fbcd08 100644 --- a/silx/io/commonh5.py +++ b/silx/io/commonh5.py @@ -1,6 +1,6 @@ # coding: utf-8 # /*########################################################################## -# Copyright (C) 2016-2017 European Synchrotron Radiation Facility +# Copyright (C) 2016-2018 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -32,12 +32,13 @@ import collections import h5py import numpy from silx.third_party import six +import weakref from .utils import is_dataset __authors__ = ["V. Valls", "P. Knobel"] __license__ = "MIT" -__date__ = "02/10/2017" +__date__ = "11/10/2017" class _MappingProxyType(collections.MutableMapping): @@ -90,7 +91,7 @@ class Node(object): """ def __init__(self, name, parent=None, attrs=None): - self.__parent = parent + self._set_parent(parent) self.__basename = name self.__attrs = {} if attrs is not None: @@ -114,7 +115,25 @@ class Node(object): :rtype: Node """ - return self.__parent + if self.__parent is None: + parent = None + else: + parent = self.__parent() + if parent is None: + self.__parent = None + return parent + + def _set_parent(self, parent): + """Set the parent of this node. + + It do not update the parent object. + + :param Node parent: New parent for this node + """ + if parent is not None: + self.__parent = weakref.ref(parent) + else: + self.__parent = None @property def file(self): @@ -123,22 +142,13 @@ class Node(object): :rtype: Node """ node = self - while node.__parent is not None: - node = node.__parent + while node.parent is not None: + node = node.parent if isinstance(node, File): return node else: return None - def _set_parent(self, parent): - """Set the parent of this node. - - It do not update the parent object. - - :param Node parent: New parent for this node - """ - self.__parent = parent - @property def attrs(self): """Returns HDF5 attributes of this node. @@ -154,11 +164,12 @@ class Node(object): def name(self): """Returns the HDF5 name of this node. """ - if self.__parent is None: + parent = self.parent + if parent is None: return "/" - if self.__parent.name == "/": + if parent.name == "/": return "/" + self.basename - return self.__parent.name + "/" + self.basename + return parent.name + "/" + self.basename @property def basename(self): @@ -709,7 +720,7 @@ class Group(Node): def __getitem__(self, name): """Return a child from his name. - :param name str: name of a member or a path throug members using '/' + :param str name: name of a member or a path throug members using '/' separator. A '/' as a prefix access to the root item of the tree. :rtype: Node """ @@ -806,7 +817,7 @@ class Group(Node): See the documentation for `h5py.Group.visit` for more help. :param func: Callable (function, method or callable object) - :type func: function + :type func: callable """ origin_name = self.name return self._visit(func, origin_name, visit_links) @@ -816,7 +827,7 @@ class Group(Node): See the documentation for `h5py.Group.visititems` for more help. :param func: Callable (function, method or callable object) - :type func: function + :type func: callable :param bool visit_links: If *False*, ignore links. If *True*, call `func(name)` for links and recurse into target groups. """ |