-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a25b5ec
commit 9710fcc
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from contextlib import contextmanager | ||
from ..exceptions import UraniumException | ||
|
||
|
||
class Proxy(object): | ||
""" | ||
a proxy object takes a context stack, | ||
and proxies requests to whatever the | ||
context stack is currently referring to | ||
""" | ||
|
||
def __init__(self, context_stack): | ||
object.__setattr__(self, "_context", context_stack) | ||
|
||
def _get_current_object(self): | ||
return getattr(self, "_context").obj | ||
|
||
def __getattr__(self, key): | ||
return getattr(self._get_current_object(), key) | ||
|
||
def __setattr__(self, key, value): | ||
return self._get_current_object().__setattr__(key, value) | ||
|
||
def __dir__(self): | ||
return dir(self._get_current_object()) | ||
|
||
|
||
class ContextUnavailable(UraniumException): | ||
""" raised when a context is unavailable. """ | ||
|
||
|
||
class ContextStack(object): | ||
""" | ||
keeps track of the context of an application | ||
object. | ||
""" | ||
|
||
def __init__(self): | ||
self._stack = [] | ||
|
||
def create_context(self, obj): | ||
""" | ||
create a contextmanager which will add the | ||
object as the current object for this contextstack. | ||
""" | ||
@contextmanager | ||
def with_obj_as_context(): | ||
self.push(obj) | ||
yield | ||
self.pop() | ||
return with_obj_as_context() | ||
|
||
def push(self, obj): | ||
self._stack.append(obj) | ||
|
||
def pop(self): | ||
return self._stack.pop() | ||
|
||
@property | ||
def obj(self): | ||
if len(self._stack) == 0: | ||
raise ContextUnavailable("") | ||
return self._stack[-1] |