From 5b7c6334a8922d1fe49fb713d4432cbe667133f2 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 23 May 2014 18:20:46 +0100 Subject: [PATCH] Introduce a `warn' output operation. Mostly warnings are just written to standard error. The HTML output driver captures warnings and displays them in an obvious box. --- cgi.py | 13 ++++++++++++- chpwd.css | 7 +++++++ cmd-cgi.py | 11 +++++++++++ output.py | 10 +++++++++- wrapper.fhtml | 8 ++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/cgi.py b/cgi.py index 69b9038..227d33b 100644 --- a/cgi.py +++ b/cgi.py @@ -105,6 +105,7 @@ class HTTPOutput (O.FileOutput): """Constructor: initialize `headerp' flag.""" super(HTTPOutput, me).__init__(*args, **kw) me.headerp = False + me.warnings = [] def write(me, msg): """Output protocol: print a header if we've not written one already.""" @@ -126,6 +127,15 @@ class HTTPOutput (O.FileOutput): if METHOD == 'HEAD': HEADER_DONE() + def warn(me, msg): + """ + Report a warning message. + + The warning is stashed in a list where it can be retrieved using + `warnings'. + """ + me.warnings.append(msg) + def cookie(name, value, **kw): """ Return a HTTP `Set-Cookie' header. @@ -272,7 +282,8 @@ def page(template, header = {}, title = 'Chopwood', **kw): header = dict(header, content_type = 'text/html') OUT.header(**header) format_tmpl(TMPL['wrapper.fhtml'], - title = title, payload = TMPL[template], **kw) + title = title, warnings = OUT.warnings, + payload = TMPL[template], **kw) ###-------------------------------------------------------------------------- ### Error reporting. diff --git a/chpwd.css b/chpwd.css index 64b98be..4b2a062 100644 --- a/chpwd.css +++ b/chpwd.css @@ -54,6 +54,13 @@ div.credits { font-style: italic; } +div.warn { + border: thin solid; + padding: 0ex 1em 2ex 1em; + margin: 2ex 1em; + background: red; +} + /*----- Form layout -------------------------------------------------------*/ /* Common form validation styling. */ diff --git a/cmd-cgi.py b/cmd-cgi.py index 104dd6a..f6bbe94 100644 --- a/cmd-cgi.py +++ b/cmd-cgi.py @@ -112,6 +112,17 @@ def cmd_fail_cgi(partial = False):

This is some normal output which will be rudely interrupted.""") raise Exception, 'You asked for this.' +@CGI.subcommand( + 'warn', ['cgi-noauth'], + 'Raise an exception, to test the error reporting machinery.') +def cmd_warn_cgi(): + OUT.header(content_type = 'text/html') + OUT.warn("Here's a very important warning.") + CGI.format_tmpl(CGI.TMPL['wrapper.fhtml'], + title = "Warning test", warnings = OUT.warnings, + payload = "

Chopwood: warning test

\n" + "

There ought to be a warning below.\n") + ###-------------------------------------------------------------------------- ### Static content. diff --git a/output.py b/output.py index b849985..8dd967f 100644 --- a/output.py +++ b/output.py @@ -26,6 +26,7 @@ from __future__ import with_statement import contextlib as CTX +import os as OS from cStringIO import StringIO import sys as SYS @@ -77,7 +78,7 @@ class BasicOutputDriver (object): def __init__(me): """Trivial constructor.""" - pass + me.warnings = [] def writeln(me, msg): """Write MSG, as a complete line.""" @@ -87,6 +88,10 @@ class BasicOutputDriver (object): """Write MSG to the output, with any necessary decoration.""" me._write(str(msg)) + def warn(me, msg): + """Write MSG as a warning message.""" + SYS.stderr.write('%s: %s\n' % (OS.path.basename(SYS.argv[0]), msg)) + def close(me): """Wrap up when everything that needs saying has been said.""" pass @@ -194,10 +199,13 @@ class DelegatingOutput (BasicOutputDriver): def writeln(me, msg): me._fluid.target.writeln(msg) def close(me): me._fluid.target.close() def header(me, **kw): me._fluid.target.header(**kw) + def warn(me, msg): me._fluid.target.warn(msg) ## Delegating properties. @property def headerp(me): return me._fluid.target.headerp + @property + def warnings(me): return me._fluid.target.warnings ## The selected output driver. Set this with `output_to'. OUT = DelegatingOutput() diff --git a/wrapper.fhtml b/wrapper.fhtml index 18cc77f..854fa34 100644 --- a/wrapper.fhtml +++ b/wrapper.fhtml @@ -39,6 +39,14 @@ ~={payload}@?~ +~={warnings}{~ +

+

Warnings

+ +
~2%~}~ +
Chopwood, version ~={version}H: copyright © 2012 Mark Wooding -- 2.11.0