Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial code dump. Needs more tests.

  • Loading branch information...
commit f0b99a0462e3a6710595491a7d4cc59d326060dc 1 parent 85ca360
@koenedaele authored
View
1  MANIFEST.in
@@ -0,0 +1 @@
+include README.rst LICENSE HISTORY.rst
View
6 setup.cfg
@@ -0,0 +1,6 @@
+[nosetests]
+match=^test
+nocapture=1
+cover-package=skosprovider_oe
+with-coverage=1
+cover-erase=1
View
50 setup.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+import os
+import sys
+
+import skosprovider_oe
+
+try:
+ from setuptools import setup
+except ImportError:
+ from distutils.core import setup
+
+os.environ['PYTHONDONTWRITEBYTECODE'] = '1'
+
+packages = [
+ 'skosprovider_oe',
+]
+
+requires = [
+ 'skosprovider',
+ 'requests>0.14,<0.14.99'
+]
+
+setup(
+ name='skosprovider_oe',
+ version='0.1.0',
+ description='A SKOS provider for OE vocabularies.',
+ long_description=open('README.rst').read() + '\n\n' +
+ open('HISTORY.rst').read(),
+ author='Koen Van Daele',
+ author_email='koen_van_daele@telenet.be',
+ url='http://github.com/koenedaele/skosprovider_oe',
+ packages=packages,
+ package_data={'': ['LICENSE']},
+ package_dir={'skosprovider': 'skosprovider'},
+ include_package_data=True,
+ install_requires=requires,
+ license=open('LICENSE').read(),
+ zip_safe=False,
+ classifiers=(
+ 'Development Status :: 3 - Alpha',
+ 'Intended Audience :: Developers',
+ 'Natural Language :: English',
+ 'License :: OSI Approved :: MIT License',
+ 'Programming Language :: Python',
+ 'Programming Language :: Python :: 2.7',
+ ),
+)
+
+del os.environ['PYTHONDONTWRITEBYTECODE']
View
0  skosprovider_oe/__init__.py
No changes.
View
69 skosprovider_oe/providers.py
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+
+from skosprovider.providers import VocabularyProvider
+
+import requests
+
+class OnroerendErfgoedProvider(VocabularyProvider):
+ '''A provider that can work with the REST-services of https://inventaris.onroerenderfgoed.be/thesaurus
+
+ '''
+
+ def __init__(self, metadata, url = None):
+ if not 'default_language' in metadata:
+ metadata['default_language'] = 'nl'
+ super(OnroerendErfgoedProvider,self).__init__(metadata)
+ if url is None:
+ url = 'https://inventaris.onroerenderfgoed.be/thesaurus/typologie'
+ self.url = url
+
+ def get_by_id(self,id):
+ url = (self.url + '/%s.json') % id
+ r = requests.get(url)
+ if r.status_code == 404:
+ return False
+ result = r.json
+ if result['term_type'] == 'ND':
+ return self.get_by_id(result['use'])
+ concept = {}
+ concept['id'] = result['id']
+ concept['labels'] = []
+ concept['labels'].append({'type': 'pref', 'lang': result['language'], 'label': result['term']})
+ concept['broader'] = [result['broader_term']]
+ if 'narrower_terms' in result:
+ concept['narrower'] = result['narrower_terms']
+ if 'use_for' in result:
+ for t in result['use_for']:
+ term = self._get_term_by_id(t)
+ concept['labels'].append({'type': 'alt', 'lang': term['language'], 'label': term['term']})
+ if 'related_terms' in result:
+ concept['related'] = result['related_terms']
+ return concept
+
+ def _get_term_by_id(self, id):
+ '''Simple utility function to load a term.
+ '''
+ url = (self.url + '/%s.json') % id
+ r = requests.get(url)
+ return r.json
+
+ def find(self,query):
+ return self._do_query(query)
+
+ def get_all(self):
+ return self._do_query()
+
+ def _do_query(self,query=None):
+ url = self.url + '/lijst.json'
+ if query is not None:
+ args = {'term': query['label']}
+ else:
+ args = {'type[]': ['HR', 'PT', 'NL']}
+ r = requests.get(url, params=args)
+ result = r.json
+ return [{'id': x['id'], 'label': x['omschrijving']} for x in result['items']]
+
+ def expand_concept(self,id):
+ url = (self.url + '/%s/subtree.json') % id
+ r = requests.get(url)
+ return r.json
View
49 tests/test_providers.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+
+import os
+import sys
+sys.path.insert(0, os.path.abspath('..'))
+
+from skosprovider_oe.providers import (
+ OnroerendErfgoedProvider
+ )
+
+class OnroerendErfgoedProviderTests(unittest.TestCase):
+ def setUp(self):
+ self.typologie = OnroerendErfgoedProvider(
+ {'id': 'TYPOLOGIE'},
+ 'http://localhost/oeps/thesaurus/typologie'
+ )
+
+ def tearDown(self):
+ pass
+
+ def test_get_vocabulary_id(self):
+ self.assertEquals('TYPOLOGIE', self.typologie.get_vocabulary_id())
+
+ def test_get_metadata(self):
+ self.assertEquals({'id':'TYPOLOGIE', 'default_language': 'nl'},
+ self.typologie.get_metadata())
+
+ def test_get_by_unexisting_id(self):
+ self.assertFalse(self.typologie.get_by_id('RESTAFVAL'))
+
+ def test_find(self):
+ result = self.typologie.find({'label': 'kerken'})
+ self.assertGreater(len(result),0)
+ for c in result:
+ self.assertIn('id',c)
+ self.assertIn('label',c)
+
+ def test_get_all(self):
+ result = self.typologie.get_all()
+ self.assertGreater(len(result),0)
+ for c in result:
+ self.assertIn('id',c)
+ self.assertIn('label',c)
+
+ def test_expand_concept(self):
+ result = self.typologie.expand_concept(100)
+ self.assertGreater(len(result),0)
View
7 tox.ini
@@ -0,0 +1,7 @@
+[tox]
+envlist = py27
+[testenv]
+deps =
+ nose
+ coverage
+commands = nosetests []
Please sign in to comment.
Something went wrong with that request. Please try again.