| 1 | # Copyright (c) 2017, German Neuroinformatics Node (G-Node) |
| 2 | # |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted under the terms of the BSD License. See |
| 7 | # LICENSE file in the root of the Project. |
| 8 | from .source import Source |
| 9 | from .container import LinkContainer |
| 10 | from . import util |
| 11 | |
| 12 | |
| 13 | class SourceLinkContainer(LinkContainer): |
| 14 | |
| 15 | # TODO: Sources returned from this container have an incorrect _parent |
| 16 | # This is the same issue that we have with Sections. It should probably be |
| 17 | # solved the same way |
| 18 | |
| 19 | # NOTE: Source requires a different link container since a valid Source |
| 20 | # may exist anywhere beneath the root Block's child sources. The check for |
| 21 | # the Source's validity needs to be done with the find_sources() function |
| 22 | # down the entire tree, as opposed to the default LinkContainer.append() |
| 23 | # function which needs to only check the root Block's container. |
| 24 | |
| 25 | def __init__(self, parent): |
| 26 | """ |
| 27 | LinkContainer for Sources. |
| 28 | """ |
| 29 | super(SourceLinkContainer, self).__init__("sources", parent, Source, |
| 30 | parent._parent.sources) |
| 31 | |
| 32 | def append(self, item): |
| 33 | if util.is_uuid(item): |
| 34 | item = self._inst_item(self._backend.get_by_id(item)) |
| 35 | |
| 36 | if not hasattr(item, "id"): |
| 37 | raise TypeError("NIX entity or id string required for append") |
| 38 | |
| 39 | if not self._itemstore._parent.find_sources(filtr=lambda x: |
| 40 | x.id == item.id): |
| 41 | raise RuntimeError("This item cannot be appended here.") |
| 42 | |
| 43 | self._backend.create_link(item, item.id) |