Skip to content

Commit

Permalink
First version. Refs #44
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed Mar 20, 2018
1 parent 69bf29a commit ab9944f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
61 changes: 55 additions & 6 deletions pyramid_skosprovider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,56 @@
json_renderer
)

from pyramid.path import (
DottedNameResolver
)


class ISkosRegistry(Interface):
pass


def _build_skos_registry(registry):
def _parse_settings(settings):
defaults = {
'skosregistry_location': 'registry',
'skosregistry_factory': 'pyramid_skosprovider._skosregistry_factory',
}
args = defaults.copy()

# string setting
for short_key_name in ('skosregistry_location', 'skosregistry_factory'):
key_name = "skosprovider.%s" % short_key_name
if key_name in settings:
args[short_key_name] = settings.get(key_name)

return args


def _skosregistry_factory():
return Registry()


def _build_skos_registry(settings):
if 'skosregistry_factory' in settings:
r = DottedNameResolver()
skos_registry = r.resolve(settings['skosregistry_factory'])()
else:
skos_registry = Registry()
return skos_registry


def _register_global_skos_registry(registry, settings):
skos_registry = registry.queryUtility(ISkosRegistry)
if skos_registry is not None:
return skos_registry

skos_registry = Registry()
skos_registry = _build_skos_registry(settings)

registry.registerUtility(skos_registry, ISkosRegistry)
return registry.queryUtility(ISkosRegistry)


def get_skos_registry(registry):
def get_global_skos_registry(registry):
'''
Get the :class:`skosprovider.registry.Registry` attached to this pyramid
application.
Expand All @@ -38,13 +71,29 @@ def get_skos_registry(registry):
return regis.queryUtility(ISkosRegistry)


def get_request_skos_registry(request):
'''
Get the :class:`skosprovider.registry.Registry` attached to this request.
:rtype: :class:`skosprovider.registry.Registry`
'''
settings = _parse_settings(request.registry.settings)
skosregistry = _build_skos_registry(settings)
return skosregistry


def includeme(config):
_build_skos_registry(config.registry)
settings = _parse_settings(config.registry.settings)

if settings['skosregistry_location'] == 'registry':
_register_global_skos_registry(config.registry, settings)
config.add_request_method(get_global_skos_registry, 'skos_registry', reify=True)
else:
config.add_request_method(get_request_skos_registry, 'skos_registry', reify=True)

config.add_renderer('skosjson', json_renderer)

config.add_directive('get_skos_registry', get_skos_registry)
config.add_request_method(get_skos_registry, 'skos_registry', reify=True)
config.add_directive('get_skos_registry', get_global_skos_registry)

config.add_route(
'skosprovider.uri.deprecated',
Expand Down
4 changes: 3 additions & 1 deletion tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def skosmain(global_config, **settings):
class FunctionalTests(unittest.TestCase):

def setUp(self):
settings = {}
settings = {
'skosprovider.skosregistry_location': 'registry'
}
app = skosmain({}, **settings)
self.testapp = TestApp(app)

Expand Down
14 changes: 7 additions & 7 deletions tests/test_varia.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from pyramid_skosprovider import (
ISkosRegistry,
_build_skos_registry,
get_skos_registry,
_register_global_skos_registry,
get_global_skos_registry,
includeme
)

Expand All @@ -25,7 +26,7 @@ def __init__(self, settings=None):
if settings is None:
self.settings = {}
else: # pragma NO COVER
self.settings = settings
self.settings = settings

self.skos_registry = None

Expand All @@ -38,23 +39,22 @@ def registerUtility(self, skos_registry, iface):

class TestGetAndBuild(unittest.TestCase):

def test_get_skos_registry(self):
def test_get_global_skos_registry(self):
r = TestRegistry()
SR = Registry()
r.registerUtility(SR, ISkosRegistry)
SR2 = get_skos_registry(r)
SR2 = get_global_skos_registry(r)
self.assertEqual(SR, SR2)

def test_build_skos_registry_already_exists(self):
r = TestRegistry()
SR = Registry()
r.registerUtility(SR, ISkosRegistry)
SR2 = _build_skos_registry(r)
SR2 = get_global_skos_registry(r)
self.assertEqual(SR, SR2)

def test_build_skos_registry_default_settings(self):
r = TestRegistry()
SR = _build_skos_registry(r)
SR = _build_skos_registry({})
self.assertIsInstance(SR, Registry)


Expand Down

0 comments on commit ab9944f

Please sign in to comment.