From 63a5c54a65c314530cad65e36bef869556f7e2b0 Mon Sep 17 00:00:00 2001 From: Jonas Geuens Date: Thu, 3 Dec 2020 09:48:06 +0100 Subject: [PATCH 1/8] #66 py3 compatibel maken en py2 droppen --- .travis.yml | 5 +---- docs/source/conf.py | 21 ++++++++++----------- requirements-dev.txt | 18 +++++++++--------- setup.py | 5 +---- tests/conftest.py | 6 +++--- tests/test_functional.py | 3 ++- tests/test_general.py | 3 +-- tests/test_models.py | 7 ++++--- tests/test_registry.py | 27 +++++++-------------------- tests/test_utils.py | 3 +-- tests/test_views.py | 22 ++++++++++++++-------- uriregistry/__init__.py | 28 ++++++++++++++-------------- uriregistry/models.py | 18 ++++++++++-------- uriregistry/registry.py | 29 ++++++++++++++++------------- uriregistry/utils.py | 9 ++++----- uriregistry/views.py | 25 ++++++++++--------------- 16 files changed, 107 insertions(+), 122 deletions(-) diff --git a/.travis.yml b/.travis.yml index 998b92c..74a1438 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: python python: - - "2.7" - - "3.3" - - "3.4" - - "3.5" + - "3.6" env: - LC_ALL=en_US.utf-8 install: diff --git a/docs/source/conf.py b/docs/source/conf.py index 7c5ef4b..58da537 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # RestRegistry documentation build configuration file, created by # sphinx-quickstart on Wed Aug 20 13:33:02 2014. @@ -57,17 +56,17 @@ master_doc = 'index' # General information about the project. -project = u'UriRegistry' -copyright = u'2014-2015, Onroerend Erfgoed' +project = 'UriRegistry' +copyright = '2014-2020, Onroerend Erfgoed' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '0.1.0' +version = '0.1.3' # The full version, including alpha/beta/rc tags. -release = '0.1.1' +release = '0.1.3' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -210,8 +209,8 @@ # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ('index', 'UriRegistry.tex', u'UriRegistry Documentation', - u'Onroerend Erfgoed', 'manual'), + ('index', 'UriRegistry.tex', 'UriRegistry Documentation', + 'Onroerend Erfgoed', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -240,8 +239,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'uriregistry', u'UriRegistry Documentation', - [u'Onroerend Erfgoed'], 1) + ('index', 'uriregistry', 'UriRegistry Documentation', + ['Onroerend Erfgoed'], 1) ] # If true, show URL addresses after external links. @@ -254,8 +253,8 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'UriRegistry', u'UriRegistry Documentation', - u'Onroerend Erfgoed', 'UriRegistry', "Where's my uri?", + ('index', 'UriRegistry', 'UriRegistry Documentation', + 'Onroerend Erfgoed', 'UriRegistry', "Where's my uri?", 'Miscellaneous'), ] diff --git a/requirements-dev.txt b/requirements-dev.txt index 9ccba44..f27fef0 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,19 +2,19 @@ --requirement requirements.txt # Documentation -Sphinx==1.7.8 +Sphinx==3.1.1 sphinxcontrib-httpdomain==1.7.0 -sphinxcontrib-plantuml==0.12 +sphinxcontrib-plantuml==0.18 -waitress==1.1.0 -pyramid_debugtoolbar==4.4 +waitress==1.4.4 +pyramid_debugtoolbar==4.6.1 #testing -pytest==3.7.4 -pytest-cov==2.5.1 -webtest==2.0.30 -httpretty==0.9.5 -coveralls==1.5.0 +pytest==5.4.3 +pytest-cov==2.10.0 +webtest==2.0.35 +httpretty==1.0.3 +coveralls==2.0.0 # Wheel wheel==0.31.1 diff --git a/setup.py b/setup.py index 0cf3721..db5da88 100644 --- a/setup.py +++ b/setup.py @@ -22,10 +22,7 @@ classifiers=[ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Programming Language :: Python", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3.3", - "Programming Language :: Python :: 3.4", - "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", "Framework :: Pyramid", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: WSGI :: Application", diff --git a/tests/conftest.py b/tests/conftest.py index 39b49a4..7920d63 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,13 +7,13 @@ def registryconfig(): cfg = { 'uri_templates': [ { - 'match_uri': 'http://id.erfgoed.net/foobar/\d+', + 'match_uri': r'http://id.erfgoed.net/foobar/\d+', 'applications': ['http://localhost:5555', 'http://localhost:2222'] }, { - 'match_uri': 'http://id.erfgoed.net/bar/\w+', + 'match_uri': r'http://id.erfgoed.net/bar/\w+', 'applications': ['http://localhost:5555'] }, { - 'match_uri': 'http://id.erfgoed.net/foo/.+', + 'match_uri': r'http://id.erfgoed.net/foo/.+', 'applications': ['http://localhost:2222'] } ], diff --git a/tests/test_functional.py b/tests/test_functional.py index af709f1..be338b7 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -1,10 +1,10 @@ -# -*- coding: utf-8 -*- import os import pytest from webtest import TestApp + @pytest.fixture() def app(): settings = { @@ -13,6 +13,7 @@ def app(): from uriregistry import main return TestApp(main({}, **settings)) + class TestFunctional: def test_home(self, app): diff --git a/tests/test_general.py b/tests/test_general.py index deea14d..82b458c 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -1,5 +1,3 @@ -import pytest - import os from uriregistry import ( @@ -7,6 +5,7 @@ _load_configuration ) + class TestGeneral: def test_parse_settings(self): diff --git a/tests/test_models.py b/tests/test_models.py index 834dac5..6d17fdc 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,9 +1,10 @@ -from uriregistry.models import Application, UriTemplate +from uriregistry.models import UriTemplate + class TestUri: def test_numeric_matches(self): - u = UriTemplate('http://id.erfgoed.net/foobar/\d+$', []) + u = UriTemplate(r'http://id.erfgoed.net/foobar/\d+$', []) assert u.matches('http://id.erfgoed.net/foobar/1') assert u.matches('http://id.erfgoed.net/foobar/12') assert u.matches('http://id.erfgoed.net/foobar/123456789') @@ -14,7 +15,7 @@ def test_numeric_matches(self): assert not u.matches('http://id.erfgoed.net/foobar/1a') def test_alphanumeric_matches(self): - u = UriTemplate('http://id.erfgoed.net/foobar/\w+$', []) + u = UriTemplate(r'http://id.erfgoed.net/foobar/\w+$', []) assert u.matches('http://id.erfgoed.net/foobar/a') assert u.matches('http://id.erfgoed.net/foobar/at') assert u.matches('http://id.erfgoed.net/foobar/baz') diff --git a/tests/test_registry.py b/tests/test_registry.py index bf550c1..20c21dc 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -1,22 +1,8 @@ -import os -import unittest -import pytest - -from pyramid import testing -from pyramid.response import Response - -from uriregistry import _load_configuration -from uriregistry.registry import ( - IUriRegistry, - UriRegistry, - _build_uri_registry, - get_uri_registry -) -from uriregistry.models import Application -from uriregistry.utils import query_application -from uriregistry.views import RegistryView, _get_registry_response - -from pyramid_urireferencer.models import RegistryResponse, ApplicationResponse +from uriregistry.registry import IUriRegistry +from uriregistry.registry import UriRegistry +from uriregistry.registry import _build_uri_registry +from uriregistry.registry import get_uri_registry + class TestRegistry: @@ -35,13 +21,14 @@ def test_get_applications_alphanumeric_matches(self, uriregistry): apps = uriregistry.get_applications('http://id.erfgoed.net/foobar/a') assert len(apps) == 0 + class MockRegistry: def __init__(self, settings=None): if settings is None: self.settings = {} - else: # pragma NO COVER + else: # pragma NO COVER self.settings = settings self.uri_registry = None diff --git a/tests/test_utils.py b/tests/test_utils.py index 1a6a93d..45b07f5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,10 +1,9 @@ -# -*- coding: utf-8 -*- - from uriregistry.models import Application from pyramid_urireferencer.models import ApplicationResponse from uriregistry.utils import query_application + class TestUtils: def test_get_application_response(self): diff --git a/tests/test_views.py b/tests/test_views.py index c334673..dc1615b 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pyramid import testing @@ -12,6 +11,7 @@ def pyramid_request(): return testing.DummyRequest() + class TestViews: def test_home(self, pyramid_request): @@ -38,17 +38,23 @@ def test_get_references_uri(self, pyramid_request, uriregistry): assert not res.has_references assert not res.success + def test_get_registry_response(): from uriregistry.views import _get_registry_response uri = "http://id.erfgoed.net/foobar/2/" app_response_success_ref = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, True, 2, []) - app_response_success_ref2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, True, 3, []) - app_response_nosuccess = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", False, None, None, None) - app_response_nosuccess2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", False, None, None, None) - app_response_success_noref = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, False, None, None) - app_response_success_noref2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, False, None, None) + app_response_success_ref2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, True, 3, + []) + app_response_nosuccess = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", False, None, None, + None) + app_response_nosuccess2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", False, None, None, + None) + app_response_success_noref = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, False, + None, None) + app_response_success_noref2 = ApplicationResponse("app2_name", "http://uri/app2", "http://url/app2", True, False, + None, None) - r = _get_registry_response([app_response_success_ref,app_response_success_ref2], uri) + r = _get_registry_response([app_response_success_ref, app_response_success_ref2], uri) assert isinstance(r, RegistryResponse) assert len(r.applications) == 2 assert r.query_uri == uri @@ -56,7 +62,7 @@ def test_get_registry_response(): assert r.success assert r.count == 5 - r = _get_registry_response([app_response_success_ref,app_response_nosuccess], uri) + r = _get_registry_response([app_response_success_ref, app_response_nosuccess], uri) assert isinstance(r, RegistryResponse) assert len(r.applications) == 2 assert r.query_uri == uri diff --git a/uriregistry/__init__.py b/uriregistry/__init__.py index 6d933ca..765fdd0 100644 --- a/uriregistry/__init__.py +++ b/uriregistry/__init__.py @@ -1,12 +1,10 @@ -# -*- coding: utf-8 -*- - import logging + log = logging.getLogger(__name__) from pyramid.config import Configurator import os -import json import yaml from .models import Application, UriTemplate @@ -14,15 +12,15 @@ def _parse_settings(settings): - ''' + """ Parse the relevant settings for this application. :param dict settings: - ''' + """ log.debug(settings) - prefix='uriregistry' + prefix = 'uriregistry' defaults = { 'config': os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'sample.yaml')) @@ -30,38 +28,40 @@ def _parse_settings(settings): urireg_settings = defaults.copy() - for short_key_name in ('config', ): - key_name = '%s.%s' % (prefix, short_key_name) + for short_key_name in ('config',): + key_name = f'{prefix}.{short_key_name}' if key_name in settings: urireg_settings[short_key_name] = \ settings.get(key_name, defaults.get(short_key_name, None)) for short_key in urireg_settings: - long_key = '%s.%s' % (prefix, short_key) + long_key = f'{prefix}.{short_key}' settings[long_key] = urireg_settings[short_key] return urireg_settings + def _load_configuration(path): - ''' + """ Load the configuration for the UriRegistry. :param str path: Path to the config file in YAML format. :returns: A :class:`dict` with the config options. - ''' + """ log.debug('Loading uriregistry config from %s.' % path) - f = open(path, 'r') + f = open(path) content = yaml.load(f.read()) log.debug(content) f.close() return content + def main(global_config, **settings): - ''' + """ This function returns a Pyramid WSGI application. :param pyramid.config.Configurator global_config: - ''' + """ config = Configurator(settings=settings) diff --git a/uriregistry/models.py b/uriregistry/models.py index ee32c80..142c427 100644 --- a/uriregistry/models.py +++ b/uriregistry/models.py @@ -1,37 +1,39 @@ -# -*- coding: utf-8 -*- - import re + class UriTemplate: - ''' + """ Represents the config for a certain uri template. :param string match_uri: A regex that needs to be matched. :param list applications: A list of application uri's. - ''' + """ + def __init__(self, match_uri, applications): self.id = id self.match_uri = re.compile(match_uri) self.applications = applications def matches(self, uri): - ''' + """ Does the URI match this template? :param string uri: URI to be matched :rtype: boolean - ''' + """ res = self.match_uri.match(uri) return res is not None + class Application: - ''' + """ Represents the config for an application. :param string uri: A uri that identifies the application :param string title: A title for the application :param string service_url: The url for the service that can be queried - ''' + """ + def __init__(self, uri, title, service_url): self.uri = uri self.title = title diff --git a/uriregistry/registry.py b/uriregistry/registry.py index fb22d83..e33ed7e 100644 --- a/uriregistry/registry.py +++ b/uriregistry/registry.py @@ -1,18 +1,19 @@ -# -*- coding: utf-8 -*- - from zope.interface import Interface -from .models import Application, UriTemplate +from .models import Application +from .models import UriTemplate + class IUriRegistry(Interface): pass + class UriRegistry: - ''' + """ Central registry that tracks uris and the applications they are being used in. - ''' + """ - def __init__(self, applications = [], uris = []): + def __init__(self, applications=[], uris=[]): self.applications = [Application(app['uri'], app['name'], app['service_url']) for app in applications] self.uris = [ UriTemplate( @@ -22,11 +23,11 @@ def __init__(self, applications = [], uris = []): ] def get_applications(self, uri): - ''' + """ Get all applications that might have a reference to this URI. :param string uri: Uri for which the applications need to be found. - ''' + """ applications = [] for u in self.uris: if u.matches(uri): @@ -34,12 +35,13 @@ def get_applications(self, uri): applications = list(set(applications)) return applications + def _build_uri_registry(registry, registryconfig): - ''' + """ :param pyramid.registry.Registry registry: Pyramid registry :param dict registryconfig: UriRegistry config in dict form. :rtype: :class:`uriregistry.registry.UriRegistry` - ''' + """ uri_registry = registry.queryUtility(IUriRegistry) if uri_registry is not None: return uri_registry @@ -52,14 +54,15 @@ def _build_uri_registry(registry, registryconfig): registry.registerUtility(uri_registry, IUriRegistry) return registry.queryUtility(IUriRegistry) + def get_uri_registry(registry): - ''' + """ Get the :class:`uriregistry.registry.UriRegistry` attached to this pyramid application. :rtype: :class:`uriregistry.registry.UriRegistry` - ''' - #Argument might be a config or request + """ + # Argument might be a config or request regis = getattr(registry, 'registry', None) if regis is None: regis = registry diff --git a/uriregistry/utils.py b/uriregistry/utils.py index 961fdf9..9036a62 100644 --- a/uriregistry/utils.py +++ b/uriregistry/utils.py @@ -1,12 +1,11 @@ -# -*- coding: utf-8 -*- - import logging -log = logging.getLogger(__name__) - import requests from pyramid_urireferencer.models import ApplicationResponse +log = logging.getLogger(__name__) + + def query_application(app, uri): """ Checks if a certain app has references to a URI. @@ -20,5 +19,5 @@ def query_application(app, uri): a = ApplicationResponse.load_from_json(r.json()) return a except Exception as e: - log.error('Could not check if uri %s is known to app %s (uri: %s)' % (uri, app.title, app.uri)) + log.error(f'Could not check if uri {uri} is known to app {app.title} (uri: {app.uri})') return ApplicationResponse(app.title, app.uri, app.service_url, False, None, None, None) diff --git a/uriregistry/views.py b/uriregistry/views.py index e888330..09ca1b7 100644 --- a/uriregistry/views.py +++ b/uriregistry/views.py @@ -1,36 +1,31 @@ -import requests +import logging +from pyramid.httpexceptions import HTTPBadRequest from pyramid.response import Response from pyramid.view import view_config -from pyramid.httpexceptions import ( - HTTPBadRequest -) - -from pyramid_urireferencer.models import ( - ApplicationResponse, - RegistryResponse -) +from pyramid_urireferencer.models import RegistryResponse from .utils import query_application -import logging log = logging.getLogger(__name__) -class ApplicationView(object): +class ApplicationView: def __init__(self, request): self.request = request + class RestView(ApplicationView): pass + class RegistryView(RestView): @view_config(route_name='references', renderer='json', accept='application/json') def get_references(self): - ''' + """ Collect the references for a URI and relay them to the client. - ''' + """ uri = self.request.params.get('uri', None) if not uri: raise HTTPBadRequest('Please include a URI parameter.') @@ -42,9 +37,9 @@ def get_references(self): @view_config(route_name='home', request_method='GET') def home(self): - ''' + """ The root information page - ''' + """ return Response(service_info, content_type='text/plain', status_int=200) From f1797280e980cb2ec5de2baf6156d5ad503171e7 Mon Sep 17 00:00:00 2001 From: Jonas Geuens Date: Thu, 3 Dec 2020 09:50:19 +0100 Subject: [PATCH 2/8] pyYAML upgrade --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 67238f6..9a1f0dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ requests==2.21.0 pyramid==1.10.1 -PyYAML==3.13 +PyYAML==5.3.1 pyramid_urireferencer==0.6.0 From 3b3a1d55a3668cee2982c9fada6ebf1830cd891f Mon Sep 17 00:00:00 2001 From: Jonas Geuens Date: Thu, 3 Dec 2020 09:52:30 +0100 Subject: [PATCH 3/8] import cleanup --- uriregistry/__init__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/uriregistry/__init__.py b/uriregistry/__init__.py index 765fdd0..78b5004 100644 --- a/uriregistry/__init__.py +++ b/uriregistry/__init__.py @@ -1,15 +1,14 @@ +import os +import yaml import logging -log = logging.getLogger(__name__) - from pyramid.config import Configurator -import os -import yaml - from .models import Application, UriTemplate from .registry import get_uri_registry, _build_uri_registry +log = logging.getLogger(__name__) + def _parse_settings(settings): """ From 6f1cd478127f42849af893e574ef6890072c46cd Mon Sep 17 00:00:00 2001 From: Jonas Geuens Date: Fri, 4 Dec 2020 09:40:41 +0100 Subject: [PATCH 4/8] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 74a1438..3c1e57b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: python python: - - "3.6" + - "3.8" env: - LC_ALL=en_US.utf-8 install: From 03eb7308a542e9389930195e4f695b985d3cde52 Mon Sep 17 00:00:00 2001 From: Jonas Geuens Date: Fri, 4 Dec 2020 09:40:56 +0100 Subject: [PATCH 5/8] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index db5da88..0344bea 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ classifiers=[ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Programming Language :: Python", - "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.8", "Framework :: Pyramid", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: WSGI :: Application", From 07224836698968b5c7c3406adf34daf2122a1f40 Mon Sep 17 00:00:00 2001 From: Wouter Claeys Date: Fri, 4 Dec 2020 11:20:03 +0100 Subject: [PATCH 6/8] Refs #69 --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9a1f0dc..a9d96ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -requests==2.21.0 -pyramid==1.10.1 +requests==2.25.0 +pyramid==1.10.5 PyYAML==5.3.1 -pyramid_urireferencer==0.6.0 +pyramid_urireferencer==0.7.0 From 1bdf6adf8fc5eb96eb7f4f361c69a327aae3cb6b Mon Sep 17 00:00:00 2001 From: Wouter Claeys Date: Mon, 7 Dec 2020 11:39:20 +0100 Subject: [PATCH 7/8] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0344bea..c239930 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ ] setup(name='uriregistry', - version='0.1.2', + version='0.2.0', description='A central URI registry that tracks where a certain URI is being used.', long_description=README + '\n\n' + CHANGES, classifiers=[ From 3c85c6bd865bc06992c3ec7bac1f541588179fb9 Mon Sep 17 00:00:00 2001 From: Wouter Claeys Date: Mon, 7 Dec 2020 11:40:05 +0100 Subject: [PATCH 8/8] Update CHANGES.rst --- CHANGES.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index d3f69dd..0be9e43 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +0.2.0 (07-12-2020) +------------------ + +- Library update(#69) +- Py3 compatibel maken (#66) + 0.1.2 (08-06-2017) ------------------