Created
April 19, 2013 13:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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