| @@ -23,8 +23,6 @@ print "<h3>ambiente</h3>" | |||||
| keys = os.environ.keys() | keys = os.environ.keys() | ||||
| keys.sort() | keys.sort() | ||||
| for k in keys: | for k in keys: | ||||
| print "%s = %s<br>\n" % ( k, str( os.environ[k] ) ) | print "%s = %s<br>\n" % ( k, str( os.environ[k] ) ) | ||||
| @@ -1,20 +1,19 @@ | |||||
| #!/usr/bin/python | #!/usr/bin/python | ||||
| # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- | # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- | ||||
| import deco # decoratori | |||||
| import traceback | |||||
| from decorators import WSGITemplate # decoratore ( singleton ) | |||||
| wsgit = WSGITemplate() | |||||
| # | # | ||||
| # esempio minimo di controller WSGI | # esempio minimo di controller WSGI | ||||
| # | # | ||||
| @deco.template( 'template1.tmpl' ) | |||||
| @wsgit.template( 'template1.tmpl' ) | |||||
| def application( environ, start_response ): | def application( environ, start_response ): | ||||
| from pprint import pformat | |||||
| try: | |||||
| html = environ['template']( context=dict( v1=1, v2='pippo') ) | |||||
| except: | |||||
| html = "<pre>" + traceback.format_exc() + "</pre>" | |||||
| html = environ['template']( context=dict( v1=1, v2='pippo') ) | |||||
| start_response( '200 OK', [('content-type', 'text/html; charset=utf-8')] ) | start_response( '200 OK', [('content-type', 'text/html; charset=utf-8')] ) | ||||
| return [ html ] | |||||
| return [ html, pformat( environ, width=132 ).replace('\n','<br>\n') ] # TODO: pformat..... ---> trasformarlo in un decoratore | |||||
| @@ -1,13 +0,0 @@ | |||||
| #!/usr/bin/python | |||||
| # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- | |||||
| import template as tmpl | |||||
| from functools import partial | |||||
| def template( filename=None ): | |||||
| def real_decorator( wsgi_application ): | |||||
| def wrapper( environ, start_response ): | |||||
| environ[ 'template' ] = partial( tmpl.render, filename=filename ) | |||||
| return wsgi_application( environ, start_response ) | |||||
| return wrapper | |||||
| return real_decorator | |||||
| @@ -0,0 +1,24 @@ | |||||
| #!/usr/bin/python | |||||
| # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- | |||||
| from singleton import Singleton | |||||
| from template import render | |||||
| from functools import partial | |||||
| class WSGITemplate( object ): | |||||
| __metaclass__ = Singleton | |||||
| def __init__( self, basedir=None ): | |||||
| import os | |||||
| self.__basedir = os.path.normpath( os.path.join( os.path.split(__file__)[0], basedir ) ) + '/' | |||||
| def template(self, filename=None ): | |||||
| def real_decorator( wsgi_application ): | |||||
| def wrapper( environ, start_response ): | |||||
| environ[ 'template' ] = partial( render, filename= self.__basedir + filename ) | |||||
| environ[ 'basedir' ] = self.__basedir | |||||
| return wsgi_application( environ, start_response ) | |||||
| return wrapper | |||||
| return real_decorator | |||||
| @@ -10,6 +10,12 @@ wsgilib = '/'.join( __file__.split('/')[:-1] ) | |||||
| if wsgilib not in sys.path: | if wsgilib not in sys.path: | ||||
| sys.path.insert(0, wsgilib) | sys.path.insert(0, wsgilib) | ||||
| # | |||||
| # inizializzazione path per templating | |||||
| # | |||||
| from decorators import WSGITemplate | |||||
| WSGITemplate( basedir='views' ) | |||||
| # | # | ||||
| # importazione handler | # importazione handler | ||||
| # | # | ||||
| @@ -52,6 +58,7 @@ handlers = ( | |||||
| ( r'/mysql', simple_mysql ), | ( r'/mysql', simple_mysql ), | ||||
| ) | ) | ||||
| # | # | ||||
| # !!! mod_wsgi richiede che la nostra applicazione si chiami 'application' | # !!! mod_wsgi richiede che la nostra applicazione si chiami 'application' | ||||
| # | # | ||||
| @@ -0,0 +1,12 @@ | |||||
| #!/usr/bin/python | |||||
| # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*- | |||||
| class Singleton(type): | |||||
| def __init__(cls, name, bases, dict): | |||||
| super(Singleton, cls).__init__(name, bases, dict) | |||||
| cls.instance = None | |||||
| def __call__(cls,*args,**kw): | |||||
| if cls.instance is None: | |||||
| cls.instance = super(Singleton, cls).__call__(*args, **kw) | |||||
| return cls.instance | |||||