From 3a0df10866cdc273f3e798a20d4c81cee07675f3 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Mon, 31 Oct 2016 18:12:39 +0100 Subject: [PATCH] Create arguments from edge settings starting with FingerJointSettings --- boxes/__init__.py | 32 ++++++++++++++++---------------- boxes/edges.py | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/boxes/__init__.py b/boxes/__init__.py index 128176d..de92f7c 100755 --- a/boxes/__init__.py +++ b/boxes/__init__.py @@ -191,18 +191,10 @@ class Boxes: def __init__(self): self.formats = formats.Formats() self.argparser = ArgumentParser(description=self.__doc__) + self.edgesettings = {} self.argparser._action_groups[1].title = "Generator Settings" - fingergroup = self.argparser.add_argument_group( - "Finger Joint Settings") - fingergroup.add_argument( - "--fingerjointfinger", action="store", type=float, default=1.0, - help="width of the fingers in multiples of thickness") - fingergroup.add_argument( - "--fingerjointspace", action="store", type=float, default=1.0, - help="width of the space between fingers in multiples of thickness") - fingergroup.add_argument( - "--fingerjointsurrounding", action="store", type=float, default=1.0, - help="amount of space needed at the end in multiples of normal spaces") + self.addSettingsArgs(edges.FingerJointSettings) + defaultgroup = self.argparser.add_argument_group( "Default Settings") defaultgroup.add_argument( @@ -302,6 +294,12 @@ class Boxes: else: raise ValueError("No default for argument", arg) + def addSettingsArgs(self, settings, prefix=None): + prefix = prefix or settings.__name__[:-len("Settings")] + settings.parserArguments(self.argparser, prefix) + self.edgesettings[prefix] = {} + + def parseArgs(self, args=None): """ Parse command line parameters @@ -310,6 +308,11 @@ class Boxes: """ for key, value in vars(self.argparser.parse_args(args=args)).items(): + # treat edge settings separately + for setting in self.edgesettings: + if key.startswith(setting + '_'): + self.edgesettings[setting][key[len(setting)+1:]] = value + continue setattr(self, key, value) # Change file ending to format if not given explicitly @@ -343,11 +346,8 @@ class Boxes: # Finger joints # Share settings object - s = edges.FingerJointSettings(self.thickness) - s.setValues(self.thickness, - finger=getattr(self, "fingerjointfinger", 1.0), - space=getattr(self, "fingerjointspace", 1.0), - surroundingspaces=getattr(self, "fingerjointsurrounding", 1.0)) + s = edges.FingerJointSettings(self.thickness, True, + **self.edgesettings.get("FingerJoint", {})) self.addPart(edges.FingerJointEdge(self, s)) self.addPart(edges.FingerJointEdgeCounterPart(self, s)) self.addPart(edges.FingerHoles(self, s), name="fingerHolesAt") diff --git a/boxes/edges.py b/boxes/edges.py index c4e38c1..aaa9d85 100644 --- a/boxes/edges.py +++ b/boxes/edges.py @@ -17,6 +17,9 @@ import math import inspect +import argparse +import re + from boxes import gears def getDescriptions(): @@ -125,6 +128,28 @@ class Settings(object): absolute_params = {} relative_params = {} + @classmethod + def parserArguments(cls, parser, prefix=None): + prefix = prefix or cls.__name__[:-len("Settings")] + + lines = cls.__doc__.split("\n") + + # Parse doc string + descriptions = {} + r = re.compile(r"^ +\* +(\S+) +: .* : +(.*)") + for l in lines: + m = r.search(l) + if m: + descriptions[m.group(1)] = m.group(2) + + group = parser.add_argument_group(lines[0] or lines[1]) + for name, default in (list(cls.absolute_params.items()) + + list(cls.relative_params.items())): + group.add_argument("--%s_%s" % (prefix, name), + type=type(default), + action="store", default=default, + help=descriptions.get(name)) + def __init__(self, thickness, relative=True, **kw): self.values = self.absolute_params.copy() self.thickness = thickness @@ -147,6 +172,7 @@ class Settings(object): factor = 1.0 if relative: factor = thickness + print(kw.items()) for name, value in kw.items(): if name in self.absolute_params: self.values[name] = value