Fix boolean params. Not passing 0, None, False or off works for setting them to False

Thanks to chrisjshull for pointing out how to fix this in the web UI.
Fixes #19
This commit is contained in:
Florian Festi 2016-12-18 23:28:15 +01:00
parent 71d918f6a5
commit 8dc13440ee
4 changed files with 17 additions and 8 deletions

View File

@ -178,6 +178,18 @@ class ArgparseEdgeType:
e, self.names.get(e, "")) for e in self.edges))
return """<select name="%s" size="1">\n%s</select>\n""" % (name, options)
class BoolArg:
def __call__(self, arg):
if not arg or arg in ("None", "0", "off", "False"):
return False
return True
def html(self, name, default):
return """<input name="%s" type="hidden" value="0">
<input name="%s" type="checkbox" value="1"%s>""" % \
(name, name, ' checked="checked"' if default else "")
boolarg = BoolArg()
##############################################################################
### Main class
@ -207,7 +219,7 @@ class Boxes:
choices=self.formats.getFormats(),
help="format of resulting file")
defaultgroup.add_argument(
"--debug", action="store", type=bool, default=False,
"--debug", action="store", type=boolarg, default=False,
help="print surrounding boxes for some structures")
defaultgroup.add_argument(
"--reference", action="store", type=float, default=100,
@ -288,7 +300,7 @@ class Boxes:
default="e", help="edge type for top edge")
elif arg == "outside":
self.argparser.add_argument(
"--outside", action="store", type=bool, default=False,
"--outside", action="store", type=boolarg, default=True,
help="treat sizes as outside measurements that include the walls")
else:
raise ValueError("No default for argument", arg)

View File

@ -31,7 +31,7 @@ class Box2(Boxes):
self.addSettingsArgs(edges.FlexSettings)
self.buildArgParser("top_edge", "bottom_edge", "x", "y", "h")
self.argparser.add_argument(
"--chestlid", action="store", type=bool, default=False,
"--chestlid", action="store", type=boolarg, default=False,
help="add chest lid (needs hinges)")
self.angle = 0

View File

@ -56,7 +56,7 @@ class TrafficLight(Boxes): # change class name here and below
"--n", action="store", type=int, default=3,
help="number of lights")
self.argparser.add_argument(
"--upright", action="store", type=bool, default=False,
"--upright", action="store", type=boolarg, default=True,
help="stack lights upright (or side by side)")
def backCB(self):

View File

@ -86,15 +86,12 @@ class BServer:
row = """<tr><td>%s</td><td>%%s</td><td>%s</td></tr>\n""" % \
(viewname, a.help or "")
if (isinstance(a, argparse._StoreAction) and
isinstance(a.type, boxes.ArgparseEdgeType)):
hasattr(a.type, "html")):
input = a.type.html(name, a.default)
elif a.dest == "layout":
val = a.default.split("\n")
input = """<textarea name="%s" cols="%s" rows="%s">%s</textarea>""" % \
(name, max((len(l) for l in val))+10, len(val)+1, a.default)
elif a.type is bool:
input = """<input name="%s" type="checkbox" value="1"%s>""" % \
(name, ' checked="checked"' if a.default else "")
elif a.choices:
options = "\n".join(
("""<option value="%s"%s>%s</option>""" %