Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-soft-links #214

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions COPYING
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ About the NeXpy Development Team
----------------------------------

The NeXpy Development Team is the set of all contributors to the NeXpy
project on the Github at https://github.com/nexpy/nexpy. The project is
project on the Github at https://github.com/nexpy. The project is
currently led by Ray Osborn.

Copyright
Expand All @@ -47,7 +47,7 @@ The following banner should be used in any source code file to indicate the
copyright and license terms:

# -----------------------------------------------------------------------------
# Copyright (c) 2014-2021, NeXpy Development Team.
# Copyright (c) 2014-2024, NeXpy Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
Expand Down
10 changes: 7 additions & 3 deletions src/nexusformat/nexus/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5271,8 +5271,6 @@ def __init__(self, target=None, file=None, name=None, group=None,
if name is None and is_text(target):
self._name = target.rsplit('/', 1)[1]
self._target = text(target)
if not self._target.startswith('/'):
self._target = '/' + self._target
self._link = None

def __repr__(self):
Expand Down Expand Up @@ -5422,7 +5420,13 @@ def initialize_link(self):
@property
def internal_link(self):
"""Return NXfield or NXgroup targeted by an internal link."""
return self.nxroot[self._target]
if Path(self._target).is_absolute():
return self.nxroot[self._target]
else:
try:
return self.nxgroup[self._target]
except NeXusError:
return self.nxroot[self._target]

@property
def external_link(self):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,36 @@ def test_external_group_files(tmpdir, field1a):
assert not root["entry/g1_link"].nxfile.locked

assert not root.nxfile.locked

@pytest.mark.parametrize("save", ["False", "True"])
def test_soft_field_links(tmpdir, field1a, field2a,save):

root = NXroot()
root["g1"] = NXgroup()
root["g1/g2"] = NXgroup(f1=field1a, f2=field2a)
root["g1"].f1_link = NXlink(target="g2/f1", soft=True)
root["g1"].f2_link = NXlink(target="g1/g2/f2", soft=True)

if save:
filename = os.path.join(tmpdir, "file1.nxs")
root.save(filename, mode="w")

assert root["g1/f1_link"].shape == (2,)
assert root["g1/f1_link"].dtype == np.float32

root["g1/g2/f1"].attrs["a1"] = 1

assert "a1" in root["g1/f1_link"].attrs
assert root["g1/f1_link"].a1 == 1

assert root["g1/f1_link"].nxdata[0] == root["g1/g2/f1"].nxdata[0]

assert root["g1/f2_link"].shape == (2,)
assert root["g1/f2_link"].dtype == np.int16

root["g1/g2/f2"].attrs["a2"] = 2

assert "a2" in root["g1/f2_link"].attrs
assert root["g1/f2_link"].a2 == 2

assert root["g1/f2_link"].nxdata[0] == root["g1/g2/f2"].nxdata[0]
Loading