Bläddra i källkod

ripulito decoratore specifica template

db_access
Andrea Papotti 13 år sedan
förälder
incheckning
2af1a20854
7 ändrade filer med 50 tillägg och 23 borttagningar
  1. +0
    -2
      cgi/cgi_test.py
  2. +7
    -8
      controllers/demo/due.py
  3. +0
    -13
      deco.py
  4. +24
    -0
      decorators.py
  5. +7
    -0
      dispatch_wsgi.py
  6. +12
    -0
      singleton.py
  7. +0
    -0
      views/template1.tmpl

+ 0
- 2
cgi/cgi_test.py Visa fil

@@ -23,8 +23,6 @@ print "<h3>ambiente</h3>"
keys = os.environ.keys()
keys.sort()



for k in keys:
print "%s = %s<br>\n" % ( k, str( os.environ[k] ) )



+ 7
- 8
controllers/demo/due.py Visa fil

@@ -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 = "<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')] )

return [ html ]
return [ html, pformat( environ, width=132 ).replace('\n','<br>\n') ] # TODO: pformat..... ---> trasformarlo in un decoratore

+ 0
- 13
deco.py Visa fil

@@ -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

+ 24
- 0
decorators.py Visa fil

@@ -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

+ 7
- 0
dispatch_wsgi.py Visa fil

@@ -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'
#


+ 12
- 0
singleton.py Visa fil

@@ -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

cgi/template1.tmpl → views/template1.tmpl Visa fil


Laddar…
Avbryt
Spara