def _render_pt(self, request, template): """Render a page template. We pass in request so that we can put it in state.py's namespace. """ # Start building context and hand off to local hook. # ================================================== context = simpleTALES.Context() context.addGlobal("frame", self.frame()) state_path = os.path.join(self.__, 'state.py') if os.path.isfile(state_path): execfile(state_path, { 'request':request , 'context':context }) # Expand and return the template. # =============================== out = simpleTALUtils.FastStringOutput() template.expand( context , out , docType = self.DOC_TYPE # this doesn't appear to work with PyXML , suppressXMLDeclaration = True ) return out.getvalue()
def getpage(self, fields): """fields - dict of HTTP pears. Returns formatted page string, according to fields['job']""" job = fields.get('job') or 'view' # Creat the context that is used by the template context = simpleTALES.Context(allowPythonPath=1) context.addGlobal( "cfg", { 'version': Config.get('version'), 'link': Config.get('link'), 'logo': Config.get('logo'), 'css': Config.get('css') }) # Add objects into the template context context = getattr(self, "_context_" + job)(context, fields) # Open the template file templateFile = open( os.path.join(Config.get('zpt_path'), "".join([job, ".zpt"])), 'r') # Compile a template template = simpleTAL.compileHTMLTemplate(templateFile, self.encoding) # Close the template file templateFile.close() # Create fake file that lets print file as a string fastFile = simpleTALUtils.FastStringOutput() # Expand the template as HTML using this context template.expand(context, fastFile, self.encoding) return fastFile.getvalue() #yo people! it's ZPT content"
def _gettemplate(self, request): """Process a request for a page template. This method can raise anything, since we have a site-specific hook. """ # Build the context. # ================== context = simpleTALES.Context() context.addGlobal("frame", self._getframe()) _path = os.path.join(self.__, 'context.py') if os.path.isfile(_path): execfile( _path, { 'request': request, 'context': context, 'Redirect': Redirect, 'RequestError': RequestError }) # Expand the template. # ==================== out = simpleTALUtils.FastStringOutput() template = self.templates.getXMLTemplate(request.path) template.expand( context, out, docType='' # It appears that this argument is # ignored when PyXML is installed. , suppressXMLDeclaration=True) # Set headers and return the content. # =================================== content = out.getvalue() request['Content-Length'] = long(len(content)) request['Content-Type'] = 'text/html' return content
last_tuesday_date) = Utils.get_set_days(cursor, sniffer, 1).next() cursor.execute( "SELECT count(id) FROM DNS_packets WHERE query AND (file=%(sunday)s OR file=%(tuesday)s);", { 'sunday': last_sunday_id, 'tuesday': last_tuesday_id }) total_queries = int(cursor.fetchone()[0]) cursor.execute( "SELECT count(id) FROM DNS_packets WHERE query AND family(src_address)=6 AND (file=%(sunday)s OR file=%(tuesday)s);", { 'sunday': last_sunday_id, 'tuesday': last_tuesday_id }) context.addGlobal( "respsize", "%2.1f" % ((float(cursor.fetchone()[0]) * 100) / total_queries)) now = time.localtime(time.time()) now_str = time.strftime("%d %B %Y à %H:%M", now) context.addGlobal("now", unicode(now_str, encoding)) rendered = simpleTALUtils.FastStringOutput() template.expand(context, rendered, outputEncoding=encoding) output = open("respsize.html", 'w') output.write(rendered.getvalue()) output.write("\n") output.close() conn.close()
def _geterror(self, request, error): """Process a RequestError. Since this method is complex and therefore error-prone, we wrap it in _handle_request_safely. """ # Do error-specific processing. # ============================= if error.code in (301, 302): # Moved Permanently, Found request['Location'] = error.new_location error.message = 'Resource now resides at ' +\ '<a href="%s">%s</a>.' % ( error.new_location , error.new_location ) elif error.code == 304: # Not Modified pass request.reply_code = error.code # Generate an error page if we need to. # ===================================== if (request.command == 'HEAD') or (error.code == 304): content = '' else: template = None template_path = os.path.join(self.__, 'error.pt') if os.path.exists(template_path): if not os.stat(template_path)[stat.ST_SIZE]: print "Empty error.pt: %s" % template_path else: template = self.templates.getXMLTemplate(template_path) if template is None: errmsg = request.DEFAULT_ERROR_MESSAGE content = errmsg % (error.code, error.msg, error.message) else: context = simpleTALES.Context() context.addGlobal("request", request) context.addGlobal("error", error) out = simpleTALUtils.FastStringOutput() template.expand( context , out , docType = '' # It appears that this argument is # ignored when PyXML is installed. , suppressXMLDeclaration = True ) content = out.getvalue() request['Content-Length'] = long(len(content)) request['Content-Type'] = 'text/html' return content