Changeset 2485 for package_branches/invirt-web/cherrypy/code/view.py
- Timestamp:
- Sep 28, 2009, 3:04:33 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
package_branches/invirt-web/cherrypy/code/view.py
r2482 r2485 1 import os 1 import os, sys 2 2 3 3 import cherrypy … … 6 6 import simplejson 7 7 import datetime, decimal 8 from StringIO import StringIO 8 9 from invirt.config import structs as config 9 10 from webcommon import State … … 28 29 def __init__(self): 29 30 self.lookups = {} 30 31 def __call__(self, filename, directories, module_directory=None, 32 collection_size=-1, content_type='text/html; charset=utf-8', 33 imports=[]): 31 32 def get_lookup(self, directories, module_directory=None, 33 collection_size=-1, imports=[], **kwargs): 34 34 # Find the appropriate template lookup. 35 35 key = (tuple(directories), module_directory) … … 46 46 ) 47 47 self.lookups[key] = lookup 48 cherrypy.request.lookup = lookup 48 return lookup 49 50 def __call__(self, filename, directories, module_directory=None, 51 collection_size=-1, content_type='text/html; charset=utf-8', 52 imports=[]): 53 cherrypy.request.lookup = lookup = self.get_lookup(directories, module_directory, 54 collection_size, imports) 49 55 50 56 # Replace the current handler. … … 54 60 main = MakoLoader() 55 61 cherrypy.tools.mako = cherrypy.Tool('on_start_resource', main) 62 63 def revertStandardError(): 64 """Move stderr to stdout, and return the contents of the old stderr.""" 65 errio = sys.stderr 66 if not isinstance(errio, StringIO): 67 return '' 68 sys.stderr = sys.stdout 69 errio.seek(0) 70 return errio.read() 71 72 def catchStderr(): 73 old_handler = cherrypy.request.handler 74 def wrapper(*args, **kwargs): 75 sys.stderr = StringIO() 76 ret = old_handler(*args, **kwargs) 77 e = revertStandardError() 78 if e: 79 if isinstance(ret, dict): 80 ret["error_text"] = e 81 return ret 82 if old_handler: 83 cherrypy.request.handler = wrapper 84 85 cherrypy.tools.catch_stderr = cherrypy.Tool('before_handler', catchStderr) 56 86 57 87 class JSONEncoder(simplejson.JSONEncoder):
Note: See TracChangeset
for help on using the changeset viewer.