Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Commit

Permalink
escape manager
Browse files Browse the repository at this point in the history
  • Loading branch information
baverman committed Oct 16, 2011
1 parent c72b086 commit e088773
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions uxie/escape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
import weakref
from bisect import bisect

class Escapable(object):
def __init__(self, callback, *args):
self.callback = callback
self.args = map(weakref.ref, args)

def cancel(self):
args = [a() for a in self.args]
self.callback(*args)

def is_active(self):
args = [a() for a in self.args]
return not any(a is None for a in args)


class Manager(object):
def __init__(self):
self.escape_stack = []

def push(self, obj, priority=None):
if priority is None:
priority = 0

item = (-priority, time.time(), obj)
self.escape_stack.insert(bisect(self.escape_stack, item), item)
return obj

def process(self):
while self.escape_stack:
_, _, obj = self.escape_stack.pop()
if obj.is_active():
obj.cancel()
return False

return True

0 comments on commit e088773

Please sign in to comment.