Skip to content

Commit

Permalink
fix: validate types of has_part relationship in MODO.add
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdoret committed Jan 16, 2024
1 parent 2c8e652 commit 283d20e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 12 additions & 4 deletions modo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,33 @@ def add_element(
If the element is part of another element, the parent metadata
will be updated."""

breakpoint()
# Copy data file to archive and update data_path in metadata
if data_file is not None:
data_path = Path(data_file)
shutil.copy(data_file, self.path / element.data_path)

# Link element to parent element
if part_of is None:
path = "/"
parent_path = "/"
else:
path = part_of
parent_path = part_of
parent_type = getattr(
model,
self.metadata[parent_path]["@type"],
)
has_prop = get_haspart_property(element.__class__.__name__)
parent_slots = parent_type.__match_args__
if has_prop not in parent_slots:
raise ValueError(
f"Cannot make {element.id} part of {part_of}: {parent_type} does not have property {has_prop}"
)
# has_part is multivalued
if has_prop not in self.archive[part_of].attrs:
self.archive[part_of].attrs[has_prop] = []
self.archive[part_of].attrs[has_prop] += [element.id]

# Add element to metadata
parent_group = self.archive[path]
parent_group = self.archive[parent_path]
attrs = json.loads(json_dumper.dumps(element))
add_metadata_group(parent_group, attrs)
zarr.consolidate_metadata(self.archive.store)
3 changes: 1 addition & 2 deletions modo/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def get_haspart_property(child_class: str) -> Optional[str]:
# find all subproperties of has_part
prop_names = load_schema().slot_children("has_part")
for prop_name in prop_names:
has_prop = load_schema().get_slot(prop_name)
targets = has_prop.range
targets = get_slot_range(prop_name)
if isinstance(targets, str):
targets = [targets]
# When considering the slot range,
Expand Down

0 comments on commit 283d20e

Please sign in to comment.