diff --git a/cnxml/parse.py b/cnxml/parse.py index 66902d9..a3d1ee0 100644 --- a/cnxml/parse.py +++ b/cnxml/parse.py @@ -60,6 +60,45 @@ def _squash_to_text(elm, remove_namespaces=False): return value +def lookup_license_text(license_url): + switcher = { + 'http://creativecommons.org/licenses/by/1.0': + 'Creative Commons Attribution License', + 'http://creativecommons.org/licenses/by-nd/1.0': + 'Creative Commons Attribution-NoDerivs License', + 'http://creativecommons.org/licenses/by-nd-nc/1.0': + 'Creative Commons Attribution-NoDerivs-NonCommercial License', + 'http://creativecommons.org/licenses/by-nc/1.0': + 'Creative Commons Attribution-NonCommercial License', + 'http://creativecommons.org/licenses/by-sa/1.0': + 'Creative Commons Attribution-ShareAlike License', + 'http://creativecommons.org/licenses/by/2.0': + 'Creative Commons Attribution License', + 'http://creativecommons.org/licenses/by-nd/2.0': + 'Creative Commons Attribution-NoDerivs License', + 'http://creativecommons.org/licenses/by-nd-nc/2.0': + 'Creative Commons Attribution-NoDerivs-NonCommercial License', + 'http://creativecommons.org/licenses/by-nc/2.0': + 'Creative Commons Attribution-NonCommercial License', + 'http://creativecommons.org/licenses/by-sa/2.0': + 'Creative Commons Attribution-ShareAlike License', + 'http://creativecommons.org/licenses/by/3.0': + 'Creative Commons Attribution License', + 'http://creativecommons.org/licenses/by/4.0': + 'Creative Commons Attribution License', + 'http://creativecommons.org/licenses/by-nc-sa/4.0': + 'Creative Commons Attribution-NonCommercial-ShareAlike License', + } + # If license_url is None, appropriately return None + if license_url is None: + return None + # If license_url is not None, we expect to return a value + license_text = switcher.get(license_url.rstrip('/'), None) + if license_text is None: + raise Exception(f'Invalid license url {license_url}') + return license_text + + def parse_metadata(elm_tree): """Given an element-like object (:mod:`lxml.etree`) lookup the metadata and return the found elements @@ -75,6 +114,7 @@ def parse_metadata(elm_tree): (_maybe(xpath(xp)) or "").split() ) + license_url = _maybe(xpath('//md:license/@url')) props = { 'id': _maybe(xpath('//md:content-id/text()')), 'uuid': _maybe(xpath('//md:uuid/text()')), @@ -87,7 +127,8 @@ def parse_metadata(elm_tree): 'title': _maybe(xpath('//md:title/text()')) or xpath('//c:title/text()')[0], 'slug': _maybe(xpath('//md:slug/text()')), - 'license_url': _maybe(xpath('//md:license/@url')), + 'license_url': license_url, + 'license_text': lookup_license_text(license_url), 'language': _maybe(xpath('//md:language/text()')), 'authors': role_xpath('//md:role[@type="author"]/text()'), 'maintainers': role_xpath('//md:role[@type="maintainer"]/text()'), diff --git a/cnxml/tests/test_parse.py b/cnxml/tests/test_parse.py index f83b978..5a1ef45 100644 --- a/cnxml/tests/test_parse.py +++ b/cnxml/tests/test_parse.py @@ -1,7 +1,7 @@ import pytest from lxml import etree -from cnxml.parse import NSMAP, parse_metadata +from cnxml.parse import NSMAP, parse_metadata, lookup_license_text @pytest.fixture @@ -45,6 +45,7 @@ def test_parse(xml): ), 'language': 'en', 'license_url': 'http://creativecommons.org/licenses/by/4.0/', + 'license_text': 'Creative Commons Attribution License', 'licensors': ('OSCRiceUniversity',), 'maintainers': ('OpenStaxCollege', 'cnxcap'), 'print_style': 'ccap-physics', @@ -73,6 +74,7 @@ def test_git_parse(git_xml): 'keywords': (), 'language': 'en', 'license_url': 'http://creativecommons.org/licenses/by/4.0/', + 'license_text': 'Creative Commons Attribution License', 'licensors': (), 'maintainers': (), 'print_style': None, @@ -174,6 +176,7 @@ def test_parse_with_minimal_metadata(): 'keywords': (), 'language': None, 'license_url': None, + 'license_text': None, 'licensors': (), 'maintainers': (), 'print_style': None, @@ -214,6 +217,7 @@ def test_parse_with_optional_metadata(): 'keywords': (), 'language': None, 'license_url': None, + 'license_text': None, 'licensors': (), 'maintainers': (), 'print_style': None, @@ -227,3 +231,9 @@ def test_parse_with_optional_metadata(): } # Verify the metadata assert props == expected_props + + +def test_invalid_license_url(): + with pytest.raises(Exception) as e: + lookup_license_text('http://www.example.com/') + assert 'Invalid license url' in str(e)