You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
511 B
27 lines
511 B
6 years ago
|
from threading import Lock
|
||
|
|
||
|
|
||
|
class Context(object):
|
||
|
def __init__(self, **kwargs):
|
||
|
self.kwargs = kwargs
|
||
|
|
||
|
def __getattr__(self, key):
|
||
|
return self.kwargs.get(key)
|
||
|
|
||
|
|
||
|
class ContextStack(object):
|
||
|
def __init__(self):
|
||
|
self._list = []
|
||
|
self._lock = Lock()
|
||
|
|
||
|
def pop(self):
|
||
|
context = self._list.pop()
|
||
|
|
||
|
self._lock.release()
|
||
|
return context
|
||
|
|
||
|
def push(self, **kwargs):
|
||
|
self._lock.acquire()
|
||
|
|
||
|
return self._list.append(Context(**kwargs))
|