def add_value(notification, kw=None): pair = (notification, kw) if pair in self.__value_notifications[prop_name]: return logger.debug("Will call %s.%s after assignment to %s.%s", observer.__class__.__name__, notification.__name__, self.__class__.__name__, prop_name) self.__value_notifications[prop_name].append(pair) return
def add_signal(notification, kw=None): if not isinstance(value, Signal): return pair = (notification, kw) if pair in self.__signal_notif[prop_name]: return logger.debug("Will call %s.%s after emit on %s.%s", observer.__class__.__name__, notification.__name__, self.__class__.__name__, prop_name) self.__signal_notif[prop_name].append(pair) return
def add_after(notification, kw=None): if (not isinstance(value, ObsWrapperBase) or isinstance(value, Signal)): return pair = (notification, kw) if pair in self.__instance_notif_after[prop_name]: return logger.debug("Will call %s.%s after mutation of %s.%s", observer.__class__.__name__, notification.__name__, self.__class__.__name__, prop_name) self.__instance_notif_after[prop_name].append(pair) return
def __remove_observer_notification(self, observer, prop_name): """ Remove all stored notifications. *observer* an instance. *prop_name* a string. """ def side_effect(seq): for meth, kw in reversed(seq): if meth.im_self is observer: seq.remove((meth, kw)) yield meth for meth in side_effect(self.__value_notifications.get(prop_name, ())): logger.debug("Stop calling '%s' after assignment", meth.__name__) for meth in side_effect(self.__signal_notif.get(prop_name, ())): logger.debug("Stop calling '%s' after emit", meth.__name__) for meth in side_effect(self.__instance_notif_before.get(prop_name, ())): logger.debug("Stop calling '%s' before mutation", meth.__name__) for meth in side_effect(self.__instance_notif_after.get(prop_name, ())): logger.debug("Stop calling '%s' after mutation", meth.__name__) return
def __remove_observer_notification(self, observer, prop_name): # retrieves the set of custom observing methods # and removes corresponding the notification methods cust_methods = observer.get_custom_observing_methods(prop_name) for meth in cust_methods: pair = (_obs_explicit, meth) for _map in (self.__value_notifications, self.__signal_notif, self.__instance_notif_before, self.__instance_notif_after,): if prop_name in _map and \ pair in _map[prop_name]: _map[prop_name].remove(pair) pass pass if self.__value_notifications.has_key(prop_name): method_name = "property_%s_value_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair in self.__value_notifications[prop_name]: self.__value_notifications[prop_name].remove(pair) logger.debug("Removed implicit value change notification '%s'", method_name) pass pass pass orig_prop = getattr(self, "_prop_%s" % prop_name, None) # is it a signal? if isinstance(orig_prop, Signal): method_name = "property_%s_signal_emit" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair in self.__signal_notif[prop_name]: self.__signal_notif[prop_name].remove(pair) logger.debug("Removed implicit signal emit notification '%s'", method_name) pass pass pass # is it an instance change notification type? elif isinstance(orig_prop, ObsWrapperBase): if self.__instance_notif_before.has_key(prop_name): method_name = "property_%s_before_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair in self.__instance_notif_before[prop_name]: self.__instance_notif_before[prop_name].remove(pair) logger.debug("Removed implicit before call "\ "notification '%s'", method_name) pass pass pass if self.__instance_notif_after.has_key(prop_name): method_name = "property_%s_after_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair in self.__instance_notif_after[prop_name]: self.__instance_notif_after[prop_name].remove(pair) logger.debug("Removed after call notification '%s'", method_name) pass pass pass pass return
def __add_observer_notification(self, observer, prop_name): """Searches in the observer for any possible listener, and stores the notification methods to be called later""" # retrieves the set of custom observing methods cust_methods = observer.get_custom_observing_methods(prop_name) method_name = "property_%s_value_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair not in self.__value_notifications[prop_name]: list.append(self.__value_notifications[prop_name], pair) logger.debug("Added implicit value change notification '%s'", method_name) pass pass # checks for custom observing methods. If it is a signal, # a method or value is decided from number of # arguments. This is not particularly robust. # self, model, prop_name, old, new for meth in (m for m in cust_methods if m.im_func.func_code.co_argcount == 5): pair = (_obs_explicit, meth) if pair not in self.__value_notifications[prop_name]: list.append(self.__value_notifications[prop_name], pair) logger.debug("Added explicit value change notification '%s'", meth.im_func.__name__) pass pass # is it a signal? orig_prop = getattr(self, "_prop_%s" % prop_name, None) if isinstance(orig_prop, Signal): method_name = "property_%s_signal_emit" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair not in self.__signal_notif[prop_name]: list.append(self.__signal_notif[prop_name], pair) logger.debug("Added implicit signal emit notification '%s'", method_name) pass pass # checks for custom observing methods. If it is a signal, # a method or value is decided from number of # arguments. This is not particularly robust. # self, model, signal_name, arg for meth in (m for m in cust_methods if m.im_func.func_code.co_argcount == 4): pair = (_obs_explicit, meth) if pair not in self.__signal_notif[prop_name]: list.append(self.__signal_notif[prop_name], pair) logger.debug("Added explicit signal emit notification '%s'", meth.im_func.__name__) pass pass pass # is it an instance change notification type? elif isinstance(orig_prop, ObsWrapperBase): method_name = "property_%s_before_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair not in self.__instance_notif_before[prop_name]: list.append(self.__instance_notif_before[prop_name], pair) logger.debug("Added implicit before call notification '%s'", method_name) pass pass # checks for custom observing methods. If it is a signal, # a method or value is decided from number of # arguments. This is not particularly robust. # self, model, prop_name, instance, meth_name, args, kwargs for meth in (m for m in cust_methods if m.im_func.func_code.co_argcount == 7): pair = (_obs_explicit, meth) if pair not in self.__instance_notif_before[prop_name]: list.append(self.__instance_notif_before[prop_name], pair) logger.debug("Added explicit before call notification '%s'", meth.im_func.__name__) pass pass method_name = "property_%s_after_change" % prop_name if hasattr(observer, method_name): pair = (_obs_implicit, getattr(observer, method_name)) if pair not in self.__instance_notif_after[prop_name]: list.append(self.__instance_notif_after[prop_name], pair) logger.debug("Added implicit after call notification '%s'", method_name) pass pass # checks for custom observing methods. If it is a signal, # a method or value is decided from number of # arguments. This is not particularly robust. # self, model, prop_name, instance, meth_name, res, args, kwargs for meth in (m for m in cust_methods if m.im_func.func_code.co_argcount == 8): pair = (_obs_explicit, meth) if pair not in self.__instance_notif_after[prop_name]: list.append(self.__instance_notif_after[prop_name], pair) logger.debug("Added explicit after call notification '%s'", meth.im_func.__name__) pass pass pass return