Skip to content

Commit d1adef9

Browse files
committed
keep conformsTo in Subcrate
1 parent 67ebb19 commit d1adef9

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

rocrate/rocrate.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ def __read_data_entities(self, entities, source, gen_preview):
209209
self.__add_parts(parts, entities, source)
210210

211211
def __add_parts(self, parts, entities, source):
212+
"""
213+
Add entities to the crate from a list of entities id and Entity object.
214+
215+
:param self: Description
216+
:param parts: a list of dicts (one dict per entity) in the form {@id : "entity_id"}
217+
:param entities: a dict with the full list of entities information as in the hasPart of the root dataset of the crate.
218+
:param source: Description
219+
"""
212220
type_map = OrderedDict((_.__name__, _) for _ in subclasses(FileOrDir))
213221
for ref in parts:
214222
id_ = ref['@id']
@@ -222,10 +230,11 @@ def __add_parts(self, parts, entities, source):
222230
cls = pick_type(entity, type_map, fallback=DataEntity, parse_subcrate=self.parse_subcrate)
223231

224232
if cls is Subcrate:
233+
225234
if is_url(id_):
226-
instance = Subcrate(self, id_)
235+
instance = Subcrate(self, source=id_, properties=entity)
227236
else:
228-
instance = Subcrate(self, source / unquote(id_))
237+
instance = Subcrate(self, source=source / unquote(id_), properties=entity)
229238

230239
elif cls is DataEntity:
231240
instance = DataEntity(self, identifier=id_, properties=entity)
@@ -239,7 +248,7 @@ def __add_parts(self, parts, entities, source):
239248
self.add(instance)
240249
if instance.type == "Dataset":
241250
# for Subcrate, type is currently Dataset too,
242-
# but the hasPart is not populated yet only once accssing a subcrate element (lazy loading)
251+
# but the hasPart is not populated yet only once accessing a subcrate element (lazy loading)
243252
self.__add_parts(as_list(entity.get("hasPart", [])), entities, source)
244253

245254
def __read_contextual_entities(self, entities):
@@ -846,19 +855,20 @@ def __validate_suite(self, suite):
846855
class Subcrate(Dataset):
847856

848857
def __init__(self, crate, source=None, dest_path=None, fetch_remote=False,
849-
validate_url=False, record_size=False):
858+
validate_url=False, properties=None, record_size=False):
850859
"""
851860
Data-entity representing a subcrate inside another RO-Crate.
852861
853862
:param crate: The parent crate
854863
:param source: The relative path to the subcrate, or its URL
855864
"""
856865
super().__init__(crate, source, dest_path, fetch_remote,
857-
validate_url, properties=None, record_size=record_size)
866+
validate_url, properties=properties, record_size=record_size)
858867

859-
self.subcrate = None
868+
self._subcrate = None
860869
"""
861870
A ROCrate instance allowing access to the nested RO-Crate.
871+
The nested RO-Crate is loaded on first access to any of its attribute.
862872
"""
863873

864874
def _load_subcrate(self):
@@ -868,31 +878,31 @@ def _load_subcrate(self):
868878
This adds an attribute "hasPart" to the `subcrate` with the entities from the nested RO-Crate,
869879
updating the JSON-LD representation accordingly.
870880
"""
871-
if self.subcrate is None:
872-
self.subcrate = ROCrate(self.source, parse_subcrate=True) # would load further nested RO-Crate
873-
if list_parts := self.subcrate.root_dataset.get("hasPart"):
881+
if self._subcrate is None:
882+
self._subcrate = ROCrate(self.source, parse_subcrate=True) # would load further nested RO-Crate
883+
if list_parts := self._subcrate.root_dataset.get("hasPart"):
874884
self._jsonld["hasPart"] = list_parts
875885

876886
def __getitem__(self, key):
877-
if self.subcrate is None:
887+
if self._subcrate is None:
878888
self._load_subcrate()
879889

880890
if key in self._jsonld:
881891
# e.g the "original" entity keys such as id or type
882892
return super().__getitem__(key)
883893

884894
# look into the subcrate entities
885-
return self.subcrate.get(key)
895+
return self._subcrate.get(key)
886896

887897
def as_jsonld(self):
888-
if self.subcrate is None:
898+
if self._subcrate is None:
889899
self._load_subcrate()
890900
return super().as_jsonld()
891901

892902
def get_entities(self):
893-
if self.subcrate is None:
903+
if self._subcrate is None:
894904
self._load_subcrate()
895-
return self.subcrate.get_entities()
905+
return self._subcrate.get_entities()
896906

897907

898908
def make_workflow_rocrate(workflow_path, wf_type, include_files=[],

test/test_read.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,15 @@ def test_crate_with_subcrate(test_data_dir):
205205
assert isinstance(subcrate, Subcrate)
206206
assert main_crate.subcrate_entities == [subcrate]
207207

208+
# Check the subcrate kept the conformsTo attribute from the original Dataset entity
209+
assert "conformsTo" in subcrate
210+
208211
# check that at this point, we have not yet loaded the subcrate
209-
assert subcrate._jsonld == subcrate._empty()
212+
# e.g the json ld should just have id, type and conformsTo
213+
jsonld = subcrate._jsonld
214+
jsonld.pop("conformsTo")
215+
216+
assert jsonld == subcrate._empty()
210217
assert "hasPart" not in subcrate
211218

212219
# check lazy loading by accessing an entity from the subcrate

0 commit comments

Comments
 (0)