Skip to content

Commit

Permalink
fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dieuska committed Sep 30, 2014
1 parent b9a8445 commit 506cbb1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/source/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Supported Heritagedata thesauri

The webservices provides by `heritagedata.org <http://www.heritagedata.org>`_
provide access to multiple vocabularies or conceptschemes. You can select
one of these vocabularies by passing a `vocab_id` to the constructor of
one of these vocabularies by passing a `scheme_id` to the constructor of
the :class:`~skosprovider_heritagedata.providers.HeritagedataProvider`.

`Heritagedata Vocabulary schemes <http://heritagedata.org/live/getAllSchemes.php>`_
Expand All @@ -31,7 +31,7 @@ Using HeritagedataProvider

The :class:`~skosprovider_heritagedata.providers.HeritagedataProvider` is a
general provider for the Heritagedata vocabularies. It's use is identical to
all other SKOSProviders. A vocab_id is required to indicate the vocabulary
all other SKOSProviders. A scheme_id is required to indicate the vocabulary
to be used. Please consult :ref:`supported_thesauri` for a complete list.

.. literalinclude:: ../../examples/period.py
Expand Down
2 changes: 1 addition & 1 deletion examples/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from skosprovider_heritagedata.providers import HeritagedataProvider

results = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').expand('PM')
results = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').expand('PM')

print('Results')
print('------')
Expand Down
2 changes: 1 addition & 1 deletion examples/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from skosprovider_heritagedata.providers import HeritagedataProvider

results = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find({'label': 'iron', 'type': 'concept'})
results = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find({'label': 'iron', 'type': 'concept'})

print('Results')
print('------')
Expand Down
2 changes: 1 addition & 1 deletion examples/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from skosprovider_heritagedata.providers import HeritagedataProvider

periodprovider = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period')
periodprovider = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period')

pm = periodprovider.get_by_id('PM')

Expand Down
41 changes: 20 additions & 21 deletions skosprovider_heritagedata/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from skosprovider.providers import VocabularyProvider
from skosprovider_heritagedata.utils import (
uri_to_id,
heritagedata_to_skos)
heritagedata_to_skos, uri_to_base_uri)

log = logging.getLogger(__name__)

Expand All @@ -23,28 +23,27 @@ def __init__(self, metadata, **kwargs):
:param (dict) metadata: metadata of the provider
:param kwargs: arguments defining the provider.
* Typical arguments are `base_url`, `vocab_id` and `url`.
The `url` is a composition of the `base_url` and `vocab_id`
* Typical argument is `scheme_uri`.
The `scheme_uri` is a composition of the `base_scheme_uri` and `scheme_id`
* The :class:`skosprovider_Heritagedata.providers.AATProvider`
is the default :class:`skosprovider_Heritagedata.providers.HeritagedataProvider`
"""
if not 'default_language' in metadata:
metadata['default_language'] = 'en'
if metadata['default_language'] != 'en':
raise ValueError("Only english('en') is supported as language for this skosprovider")
if 'base_url' in kwargs:
self.base_url = kwargs['base_url']
if 'scheme_uri' in kwargs:
self.base_scheme_uri = uri_to_base_uri(kwargs['scheme_uri'])
self.scheme_id = uri_to_id(kwargs['scheme_uri'])
else:
self.base_url = 'http://purl.org/heritagedata/'
if 'vocab_id' in kwargs:
self.vocab_id = kwargs['vocab_id']
else:
raise ValueError("Please provide a vocab_id: for example vocab_id='eh_period' ")
if not 'url' in kwargs:
self.url = self.base_url + "schemes/" + self.vocab_id
self.base_scheme_uri = 'http://purl.org/heritagedata/schemes'
self.scheme_id = 'eh_period'
self.scheme_uri = self.base_scheme_uri + "/" + self.scheme_id

if 'service_scheme_uri' in kwargs:
self.service_scheme_uri = kwargs['service_scheme_uri'].strip('/')
else:
self.url = kwargs['url']
self.service_url = "http://heritagedata.org/live/services"
self.service_scheme_uri = "http://heritagedata.org/live/services"

super(HeritagedataProvider, self).__init__(metadata, **kwargs)

Expand All @@ -60,7 +59,7 @@ def get_by_id(self, id, change_notes=False):
"""
graph = rdflib.Graph()
try:
graph.parse('%s/%s/%s.rdf' % (self.url, "concepts", id))
graph.parse('%s/%s/%s.rdf' % (self.scheme_uri, "concepts", id))
# get the concept
graph_to_skos = heritagedata_to_skos(graph).from_graph()
if len(graph_to_skos) == 0:
Expand Down Expand Up @@ -161,7 +160,7 @@ def find(self, query):
#collection
if 'collection' in query:
raise ValueError("collection: 'collection' is not used in Heritagedata")
params = {'schemeURI': self.url, 'contains': label}
params = {'schemeURI': self.scheme_uri, 'contains': label}
return self._get_items("getConceptLabelMatch", params)

