Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/constraints.py
blob: 36abdfbe9dec4460f8786dea3a41db44ab586157 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
Constraints

Defines a set of constraints with their related errors. These constraints are
made to be used inside TutoriusProperties in order to limit the values that
they might take. They can also be used to enforce a particular format or type
for some properties.
"""

# For the File Constraint
import os

class Constraint():
    """
    Basic block for defining constraints on a TutoriusProperty. Every class
    inheriting from Constraint will have a validate function that will be
    executed when the property's value is to be changed.
    """
    def validate(self, value):
        """
        This function receives the value that is proposed as a new value for
        the property. It needs to raise an Error in the case where the value
        does not respect this constraint.
        """
        raise NotImplementedError("Unable to validate a base Constraint")
    
class ValueConstraint(Constraint):
    """
    A value constraint contains a _limit member that can be used in a children
    class as a basic value. See UpperLimitConstraint for an exemple.
    """
    def __init__(self, limit):
        self.limit = limit

class UpperLimitConstraintError(Exception):
    pass

class UpperLimitConstraint(ValueConstraint):
    def validate(self, value):
        """
        Evaluates whether the given value is smaller than the limit.
        
        @raise UpperLimitConstraintError When the value is strictly higher than
        the limit.
        """
        if self.limit is not None:
            if self.limit >= value:
                return
            raise UpperLimitConstraintError()
        return

class LowerLimitConstraintError(Exception):
    pass

class LowerLimitConstraint(ValueConstraint):
    def validate(self, value):
        """
        If the value is lower than the limit, this function raises an error.
        
        @raise LowerLimitConstraintError When the value is strictly smaller
        than the limit.
        """
        if self.limit is not None:
            if value >= self.limit:
                return
            raise LowerLimitConstraintError()
        return

class MaxSizeConstraintError(Exception):
    pass
    
class MaxSizeConstraint(ValueConstraint):
    def validate(self, value):
        """
        Evaluate whether a given object is smaller than the given size when
        run through len(). Great for string, lists and the like. ;)
        
        @raise SizeConstraintError If the length of the value is strictly
        bigger than the limit.
        """
        if self.limit is not None:
            if self.limit >= len(value):
                return
            raise MaxSizeConstraintError("Setter : trying to set value of length %d while limit is %d"%(len(value), self.limit))
        return

class MinSizeConstraintError(Exception):
    pass
    
class MinSizeConstraint(ValueConstraint):
    def validate(self, value):
        """
        Evaluate whether a given object is smaller than the given size when
        run through len(). Great for string, lists and the like. ;)
        
        @raise SizeConstraintError If the length of the value is strictly
        bigger than the limit.
        """
        if self.limit is not None:
            if self.limit <= len(value):
                return
            raise MinSizeConstraintError("Setter : trying to set value of length %d while limit is %d"%(len(value), self.limit))
        return

class ColorConstraintError(Exception):
    pass

class ColorArraySizeError(ColorConstraintError):
    pass
    
class ColorTypeError(ColorConstraintError):
    pass

class ColorValueError(ColorConstraintError):
    pass

class ColorConstraint(Constraint):
    """
    Validates that the value is an array of size 3 with three numbers between 
    0 and 255 (inclusively) in it.
    
    """
    def validate(self, value):
        if len(value) != 3:
            raise ColorArraySizeError("The value is not an array of size 3")
        
        if not (type(value[0]) == type(22) and type(value[1]) ==  type(22) and type(value[2]) == type(22)):
            raise ColorTypeError("Not all the elements of the array are integers")
        
        if value[0] > 255 or value[0] <0:
            raise ColorValueError("Red value is not between 0 and 255")

        if value[1] > 255 or value[1] <0:
            raise ColorValueError("Green value is not between 0 and 255")
        
        if value[2] > 255 or value[2] <0:
            raise ColorValueError("Blue value is not between 0 and 255")
        
        return
    
class BooleanConstraintError(Exception):
    pass

class BooleanConstraint(Constraint):
    """
    Validates that the value is either True or False.
    """
    def validate(self, value):
        if value == True or value == False:
            return
        raise BooleanConstraintError("Value is not True or False")
    
class EnumConstraintError(Exception):
    pass

class EnumConstraint(Constraint):
    """
    Validates that the value is part of a set of well-defined values.
    """
    def __init__(self, accepted_values):
        """
        Creates the constraint and stores the list of accepted values.
        
        @param correct_values A list that contains all the values that will
        be declared as satisfying the constraint
        """
        self._accepted_values = accepted_values
        
    def validate(self, value):
        """
        Ensures that the value that is passed is part of the list of accepted 
        values.
        """
        if not value in self._accepted_values:
            raise EnumConstraintError("Value is not part of the enumeration")
        return
    
class FileConstraintError(Exception):
    pass
    
class FileConstraint(Constraint):
    """
    Ensures that the string given corresponds to an existing file on disk.
    """
    def validate(self, value):
        # TODO : Decide on the architecture for file retrieval on disk
        # Relative paths? From where? Support macros?
        # 
        if not os.path.isfile(value):
            raise FileConstraintError("Non-existing file : %s"%value)
        return