Logo Search packages:      
Sourcecode: karrigell version File versions  Download package

def k_script::BaseScript::render (   self,
  nameSpace = {},
  args 
) [inherited]

Run the script and return an instance of Output

Definition at line 79 of file k_script.py.

00079                               {},**args):
        """Run the script and return an instance of Output"""

        # build a list of parents and detect if there is a recursion error
        # if the script is included in itself
        s=self
        tree=[s]
        while s.parent is not None and hasattr(s.parent,"name"):
            tree.insert(0,s.parent)
            if s.parent.name==self.name:
                raise RecursionError,tree
            s=s.parent
            
        ns = nameSpace
        # put form fields in name space with a leading _
        if nameSpace.has_key("QUERY"):
            for item in nameSpace["QUERY"].keys():
                nameSpace["_"+item]=nameSpace["QUERY"][item]
        for n,v in nameSpace.items():
            ns[n] = v
        for n,v in args.items():
            ns[n] = v
        # set exceptions, only for use as a templating system
        ns['SCRIPT_END']=SCRIPT_END
        ns['SCRIPT_ERROR']=SCRIPT_ERROR
        HTTP_REDIRECTION=ns.setdefault('HTTP_REDIRECTION',Exception)
        HTTP_ERROR=ns.setdefault('HTTP_ERROR',Exception)
        AUTH_ABORT=ns.setdefault('AUTH_ABORT',Exception)

        # adds a variable THIS in namespace = the Script instance itself
        ns['THIS']=self

        # translation
        self.loadTranslations(nameSpace, self.dirname) 

        # if script in protected zone, include AuthentScript
        pythonCode=self.pythonCode()
        (protectedDir,depth) = k_utils.pathInDirs(self.name,k_config.protectedDirs)
        if protectedDir is not None:
            authentScriptUrl="../"*depth+"AuthentScript.py"
            if self.name.lower() != \
                os.path.join(protectedDir,'AuthentScript.py').lower():
                self.code='Include("%s");'%authentScriptUrl + self.pythonCode()

        saveStdout=sys.stdout
        sys.stdout=cStringIO.StringIO()
        saveNs=copy.copy(ns)
        status=1    # becomes 0 if error in script
        
        try:
            for globalScript in k_config.globalScripts:
                ns[globalScript]=sys.modules[globalScript]
            self.run_script(ns)
            # restore THIS if script was included
            ns['THIS']=self.parent
        except SCRIPT_END:
            # if SCRIPT_END, return the output produced 
            # before the exception occured
            pass
        except SCRIPT_ERROR, msg:
            sys.stdout = cStringIO.StringIO(str(msg))
        except (HTTP_REDIRECTION,HTTP_ERROR,AUTH_ABORT):
            # these exceptions must be relayed to Karrigell
            raise
        except:
            # an exception was raised when executing the script
            status=0
            # create an instance of the Error class in module debugger.k_debugger
            # for traceback printing and debugging
            key=k_utils.generateRandom(8)
            error=debugger.k_debugger.Error(tree,
                traceback.extract_tb(sys.exc_info()[2]),
                sys.exc_info(),
                copy.copy(ns),
                saveNs,
                key)
            traceback.print_exc(file=error.getRawTraceback())
            sys.stdout=cStringIO.StringIO()
            sys.stdout.write(error.HTML())
        output=sys.stdout.getvalue()
        sys.stdout=saveStdout
        return Output(self,status,output)


Generated by  Doxygen 1.6.0   Back to index