Skip to content

Commit c72353e

Browse files
authored
Merge branch 'refactor/data-model' into refactor/384-mark-expected-failures
2 parents e9ca175 + 460e3d4 commit c72353e

6 files changed

Lines changed: 440 additions & 12 deletions

File tree

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hermes/model/types/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,30 @@
3333
(ld_dict.is_ld_dict, dict(ld_container=ld_dict)),
3434

3535
# Expand and access JSON data
36-
(ld_container.is_json_id, dict(python=lambda c: c["@id"], expanded_json=lambda c, **_: [c])),
36+
(ld_container.is_json_id, dict(python=lambda c, **_: c["@id"], expanded_json=lambda c, **_: [c])),
3737
(ld_container.is_typed_json_value, dict(python=ld_container.typed_ld_to_py)),
3838
(ld_container.is_json_value, dict(python=lambda c, **_: c["@value"], expanded_json=lambda c, **_: [c])),
3939
(ld_list.is_container, dict(ld_container=lambda c, **kw: ld_list([c], **kw))),
40+
41+
# FIXME: add conversion from list and json dict to expanded_json
42+
# to parse nested dicts and lists when using for example __setitem__(key, value) from ld_dict
43+
# where value is converted to expanded_json bevor adding it to data_dict
44+
# Suggested:
45+
# (
46+
# ld_dict.is_json_dict,
47+
# {
48+
# "ld_container": ld_dict.from_dict,
49+
# "expanded_json": lambda c, **kw: kw["parent"]._to_expanded_json(kw["key"], ld_dict.from_dict(c, **kw))
50+
# }
51+
# ),
52+
#
53+
# (
54+
# lambda c: isinstance(c, list),
55+
# {
56+
# "ld_container": ld_list.from_list,
57+
# "expanded_json": lambda c, **kw: kw["parent"]._to_expanded_json(kw["key"], ld_list.from_list(c, **kw))
58+
# }
59+
# ),
4060
(ld_dict.is_json_dict, dict(ld_container=ld_dict.from_dict)),
4161

4262
(lambda c: isinstance(c, list), dict(ld_container=ld_list.from_list)),

src/hermes/model/types/ld_container.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class ld_container:
1111
"""
1212
Base class for Linked Data containers.
1313
14-
A linked data container impelements a view on the expanded form of an JSON-LD document.
15-
It allows to easily interacts them by hinding all the nesting and automatically mapping
16-
between different forms.
14+
A linked data container implements a view on the expanded form of an JSON-LD document.
15+
It allows to easily interact with such documents by hiding all the nesting and
16+
automatically mapping between different forms.
1717
"""
1818

1919
ld_proc = JsonLdProcessor()
@@ -81,6 +81,7 @@ def ld_value(self):
8181
return self._data
8282

8383
def _to_python(self, full_iri, ld_value):
84+
# FIXME: #434 dates are not returned as datetime/ date/ time but as string
8485
if full_iri == "@id":
8586
value = self.ld_proc.compact_iri(self.active_ctx, ld_value, vocab=False)
8687
elif full_iri == "@type":
@@ -186,6 +187,7 @@ def is_typed_json_value(cls, ld_value):
186187

187188
@classmethod
188189
def typed_ld_to_py(cls, data, **kwargs):
190+
# FIXME: #434 dates are not returned as datetime/ date/ time but as string
189191
ld_value = data[0]['@value']
190192

191193
return ld_value

src/hermes/model/types/ld_dict.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ld_dict(ld_container):
1313
_NO_DEFAULT = type("NO DEFAULT")
1414

1515
def __init__(self, data, *, parent=None, key=None, index=None, context=None):
16+
if not self.is_ld_dict(data):
17+
raise ValueError("The given data does not represent a ld_dict.")
1618
super().__init__(data, parent=parent, key=key, index=index, context=context)
1719

1820
self.data_dict = data[0]
@@ -83,14 +85,15 @@ def from_dict(cls, value, *, parent=None, key=None, context=None, ld_type=None):
8385
ld_data["@type"] = ld_type
8486

8587
data_context = ld_data.pop('@context', [])
86-
full_context = ld_container.merge_to_list(context or [], data_context)
87-
if parent is None and data_context:
88-
ld_data["@context"] = data_context
88+
merged_contexts = ld_container.merge_to_list(data_context, context or [])
89+
full_context = []
90+
if parent is None and merged_contexts:
91+
ld_data["@context"] = merged_contexts
8992
elif parent is not None:
90-
full_context[:0] = parent.full_context
93+
full_context = parent.full_context + merged_contexts
9194

9295
ld_value = cls.ld_proc.expand(ld_data, {"expandContext": full_context, "documentLoader": bundled_loader})
93-
ld_value = cls(ld_value, parent=parent, key=key, context=data_context)
96+
ld_value = cls(ld_value, parent=parent, key=key, context=merged_contexts)
9497

9598
return ld_value
9699

0 commit comments

Comments
 (0)