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

[WIP] Fix relative references when store not in cwd #256

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
23 changes: 14 additions & 9 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def write_attributes(self, **kwargs):
raise TypeError(str(e) + " type=" + str(type(value)) + " data=" + str(value)) from e
# Case 2: References
elif isinstance(value, (Builder, ReferenceBuilder)):
refs = self._create_ref(value, self.path)
refs = self._create_ref(value, ref_link_source=self.path)
tmp = {"zarr_dtype": "object", "value": refs}
obj.attrs[key] = tmp
# Case 3: Scalar attributes
Expand Down Expand Up @@ -801,12 +801,13 @@ def resolve_ref(self, zarr_ref):
source_file = str(zarr_ref["path"])
else:
source_file = str(zarr_ref["source"])
# Resolve the path relative to the current file

if not self.is_remote():
if isinstance(self.source, str) and self.source.startswith(("s3://")):
source_file = self.source
else:
source_file = os.path.abspath(source_file)
# Join with source_file to resolve the relative path
source_file = os.path.normpath(os.path.join(self.source, source_file))
else:
# get rid of extra "/" and "./" in the path root and source_file
root_path = str(self.path).rstrip("/")
Expand All @@ -817,7 +818,7 @@ def resolve_ref(self, zarr_ref):
if object_path:
target_name = os.path.basename(object_path)
else:
target_name = ROOT_NAME
target_name = ROOT_NAME

target_zarr_obj = self.__open_file_consolidated(
store=source_file,
Expand Down Expand Up @@ -895,7 +896,7 @@ def _create_ref(self, ref_object, ref_link_source=None):
str_path = self.path.path
else:
str_path = self.path
rel_source = os.path.relpath(os.path.abspath(ref_link_source), os.path.dirname(os.path.abspath(str_path)))
rel_source = os.path.relpath(os.path.abspath(ref_link_source), os.path.abspath(str_path))

# Return the ZarrReference object
ref = ZarrReference(
Expand Down Expand Up @@ -965,7 +966,7 @@ def write_link(self, **kwargs):

name = builder.name
# Get the reference
zarr_ref = self._create_ref(builder, ref_link_source)
zarr_ref = self._create_ref(builder, ref_link_source=ref_link_source)

self.__add_link__(parent, zarr_ref.source, zarr_ref.path, name)
self._written_builders.set_written(builder) # record that the builder has been written
Expand Down Expand Up @@ -1078,9 +1079,13 @@ def write_dataset(self, **kwargs): # noqa: C901
if isinstance(data, Array):
# copy the dataset
data_filename = self.__get_store_path(data.store)
str_path = self.path
if not isinstance(str_path, str): # a store
str_path = self.path.path
rel_data_filename = os.path.relpath(os.path.abspath(data_filename), os.path.abspath(str_path))
if link_data:
if export_source is None: # not exporting
self.__add_link__(parent, data_filename, data.name, name)
self.__add_link__(parent, rel_data_filename, data.name, name)
linked = True
dset = None
else: # exporting
Expand All @@ -1089,7 +1094,7 @@ def write_dataset(self, **kwargs): # noqa: C901
# I have three files, FileA, FileB, FileC. I want to export FileA to FileB. FileA has an
# EXTERNAL link to a dataset in Filec. This case preserves the link to FileC to also be in FileB.
if data_filename != export_source:
self.__add_link__(parent, data_filename, data.name, name)
self.__add_link__(parent, rel_data_filename, data.name, name)
linked = True
dset = None
# Case 2: If the dataset is in the export source and has a DIFFERENT path as the builder,
Expand All @@ -1098,7 +1103,7 @@ def write_dataset(self, **kwargs): # noqa: C901
# INTERNAL link. This case preserves the link to also be in FileB.
###############
elif parent.name != data_parent:
self.__add_link__(parent, self.path, data.name, name)
self.__add_link__(parent, ".", data.name, name)
linked = True
dset = None

Expand Down
Loading