"""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."""
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.
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.
font-style: italic;
}
+div.warn {
+ border: thin solid;
+ padding: 0ex 1em 2ex 1em;
+ margin: 2ex 1em;
+ background: red;
+}
+
/*----- Form layout -------------------------------------------------------*/
/* Common form validation styling. */
<p>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 = "<h1>Chopwood: warning test</h1>\n"
+ "<p>There ought to be a warning below.\n")
+
###--------------------------------------------------------------------------
### Static content.
from __future__ import with_statement
import contextlib as CTX
+import os as OS
from cStringIO import StringIO
import sys as SYS
def __init__(me):
"""Trivial constructor."""
- pass
+ me.warnings = []
def writeln(me, msg):
"""Write MSG, as a complete line."""
"""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
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()
~={payload}@?~
+~={warnings}{~
+<div class=warn>
+<h2>Warnings</h2>
+<ul>
+~@{<li>~:H~%~}~
+</ul>
+</div>~2%~}~
+
<div class=credits>
<a href="~={static}H/about.html">Chopwood</a>, version ~={version}H:
copyright © 2012 Mark Wooding