-
-
Notifications
You must be signed in to change notification settings - Fork 396
Context manager / display hook #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
467b029
d852668
b5f8eae
1d36ace
2b7a6b7
832af7b
b169fd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| A context manager that will register a hook on a | ||
| Display Publisher for storing messages. | ||
| """ | ||
|
|
||
| # Copyright (c) IPython Development Team. | ||
| # Distributed under the terms of the Modified BSD License. | ||
|
|
||
| from io import StringIO | ||
| import sys | ||
|
|
||
| from IPython.core.getipython import get_ipython | ||
|
|
||
| from .displayhook import ZMQMessageHook | ||
|
|
||
|
|
||
| class MessageHookFor(object): | ||
| """ | ||
| A context manager which takes the name of a message field, | ||
| and installs a hook to intercept messages of that type. | ||
|
|
||
| Usage | ||
| ----- | ||
| >>> with MessageHookFor('display_data'): | ||
| clear_output() | ||
| """ | ||
| def __init__(self, message_name, parent=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring and signature suggest that this is a message for a single type, but it also captures stream messages unconditionally. |
||
| self._parent = parent | ||
| self._message_name = message_name | ||
| self._pub = get_ipython().display_pub | ||
| self._hook = ZMQMessageHook(message_name, parent.store) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also makes parent a required arg, because |
||
| self._callback = parent.store | ||
| self._std_buffer = StringIO() | ||
|
|
||
| def clear_output(self, *args, **kwargs): | ||
| self._std_buffer.truncate(0) | ||
| self._parent.clear() | ||
|
|
||
| def __enter__(self): | ||
| """ | ||
| Called on entry to the context. | ||
|
|
||
| Installs the message hook on the current ipython | ||
| display publisher. | ||
| """ | ||
| self._pub.register_hook(self._hook) | ||
| self._old_clear = self._pub.clear_output | ||
| self._pub.clear_output = self.clear_output | ||
|
|
||
| self._old_stdout = sys.stdout | ||
| self._old_stderr = sys.stderr | ||
| sys.stdout = self._std_buffer | ||
| sys.stderr = self._std_buffer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it deliberate to eliminate the distinction between stdout and stderr? |
||
|
|
||
| def __exit__(self, tp, value, tb): | ||
| if tp is not None: | ||
| # Exception occurred... log and continue. | ||
| pass | ||
|
|
||
| self._pub.unregister_hook(self._hook) | ||
| self._pub.clear_output = self._old_clear | ||
| sys.stdout = self._old_stdout | ||
| sys.stderr = self._old_stderr | ||
|
|
||
| std = self._std_buffer.getvalue() | ||
| if std: | ||
| # TODO : update this once rendermime is available here. | ||
| temp = {'content': {'data': {'text/plain': std}}} | ||
| self._callback(temp) | ||
| self._std_buffer.truncate(0) | ||
|
|
||
| return False | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For context managers that are typically treated as functions, I think it's more conventional to use
function_style_namerather thanObjectStyleName, even though it's technically a type.