diff --git a/cgi/cgi_test.py b/cgi/cgi_test.py
index 66cadc6..7a64330 100755
--- a/cgi/cgi_test.py
+++ b/cgi/cgi_test.py
@@ -23,8 +23,6 @@ print "
ambiente
"
keys = os.environ.keys()
keys.sort()
-
-
for k in keys:
print "%s = %s
\n" % ( k, str( os.environ[k] ) )
diff --git a/controllers/demo/due.py b/controllers/demo/due.py
index 848ebe2..6652729 100644
--- a/controllers/demo/due.py
+++ b/controllers/demo/due.py
@@ -1,20 +1,19 @@
#!/usr/bin/python
# -*- 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
#
-@deco.template( 'template1.tmpl' )
+@wsgit.template( 'template1.tmpl' )
def application( environ, start_response ):
+ from pprint import pformat
- try:
- html = environ['template']( context=dict( v1=1, v2='pippo') )
- except:
- html = "" + traceback.format_exc() + "
"
+ html = environ['template']( context=dict( v1=1, v2='pippo') )
start_response( '200 OK', [('content-type', 'text/html; charset=utf-8')] )
- return [ html ]
+ return [ html, pformat( environ, width=132 ).replace('\n','
\n') ] # TODO: pformat..... ---> trasformarlo in un decoratore
diff --git a/deco.py b/deco.py
deleted file mode 100644
index c9a607b..0000000
--- a/deco.py
+++ /dev/null
@@ -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
\ No newline at end of file
diff --git a/decorators.py b/decorators.py
new file mode 100644
index 0000000..1a1e280
--- /dev/null
+++ b/decorators.py
@@ -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
\ No newline at end of file
diff --git a/dispatch_wsgi.py b/dispatch_wsgi.py
index 744d17c..5a3fb7a 100755
--- a/dispatch_wsgi.py
+++ b/dispatch_wsgi.py
@@ -10,6 +10,12 @@ wsgilib = '/'.join( __file__.split('/')[:-1] )
if wsgilib not in sys.path:
sys.path.insert(0, wsgilib)
+#
+# inizializzazione path per templating
+#
+from decorators import WSGITemplate
+WSGITemplate( basedir='views' )
+
#
# importazione handler
#
@@ -52,6 +58,7 @@ handlers = (
( r'/mysql', simple_mysql ),
)
+
#
# !!! mod_wsgi richiede che la nostra applicazione si chiami 'application'
#
diff --git a/singleton.py b/singleton.py
new file mode 100644
index 0000000..69427c1
--- /dev/null
+++ b/singleton.py
@@ -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
diff --git a/cgi/template1.tmpl b/views/template1.tmpl
similarity index 100%
rename from cgi/template1.tmpl
rename to views/template1.tmpl