def test_stml(): Configuration.load_kw(componentRoot=here) @template('stml:/stmltest.stml') def tester(): return dict(message="my message", mytitle="my title") res=tester() assert "my message" in res assert "my title" in res
def _test(): import sys logging.basicConfig(level=logging.DEBUG) args = sys.argv[1:] if args: Configuration.load_kw(componentRoot=args[0]) from wsgiref.simple_server import make_server from skunk.web.context import ContextMiddleware make_server("localhost", 7777, ContextMiddleware(DispatchingFileServer())).serve_forever()
def test_componentRoot1(): tmpdir=tempfile.mkdtemp() Cfg.load_kw(componentRoot=tmpdir) try: fname=os.path.join(tmpdir, 'index.stml') fp=open(fname, 'w') fp.write('\n'.join([ "<:default kong Chubby:>", "Hello <:val `kong`:>" "<:component /nougat.comp cache=yes:>" ])) fp.close() fp=open(os.path.join(tmpdir, 'nougat.comp'), 'w') fp.write('SERVES YOU RIGHT') fp.close() res=stringcomp('/index.stml', kong='Kong') assert 'Hello Kong' in res finally: Cfg.reset() shutil.rmtree(tmpdir)
import os import sys from skunk.config import Configuration from skunk.web import ContextMiddleware, RoutingMiddleware, ControllerServer, expose, bootstrap @expose(content_type='text/plain') def helloworld(): return "Hello World" Configuration.load_kw( logConfig={'level' : 'debug', 'filename' : os.path.abspath('debug.log')}, daemonize=True, useThreads=True, pidfile=os.path.abspath('simplerunner.pid'), controllers=dict(hello=__name__), routes=[(('index', '/'), {'controller' : 'hello', 'action' : 'helloworld'})], bindAddress='TCP:0.0.0.0:7777') app=ContextMiddleware(RoutingMiddleware(ControllerServer())) bootstrap(app)
""" This uses the logconfig service with minimal customization. """ import os from skunk.config import Configuration from skunk.web import expose, bootstrap @expose(content_type='text/plain') def index(): return "A truly pleasant experience\n" Configuration.load_kw( logConfig={'level' : 'debug', 'filename' : 'debug.log'}, routes=[ ((':action',),{'controller' : 'main'}), ], controllers={'main' : __name__}) if __name__=='__main__': bootstrap()
log=logging.getLogger(__name__) sessiondir=tempfile.mkdtemp() def cleanup(): log.info('cleaning up temporary session directory') shutil.rmtree(sessiondir) atexit.register(cleanup) Configuration.load_kw( logConfig={'level' : 'debug'}, sessionEnabled=True, sessionStore=DiskSessionStore(sessiondir), sessionCookieSalt='fudgemeaweatherby', services=['skunk.web.sessions'], controllers=dict(main=__name__), routes=[ (('index', '/'), dict(controller='main', action='dopage')) ]) @expose() def dopage(): session=Context.session number=session.get('number', 0) session['number']=number+1 session.save() return '<em>Your number is %d</em>' % number
""" @expose(content_type="text/plain") def robots(self, color): return "Your robots are about to turn " + color @expose() def wsgi(self): def an_app(environ, start_response): start_response("200 OK", [("Content-Type", "text/plain")]) return ["Hello from a WSGI application"] return an_app comproot = os.path.join(os.path.dirname(__file__), "files") Configuration.load_kw( logConfig=dict(level="debug"), componentRoot=comproot, routes=[ (("robots", "/robots/:color"), {"controller": "simple", "action": "robots"}), (("wsgi", "/wsgi"), {"controller": "simple", "action": "wsgi"}), (("hello", "/hello"), {"controller": "simple"}), ], controllers={"simple": SimpleController()}, ) if __name__ == "__main__": bootstrap()
import os from skunk.config import Configuration from skunk.web import bootstrap, expose, template @expose(content_type='text/plain') def helloworld(name): return 'Hello, World, and furthermore, hi there %s' % name @template('/witch.comp') @expose() def dingdong(): return dict(witch='Brenda', status='dead or missing') Configuration.load_kw( logConfig=dict(level='debug'), componentRoot=os.path.join(os.path.dirname(__file__), 'files'), routes=[ (('hi', 'helloworld/:name'), dict(controller='daone', action='helloworld')), (('ding', 'dingdong'), dict(controller='daone', action='dingdong')), ], controllers={'daone' : __name__}, showTracebacks=True) if __name__=='__main__': bootstrap()
def setUp(self): self.componentRoot=tempfile.mkdtemp() Cfg.load_kw(componentRoot=self.componentRoot, useCompileMemoryCache=False)
import os from skunk.config import Configuration, GlobMatcher from skunk.util.pathutil import relpath from skunk.web.auth import CookieFileAuthorizer from skunk.web import ContextMiddleware, DispatchingFileServer, bootstrap Configuration.load_kw(componentRoot=_relpath(__file__, 'files'), services=['skunk.web.auth'], logConfig=dict(level='debug') ) Configuration.addMatcher(GlobMatcher('path', '/protected/*', authorizer=CookieFileAuthorizer( _relpath(__file__, 'auth.conf'), 'nancynonce', '/login.comp'))) app=ContextMiddleware(DispatchingFileServer()) if __name__=='__main__': bootstrap(app)
return dict(message="Edit saved", title=title, body=page.body) routes=[ (('littlewiki', '*title'), dict(controller='littlewiki', action='wikipage')) ] controllers=dict(littlewiki=__name__) comproot=os.path.join(os.path.dirname(__file__), 'files') Configuration.load_kw(routes=routes, componentRoot=comproot, controllers=controllers, logConfig=dict(level='debug'), showTraceback=True) def rollbackConnection(*args, **kwargs): db=pydo.getConnection('littlewiki', False) if db: log.debug("rolling back littlewiki connection") db.rollback() CleanupContextHook.append(rollbackConnection) if __name__=='__main__': bootstrap()
log=logging.getLogger(__name__) def rollbackConnection(*args, **kwargs): db=pydo.getConnection('hitcounter', False) if db: log.debug("rolling back hitcounter connection") db.rollback() def record_hit(Context, environ): Hits.register(environ).commit() CleanupContextHook.append(record_hit) CleanupContextHook.append(rollbackConnection) comproot=os.path.join(os.path.dirname(__file__), 'files') Configuration.load_kw(componentRoot=comproot, logConfig=dict(level='debug'), routes=[ (('hits', '/hits'), {'controller' : 'hits', 'action' : 'show_hits'}), ], controllers={'hits' : HitController()}) if __name__=='__main__': bootstrap()
import os from skunk.config import Configuration from skunk.web import bootstrap, expose class BlowController(object): @expose() def blowup(self): raise ValueError, "hey there!" comproot=os.path.join(os.path.dirname(__file__), 'files') Configuration.load_kw(componentRoot=comproot, logConfig={'level' : 'debug'}, errorPage='/500.html', notFoundPage='/404.html', routes=[ (('blowup', '/blowup'), {'controller' : 'blow', 'action' : 'blowup'}) ], controllers={'blow' : BlowController()}, showTraceback=True) if __name__=='__main__': bootstrap()