Skip to content

Commit

Permalink
[#1359,licenses][s]: refactor to remove dependency on external licens…
Browse files Browse the repository at this point in the history
…es package.

* Inline list of default licenses (those from http://licenses.opendefinition.org/licenses/groups/ckan.json)
* Minor refactoring on initialization of LicenseRegister to support dicts or lists or licenses
* Fix up tests (now that we do not have gpl in our default lists of licenses)
  • Loading branch information
rufuspollock committed Feb 13, 2012
1 parent 241687f commit 55c8ff6
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ckan/lib/create_test_data.py
Expand Up @@ -546,7 +546,7 @@ def get_all_data(cls):
'format':'DOC',
'description':'http://www.statistics.gov.uk/hub/id/119-34565'}],
'groups':'ukgov test1 test2 penguin',
'license':'gpl-3.0',
'license':'odc-by',
'notes':u'''From <http://www.gpoaccess.gov/gils/about.html>
> The Government Information Locator Service (GILS) is an effort to identify, locate, and describe publicly available Federal
Expand Down
237 changes: 231 additions & 6 deletions ckan/model/license.py
@@ -1,5 +1,7 @@
from pylons import config
import datetime
import urllib2
from ckan.lib.helpers import json
import re

class License(object):
Expand Down Expand Up @@ -41,13 +43,32 @@ class LicenseRegister(object):
def __init__(self):
group_url = config.get('licenses_group_url', None)
if group_url:
from licenses.service import LicensesService2
self.service = LicensesService2(group_url)
entity_list = self.service.get_licenses()
self.load_licenses(group_url)
else:
from licenses import Licenses
entity_list = Licenses().get_group_licenses('ckan_original')
self.licenses = [License(entity) for entity in entity_list]
self._create_license_list(self.default_license_list)

def load_licenses(self, license_url):
try:
response = urllib2.urlopen(license_url)
response_body = response.read()
except Exception, inst:
msg = "Couldn't connect to licenses service %r: %s" % (license_url, inst)
raise Exception, msg
try:
license_data = json.loads(response_body)
except Exception, inst:
msg = "Couldn't read response from licenses service %r: %s" % (response_body, inst)
raise Exception, inst
self._create_license_list(license_data)

def _create_license_list(self, license_data):
if isinstance(license_data, dict):
self.licenses = [License(entity) for entity in license_data.values()]
elif isinstance(license_data, list):
self.licenses = [License(entity) for entity in license_data]
else:
msg = "Licenses at %s must be dictionary or list" % license_url
raise ValueError(msg)

def __getitem__(self, key, default=Exception):
for license in self.licenses:
Expand Down Expand Up @@ -85,3 +106,207 @@ def get_by_title(self, title, default=None):
else:
return default


default_license_list = [
{
"domain_content": False,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "notspecified",
"is_generic": True,
"is_okd_compliant": False,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "License Not Specified",
"url": ""
},
{
"domain_content": False,
"domain_data": True,
"domain_software": False,
"family": "",
"id": "odc-pddl",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Open Data Commons Public Domain Dedication and Licence (PDDL)",
"url": "http://www.opendefinition.org/licenses/odc-pddl"
},
{
"domain_content": False,
"domain_data": True,
"domain_software": False,
"family": "",
"id": "odc-odbl",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Open Data Commons Open Database License (ODbL)",
"url": "http://www.opendefinition.org/licenses/odc-odbl"
},
{
"domain_content": False,
"domain_data": True,
"domain_software": False,
"family": "",
"id": "odc-by",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Open Data Commons Attribution License",
"url": "http://www.opendefinition.org/licenses/odc-by"
},
{
"domain_content": True,
"domain_data": True,
"domain_software": False,
"family": "",
"id": "cc-zero",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Creative Commons CCZero",
"url": "http://www.opendefinition.org/licenses/cc-zero"
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "cc-by",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Creative Commons Attribution",
"url": "http://www.opendefinition.org/licenses/cc-by"
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "cc-by-sa",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Creative Commons Attribution Share-Alike",
"url": "http://www.opendefinition.org/licenses/cc-by-sa"
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "gfdl",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "GNU Free Documentation License",
"url": "http://www.opendefinition.org/licenses/gfdl"
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "other-open",
"is_generic": True,
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Other (Open)",
"url": ""
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "other-pd",
"is_generic": True,
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Other (Public Domain)",
"url": ""
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "other-at",
"is_generic": True,
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Other (Attribution)",
"url": ""
},
{
"domain_content": True,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "uk-ogl",
"is_okd_compliant": True,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "UK Open Government Licence (OGL)",
"url": "http://reference.data.gov.uk/id/open-government-licence"
},
{
"domain_content": False,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "cc-nc",
"is_okd_compliant": False,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Creative Commons Non-Commercial (Any)",
"url": "http://creativecommons.org/licenses/by-nc/2.0/"
},
{
"domain_content": False,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "other-nc",
"is_generic": True,
"is_okd_compliant": False,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Other (Non-Commercial)",
"url": ""
},
{
"domain_content": False,
"domain_data": False,
"domain_software": False,
"family": "",
"id": "other-closed",
"is_generic": True,
"is_okd_compliant": False,
"is_osi_compliant": False,
"maintainer": "",
"status": "active",
"title": "Other (Not Open)",
"url": ""
}
]
4 changes: 2 additions & 2 deletions ckan/tests/functional/test_package.py
Expand Up @@ -788,7 +788,7 @@ def test_edit_all_fields(self):
)
assert len(resources[0]) == 5
notes = u'Very important'
license_id = u'gpl-3.0'
license_id = u'odc-by'
state = model.State.ACTIVE
tags = (u'tag1', u'tag2', u'tag 3')
tags_txt = u','.join(tags)
Expand Down Expand Up @@ -1152,7 +1152,7 @@ def test_new_all_fields(self):
url = u'http://something.com/somewhere.zip'
download_url = u'http://something.com/somewhere-else.zip'
notes = u'Very important'
license_id = u'gpl-3.0'
license_id = u'odc-by'
tags = (u'tag1', u'tag2.', u'tag 3', u'SomeCaps')
tags_txt = u','.join(tags)
extras = {self.key1:self.value1, 'key2':'value2', 'key3':'value3'}
Expand Down
1 change: 0 additions & 1 deletion ckan/tests/models/test_license.py
Expand Up @@ -35,6 +35,5 @@ def test_getitem(self):
license = self.licenses[license_id]
self.assert_unicode(license.id)
self.assert_unicode(license.title)
self.assert_datetime(license.date_created)
self.assert_unicode(license.url)

6 changes: 3 additions & 3 deletions ckan/tests/models/test_package.py
Expand Up @@ -19,7 +19,7 @@ def setup_class(self):
self.pkg1 = model.Package(name=self.name)
model.Session.add(self.pkg1)
self.pkg1.notes = self.notes
self.pkg1.license_id = u'gpl-3.0'
self.pkg1.license_id = u'odc-by'
model.Session.commit()
model.Session.remove()

Expand Down Expand Up @@ -71,8 +71,8 @@ def test_create_package(self):
package = model.Package.by_name(self.name)
assert package.name == self.name
assert package.notes == self.notes
assert package.license.id == u'gpl-3.0'
assert package.license.title == u'OSI Approved::GNU General Public License version 3.0 (GPLv3)', package.license.title
assert package.license.id == u'odc-by'
assert package.license.title == u'Open Data Commons Attribution License', package.license.title

def test_update_package(self):
newnotes = u'Written by Beethoven'
Expand Down
1 change: 0 additions & 1 deletion requires/lucid_missing.txt
Expand Up @@ -18,6 +18,5 @@ pairtree==0.7.1-T
ofs==0.4.1

apachemiddleware==0.1.1
licenses==0.6.1
# markupsafe is required by webhelpers==1.2 required by formalchemy with SQLAlchemy 0.6
markupsafe==0.9.2

0 comments on commit 55c8ff6

Please sign in to comment.