Add argparse type to handle edges

Switch over bottom_edge and top_edge args
Support this type in web UI
Refactor creating the table row out of the different UI inputs
This commit is contained in:
Florian Festi 2016-04-10 22:51:57 +02:00
parent 1115683449
commit b4f4369faa
2 changed files with 47 additions and 10 deletions

View File

@ -119,6 +119,34 @@ def argparseSections(s):
except ValueError:
raise argparse.ArgumentTypeError("Don't understand sections string")
class ArgparseEdgeType:
edges = []
names = {
"e" : "Straight Edge",
"E" : "Outset Edge",
"f" : "Finger Joint",
"F" : "Finger Joint other side",
}
def __init__(self, edges=None):
if edges:
self.edges = list(edges)
def __call__(self, pattern):
if len(pattern) != 1:
raise ValueError("Edge type can only have one letter.")
if pattern not in self.edges:
raise ValueError("Use one of the following values: " +
", ".join(edges))
return pattern
def html(self, name, default):
options = "\n".join(
("""<option value="%s"%s>%s %s</option>""" %
(e, ' selected="selected"' if e == default else "",
e, self.names.get(e, "")) for e in self.edges))
return """<select name="%s" size="1">\n%s</select>\n""" % (name, options)
##############################################################################
### Main class
##############################################################################
@ -205,11 +233,13 @@ class Boxes:
help="inner height of inner walls in mm (leave to zero for same as outer walls)")
elif arg == "bottom_edge":
self.argparser.add_argument(
"--bottom_edge", action="store", type=str, default="h",
"--bottom_edge", action="store",
type=ArgparseEdgeType("Fhs"), default="h",
help="edge type for bottom edge")
elif arg == "top_edge":
self.argparser.add_argument(
"--top_edge", action="store", type=str, default="e",
"--top_edge", action="store",
type=ArgparseEdgeType("eES"), default="e",
help="edge type for top edge")
else:
raise ValueError("No default for argument", arg)

View File

@ -54,16 +54,23 @@ class BServer:
name = a.option_strings[0].replace("-", "")
if isinstance(a, argparse._HelpAction):
return ""
row = """<tr><td>%s</td><td>%%s</td><td>%s</td></tr>\n""" % \
(name, a.help or "")
if isinstance(a, argparse._StoreTrueAction):
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)
if a.dest == "layout":
input = """<input name="%s" type="checkbox" value="%s">""" % \
(name, a.default)
elif (isinstance(a, argparse._StoreAction) and
isinstance(a.type, boxes.ArgparseEdgeType)):
input = a.type.html(name, a.default)
elif 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""" % \
(name, name, a.default, a.help)
input = """<textarea name="%s" cols="%s" rows="%s">%s</textarea>""" % \
(name, max((len(l) for l in val))+10, len(val)+1, a.default)
else:
input = """<input name="%s" type="text" value="%s">""" % \
(name, a.default)
return row % input
def args2html(self, name, box, action=""):
result = ["""<html><head><title>Boxes - """, name, """</title></head>