Skip to content

Commit

Permalink
Allow NXlinks to have different names from the target
Browse files Browse the repository at this point in the history
There is not a technical reason for preventing linked items to have a
different name from the target, as they are implemented as hard links
in the HDF5 files.
  • Loading branch information
rayosborn committed Jul 3, 2015
1 parent 0f7cb4b commit 83e8371
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/nexusformat/nexus/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2825,8 +2825,6 @@ def __setitem__(self, key, value):
group._entries[key] = value
elif isinstance(value, NXlink) and group.nxroot is value.nxroot:
group._entries[key] = copy(value)
elif isinstance(value, NXlink) and key != value.nxname:
raise NeXusError("Cannot change the name of a linked object")
elif isinstance(value, NXobject):
if value.nxgroup:
memo = {}
Expand Down Expand Up @@ -3004,7 +3002,7 @@ def insert(self, value, name='unknown'):
else:
self[name] = NXfield(value=value, name=name, group=self)

def makelink(self, target):
def makelink(self, target, name=None):
"""
Creates a linked NXobject within the group.
Expand All @@ -3016,18 +3014,26 @@ def makelink(self, target):
if isinstance(self.nxroot, NXroot):
if self.nxroot == target.nxroot:
if isinstance(target, NXobject):
if target.nxname in self:
if name is None:
name = target.nxname
if name in self:
raise NeXusError(
"Object with the same name already exists in '%s'"
% self.nxpath)
self[target.nxname] = NXlink(target=target)
self[name] = NXlink(target=target, name=name)
else:
raise NeXusError("Link target must be an NXobject")
elif isinstance(target, NXfield):
if isinstance(target, NXlinkfield):
target = target.nxlink
self[target.nxname] = NXlink(target=target.nxpath,
file=target.nxfilename)
if name is None:
name = target.nxname
if name in self:
raise NeXusError(
"Object with the same name already exists in '%s'"
% self.nxpath)
self[name] = NXlink(target=target.nxpath, name=name,
file=target.nxfilename)
else:
raise NeXusError("Only a field can currently be linked externally")
else:
Expand Down Expand Up @@ -3209,7 +3215,10 @@ class NXlink(NXobject):
def __init__(self, target=None, name=None, file=None, **opts):
self._class = "NXlink"
if isinstance(target, NXobject):
self._name = target.nxname
if name is None:
self._name = target.nxname
else:
self._name = name
self._target = target.attrs["target"] = target.nxpath
if target.nxclass == "NXlink":
raise NeXusError("Cannot link to another NXlink object")
Expand Down Expand Up @@ -3259,9 +3268,6 @@ def _str_tree(self, indent=0, attrs=False, recursive=False):
else:
return " "*indent+self.nxname+' -> '+self._target

def rename(self, name):
raise NeXusError("Cannot rename a linked object")

def _getlink(self):
return self.nxroot[self._target]

Expand Down

0 comments on commit 83e8371

Please sign in to comment.