Add traylayout to the web interface
This commit is contained in:
parent
91c60f8e1a
commit
de04981562
1
TODO.txt
1
TODO.txt
|
@ -7,4 +7,3 @@
|
||||||
* offer a collection of different settings
|
* offer a collection of different settings
|
||||||
* Make bolts configurable (e.g. box2.py)
|
* Make bolts configurable (e.g. box2.py)
|
||||||
* setup.py
|
* setup.py
|
||||||
* Fix error message on parsing error in web interface
|
|
||||||
|
|
37
bserver.py
37
bserver.py
|
@ -6,13 +6,14 @@ import argparse
|
||||||
import cgi
|
import cgi
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
|
import urllib
|
||||||
|
|
||||||
from wsgiref.util import setup_testing_defaults
|
from wsgiref.util import setup_testing_defaults
|
||||||
from wsgiref.simple_server import make_server
|
from wsgiref.simple_server import make_server
|
||||||
import wsgiref.util
|
import wsgiref.util
|
||||||
|
|
||||||
import box, box2, box3, drillbox, flexbox, flexbox2, flexbox3, flextest, folder
|
import box, box2, box3, drillbox, flexbox, flexbox2, flexbox3, flextest, folder
|
||||||
import magazinefile, trayinsert, typetray, silverwarebox
|
import magazinefile, trayinsert, traylayout, typetray, silverwarebox
|
||||||
|
|
||||||
|
|
||||||
class ArgumentParserError(Exception): pass
|
class ArgumentParserError(Exception): pass
|
||||||
|
@ -39,8 +40,9 @@ class BServer:
|
||||||
"TrayInsert" : trayinsert.TrayInsert(),
|
"TrayInsert" : trayinsert.TrayInsert(),
|
||||||
"TypeTray" : typetray.TypeTray(),
|
"TypeTray" : typetray.TypeTray(),
|
||||||
"SilverwareBox" : silverwarebox.Silverware(),
|
"SilverwareBox" : silverwarebox.Silverware(),
|
||||||
|
"TrayLayout" : traylayout.LayoutGenerator(),
|
||||||
|
"TrayLayout2" : traylayout.Layout(webargs=True),
|
||||||
}
|
}
|
||||||
|
|
||||||
def arg2html(self, a):
|
def arg2html(self, a):
|
||||||
name = a.option_strings[0].replace("-", "")
|
name = a.option_strings[0].replace("-", "")
|
||||||
if isinstance(a, argparse._HelpAction):
|
if isinstance(a, argparse._HelpAction):
|
||||||
|
@ -48,20 +50,24 @@ class BServer:
|
||||||
if isinstance(a, argparse._StoreTrueAction):
|
if isinstance(a, argparse._StoreTrueAction):
|
||||||
return """<tr><td>%s</td><td><input name="%s" type="checkbox" value="%s"></td><td>%s</td></tr>\n""" % \
|
return """<tr><td>%s</td><td><input name="%s" type="checkbox" value="%s"></td><td>%s</td></tr>\n""" % \
|
||||||
(name, name, a.default, a.help)
|
(name, name, a.default, a.help)
|
||||||
|
if a.dest == "layout":
|
||||||
|
val = a.default.split("\n")
|
||||||
|
return """<tr><td>%s</td><td><textarea name="%s" cols="%s" rows="%s">%s</textarea></td><td>%s</td></tr>\n""" % \
|
||||||
|
(name, name, max((len(l) for l in val))+10, len(val)+1,
|
||||||
|
a.default, a.help or "")
|
||||||
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, name, box):
|
def args2html(self, name, box, action=""):
|
||||||
result = ["""<html><head><title>Boxes - """, name, """</title></head>
|
result = ["""<html><head><title>Boxes - """, name, """</title></head>
|
||||||
<body>
|
<body>
|
||||||
<h1>""", name, """</h1>
|
<h1>""", name, """</h1>
|
||||||
<p>""", box.__doc__, """</p>
|
<p>""", box.__doc__, """</p>
|
||||||
<form action="" method="POST" target="_blank">
|
<form action="%s" method="POST" target="_blank">
|
||||||
<table>
|
<table>
|
||||||
"""]
|
""" % (action)]
|
||||||
for a in box.argparser._actions:
|
for a in box.argparser._actions:
|
||||||
if a.dest == "output":
|
if a.dest in ("input", "output"):
|
||||||
continue
|
continue
|
||||||
result.append(self.arg2html(a))
|
result.append(self.arg2html(a))
|
||||||
if a.dest == "burn":
|
if a.dest == "burn":
|
||||||
|
@ -90,6 +96,8 @@ flex cuts, holes and slots for screws and more high level functions.
|
||||||
<ul>
|
<ul>
|
||||||
""" ]
|
""" ]
|
||||||
for name in sorted(self.boxes):
|
for name in sorted(self.boxes):
|
||||||
|
if name in ("TrayLayout2", ):
|
||||||
|
continue
|
||||||
box = self.boxes[name]
|
box = self.boxes[name]
|
||||||
docs = ""
|
docs = ""
|
||||||
if box.__doc__:
|
if box.__doc__:
|
||||||
|
@ -140,6 +148,21 @@ flex cuts, holes and slots for screws and more high level functions.
|
||||||
except (ArgumentParserError) as e:
|
except (ArgumentParserError) as e:
|
||||||
start_response(status, headers)
|
start_response(status, headers)
|
||||||
return self.errorMessage(name, e)
|
return self.errorMessage(name, e)
|
||||||
|
if name == "TrayLayout":
|
||||||
|
start_response(status, headers)
|
||||||
|
box.fillDefault(box.x, box.y)
|
||||||
|
self.boxes["TrayLayout2"].argparser.set_defaults(layout=str(box))
|
||||||
|
return self.args2html(
|
||||||
|
name, self.boxes["TrayLayout2"], action="TrayLayout2")
|
||||||
|
if name == "TrayLayout2":
|
||||||
|
try:
|
||||||
|
print(urllib.parse.unquote_plus(box.layout))
|
||||||
|
box.parse(urllib.parse.unquote_plus(box.layout).split("\n"))
|
||||||
|
except Exception as e:
|
||||||
|
raise
|
||||||
|
start_response(status, headers)
|
||||||
|
return self.errorMessage(name, e)
|
||||||
|
|
||||||
start_response(status,
|
start_response(status,
|
||||||
[('Content-type', 'image/svg+xml; charset=utf-8')])
|
[('Content-type', 'image/svg+xml; charset=utf-8')])
|
||||||
fd, box.output = tempfile.mkstemp()
|
fd, box.output = tempfile.mkstemp()
|
||||||
|
|
|
@ -16,22 +16,26 @@
|
||||||
|
|
||||||
import sys, re
|
import sys, re
|
||||||
from boxes import *
|
from boxes import *
|
||||||
import argparse
|
import boxes
|
||||||
|
|
||||||
class Layout(Boxes):
|
class Layout(Boxes):
|
||||||
"""Generate a typetray from a layout file"""
|
"""Generate a typetray from a layout file"""
|
||||||
def __init__(self, input=None):
|
def __init__(self, input=None, webargs=False):
|
||||||
Boxes.__init__(self)
|
Boxes.__init__(self)
|
||||||
self.buildArgParser("h", "hi")
|
self.buildArgParser("h", "hi")
|
||||||
self.argparser.add_argument(
|
if not webargs:
|
||||||
"--input", action="store", type=argparse.FileType('r'),
|
self.argparser.add_argument(
|
||||||
help="layout file")
|
"--input", action="store", type=argparse.FileType('r'),
|
||||||
self.argparser.add_argument(
|
help="layout file")
|
||||||
"--x", action="store", type=int, default=None,
|
self.argparser.add_argument(
|
||||||
help="number of compartments side by side")
|
"--x", action="store", type=int, default=None,
|
||||||
self.argparser.add_argument(
|
help="number of compartments side by side")
|
||||||
"--y", action="store", type=int, default=None,
|
self.argparser.add_argument(
|
||||||
help="number of compartments back to front")
|
"--y", action="store", type=int, default=None,
|
||||||
|
help="number of compartments back to front")
|
||||||
|
else:
|
||||||
|
self.argparser.add_argument(
|
||||||
|
"--layout", action="store", type=str)
|
||||||
|
|
||||||
def fillDefault(self, x, y):
|
def fillDefault(self, x, y):
|
||||||
self.x = [0.0] * x
|
self.x = [0.0] * x
|
||||||
|
@ -255,7 +259,7 @@ class Layout(Boxes):
|
||||||
vwalls = []
|
vwalls = []
|
||||||
floors = []
|
floors = []
|
||||||
for line in input:
|
for line in input:
|
||||||
if line[0] == "#":
|
if not line or line[0] == "#":
|
||||||
continue
|
continue
|
||||||
m = re.match(r"( \|)* ,>\s*(\d*\.?\d+)\s*mm\s*", line)
|
m = re.match(r"( \|)* ,>\s*(\d*\.?\d+)\s*mm\s*", line)
|
||||||
if m:
|
if m:
|
||||||
|
@ -326,6 +330,21 @@ class Layout(Boxes):
|
||||||
self.vwalls = vwalls
|
self.vwalls = vwalls
|
||||||
self.floors = floors
|
self.floors = floors
|
||||||
|
|
||||||
|
class LayoutGenerator(Layout):
|
||||||
|
"""Helper class for bserver with only args for generating layout file"""
|
||||||
|
def __init__(self):
|
||||||
|
Boxes.__init__(self)
|
||||||
|
self.argparser = boxes.ArgumentParser()
|
||||||
|
self.argparser.add_argument(
|
||||||
|
"--x", action="store", type=int, default=2,
|
||||||
|
help="number of compartments side by side")
|
||||||
|
self.argparser.add_argument(
|
||||||
|
"--y", action="store", type=int, default=2,
|
||||||
|
help="number of compartments back to front")
|
||||||
|
|
||||||
|
def render(self):
|
||||||
|
return
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
l = Layout()
|
l = Layout()
|
||||||
l.parseArgs()
|
l.parseArgs()
|
||||||
|
|
Loading…
Reference in New Issue