def get_all(self):
Expand All @@ -186,7 +185,7 @@ def get_top_display(self):
""" Returns all concepts or collections that form the top-level of a display hierarchy.
:return: A :class:`lst` of concepts and collections.
"""
params = {'schemeURI': self.url}
params = {'schemeURI': self.scheme_uri}
return self._get_items("getTopConceptsForScheme", params)

def get_children_display(self, id):
Expand All @@ -195,7 +194,7 @@ def get_children_display(self, id):
:param str id: A concept or collection id.
:returns: A :class:`lst` of concepts and collections.
"""
params = {'conceptURI': self.url + "/concepts/" + id}
params = {'conceptURI': self.scheme_uri + "/concepts/" + id}
return self._get_items("getConceptRelations", params)

def expand(self, id):
Expand All @@ -216,8 +215,8 @@ def expand(self, id):

def _get_children(self, id, all=False):
#If all=True this method works recursive
request = self.service_url + "/getConceptRelations"
res = requests.get(request, params={'conceptURI': self.url + "/concepts/" + id})
request = self.service_scheme_uri + "/getConceptRelations"
res = requests.get(request, params={'conceptURI': self.scheme_uri + "/concepts/" + id})
res.encoding = 'utf-8'
result = res.json()
answer = []
Expand Down Expand Up @@ -246,7 +245,7 @@ def _get_items(self, service, params):
"""

try:
request = self.service_url + "/" + service
request = self.service_scheme_uri + "/" + service
res = requests.get(request, params=params)
res.encoding = 'utf-8'
result = res.json()
Expand Down
5 changes: 4 additions & 1 deletion skosprovider_heritagedata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@ def hierarchy_notetypes(list):
return list

def uri_to_id(uri):
return uri.strip('/').rsplit('/',1)[1]
return uri.strip('/').rsplit('/',1)[1]

def uri_to_base_uri(uri):
return uri.strip('/').rsplit('/',1)[0]
35 changes: 16 additions & 19 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
class HeritagedataProviderTests(unittest.TestCase):

def test_default_language_error(self):
self.assertRaises(ValueError, HeritagedataProvider, {'id': 'Heritagedata', 'default_language': 'nl'}, vocab_id='eh_period')

