durand
August 13th, 2010, 08:45 PM
Hi,
I'm wondering if there's a better solution than what I'm doing right now in the context of web applications. Basically, I thought it would be pretty efficient to just have one python script which sets up the environment, loads the template engine and so on and then just get all the other scripts (which are also separate web pages) to execfile() that one rather than doing the exact same thing in every file.
I've read a bit about the security risks of execfile() but I'm not too bothered about that because its going to be only trusted code running.
An example of what I'm doing:
initiate.py
#!/usr/bin/env python
## Add the right paths into PATH
import os
import sys
sys.path.insert(0, os.path.abspath("libraries"))
sys.path.insert(0, os.path.abspath("../"))
## Enable error catching and nicer tracebacks
import cgitb
cgitb.enable()
## Load up jinja
from jinja2 import Environment, PackageLoader
environment = Environment(loader=PackageLoader('website', 'templates'))
import __builtin__
import cgi
from downloads.pydownloads import downloads as pyd
__builtin__.GET_VARIABLES = cgi.FieldStorage()
def pydo(arguments={}):
variables = pyd.CLI_Arguments()
for argument in arguments:
if type(arguments[argument]) == type(""):
exec('variables.%s = "%s"'%(argument, arguments[argument]))
else:
exec('variables.%s = %s'%(argument, arguments[argument]))
__builtin__.CLI_ARGUMENTS = variables.get()
return pyd.__init__()
home.py
#!/usr/bin/env python
execfile('initiate.py')
template = environment.get_template('home.html')
print "Content-type: text/html\n"
print template.render({'source':open(__file__).read(), 'pydo':pydo}).encode('utf-8')
Is there a better way to do something like this?
Thanks!
I'm wondering if there's a better solution than what I'm doing right now in the context of web applications. Basically, I thought it would be pretty efficient to just have one python script which sets up the environment, loads the template engine and so on and then just get all the other scripts (which are also separate web pages) to execfile() that one rather than doing the exact same thing in every file.
I've read a bit about the security risks of execfile() but I'm not too bothered about that because its going to be only trusted code running.
An example of what I'm doing:
initiate.py
#!/usr/bin/env python
## Add the right paths into PATH
import os
import sys
sys.path.insert(0, os.path.abspath("libraries"))
sys.path.insert(0, os.path.abspath("../"))
## Enable error catching and nicer tracebacks
import cgitb
cgitb.enable()
## Load up jinja
from jinja2 import Environment, PackageLoader
environment = Environment(loader=PackageLoader('website', 'templates'))
import __builtin__
import cgi
from downloads.pydownloads import downloads as pyd
__builtin__.GET_VARIABLES = cgi.FieldStorage()
def pydo(arguments={}):
variables = pyd.CLI_Arguments()
for argument in arguments:
if type(arguments[argument]) == type(""):
exec('variables.%s = "%s"'%(argument, arguments[argument]))
else:
exec('variables.%s = %s'%(argument, arguments[argument]))
__builtin__.CLI_ARGUMENTS = variables.get()
return pyd.__init__()
home.py
#!/usr/bin/env python
execfile('initiate.py')
template = environment.get_template('home.html')
print "Content-type: text/html\n"
print template.render({'source':open(__file__).read(), 'pydo':pydo}).encode('utf-8')
Is there a better way to do something like this?
Thanks!