/
interactive.py
60 lines (47 loc) · 1.56 KB
/
interactive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""An interactive interpreter inside of a diesel event loop.
It's useful for importing and interacting with code that expects to run
inside of a diesel event loop. It works especially well for interactive
sessions with diesel's various network protocol clients.
Supports both the standard Python interactive interpreter and IPython (if
installed).
"""
import code
import sys
sys.path.insert(0, '.')
import diesel
try:
from IPython.Shell import IPShell
IPYTHON_AVAILABLE = True
except ImportError:
try:
# Support changes made in iPython 0.11
from IPython.frontend.terminal.ipapp import TerminalInteractiveShell as IPShell
IPYTHON_AVAILABLE = True
except ImportError:
IPYTHON_AVAILABLE = False
# Library Functions:
# ==================
def interact_python():
"""Runs an interactive interpreter; halts the diesel app when finished."""
globals_ = globals()
env = {
'__builtins__':globals_['__builtins__'],
'__doc__':globals_['__doc__'],
'__name__':globals_['__name__'],
'diesel':diesel,
}
code.interact(None, None, env)
diesel.quickstop()
def interact_ipython():
"""Starts an IPython instance; halts the diesel app when finished."""
IPShell(user_ns={'diesel':diesel}).mainloop()
diesel.quickstop()
# Interpreter entry points:
# =========================
def python():
diesel.quickstart(interact_python)
def ipython():
if not IPYTHON_AVAILABLE:
print >> sys.stderr, "IPython not found."
raise SystemExit(1)
diesel.quickstart(interact_ipython)