From 86baeb8e6b7e538e823574773c41536e38ea9d42 Mon Sep 17 00:00:00 2001 From: mike Date: Sat, 04 Apr 2009 16:23:00 +0000 Subject: LP 348570 Core : Adding properties for actions --- (limited to 'src/sugar/tutorius/properties.py') diff --git a/src/sugar/tutorius/properties.py b/src/sugar/tutorius/properties.py new file mode 100644 index 0000000..5be7e1c --- /dev/null +++ b/src/sugar/tutorius/properties.py @@ -0,0 +1,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 -- cgit v0.9.1