Skip to content

Commit

Permalink
Merge 3230c26 into 55ff45a
Browse files Browse the repository at this point in the history
  • Loading branch information
roefem committed Dec 15, 2021
2 parents 55ff45a + 3230c26 commit 473ae77
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 91 deletions.
5 changes: 2 additions & 3 deletions .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
Expand Down
17 changes: 8 additions & 9 deletions 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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'),
]

Expand Down
1 change: 0 additions & 1 deletion examples/expand.py
@@ -1,5 +1,4 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
This script demonstrates using the HeritagedataProvider to expand a concept
'''
Expand Down
1 change: 0 additions & 1 deletion 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
'''
Expand Down
1 change: 0 additions & 1 deletion 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'.
Expand Down
6 changes: 3 additions & 3 deletions requirements-dev.txt
Expand Up @@ -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
6 changes: 3 additions & 3 deletions 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
65 changes: 31 additions & 34 deletions 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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):
Expand Down
39 changes: 15 additions & 24 deletions 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):
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions 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):
Expand Down Expand Up @@ -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'})
Expand Down
13 changes: 7 additions & 6 deletions 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):
Expand All @@ -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')
Expand Down

0 comments on commit 473ae77

Please sign in to comment.