-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from tangkong/startup_hook
ENH: add post-IPython-initialization routines, better banner
- Loading branch information
Showing
6 changed files
with
124 additions
and
19 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
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
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
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,51 @@ | ||
""" | ||
Print a custom banner with some more helpful hints and information | ||
for hutch-python sessions. | ||
Startup script files to be run after ipython is initialized via: | ||
``c.InteractiveTerminalApp.exec_files`` | ||
Code here will take executed after both IPython has been loaded and | ||
the namespace has been populated with hutch objects. | ||
These will be run as standalone python files, and should not be | ||
imported from. | ||
""" | ||
|
||
from typing import List | ||
|
||
from hutch_python.env_version import get_env_info | ||
|
||
default_namespaces = ['a', 'm', 's', 'd', 'x', 'sim', 'camviewer', | ||
'bp', 're'] | ||
default_objects = ['RE', 'daq', 'elog', 'archive'] | ||
|
||
|
||
def gather_hint_table(namespace: List[str]) -> str: | ||
""" | ||
Gather variable name and short description into a table if the | ||
variable name is in the current global namespace | ||
""" | ||
global_ns = globals() | ||
ns = [x for x in namespace if x in global_ns] | ||
|
||
out = '' | ||
for k in ns: | ||
out += f" {k} - {getattr(global_ns[k], '_desc', 'N/A')}\n" | ||
|
||
return out | ||
|
||
|
||
base_banner = f""" | ||
----------------------------------- | ||
{get_env_info()} | ||
----------------------------------- | ||
Helpful Namespaces: | ||
{gather_hint_table(default_namespaces)} | ||
Useful objects: | ||
{gather_hint_table(default_objects)} | ||
""" | ||
|
||
|
||
if __name__ == '__main__': | ||
print(base_banner) |
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,21 @@ | ||
""" | ||
This file defines an ipython extension to configure the terminal app. | ||
Currently this configures: | ||
- disabling the Ctrl+\\ keybind | ||
""" | ||
|
||
import logging | ||
|
||
from prompt_toolkit.keys import Keys | ||
|
||
from .utils import safe_load | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def load_ipython_extension(ipython): | ||
# Unbind Ctrl+\\ | ||
with safe_load('disable ctrl+\\'): | ||
ipython.pt_app.key_bindings.remove( | ||
Keys.ControlBackslash | ||
) |
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,32 @@ | ||
""" | ||
Initialize the ELogPoster callback and subscribe it to the RunEngine | ||
Startup script files to be run after ipython is initialized via: | ||
``c.InteractiveTerminalApp.exec_files`` | ||
Code here will take executed after both IPython has been loaded and | ||
the namespace has been populated with hutch objects. | ||
These will be run as standalone python files, and should not be | ||
imported from. | ||
""" | ||
|
||
|
||
def _configure_elog_poster(): | ||
import IPython | ||
from nabs.callbacks import ELogPoster | ||
|
||
from hutch_python.utils import safe_load | ||
|
||
with safe_load('ELogPoster'): | ||
# RE, ELog will already exist by now | ||
elog = globals().get('elog', None) | ||
RE = globals().get('RE', None) | ||
assert elog is not None | ||
|
||
elogc = ELogPoster(elog, IPython.get_ipython()) | ||
RE.subscribe(elogc) | ||
|
||
|
||
if __name__ == '__main__': | ||
_configure_elog_poster() |