Generate svg in new window

Put error message in place of svg and keep form unchanged
This commit is contained in:
Florian Festi 2016-03-09 19:36:33 +01:00
parent c4c8e35beb
commit a6c590af31
1 changed files with 28 additions and 24 deletions

View File

@ -52,18 +52,12 @@ class BServer:
return """<tr><td>%s</td><td><input name="%s" type="text" value="%s"></td><td>%s</td></tr>\n""" % \ return """<tr><td>%s</td><td><input name="%s" type="text" value="%s"></td><td>%s</td></tr>\n""" % \
(name, name, a.default, a.help) (name, name, a.default, a.help)
def args2html(self, args, msg=""): def args2html(self, name, args):
if msg: result = ["""<html><head><title>Boxes - """, name, """</title></head>
msg = str(msg).replace("--", "") <body>
msg = """\n<p><span style="color:red">%s</span></p>\n""" % msg <form action="" method="POST" target="_blank">
result = ["""<html><head><title>Foo</title></head>
<body>%s
<form action="" method="POST" target="svg">
<table> <table>
""" % msg ] """]
#for a in args._actions:
# print(a.__class__.__name__, a.option_strings, repr(a))
for a in args._actions: for a in args._actions:
if a.dest == "output": if a.dest == "output":
continue continue
@ -73,8 +67,6 @@ class BServer:
result.append("""</table> result.append("""</table>
<button>Generate</button> <button>Generate</button>
</form> </form>
<iframe width=100% height=100% name="svg">
</iframe>
</body> </body>
</html> </html>
""") """)
@ -100,21 +92,32 @@ Text
""") """)
return (s.encode("utf-8") for s in result) return (s.encode("utf-8") for s in result)
def errorMessage(self, name, e):
return [
b"""<html><head><title>Error generating""", name.encode(),
b"""</title><head>
<body>
<h1>An error occurred!</h1>
<p>""", str(e).encode(), b"""</p>
</body>
</html>
""" ]
def serve(self, environ, start_response): def serve(self, environ, start_response):
status = '200 OK' status = '200 OK'
headers = [('Content-type', 'text/html; charset=utf-8')] 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']) 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 environ["REQUEST_METHOD"] == "GET":
if box: start_response(status, headers)
return self.args2html(box.argparser) return self.args2html(name, box.argparser)
else:
return self.menu()
elif environ["REQUEST_METHOD"] == "POST": elif environ["REQUEST_METHOD"] == "POST":
try: try:
length = int(environ.get('CONTENT_LENGTH', '0')) length = int(environ.get('CONTENT_LENGTH', '0'))
@ -125,7 +128,10 @@ Text
try: try:
box.parseArgs(args) box.parseArgs(args)
except (ArgumentParserError) as e: 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() fd, box.output = tempfile.mkstemp()
box.render() box.render()
result = open(box.output).readlines() result = open(box.output).readlines()
@ -133,8 +139,6 @@ Text
os.close(fd) os.close(fd)
return (l.encode("utf-8") for l in result) return (l.encode("utf-8") for l in result)
return [b"???"]
if __name__=="__main__": if __name__=="__main__":
boxserver = BServer() boxserver = BServer()
httpd = make_server('', 8000, boxserver.serve) httpd = make_server('', 8000, boxserver.serve)