Skip to content

Instantly share code, notes, and snippets.

@twillis
Created April 19, 2013 13:58
Show Gist options
  • Save twillis/5420533 to your computer and use it in GitHub Desktop.
Save twillis/5420533 to your computer and use it in GitHub Desktop.
I needed a way to register classes used in the application and wondered how easy it would be to hook into pyramids Configurator.scan method. Turns out it's not that hard.
"""play with venusian until you have a decorator that registers
classes with request.registry["service"] """
import unittest
import venusian
from pyramid.config import Configurator
class service(object):
def __init__(self, name):
self.name = name
def __call__(self, cls):
self.cls = cls
venusian.attach(self.cls, self.on_scan)
return self.cls
def on_scan(self, scanner, name, ob):
scanner.config.registry["services"][self.name] = self.cls
@service("testservice")
class MyTestService(object):
pass
class TestServiceScanner(unittest.TestCase):
def testScanner(self):
config = Configurator()
config.registry["services"] = {}
config.scan()
self.assertEquals(config.registry["services"]["testservice"], MyTestService)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment