# # -*- coding: utf-8 -*- """ The executable file -- './uvw/xyz.cgi' or whatever -- should contain lines like these:: fundamentalsPath = '../fundamentals.py' execfile( fundamentalsPath ) and then do ``g.callCgi(callable)`` with ``callCgi()`` being defined here in the ``fundamentals`` module -- basically, a safety-wrapper that catches a lot of exceptions and displays them -- and ``callable`` being any suitable action defined at some reachable point (i.e. a functioned defined in that file, or defined somewhere that is reachable using a ``g.whatever.xy`` notation). """ ############################################################################################################ # # IMPORTS AND CONSTANTS # #=========================================================================================================== import traceback # import cgi # import imp # from sets import Set as _S import inspect as _i import os as _os import os.path as _p import sys as sys from cgi import escape as _escape #import cgitb as _cgitb; _cgitb.enable() #----------------------------------------------------------------------------------------------------------- escape = lambda x: _escape( '%s' % ( x, ) ) STANDARD_HTTP_HEADER_HTML = 'Content-Type: text/html; charset=UTF-8\n' STANDARD_HTTP_HEADER_PLAIN = 'Content-Type: text/plain; charset=UTF-8\n' #----------------------------------------------------------------------------------------------------------- REVEAL = False #----------------------------------------------------------------------------------------------------------- # REVEAL = True adjustpathLocator = _p.join( _p.abspath( _p.dirname( _i.getfile( lambda:None ) ) ), 'adjustpath.py' ) ############################################################################################################ # # BOOT ENVIRONMENT # #=========================================================================================================== def _boot(): """Prepend additional to ``sys.path``, install global namebase as ``g``, and install global output encoding for all ``print`` statements.""" #------------------------------------------------------------------------------------------------------- try: from biocase.configuration import Cfg except ImportError: locals = {} execfile( adjustpathLocator, globals(), locals ) from biocase.configuration import Cfg #------------------------------------------------------------------------------------------------------- # Install global namebase as ``g``: import biocase.simpleGlobalNamebase # #------------------------------------------------------------------------------------------------------- # # Install global output encoding for all ``print`` statements: # g.IO.installStdOutEncoder() g.callCgi = callCgi #g.cfg = Cfg.getCfg() g.here = g.configtool.utilities.filesystemIdioms.here # g.cfg = g.configuration.getCfg( _Cfg, 'configtool' ) g.cfg = Cfg( 'configtool' ) for postprocessingMethod in g.cfg._postprocessingMethods: postprocessingMethod() ############################################################################################################ # # SAFETY WRAPPER FOR CGI SCRIPTS # #=========================================================================================================== def callCgi( callable ): # print STANDARD_HTTP_HEADER_HTML # import os # print os.getcwd() # import sys; sys.exit() #------------------------------------------------------------------------------------------------------- try: #--------------------------------------------------------------------------------------------------- try: #----------------------------------------------------------------------------------------------- try: if REVEAL: print STANDARD_HTTP_HEADER_HTML print '
' callable() if REVEAL: print '' #----------------------------------------------------------------------------------------------- except: e = sys.exc_info()[ 0 ] if isinstance( e, basestring ): info = ''.join( traceback.format_exception( *sys.exc_info() ) ) msg = '%s\n%s' % ( e, info, ) raise Exception( msg ) raise #--------------------------------------------------------------------------------------------------- finally: #finalize() g.configtool.utilities.finalizer.finalize() pass #------------------------------------------------------------------------------------------------------- except Exception, e: # print '(%s) %s' % ( e.__class__, e, ) # #print # traceback.print_exception( *sys.exc_info() ) #--------------------------------------------------------------------------------------------------- try: # Fancier error report: print STANDARD_HTTP_HEADER_HTML #g.configtool.utilities.cgiIdioms.print_exception( e ) print g.configtool.utilities.cgiIdioms._getTracebackReport( e ) #--------------------------------------------------------------------------------------------------- except: # Fallback error report in case *NOTHING* works: info = ''.join( traceback.format_exception( *sys.exc_info() ) ) msg = '%s\n%s' % ( e, info, ) print STANDARD_HTTP_HEADER_PLAIN print 'Nothing works. %s' % ( msg, ) #raise ###test ############################################################################################################ # # GET CONFIGURATION AND BOOT # #=========================================================================================================== _boot() # for word in _Cfg.cfgVocabulary: # print word, getattr( cfg, word ) # print dir( g ) # print g.cinfigtool.utilities.pylon.acquire # # print '
%s | %s |