Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/properties.py
blob: 5be7e1c6b1feb6bccc7401615c5f8a124ad77b5d (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
# 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

from sugar.tutorius.constraints import *

class TutoriusProperty():
    """
    The base class for all actions' properties. The interface is the following :
    
    set() : attempts to change the value (may throw an exception if constraints
    are not respected
    
    value : the value of the property
    
    type : the type of the property
    
    get_contraints() : the constraints inserted on this property. They define
    what is acceptable or not as values.
    """
    def __init__(self):
        self._type = None
        self._constraints = None
        self._value = None
        
    def set(self, value):
        """
        Attempts to set the value of the property. If the value does not respect
        the constraints on the property, this method will raise an exception.
        
        The exception should be of the type related to the constraint that
        failed. E.g. When a int is to be set with a value that 
        """
        for constraint_name in self.get_constraints():
            constraint = getattr(self, constraint_name)
            constraint.validate(value)
        self._value = value
        return True
    
    def get(self):
        return self._value
    
    value = property(fget=get)
    
    def get_constraints(self):
        """
        Returns the list of constraints associated to this property.
        """
        if self._constraints is None:
            self._constraints = []
            for i in dir(self):
                t = getattr(self,i)
                if isinstance(t, Constraint):
                    self._constraints.append(i)
        return self._constraints
    
    def get_type(self):
        return self._type
    
    type = property(fget=get_type)

class TIntProperty(TutoriusProperty):
    """
    Represents an integer. Can have an upper value limit and/or a lower value
    limit.
    """
    
    def __init__(self, value, lower_limit=None, upper_limit=None):
        TutoriusProperty.__init__(self)
        self._type = "int"
        self.upper_limit = UpperLimitConstraint(upper_limit)
        self.lower_limit = LowerLimitConstraint(lower_limit)
        
        self.set(value)
    
class TFloatProperty(TutoriusProperty):
    """
    Represents a floting point number. Can have an upper value limit and/or
    a lower value limit.
    """
    def __init__(self, value, lower_limit=None, upper_limit=None):
        TutoriusProperty.__init__(self)
        self._type = "float"
        
        self.upper_limit = UpperLimitConstraint(upper_limit)
        self.lower_limit = LowerLimitConstraint(lower_limit)
        
        self.set(value)
    
class TStringProperty(TutoriusProperty):
    """
    Represents a string. Can have a maximum size limit.
    """
    def __init__(self, value, size_limit=None):
        TutoriusProperty.__init__(self)
        self._type = "string"
        self.size_limit = SizeConstraint(size_limit)
        
        self.set(value)
    
class TArrayProperty(TutoriusProperty):
    """
    Represents an array of properties. Can have a maximum number of element 
    limit, but there are no constraints on the content of the array.
    """
    def __init__(self, value, size_limit=None):
        TutoriusProperty.__init__(self)
        self._type = "array"
        self.size_limit = SizeConstraint(size_limit)
        
        self.set(value)
    
class TColorProperty(TutoriusProperty):
    """
    Represents a RGB color with 3 8-bit integer values.
    
    The value of the property is the array [R, G, B]
    """
    def __init__(self, red=None, green=None, blue=None):
        TutoriusProperty.__init__(self)
        self._type = "color"
        
        self.color_constraint = ColorConstraint()
        
        self._red = red or 0
        self._green = green or 0
        self._blue = blue or 0
        
        self.set([self._red, self._green, self._blue])
    
class TFileProperty(TutoriusProperty):
    """
    Represents a path to a file on the disk.
    """
    def __init__(self, path):
        """
        Defines the path to an existing file on disk file.
        
        For now, the path may be relative or absolute, as long as it exists on
        the local machine.
        TODO : Make sure that we have a file scheme that supports distribution
        on other computers (LP 355197)
        """
        TutoriusProperty.__init__(self)
        
        self._type = "file"
        
        self.file_constraint = FileConstraint()
        
        self.set(path)
    
class TEnumProperty(TutoriusProperty):
    """
    Represents a value in a given enumeration. This means that the value will
    always be one in the enumeration and nothing else.
    
    """
    def __init__(self, value, accepted_values):
        """
        Creates the enumeration property.
        
        @param value The initial value of the enum. Must be part of 
        accepted_values
        @param accepted_values A list of values that the property can take
        """
        TutoriusProperty.__init__(self)
        
        self._type = "enum"
        
        self.enum_constraint = EnumConstraint(accepted_values)
        
        self.set(value)
    
class TBooleanProperty(TutoriusProperty):
    """
    Represents a True of False value.
    """
    def __init__(self, value=False):
        TutoriusProperty.__init__(self)
        
        self._type = "boolean"
        
        self.boolean_constraint = BooleanConstraint()
        
        self.set(value)
    
class TUAMProperty(TutoriusProperty):
    """
    Represents a widget of the interface by storing its UAM.
    """
    # TODO : Pending UAM check-in (LP 355199)
    pass