From 2ace15475a3fa4e2daf77db22183c596865de463 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Tue, 1 Nov 2016 20:12:47 +0100 Subject: [PATCH] Check types in Settings class and support choices --- boxes/edges.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/boxes/edges.py b/boxes/edges.py index 6c0f008..225c540 100644 --- a/boxes/edges.py +++ b/boxes/edges.py @@ -146,15 +146,39 @@ class Settings(object): group.prefix = prefix for name, default in (sorted(cls.absolute_params.items()) + sorted(cls.relative_params.items())): + # Handle choices + choices = None + if isinstance(default, tuple): + choices = default + t = type(default[0]) + for val in default: + if (type(val) is not t or + type(val) not in (bool, int, float, str)): + raise ValueError("Type not supported: %r", val) + default = default[0] + + # Overwrite default if name in defaults: default = defaults[name] + + if type(default) not in (bool, int, float, str): + raise ValueError("Type not supported: %r", default) + group.add_argument("--%s_%s" % (prefix, name), type=type(default), action="store", default=default, + choices=choices, help=descriptions.get(name)) def __init__(self, thickness, relative=True, **kw): - self.values = self.absolute_params.copy() + self.values = {} + for name, value in self.absolute_params.items(): + if isinstance(value, tuple): + value = value[0] + if type(value) not in (bool, int, float, str): + raise ValueError("Type not supported: %r", value) + self.values[name] = value + self.thickness = thickness factor = 1.0 if relative: @@ -273,7 +297,7 @@ Values: """ absolute_params = { - "style": "A", + "style": ("A", "B"), "outset": True, } @@ -746,7 +770,7 @@ Values: """ absolute_params = { - "style": "A", + "style": ("A", "B"), "outset": False, "pinwidth": 0.5, "grip_percentage": 0,