From a6c590af3106caa712e1fc0d8f649a53914cab61 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Wed, 9 Mar 2016 19:36:33 +0100 Subject: [PATCH] Generate svg in new window Put error message in place of svg and keep form unchanged --- bserver.py | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/bserver.py b/bserver.py index 5c81ad9..1c591c0 100755 --- a/bserver.py +++ b/bserver.py @@ -52,18 +52,12 @@ class BServer: return """%s%s\n""" % \ (name, name, a.default, a.help) - def args2html(self, args, msg=""): - if msg: - msg = str(msg).replace("--", "") - msg = """\n

%s

\n""" % msg - - result = ["""Foo -%s -
+ def args2html(self, name, args): + result = ["""Boxes - """, name, """ + + -""" % msg ] - #for a in args._actions: - # print(a.__class__.__name__, a.option_strings, repr(a)) +"""] for a in args._actions: if a.dest == "output": continue @@ -73,8 +67,6 @@ class BServer: result.append("""
- """) @@ -100,21 +92,32 @@ Text """) return (s.encode("utf-8") for s in result) + + def errorMessage(self, name, e): + return [ + b"""Error generating""", name.encode(), + b""" + +

An error occurred!

+

""", str(e).encode(), b"""

+ + +""" ] + def serve(self, environ, start_response): status = '200 OK' headers = [('Content-type', 'text/html; charset=utf-8')] - #headers = [('Content-type', 'text/plain; charset=utf-8')] - start_response(status, headers) d = cgi.parse_qs(environ['QUERY_STRING']) + name = environ["PATH_INFO"][1:] + box = self.boxes.get(name, None) + if not box: + start_response(status, headers) + return self.menu() - box = self.boxes.get(environ["PATH_INFO"][1:], None) if environ["REQUEST_METHOD"] == "GET": - if box: - return self.args2html(box.argparser) - else: - return self.menu() - + start_response(status, headers) + return self.args2html(name, box.argparser) elif environ["REQUEST_METHOD"] == "POST": try: length = int(environ.get('CONTENT_LENGTH', '0')) @@ -125,7 +128,10 @@ Text try: box.parseArgs(args) except (ArgumentParserError) as e: - return self.args2html(box.argparser, e) + start_response(status, headers) + return self.errorMessage(name, e) + start_response(status, + [('Content-type', 'image/svg+xml; charset=utf-8')]) fd, box.output = tempfile.mkstemp() box.render() result = open(box.output).readlines() @@ -133,8 +139,6 @@ Text os.close(fd) return (l.encode("utf-8") for l in result) - return [b"???"] - if __name__=="__main__": boxserver = BServer() httpd = make_server('', 8000, boxserver.serve)