From 6584510d390a37153c20974da6704a907058fea0 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 19 Oct 2009 04:38:32 +0000 Subject: Merge gitorious@git.sugarlabs.org:tutorius/michaeljm-dev into merge_michaeljm-dev --- (limited to 'tutorius/properties.py') diff --git a/tutorius/properties.py b/tutorius/properties.py index abf76e5..4c34511 100644 --- a/tutorius/properties.py +++ b/tutorius/properties.py @@ -95,6 +95,89 @@ class TPropContainer(object): """ return object.__getattribute__(self, "_props").keys() + def __eq__(self, otherContainer): + """ + Compare this property container to the other one and returns True only if + the every property of the first one can be found in the other container, with + the same name and the same value. + + @param otherContainer The other container that we wish to test for equality. + @returns True if every property in the first container can be found with the same + value and the same name in the second container. + """ + # Make sure both have the same number of properties + if len(self._props) != len(otherContainer._props): + return False + + if not(type(self) == type(otherContainer)): + return False + + # For every property in this container + for prop in self._props.keys(): + found = False + # Try to match it with another property present in the other container + for otherProp in otherContainer._props.keys(): + # If we were able to match the name, then we look up the value + if prop == otherProp: + this_type = getattr(type(self), prop).type + other_type = getattr(type(otherContainer), prop).type + if this_type != other_type: + return False + + # If this is an addon list, then we need to make sure that + # every element of the list is also present in the other list + if this_type == "addonlist": + if not self._are_lists_identical(self._props[prop], otherContainer._props[prop]): + return False + found = True + break + + # If this is just an embedded / decorated container, then we want to + # make sure the sub component are identical. + elif this_type == "addon": + if not (self._props[prop] == otherContainer._props[prop]): + return False + found = True + break + else: + if self._props[prop] == otherContainer._props[prop]: + found = True + break + # If we arrive here, then we couldn't find any property in the second + # container that matched the current one. We know that the two containers are + # not equal. + if found == False: + return False + return True + + def _are_lists_identical(self, myList, otherList): + """ + Compares two lists of property containers to see if they are identical ( + they have the same properties + + @param myList The first list of properties containers + @param otherList The second list of properties containers + @return True if all of the properties inside the list are identical. False otherwise. + """ + # For each property in the first list, + for container in myList: + found = False + # Attempt to match it with every property in the other list + for other_container in otherList: + # If the containers are identical, + if container == other_container: + # We found a matching container. We don't need to search in the + # second list anymore, so we break + found = True + break + # In the case the property was not found inside the second list + if found == False: + # We know right away that the two lists are not identical + return False + # If we were able to match each property in the first list, then we + # can say the lists are equal. + return True + class TutoriusProperty(object): """ The base class for all actions' properties. The interface is the following : @@ -145,19 +228,6 @@ class TAddonListProperty(TutoriusProperty): """ pass - - 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): - typ = getattr(self, i) - if isinstance(typ, Constraint): - self._constraints.append(i) - return self._constraints - class TIntProperty(TutoriusProperty): """ Represents an integer. Can have an upper value limit and/or a lower value @@ -317,8 +387,15 @@ class TAddonListProperty(TutoriusProperty): See TAddonProperty """ def __init__(self): - super(TAddonProperty, self).__init__() + TutoriusProperty.__init__(self) self.type = "addonlist" self.default = [] + def validate(self, value): + if isinstance(value, list): + for component in value: + if not (isinstance(component, TPropContainer)): + raise ValueError("Expected a list of TPropContainer instances inside TAddonListProperty value, got a %s" % (str(type(component)))) + return value + raise ValueError("Value proposed to TAddonListProperty is not a list") -- cgit v0.9.1