diff --git a/.travis.yml b/.travis.yml index 610167b..e4dfa07 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ sudo: false language: python python: -- 2.7 -- 3.4 -- 3.5 - 3.6 +- 3.8 +- 3.9 install: - pip install -U setuptools - pip install -r requirements.txt #fix versions diff --git a/docs/source/conf.py b/docs/source/conf.py index 0955213..03e58f7 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # skosprovider_heritagedata documentation build configuration file, created by # sphinx-quickstart on Fri Sep 19 10:30:09 2014. @@ -49,8 +48,8 @@ master_doc = 'index' # General information about the project. -project = u'skosprovider_heritagedata' -copyright = u'2014-2017, Flanders Heritage Agency' +project = 'skosprovider_heritagedata' +copyright = '2014-2017, Flanders Heritage Agency' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -202,8 +201,8 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ('index', 'skosprovider_heritagedata.tex', u'skosprovider\\_heritagedata Documentation', - u'Flanders Heritage Agency', 'manual'), + ('index', 'skosprovider_heritagedata.tex', 'skosprovider\\_heritagedata Documentation', + 'Flanders Heritage Agency', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -232,8 +231,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'skosprovider_heritagedata', u'skosprovider_heritagedata Documentation', - [u'Flanders Heritage Agency'], 1) + ('index', 'skosprovider_heritagedata', 'skosprovider_heritagedata Documentation', + ['Flanders Heritage Agency'], 1) ] # If true, show URL addresses after external links. @@ -246,8 +245,8 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'skosprovider_heritagedata', u'skosprovider_heritagedata Documentation', - u'Flanders Heritage Agency', 'skosprovider_heritagedata', 'One line description of project.', + ('index', 'skosprovider_heritagedata', 'skosprovider_heritagedata Documentation', + 'Flanders Heritage Agency', 'skosprovider_heritagedata', 'One line description of project.', 'Miscellaneous'), ] diff --git a/examples/expand.py b/examples/expand.py index 9b33059..434f318 100755 --- a/examples/expand.py +++ b/examples/expand.py @@ -1,5 +1,4 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- ''' This script demonstrates using the HeritagedataProvider to expand a concept ''' diff --git a/examples/find.py b/examples/find.py index b876870..8061c85 100755 --- a/examples/find.py +++ b/examples/find.py @@ -1,5 +1,4 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- ''' This script demonstrates using the HeritagedataProvider to find the concepts with 'iron' in their label ''' diff --git a/examples/period.py b/examples/period.py index 7253f4a..c894385 100755 --- a/examples/period.py +++ b/examples/period.py @@ -1,5 +1,4 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- ''' This script demonstrates using the HeritagedataProvider to get the concept of 'POST MEDIEVAL'. diff --git a/requirements-dev.txt b/requirements-dev.txt index 9876ec5..bd6c8dd 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,8 +2,8 @@ --requirement requirements.txt # Documentation -Sphinx==1.7.8 +Sphinx==4.3.0 #testing -pytest==3.7.4 -pytest-cov==2.5.1 +pytest==6.2.5 +pytest-cov==3.0.0 diff --git a/requirements.txt b/requirements.txt index efdc1f6..851858a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -requests==2.19.1 -#skosprovider==0.6.0 --e git+https://github.com/koenedaele/skosprovider.git#egg=skosprovider +requests==2.26.0 +skosprovider==1.1.0 +#-e git+https://github.com/OnroerendErfgoed/skosprovider.git#egg=skosprovider rdflib==4.2.2 diff --git a/skosprovider_heritagedata/providers.py b/skosprovider_heritagedata/providers.py index a4ea1b6..8056096 100644 --- a/skosprovider_heritagedata/providers.py +++ b/skosprovider_heritagedata/providers.py @@ -1,28 +1,25 @@ -# -*- coding: utf-8 -*- ''' This module implements a :class:`skosprovider.providers.VocabularyProvider` for http://www.heritagedata.org. ''' -import requests -from requests.exceptions import ConnectionError - -import warnings import logging -log = logging.getLogger(__name__) +import warnings +import requests from language_tags import tags from rdflib.namespace import SKOS - +from requests.exceptions import ConnectionError from skosprovider.exceptions import ProviderUnavailableException from skosprovider.providers import VocabularyProvider -from skosprovider_heritagedata.utils import ( - _split_uri, - uri_to_graph, - conceptscheme_from_uri, - things_from_graph -) +from skosprovider_heritagedata.utils import _split_uri +from skosprovider_heritagedata.utils import conceptscheme_from_uri +from skosprovider_heritagedata.utils import things_from_graph +from skosprovider_heritagedata.utils import uri_to_graph + +log = logging.getLogger(__name__) + class HeritagedataProvider(VocabularyProvider): """A provider that can work with the Heritagedata services of @@ -82,7 +79,7 @@ def get_by_id(self, id): Returns False if non-existing id """ graph = uri_to_graph( - '%s/%s/%s.rdf' % (self.scheme_uri, "concepts", id), + '{}/{}/{}.rdf'.format(self.scheme_uri, "concepts", id), session=self.session ) if graph is False: @@ -218,7 +215,7 @@ def get_top_display(self, **kwargs): """ 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.scheme_uri} + params = {'schemeURI': self.scheme_uri, 'alias': True} ret = self._get_items("getTopConceptsForScheme", params, **kwargs) language = self._get_language(**kwargs) sort = self._get_sort(**kwargs) @@ -231,7 +228,7 @@ def get_children_display(self, id, **kwargs): :param str id: A concept or collection id. :returns: A :class:`lst` of concepts and collections. """ - params = {'conceptURI': self.scheme_uri + "/concepts/" + id} + params = {'conceptURI': self.scheme_uri + "/concepts/" + id, 'alias': True} ret = self._get_items("getConceptRelations", params, **kwargs) language = self._get_language(**kwargs) sort = self._get_sort(**kwargs) @@ -289,9 +286,9 @@ def _get_items(self, service, params, **kwargs): try: res = self.session.get(request, params=params) except ConnectionError as e: - raise ProviderUnavailableException("Request could not be executed - Request: %s - Params: %s" % (request, params)) + raise ProviderUnavailableException(f"Request could not be executed - Request: {request} - Params: {params}") if res.status_code == 404: - raise ProviderUnavailableException("Service not found (status_code 404) - Request: %s - Params: %s" % (request, params)) + raise ProviderUnavailableException(f"Service not found (status_code 404) - Request: {request} - Params: {params}") res.encoding = 'utf-8' result = res.json() d = {} @@ -306,24 +303,24 @@ def _get_items(self, service, params, **kwargs): property = None if 'property' in r.keys(): property = r['property'] - if not service == 'getConceptRelations' or property == str(SKOS.narrower): + if not service == 'getConceptRelations' or property == "skos:narrower": item = { - 'id': _split_uri(uri, 1), - 'uri': uri, - 'type': 'concept', - 'label': label, - 'lang': language + 'id': _split_uri(uri, 1), + 'uri': uri, + 'type': 'concept', + 'label': label, + 'lang': language } - if uri not in d: - d[uri] = item - if tags.tag(d[uri]['lang']).format == tags.tag(self._get_language(**kwargs)).format: - pass - elif tags.tag(item['lang']).format == tags.tag(self._get_language(**kwargs)).format: - d[uri] = item - elif tags.tag(item['lang']).language and (tags.tag(item['lang']).language.format == tags.tag(self._get_language(**kwargs)).language.format): - d[uri] = item - elif tags.tag(item['lang']).format == 'en': - d[uri] = item + if uri not in d: + d[uri] = item + if tags.tag(d[uri]['lang']).format == tags.tag(self._get_language(**kwargs)).format: + pass + elif tags.tag(item['lang']).format == tags.tag(self._get_language(**kwargs)).format: + d[uri] = item + elif tags.tag(item['lang']).language and (tags.tag(item['lang']).language.format == tags.tag(self._get_language(**kwargs)).language.format): + d[uri] = item + elif tags.tag(item['lang']).format == 'en': + d[uri] = item return list(d.values()) def _sort(self, items, sort, language='en', reverse=False): diff --git a/skosprovider_heritagedata/utils.py b/skosprovider_heritagedata/utils.py index 61b3c8b..4d41d35 100644 --- a/skosprovider_heritagedata/utils.py +++ b/skosprovider_heritagedata/utils.py @@ -1,33 +1,23 @@ -# -*- coding: utf-8 -*- ''' Utility functions for :mod:`skosprovider_heritagedata`. ''' -import requests -from skosprovider.skos import ( - Concept, - Label, - Note, - ConceptScheme) -from skosprovider.exceptions import ProviderUnavailableException - import logging -import sys -import requests - -log = logging.getLogger(__name__) - -PY3 = sys.version_info[0] == 3 - -if PY3: # pragma: no cover - binary_type = bytes -else: # pragma: no cover - binary_type = str import rdflib +import requests +from rdflib.namespace import DCTERMS +from rdflib.namespace import RDF +from rdflib.namespace import RDFS +from rdflib.namespace import SKOS from rdflib.term import URIRef -from rdflib.namespace import RDF, SKOS, DC, DCTERMS, RDFS +from skosprovider.exceptions import ProviderUnavailableException +from skosprovider.skos import Concept +from skosprovider.skos import ConceptScheme +from skosprovider.skos import Label +from skosprovider.skos import Note +log = logging.getLogger(__name__) PROV = rdflib.Namespace('http://www.w3.org/ns/prov#') def conceptscheme_from_uri(conceptscheme_uri, **kwargs): @@ -160,15 +150,16 @@ def uri_to_graph(uri, **kwargs): if res.status_code == 404: return False graph.parse(data=res.content) - #heritagedata.org returns a empy page/graph when a resource does not exists (statsu_code 200). For this reason we return False if the graph is empty + # heritagedata.org returns a empy page/graph when a resource does not exists + # (statsu_code 200). For this reason we return False if the graph is empty if len(graph) == 0: return False return graph def text_(s, encoding='latin-1', errors='strict'): - """ If ``s`` is an instance of ``binary_type``, return + """ If ``s`` is an instance of ``bytes``, return ``s.decode(encoding, errors)``, otherwise return ``s``""" - if isinstance(s, binary_type): + if isinstance(s, bytes): return s.decode(encoding, errors) return s diff --git a/tests/test_providers.py b/tests/test_providers.py index e9dfe73..b3196f9 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -1,12 +1,12 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- -from skosprovider_heritagedata.providers import ( - HeritagedataProvider -) -from skosprovider.exceptions import ProviderUnavailableException import unittest +from skosprovider.exceptions import ProviderUnavailableException + +from skosprovider_heritagedata.providers import HeritagedataProvider + + class HeritagedataProviderTests(unittest.TestCase): def test_set_custom_session(self): @@ -189,7 +189,7 @@ def test_find_with_collection(self): def test_find_collections(self): r = HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find({'type': 'collection'}) - self.assertEquals(r, []) + self.assertEqual(r, []) def test_find_wrong_type(self): self.assertRaises(ValueError, HeritagedataProvider({'id': 'Heritagedata'}, scheme_uri='http://purl.org/heritagedata/schemes/eh_period').find, {'type': 'collectie'}) diff --git a/tests/test_utils.py b/tests/test_utils.py index 563c9b8..baa2a3c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,10 @@ -# -*- coding: utf-8 -*- import unittest + from rdflib import Graph from skosprovider.exceptions import ProviderUnavailableException -from skosprovider_heritagedata.utils import text_, uri_to_graph +from skosprovider_heritagedata.utils import text_ +from skosprovider_heritagedata.utils import uri_to_graph class UtilsTests(unittest.TestCase): @@ -16,15 +17,15 @@ def tearDown(self): def test_text(self): res = text_(b'test123') - self.assertEqual(u'test123', res) + self.assertEqual('test123', res) def test_text_unicode(self): - res = text_(u'test123') - self.assertEqual(u'test123', res) + res = text_('test123') + self.assertEqual('test123', res) def test_text_utf8(self): res = text_(b'LaPe\xc3\xb1a', 'utf-8') - self.assertEqual(u'LaPe\xf1a', res) + self.assertEqual('LaPe\xf1a', res) def test_uri_to_graph(self): res = uri_to_graph('http://purl.org/heritagedata/schemes/eh_period.rdf')