From ce9b3ada9e76bab3cc0e6b16857ba9032904ec0a Mon Sep 17 00:00:00 2001 From: Stuart Chalk Date: Thu, 14 Dec 2023 13:38:25 -0500 Subject: [PATCH] SciDataLib-76 Scidata 'rights' limited to 1 entry Updated code to handle multiple entries (equivalent to sources). Add tests for both the append and replace functionalities. --- scidatalib/scidata.py | 33 ++++++++++++++++++++------------- tests/test_scidata.py | 19 +++++++++++++++++-- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/scidatalib/scidata.py b/scidatalib/scidata.py index 95b8cc5..3a118c7 100644 --- a/scidatalib/scidata.py +++ b/scidatalib/scidata.py @@ -917,25 +917,32 @@ def sources(self, sources: list, replace=False) -> dict: } ld.update(x) srcs.append(ld) + self.meta['@graph']['sources'] = srcs return self.meta['@graph']['sources'] - def rights(self, holder: str, lic: str) -> dict: + def rights(self, rights: list, replace=False) -> dict: """ - Add the rights section to the file (max: 1 entry) + Add the rights section to the file (min: 1 entry) - :param holder: the entity that holds the license to this data - :param lic: the assigned license + :param rights: the rights of the data consisting of: + holder: the rights holder + license: the license given by the holder + :param replace: the assigned license """ - rights = [] - if isinstance(holder, str) and isinstance(lic, str): - rights = [{ - '@id': 'rights/1/', - '@type': 'dc:rights', - 'holder': holder, - 'license': lic, - }] - self.meta['@graph']['rights'] = rights + rites = [] + if not replace: + rites = self.meta['@graph']['rights'] + for x in rights: + ld = { + '@id': 'rights/' + str(len(rites) + 1) + '/', + '@type': 'dc:rights' + } + ld.update(x) + rites.append(ld) + else: + rites = rights + self.meta['@graph']['rights'] = rites return self.meta['@graph']['rights'] # private class functions diff --git a/tests/test_scidata.py b/tests/test_scidata.py index 21c4b84..d140ecb 100644 --- a/tests/test_scidata.py +++ b/tests/test_scidata.py @@ -675,10 +675,25 @@ def test_source(sd): assert sd.sources(src) == out -def test_rights(sd): +def test_rights_append(sd): holder = 'Megadodo Productions' licurl = 'https://creativecommons.org/licenses/by/4.0/' lic = 'Creative Commons, Attribution 4.0 Galactic (CC BY 4.0) ' + licurl out = [{"@id": "rights/1/", "@type": "dc:rights", + "holder": holder, "license": lic}, + {"@id": "rights/2/", "@type": "dc:rights", "holder": holder, "license": lic}] - assert sd.rights(holder, lic) == out + inn = {'holder': holder, 'license': lic} + sd.rights([inn]) + assert sd.rights([inn]) == out + + +def test_rights_replace(sd): + holder = 'Megadodo Productions' + licurl = 'https://creativecommons.org/licenses/by/4.0/' + lic = 'Creative Commons, Attribution 4.0 Galactic (CC BY 4.0) ' + licurl + out = [{"@id": "rights/1/", "@type": "dc:rights", + "holder": holder, "license": lic}] + inn = {'holder': holder, 'license': lic} + sd.rights([inn, inn]) + assert sd.rights([inn, True]) == out