| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from sys import maxsize |
| 10 |
|
| 11 |
from .import exceptions |
| 12 |
from .entity import Entity |
| 13 |
from .container import SourceContainer |
| 14 |
from . import util |
| 15 |
from .util import find as finders |
| 16 |
from .section import Section |
| 17 |
|
| 18 |
|
| 19 |
class Source(Entity): |
| 20 |
|
| 21 |
def __init__(self, nixfile, nixparent, h5group): |
| 22 |
super(Source, self).__init__(nixfile, nixparent, h5group) |
| 23 |
self._sources = None |
| 24 |
self._parent_block = None |
| 25 |
|
| 26 |
@classmethod |
| 27 |
def create_new(cls, nixfile, nixparent, h5parent, name, type_): |
| 28 |
newentity = super(Source, cls).create_new(nixfile, nixparent, h5parent, |
| 29 |
name, type_) |
| 30 |
return newentity |
| 31 |
|
| 32 |
|
| 33 |
def create_source(self, name, type_): |
| 34 |
""" |
| 35 |
Create a new source as a child of the current Source. |
| 36 |
|
| 37 |
:param name: The name of the source to create. |
| 38 |
:type name: str |
| 39 |
:param type_: The type of the source. |
| 40 |
:type type_: str |
| 41 |
|
| 42 |
:returns: The newly created source. |
| 43 |
:rtype: nixio.Source |
| 44 |
""" |
| 45 |
util.check_entity_name_and_type(name, type_) |
| 46 |
sources = self._h5group.open_group("sources", True) |
| 47 |
if name in sources: |
| 48 |
raise exceptions.DuplicateName("create_source") |
| 49 |
src = Source.create_new(self.file, self, sources, name, type_) |
| 50 |
return src |
| 51 |
|
| 52 |
@property |
| 53 |
def parent_block(self): |
| 54 |
""" |
| 55 |
Returns the block this source is contained in. |
| 56 |
|
| 57 |
:returns: the Block containing this source |
| 58 |
:rtype: nixio.Block |
| 59 |
""" |
| 60 |
if self._parent_block is not None: |
| 61 |
return self._parent_block |
| 62 |
maybe_block = self._parent |
| 63 |
if not hasattr(maybe_block, "data_arrays"): |
| 64 |
maybe_block = maybe_block.parent_block |
| 65 |
self._parent_block = maybe_block |
| 66 |
return self._parent_block |
| 67 |
|
| 68 |
def _find_parent_recursive(self, child_id, check_id=True): |
| 69 |
""" |
| 70 |
Find the parent source of this source. Search is based on the ID of of a child source. Searching by name might be ambiguous. |
| 71 |
By default it will raise an ValueError if the child_id does not look like an UUID. |
| 72 |
|
| 73 |
:param child_id: The ID of the child whose parent is searched. |
| 74 |
:type child_id: str |
| 75 |
:param check_id: Controls whether or not to check if the child_is looks like an UUID. Defaults to True. |
| 76 |
:type check_id: bool |
| 77 |
|
| 78 |
:returns: the parent source, if any, otherwise None. |
| 79 |
:rtype: nixio.Source |
| 80 |
""" |
| 81 |
if check_id and not util.is_uuid(child_id): |
| 82 |
raise ValueError("Error calling find_parent_source: argument child_id (%s) does not look like an UUID " % child_id) |
| 83 |
if child_id in self.sources: |
| 84 |
return self |
| 85 |
else: |
| 86 |
for s in self.sources: |
| 87 |
src = s._find_parent_recursive(child_id, False) |
| 88 |
if src is not None: |
| 89 |
return src |
| 90 |
return None |
| 91 |
|
| 92 |
@property |
| 93 |
def parent_source(self): |
| 94 |
""" |
| 95 |
Get the parent source of this source. If this source is at the root, None will be returned |
| 96 |
|
| 97 |
:returns: the parent source |
| 98 |
:rtype: Source or None |
| 99 |
""" |
| 100 |
|
| 101 |
block = self.parent_block |
| 102 |
if self in block.sources: |
| 103 |
return None |
| 104 |
for s in block.sources: |
| 105 |
p = s._find_parent_recursive(self.id, False) |
| 106 |
if p is not None: |
| 107 |
return p |
| 108 |
return None |
| 109 |
|
| 110 |
@property |
| 111 |
def referring_objects(self): |
| 112 |
""" |
| 113 |
Returns the list of entities that link to this source. |
| 114 |
|
| 115 |
:returns: all objects referring to this source. |
| 116 |
:rtype: list |
| 117 |
""" |
| 118 |
objs = [] |
| 119 |
objs.extend(self.referring_data_arrays) |
| 120 |
objs.extend(self.referring_tags) |
| 121 |
objs.extend(self.referring_multi_tags) |
| 122 |
return objs |
| 123 |
|
| 124 |
@property |
| 125 |
def referring_data_arrays(self): |
| 126 |
""" |
| 127 |
Returns all DataArray entities linking to this source. |
| 128 |
|
| 129 |
:returns: all DataArrays referring to this source. |
| 130 |
:rtype: list |
| 131 |
""" |
| 132 |
block = self.parent_block |
| 133 |
return [da for da in block.data_arrays if self in da.sources] |
| 134 |
|
| 135 |
@property |
| 136 |
def referring_tags(self): |
| 137 |
""" |
| 138 |
Returns all Tag entities linking to this source. |
| 139 |
|
| 140 |
:returns: all Tags referring to this source. |
| 141 |
:rtype: list |
| 142 |
""" |
| 143 |
block = self.parent_block |
| 144 |
return [tg for tg in block.tags if self in tg.sources] |
| 145 |
|
| 146 |
@property |
| 147 |
def referring_multi_tags(self): |
| 148 |
""" |
| 149 |
Returns all MultiTag entities linking to this source. |
| 150 |
|
| 151 |
:returns: all MultiTags referring to this source. |
| 152 |
:rtype: list |
| 153 |
""" |
| 154 |
block = self.parent_block |
| 155 |
return [mt for mt in block.multi_tags if self in mt.sources] |
| 156 |
|
| 157 |
def find_sources(self, filtr=lambda _: True, limit=None): |
| 158 |
""" |
| 159 |
Get all child sources of this source recursively. |
| 160 |
|
| 161 |
This method traverses the tree of all sources. The traversal |
| 162 |
is accomplished via breadth first and can be limited in depth. On each |
| 163 |
node or source a filter is applied. If the filter returns true the |
| 164 |
respective source will be added to the result list. |
| 165 |
By default a filter is used that accepts all sources. |
| 166 |
|
| 167 |
:param filtr: A filter function |
| 168 |
:type filtr: function |
| 169 |
:param limit: The maximum depth of traversal |
| 170 |
:type limit: int |
| 171 |
|
| 172 |
:returns: A list containing the matching sources. |
| 173 |
:rtype: list of nixio.Source |
| 174 |
""" |
| 175 |
if limit is None: |
| 176 |
limit = maxsize |
| 177 |
return finders._find_sources(self, filtr, limit) |
| 178 |
|
| 179 |
@property |
| 180 |
def sources(self): |
| 181 |
""" |
| 182 |
A property containing child sources of a Source. Sources can be |
| 183 |
obtained via their name, index, id. Sources can be deleted from the |
| 184 |
list. Adding sources is done using the Blocks create_source method. |
| 185 |
This is a read only attribute. |
| 186 |
""" |
| 187 |
if self._sources is None: |
| 188 |
self._sources = SourceContainer("sources", self.file, self, Source) |
| 189 |
return self._sources |
| 190 |
|
| 191 |
|
| 192 |
@property |
| 193 |
def metadata(self): |
| 194 |
""" |
| 195 |
|
| 196 |
Associated metadata of the entity. Sections attached to the entity via |
| 197 |
this attribute can provide additional annotations. This is an optional |
| 198 |
read-write property, and can be None if no metadata is available. |
| 199 |
|
| 200 |
:type: nixio.Section |
| 201 |
""" |
| 202 |
if "metadata" in self._h5group: |
| 203 |
return Section(self.file, None, self._h5group.open_group("metadata")) |
| 204 |
else: |
| 205 |
return None |
| 206 |
|
| 207 |
@metadata.setter |
| 208 |
def metadata(self, sect): |
| 209 |
""" |
| 210 |
Setter for the metadata section that contains meta information about this Source. |
| 211 |
|
| 212 |
:param sect: The metadata section. |
| 213 |
:type sect: nixio.Section |
| 214 |
|
| 215 |
:raises: TypeError if sect is not of type Section |
| 216 |
""" |
| 217 |
if not isinstance(sect, Section): |
| 218 |
raise TypeError("{} is not of type Section".format(sect)) |
| 219 |
self._h5group.create_link(sect, "metadata") |
| 220 |
|
| 221 |
@metadata.deleter |
| 222 |
def metadata(self): |
| 223 |
if "metadata" in self._h5group: |
| 224 |
self._h5group.delete("metadata") |