# HG changeset patch # User Tero Marttila # Date 1358786409 -7200 # Node ID 2c66ab45d91e01a090d205dcd5ee6d163ef76b0f # Parent 96d551b90734adc84864271a3690b445f094c6e3 pvl.web.rrd: support --web-layout to use external template diff -r 96d551b90734 -r 2c66ab45d91e bin/pvl.verkko-rrd --- a/bin/pvl.verkko-rrd Mon Jan 21 18:39:53 2013 +0200 +++ b/bin/pvl.verkko-rrd Mon Jan 21 18:40:09 2013 +0200 @@ -14,6 +14,8 @@ import optparse import logging; log = logging.getLogger('main') +import codecs # --web-layout + def parse_argv (argv, doc = __doc__) : """ Parse command-line argv, returning (options, args). @@ -34,6 +36,9 @@ parser.add_option_group(pvl.args.parser(parser)) parser.add_option_group(pvl.rrd.args.parser(parser)) + parser.add_option('--web-layout', metavar='TEMPLATE', + help="Use template from given file for layout") + # parse options, args = parser.parse_args(args) @@ -52,9 +57,16 @@ # rrd rrd = pvl.rrd.args.apply(options) + + if options.web_layout : + layout = codecs.open(options.web_layout, 'r', 'utf-8').read() + else : + layout = None # app - application = pvl.verkko.rrd.Application(rrd) + application = pvl.verkko.rrd.Application(rrd, + layout = layout + ) # wsgi wrapper werkzeug.serving.run_simple('0.0.0.0', 8080, application, diff -r 96d551b90734 -r 2c66ab45d91e pvl/web/application.py --- a/pvl/web/application.py Mon Jan 21 18:39:53 2013 +0200 +++ b/pvl/web/application.py Mon Jan 21 18:40:09 2013 +0200 @@ -10,14 +10,17 @@ class Application (object) : """ - Main state. + Main wsgi entry point state. """ URLS = None - def __init__ (self, urls=None) : + # TODO: charset + + def __init__ (self, urls=None, layout=None) : """ urls - werkzeug.routing.Map -> Handler + layout - template with {TITLE} and {HEAD} and {BODY} """ if not urls : @@ -25,7 +28,8 @@ if not urls : raise ValueError("No URLS/urls=... given") - + + self.layout = layout self.urls = urls def respond (self, request) : @@ -121,22 +125,33 @@ """ title = self.title() - - return html.html( - html.head( - html.title(title), + head = html( ( html.link(rel='Stylesheet', type="text/css", href=src) for src in self.CSS ), ( html.script(src=src, type='text/javascript', _selfclosing=False) for src in self.JS ), - ), - html.body( - html.h1(title), - self.render() + ) + body = html(self.render()) + + if self.app.layout : + return self.app.layout.format( + TITLE = unicode(title), + HEAD = unicode(head), + BODY = unicode(body), ) - ) + else : + return html.document(html.html( + html.head( + html.title(title), + head, + ), + html.body( + html.h1(title), + body, + ) + )) def process (self, **params) : """ @@ -159,8 +174,7 @@ return response # render as html per default - render = self.render_html() - text = unicode(html.document(render)) + text = unicode(self.render_html()) return Response(text, mimetype='text/html')