def test_vocab_id_error(self):
self.assertRaises(ValueError, HeritagedataProvider, {'id': 'Heritagedata', 'default_language': 'en'})
self.assertRaises(ValueError, HeritagedataProvider, {'id': 'Heritagedata', 'default_language': 'nl'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period')

def test_get_by_id_concept(self):
concept = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_by_id('PM')
concept = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_by_id('PM')
self.assertEqual(concept['uri'], 'http://purl.org/heritagedata/schemes/eh_period/concepts/PM')
self.assertEqual(concept['type'], 'concept')
self.assertIsInstance(concept['labels'], list)
Expand All @@ -35,19 +32,19 @@ def test_get_by_id_concept(self):
self.assertIn('STU', concept['narrower'])

def test_get_by_id_nonexistant_id(self):
concept = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_by_id('123')
concept = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_by_id('123')
self.assertIsNone(concept)

def test_get_by_uri(self):
concept = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_by_uri('http://purl.org/heritagedata/schemes/eh_period/concepts/PM')
concept = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_by_uri('http://purl.org/heritagedata/schemes/eh_period/concepts/PM')
self.assertEqual(concept['uri'], 'http://purl.org/heritagedata/schemes/eh_period/concepts/PM')
self.assertEqual(concept['id'], 'PM')

def test_get_all(self):
self.assertFalse(HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_all())
self.assertFalse(HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_all())

def test_get_top_display(self):
top_heritagedata_display = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_top_display()
top_heritagedata_display = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_top_display()
self.assertIsInstance(top_heritagedata_display, list)
self.assertGreater(len(top_heritagedata_display), 0)
keys_first_display = top_heritagedata_display[0].keys()
Expand All @@ -56,12 +53,12 @@ def test_get_top_display(self):
self.assertIn('POST MEDIEVAL', [label['label'] for label in top_heritagedata_display])

def test_get_top_concepts(self):
top_heritagedata_concepts = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_top_concepts()
top_heritagedata_concepts = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_top_concepts()
self.assertIsInstance(top_heritagedata_concepts, list)
self.assertGreater(len(top_heritagedata_concepts), 0)

def test_get_childeren_display(self):
childeren_Heritagedata_pm = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').get_children_display('PM')
childeren_Heritagedata_pm = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').get_children_display('PM')
self.assertIsInstance(childeren_Heritagedata_pm, list)
self.assertGreater(len(childeren_Heritagedata_pm), 0)
keys_first_display = childeren_Heritagedata_pm[0].keys()
Expand All @@ -70,40 +67,40 @@ def test_get_childeren_display(self):
self.assertIn("VICTORIAN", [label['label'] for label in childeren_Heritagedata_pm])

def test_expand(self):
all_childeren_pm = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').expand('PM')
all_childeren_pm = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').expand('PM')
self.assertIsInstance(all_childeren_pm, list)
self.assertGreater(len(all_childeren_pm), 0)
self.assertIn('PM', all_childeren_pm)

# all_childeren_monumentsbyform = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_tmt2').expand('102872')
# all_childeren_monumentsbyform = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_tmt2').expand('102872')
# self.assertIsInstance(all_childeren_monumentsbyform, list)
# self.assertGreater(len(all_childeren_monumentsbyform), 0)
# self.assertIn('102872', all_childeren_monumentsbyform)



def test_expand_invalid(self):
all_childeren_invalid = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').expand('invalid')
all_childeren_invalid = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').expand('invalid')
self.assertFalse(all_childeren_invalid)

def test_find_with_collection(self):
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find, {'type': 'concept', 'collection': {'id': '300007466', 'depth': 'all'}})
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find, {'type': 'concept', 'collection': {'id': '300007466', 'depth': 'all'}})

def test_find_collections(self):
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find, {'type': 'collection'})
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find, {'type': 'collection'})

def test_find_wrong_type(self):
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find, {'type': 'collectie'})
self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find, {'type': 'collectie'})

def test_find_multiple_keywords(self):
r = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find({'label': 'iron Age', 'type': 'concept'})
r = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find({'label': 'iron Age', 'type': 'concept'})
self.assertIsInstance(r, list)
self.assertGreater(len(r), 0)
for res in r:
self.assertEqual(res['type'], 'concept')

def test_find_one_keywords(self):
r = HeritagedataProvider({'id': 'Heritagedata'}, vocab_id='eh_period').find({'label': 'VICTORIAN', 'type': 'concept'})
r = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find({'label': 'VICTORIAN', 'type': 'concept'})
self.assertIsInstance(r, list)
self.assertGreater(len(r), 0)
for res in r:
Expand Down

0 comments on commit 506cbb1

Please sign in to comment.