From 5636f47041a9b693eb114efca524bec631dda1cd Mon Sep 17 00:00:00 2001 From: cahytinne Date: Mon, 22 May 2017 13:29:43 +0200 Subject: [PATCH] initialize abc.ABCMeta class using six #8 --- pyramid_urireferencer/protected_resources.py | 11 ----------- pyramid_urireferencer/referencer.py | 5 +++-- requirements.txt | 1 + tests/test_protected_resources.py | 12 ------------ tests/test_views.py | 8 -------- 5 files changed, 4 insertions(+), 33 deletions(-) diff --git a/pyramid_urireferencer/protected_resources.py b/pyramid_urireferencer/protected_resources.py index d9fe104..344be68 100644 --- a/pyramid_urireferencer/protected_resources.py +++ b/pyramid_urireferencer/protected_resources.py @@ -8,7 +8,6 @@ from pyramid.httpexceptions import ( HTTPInternalServerError, - HTTPNotImplemented, HTTPConflict) from webob import Response @@ -33,16 +32,6 @@ def protected_operation(fn): def advice(parent_object, *args, **kw): referencer = pyramid_urireferencer.get_referencer(parent_object.request.registry) uri = referencer.get_uri(parent_object.request) - if uri is None: - if parent_object.request.headers.get("Accept", None) == "application/json": - return Response( - status='501 Not Implemented', - json_body={"message": "Unable to retrieve the uri."}, - content_type='application/json' - ) - else: - log.error("Urireferencer: Unable to retrieve the uri.") - raise HTTPNotImplemented(detail="Urireferencer: Unable to retrieve the uri.") registery_response = referencer.is_referenced(uri) if registery_response.has_references: if parent_object.request.headers.get("Accept", None) == "application/json": diff --git a/pyramid_urireferencer/referencer.py b/pyramid_urireferencer/referencer.py index 34f5716..6aebbee 100644 --- a/pyramid_urireferencer/referencer.py +++ b/pyramid_urireferencer/referencer.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import abc +import six import requests import logging @@ -9,6 +10,7 @@ log = logging.getLogger(__name__) +@six.add_metaclass(abc.ABCMeta) class AbstractReferencer: """ This is an abstract class that defines what a Referencer needs to be able to handle. @@ -20,7 +22,6 @@ class AbstractReferencer: * Check if a certain uri is being used in another application by query a central registry. """ - __metaclass__ = abc.ABCMeta @abc.abstractmethod def get_uri(self, request): @@ -55,12 +56,12 @@ def is_referenced(self, uri): """ +@six.add_metaclass(abc.ABCMeta) class Referencer(AbstractReferencer): """ This is an implementation of the :class:`AbstractReferencer` that adds a generic :meth:`is_referenced` method and a plain :meth:`references` method.. """ - __metaclass__ = abc.ABCMeta def __init__(self, registry_url, **kwargs): """ diff --git a/requirements.txt b/requirements.txt index c459f13..2a67985 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests==2.9.1 pyramid==1.6.1 +six==1.10.0 diff --git a/tests/test_protected_resources.py b/tests/test_protected_resources.py index 8f318ad..730c7a7 100644 --- a/tests/test_protected_resources.py +++ b/tests/test_protected_resources.py @@ -144,15 +144,3 @@ def test_protected_operation_500_json(self, is_referenced_mock): is_referenced_call = is_referenced_mock.mock_calls[0] self.assertEqual('https://id.erfgoed.net/resources/1', is_referenced_call[1][0]) - - def test_protected_operation_implementation_error(self): - dummy = DummyParentError() - self.assertRaises(HTTPNotImplemented, dummy.protected_dummy) - - dummy.request.headers = {"Accept": "application/json"} - res = dummy.protected_dummy() - self.assertEqual(501, res.status_code) - self.assertEqual(res.json_body["message"], - "Unable to retrieve the uri.") - self.assertEqual("application/json", res.content_type) - diff --git a/tests/test_views.py b/tests/test_views.py index 266371b..33a0005 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -73,11 +73,3 @@ def get_uri(self, request): return 'https://id.erfgoed.net/resources/1' -class TestReferencerError(Referencer): - def references(self, uri, request): - return None - - def get_uri(self, request): - return None - -