From b4f4369faa7cc8cf70397ffe5701ee8bdbd1cdf1 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Sun, 10 Apr 2016 22:51:57 +0200 Subject: [PATCH] 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 --- boxes/__init__.py | 34 ++++++++++++++++++++++++++++++++-- scripts/boxesserver | 23 +++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/boxes/__init__.py b/boxes/__init__.py index 9ee2a9e..46fe264 100755 --- a/boxes/__init__.py +++ b/boxes/__init__.py @@ -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( + ("""""" % + (e, ' selected="selected"' if e == default else "", + e, self.names.get(e, "")) for e in self.edges)) + return """\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) diff --git a/scripts/boxesserver b/scripts/boxesserver index ccc3cde..d3c5b4e 100755 --- a/scripts/boxesserver +++ b/scripts/boxesserver @@ -54,16 +54,23 @@ class BServer: name = a.option_strings[0].replace("-", "") if isinstance(a, argparse._HelpAction): return "" + row = """%s%%s%s\n""" % \ + (name, a.help or "") if isinstance(a, argparse._StoreTrueAction): - return """%s%s\n""" % \ - (name, name, a.default, a.help) - if a.dest == "layout": + input = """""" % \ + (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 """%s%s\n""" % \ - (name, name, max((len(l) for l in val))+10, len(val)+1, - a.default, a.help or "") - return """%s%s\n""" % \ - (name, name, a.default, a.help) + input = """""" % \ + (name, max((len(l) for l in val))+10, len(val)+1, a.default) + else: + input = """""" % \ + (name, a.default) + + return row % input def args2html(self, name, box, action=""): result = ["""Boxes - """, name, """