def changed(context): logger.debug("Context %s changed" % context) # TODO: wrap in try/except if context.evaluateRules(): if context not in current_contexts: current_contexts.add(context) notify.enter(context) context.runEnteringActions() else: if context in current_contexts: current_contexts.remove(context) notify.leave(context) context.runLeavingActions()
def reevaluate(): old_contexts = current_contexts.copy() current_contexts.clear() for c in contexts.itervalues(): if c.evaluateRules(): current_contexts.add(c) # Run leave before enter for c in old_contexts.difference(current_contexts): notify.leave(c) c.runLeavingActions() for c in current_contexts.difference(old_contexts): notify.enter(c) c.runEnteringActions()
# First run needs more magic. First populate current_contexts with the contexts # which are active. for c in contexts.itervalues(): if c.evaluateRules(): current_contexts.add(c) # Now enter all contexts we're in, and leave all contexts we're not in. It # might be a good idea to make leaving on startup an option per context. This # has to be done in two loops so that we leave before entering. # TODO: wrap in try/except for c in contexts.itervalues(): if c not in current_contexts: c.runLeavingActions() for c in contexts.itervalues(): if c in current_contexts: notify.enter(c) c.runEnteringActions() def reevaluate(): old_contexts = current_contexts.copy() current_contexts.clear() for c in contexts.itervalues(): if c.evaluateRules(): current_contexts.add(c) # Run leave before enter for c in old_contexts.difference(current_contexts): notify.leave(c) c.runLeavingActions() for c in current_contexts.difference(old_contexts):