Skip to content

Commit

Permalink
Merge pull request #53 from openstax/license
Browse files Browse the repository at this point in the history
lookup license text
  • Loading branch information
philschatz committed Feb 9, 2022
2 parents 65fc9f6 + 7aad99c commit c595d55
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
43 changes: 42 additions & 1 deletion cnxml/parse.py
Expand Up @@ -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
Expand All @@ -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()')),
Expand All @@ -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()'),
Expand Down
12 changes: 11 additions & 1 deletion 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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -174,6 +176,7 @@ def test_parse_with_minimal_metadata():
'keywords': (),
'language': None,
'license_url': None,
'license_text': None,
'licensors': (),
'maintainers': (),
'print_style': None,
Expand Down Expand Up @@ -214,6 +217,7 @@ def test_parse_with_optional_metadata():
'keywords': (),
'language': None,
'license_url': None,
'license_text': None,
'licensors': (),
'maintainers': (),
'print_style': None,
Expand All @@ -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)

0 comments on commit c595d55

Please sign in to comment.