def _load_app(self, app_fs_root): """Given an app's fs root, return an instance of the application. Each app is imported under the app_fs_root so that they don't collide in sys.modules. """ logger.debug("loading app from %s" % app_fs_root) try: fp, pathname, description = imp.find_module('app', [app_fs_root]) app = imp.load_module(app_fs_root, fp, pathname, description) except ImportError: raise ImportError("unable to import app from " + "%s: \n%s" % (app_fs_root, traceback.format_exc())) # Validate the app. # ================= # We use zope interfaces, although your app needn't implement zope # interfaces explicitly. try: verifyObject(IApp, app, tentative=True) except: logger.debug("app in %s is not valid." % app_fs_root) raise try: verifyClass(IApplication, app.Application, tentative=True) except: logger.debug("app.Application in %s is not valid." % app_fs_root) raise return self._instantiate_Application(app_fs_root, app.Application)
def load_app(site_root, app_uri_root): """Given a site's FS root and an app's URI root, load the application. """ msg = ("Found bad app `%s'. Each app must be a path rooted in the " + "website root, and it must have a subdirectory named __.") # Find the app's filesystem root. # =============================== _parts = [p for p in app_uri_root.lstrip('/').split('/') if p] _parts.insert(0, site_root) fs_root = os.sep.join(_parts) if not os.path.isdir(fs_root): raise StandardError(msg % fs_root) # Find the app's magic directory. # =============================== __ = os.path.join(fs_root, '__') if not os.path.isdir(__): if fs_root == site_root: __ = None # special case else: raise StandardError(msg % fs_root) # Load the actual module. # ======================= # We support automatically falling back to DefaultApp for the root # directory. Otherwise, we fail if there is no importable app. We also # validate the app using zope interfaces. Your app needn't implement zope # interfaces explicitly, however. Also note that we import each app under # the app_uri_root so that they don't collide in sys.modules. if __ is None: app = DefaultApp else: try: fp, pathname, description = imp.find_module('app', [__]) app = imp.load_module(app_uri_root, fp, pathname, description) except ImportError: raise ImportError("Unable to import an app from " + "%s: \n%s" % (__, traceback.format_exc())) verifyObject(IApp, app, tentative=True) verifyClass(IApplication, app.Application, tentative=True) # Add some useful API to Application, and return an instance. # =========================================================== app.Application.site_root = site_root app.Application.uri_root = app_uri_root app.Application.fs_root = fs_root app.Application.__ = __ return app.Application()
def testNotImplemented(self): class C(object): pass class I(Interface): pass self.assertRaises(DoesNotImplement, verifyClass, I, C) classImplements(C, I) verifyClass(I, C)
def testMissingAttr(self): class I(Interface): def f(): pass class C(object): implements(I) self.assertRaises(BrokenImplementation, verifyClass, I, C) C.f=lambda self: None verifyClass(I, C)
def testMethodForAttr(self): class IFoo(Interface): foo = Attribute("The foo Attribute") class Foo: implements(IFoo) def foo(self): pass verifyClass(IFoo, Foo)
def testWrongArgs(self): class I(Interface): def f(a): pass class C(object): def f(self, b): pass implements(I) # We no longer require names to match. #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) C.f=lambda self, a: None verifyClass(I, C) C.f=lambda self, **kw: None self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) C.f=lambda self, a, *args: None verifyClass(I, C) C.f=lambda self, a, *args, **kw: None verifyClass(I, C) C.f=lambda self, *args: None verifyClass(I, C)
def load_responder(self, root): """Given a responder's filesystem root, return an instance. Each responder is imported under the root so that they don't collide in sys.modules. """ logger.debug("loading responder from %s" % root) try: result = imp.find_module('responder', [root]) responder = imp.load_module(root, *result) except ImportError: raise ImportError("unable to import responder from " + "%s: \n%s" % (root, traceback.format_exc())) # Validate the responder. # ======================= # We use zope interfaces, although your responder needn't implement # them explicitly. Allow both module and class responders. try: # responder.Responder.respond() verifyObject(Iresponder, responder, tentative=True) try: responder = responder.Responder verifyClass(IBaseResponder, responder, tentative=True) except Invalid: logger.debug("responder.Responder in %s " % root + "is not valid.") raise except Invalid: # responder.respond() try: verifyObject(IBaseResponder, responder, tentative=True) except: logger.debug("responder in %s is not valid." % root) raise # Alter sys.path, add API and return. # =================================== pkg = self.sys_path(root) self.add_api(responder, root, pkg) if inspect.isclass(responder): responder = responder() return responder
def testNoKW(self): class I(Interface): def f(a, **args): pass class C(object): def f(self, a): pass implements(I) self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) C.f=lambda self, a, **foo: None verifyClass(I, C)
def test_interfaces(self): verifyObject(ITimeDelta, timedelta(minutes=20)) verifyObject(IDate, date(2000, 1, 2)) verifyObject(IDateTime, datetime(2000, 1, 2, 10, 20)) verifyObject(ITime, time(20, 30, 15, 1234)) verifyObject(ITZInfo, tzinfo()) verifyClass(ITimeDeltaClass, timedelta) verifyClass(IDateClass, date) verifyClass(IDateTimeClass, datetime) verifyClass(ITimeClass, time)
def testExtraArgs(self): class I(Interface): def f(a): pass class C(object): def f(self, a, b): pass implements(I) self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) C.f=lambda self, a: None verifyClass(I, C) C.f=lambda self, a, b=None: None verifyClass(I, C)
def _load_module(self, __, name): """Given a magic directory, return a module. We support automatically falling back to DefaultApp for the root directory. Otherwise, we fail if there is no importable app. We also validate the app using zope interfaces. Your app needn't implement zope interfaces explicitly, however. """ if __ is None: app = DefaultApp else: try: fp, pathname, description = imp.find_module('app', [__]) app = imp.load_module(name, fp, pathname, description) except ImportError: raise ConfigError("Unable to import an app from " + "%s: \n%s" % (__, traceback.format_exc())) verifyObject(IApp, app, tentative=True) verifyClass(ITransaction, app.Transaction, tentative=True) return app
def testVerifyImplementation(self): from httpy._zope.interface.verify import verifyClass self.assert_(verifyClass(FooInterface, Foo)) self.assert_(Interface.providedBy(I1))