Skip to content

Commit 09b53e0

Browse files
Merge pull request #63 from apdavison/allow-extra-keys
Provide an option to allow (and ignore) additional keys in a JSON-LD document
2 parents c3ae702 + 274b5ec commit 09b53e0

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

pipeline/src/base.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ def to_jsonld(self, include_empty_properties=True, embed_linked_nodes=True, with
112112
return {key: data[key] for key in sorted(data)}
113113

114114
@classmethod
115-
def from_jsonld(cls, data):
115+
def from_jsonld(cls, data, ignore_unexpected_keys=False):
116116
"""
117-
Create a Python object representing a metadata node from a JSON-LD-compatible dictionary
117+
Create a Python object representing a metadata node from a JSON-LD-compatible dictionary.
118+
119+
By default, a NameError will be raised if the data contain keys that are not
120+
recognised by this metadata node.
121+
If `ignore_unexpected_keys` is set to True, no error is raised.
118122
"""
119123
data_copy = data.copy()
120124
context = data_copy.pop("@context", None)
@@ -131,7 +135,7 @@ def from_jsonld(cls, data):
131135
deserialized_data[property.name] = property.deserialize(value)
132136
else:
133137
deserialized_data[property.name] = value
134-
if len(data_copy) > 0:
138+
if len(data_copy) > 0 and not ignore_unexpected_keys:
135139
raise NameError(f"Unexpected arguments for {cls}: {tuple(data_copy.keys())}")
136140
return cls(**deserialized_data)
137141

@@ -220,13 +224,17 @@ def save(self, file_path, indent=2):
220224
json.dump(self.to_jsonld(), output_file, indent=indent)
221225

222226
@classmethod
223-
def load(cls, file_path):
227+
def load(cls, file_path, ignore_unexpected_keys=False):
224228
"""
225-
Create a Python object representing a metadata node from a JSON-LD file
229+
Create a Python object representing a metadata node from a JSON-LD file.
230+
231+
By default, a NameError will be raised if the data contain keys that are not
232+
recognised by this metadata node.
233+
If `ignore_unexpected_keys` is set to True, no error is raised.
226234
"""
227235
with open(file_path, "r") as input_file:
228236
data = json.load(input_file)
229-
return cls.from_jsonld(data)
237+
return cls.from_jsonld(data, ignore_unexpected_keys=ignore_unexpected_keys)
230238

231239

232240
class EmbeddedMetadata(Node):

0 commit comments

Comments
 (0)