File: simpleforms.py

package info (click to toggle)
glueviz 0.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,180 kB
  • ctags: 6,728
  • sloc: python: 37,111; makefile: 134; sh: 60
file content (133 lines) | stat: -rw-r--r-- 3,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
The descriptors in this module are meant to be added to classes, to
specify simple user-settable forms. These classes are used to automatically
construct GUIs, without having to write GUI code in the form class itself.

:class:`Option` objects are defined at the class-level. To instances of
these classes, an :class:`Option` behaves like a normal instance attribute.

See :ref:`fit_plugins` for example usage.
"""


class Option(object):

    """
    Base class for other options.

    This shouldn't be used directly
    """

    def __init__(self, default, label):
        """
        :param default: The default value for this option.
        :type default: object
        :param label: A short label for this option, to use in the GUI
        :type label: str
        """
        self.label = label
        """A UI label for the setting"""
        self.default = default
        """The default value"""

        self._name = "__%s_%i" % (type(self), id(self))

    def __get__(self, instance, owner=None):
        if instance is None:
            return self
        return getattr(instance, self._name, self.default)

    def __set__(self, instance, value):
        value = self._validate(value)
        setattr(instance, self._name, value)

    def _validate(self, value):
        return value


class IntOption(Option):

    def __init__(self, min=0, max=10, default=1, label="Integer"):
        """
        An integer-valued option

        :param min: The minimum valid value
        :type min: integer
        :param max: The maximum valid value
        :type max: integer
        :param default: The default value
        :type default: integer
        :param label: A short label for this option
        :type label: str
        """
        super(IntOption, self).__init__(default, label)

        self.min = min
        self.max = max

    def _validate(self, value):

        try:
            if value != int(value):
                raise ValueError()
            value = int(value)
        except ValueError:
            raise ValueError("%s must be an integer" % self.label)

        if value < self.min:
            raise ValueError("%s must be >= %i" % (self.label, self.min))

        if value > self.max:
            raise ValueError("%s must be <= %i" % (self.label, self.max))

        return value


class FloatOption(Option):

    def __init__(self, min=0, max=10, default=1, label="Float"):
        """
        An floating-point option

        :param min: The minimum valid value
        :type min: float
        :param max: The maximum valid value
        :type max: float
        :param default: The default value
        :type default: float
        :param label: A short label for this option
        :type label: str
        """
        super(FloatOption, self).__init__(default, label)
        self.min = min
        self.max = max

    def _validate(self, value):
        value = float(value)

        if value < self.min or value > self.max:
            raise ValueError("%s must be between %e and %e" % (self.label,
                                                               self.min, self.max))
        return value


class BoolOption(Option):

    def __init__(self, label="Bool", default=False):
        """
        A boolean-valued option

        :param default: The default True/False value
        :type default: bool

        :param label: A short label for this option
        :type label: str
        """
        super(BoolOption, self).__init__(default, label)

    def _validate(self, value):
        if value not in [True, False]:
            raise ValueError(
                "%s must be True or False: %s" % (self.label, value))

        return value