From 49ab39870266196f6267f83dd6951f839ebde773 Mon Sep 17 00:00:00 2001 From: mike Date: Fri, 04 Dec 2009 02:21:14 +0000 Subject: Merge ../../simpoirs-clone into remote_integration Conflicts: src/extensions/tutoriusremote.py --- (limited to 'tutorius/properties.py') diff --git a/tutorius/properties.py b/tutorius/properties.py index a462782..85e8aa5 100644 --- a/tutorius/properties.py +++ b/tutorius/properties.py @@ -19,6 +19,7 @@ TutoriusProperties have the same behaviour as python properties (assuming you also use the TPropContainer), with the added benefit of having builtin dialog prompts and constraint validation. """ +import uuid from copy import copy, deepcopy from .constraints import Constraint, \ @@ -35,6 +36,9 @@ from .propwidgets import PropWidget, \ FloatPropWidget, \ IntArrayPropWidget +import logging +LOGGER = logging.getLogger("properties") + class TPropContainer(object): """ A class containing properties. This does the attribute wrapping between @@ -60,6 +64,14 @@ class TPropContainer(object): self._props[attr_name] = propinstance.validate( copy(propinstance.default)) + self.__id = hash(uuid.uuid4()) + # The differences dictionary. This is a structure that holds all the + # modifications that were made to the properties since the action + # was last installed or the last moment the notification was executed. + # Every property change will be logged inside it and it will be sent + # to the creator to update its action edition dialog. + self._diff_dict = {} + def __getattribute__(self, name): """ Process the 'fake' read of properties in the appropriate instance @@ -93,8 +105,11 @@ class TPropContainer(object): try: # We attempt to get the property object with __getattribute__ # to work through inheritance and benefit of the MRO. - return props.__setitem__(name, + real_value = props.__setitem__(name, object.__getattribute__(self, name).validate(value)) + LOGGER.debug("Action :: caching %s = %s in diff dict"%(name, str(value))) + self._diff_dict[name] = value + return real_value except AttributeError: return object.__setattr__(self, name, value) @@ -128,21 +143,23 @@ class TPropContainer(object): # Providing the hash methods necessary to use TPropContainers # in a dictionary, according to their properties def __hash__(self): - #Return a hash of properties (key, value) sorted by key - #We need to transform the list of property key, value lists into - # a tuple of key, value tuples - return hash(tuple(map(tuple,sorted(self._props.items(), cmp=lambda x, y: cmp(x[0], y[0]))))) + # many places we use containers as keys to store additional data. + # Since containers are mutable, there is a need for a hash function + # where the result is constant, so we can still lookup old instances. + return self.__id def __eq__(self, e2): - return isinstance(e2, type(self)) and self._props == e2._props + return self.__id == e2.__id or \ + (isinstance(e2, type(self)) and self._props == e2._props) # Adding methods for pickling and unpickling an object with # properties def __getstate__(self): - return self._props.copy() + return dict(id=self.__id, props=self._props.copy()) def __setstate__(self, dict): - self._props.update(dict) + self.__id = dict['id'] + self._props.update(dict['props']) class TutoriusProperty(object): """ -- cgit v0.9.1