-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathshell.py
More file actions
241 lines (188 loc) · 6.72 KB
/
shell.py
File metadata and controls
241 lines (188 loc) · 6.72 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""Utility and helper methods for tmuxp.
tmuxp.shell
~~~~~~~~~~~
"""
import logging
import os
logger = logging.getLogger(__name__)
def has_ipython():
try:
from IPython import start_ipython # NOQA F841
except ImportError:
try:
from IPython.Shell import IPShell # NOQA F841
except ImportError:
return False
return True
def has_ptpython():
try:
from ptpython.repl import embed, run_config # NOQA F841
except ImportError:
try:
from prompt_toolkit.contrib.repl import embed, run_config # NOQA F841
except ImportError:
return False
return True
def has_ptipython():
try:
from ptpython.ipython import embed # NOQA F841
from ptpython.repl import run_config # NOQA F841
except ImportError:
try:
from prompt_toolkit.contrib.ipython import embed # NOQA F841
from prompt_toolkit.contrib.repl import run_config # NOQA F841
except ImportError:
return False
return True
def has_bpython():
try:
from bpython import embed # NOQA F841
except ImportError:
return False
return True
def detect_best_shell():
if has_ptipython():
return "ptipython"
elif has_ptpython():
return "ptpython"
elif has_ipython():
return "ipython"
elif has_bpython():
return "bpython"
return "code"
def get_bpython(options, extra_args=None):
if extra_args is None:
extra_args = {}
from bpython import embed # NOQA F841
def launch_bpython():
imported_objects = get_launch_args(**options)
kwargs = {}
if extra_args:
kwargs["args"] = extra_args
embed(imported_objects, **kwargs)
return launch_bpython
def get_ipython_arguments():
ipython_args = "IPYTHON_ARGUMENTS"
return os.environ.get(ipython_args, "").split()
def get_ipython(options, **extra_args):
try:
from IPython import start_ipython
def launch_ipython():
imported_objects = get_launch_args(**options)
ipython_arguments = extra_args or get_ipython_arguments()
start_ipython(argv=ipython_arguments, user_ns=imported_objects)
return launch_ipython
except ImportError:
# IPython < 0.11
# Explicitly pass an empty list as arguments, because otherwise
# IPython would use sys.argv from this script.
# Notebook not supported for IPython < 0.11.
from IPython.Shell import IPShell
def launch_ipython():
imported_objects = get_launch_args(**options)
shell = IPShell(argv=[], user_ns=imported_objects)
shell.mainloop()
return launch_ipython
def get_ptpython(options, vi_mode=False):
try:
from ptpython.repl import embed, run_config
except ImportError:
from prompt_toolkit.contrib.repl import embed, run_config
def launch_ptpython():
imported_objects = get_launch_args(**options)
history_filename = os.path.expanduser("~/.ptpython_history")
embed(
globals=imported_objects,
history_filename=history_filename,
vi_mode=vi_mode,
configure=run_config,
)
return launch_ptpython
def get_ptipython(options, vi_mode=False):
"""Based on django-extensions
Run renamed to launch, get_imported_objects renamed to get_launch_args
"""
try:
from ptpython.ipython import embed
from ptpython.repl import run_config
except ImportError:
# prompt_toolkit < v0.27
from prompt_toolkit.contrib.ipython import embed
from prompt_toolkit.contrib.repl import run_config
def launch_ptipython():
imported_objects = get_launch_args(**options)
history_filename = os.path.expanduser("~/.ptpython_history")
embed(
user_ns=imported_objects,
history_filename=history_filename,
vi_mode=vi_mode,
configure=run_config,
)
return launch_ptipython
def get_launch_args(**kwargs):
import libtmux
return {
"libtmux": libtmux,
"Server": libtmux.Server,
"Session": libtmux.Session,
"Window": libtmux.Window,
"Pane": libtmux.Pane,
"server": kwargs.get("server"),
"session": kwargs.get("session"),
"window": kwargs.get("window"),
"pane": kwargs.get("pane"),
}
def get_code(use_pythonrc, imported_objects):
import code
try:
# Try activating rlcompleter, because it's handy.
import readline
except ImportError:
pass
else:
# We don't have to wrap the following import in a 'try', because
# we already know 'readline' was imported successfully.
import rlcompleter
readline.set_completer(rlcompleter.Completer(imported_objects).complete)
# Enable tab completion on systems using libedit (e.g. macOS).
# These lines are copied from Lib/site.py on Python 3.4.
readline_doc = getattr(readline, "__doc__", "")
if readline_doc is not None and "libedit" in readline_doc:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab:complete")
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
if use_pythonrc:
for pythonrc in {
os.environ.get("PYTHONSTARTUP"),
os.path.expanduser("~/.pythonrc.py"),
}:
if not pythonrc:
continue
if not os.path.isfile(pythonrc):
continue
with open(pythonrc) as handle:
pythonrc_code = handle.read()
# Match the behavior of the cpython shell where an error in
# PYTHONSTARTUP prints an exception and continues.
exec(compile(pythonrc_code, pythonrc, "exec"), imported_objects)
def launch_code():
code.interact(local=imported_objects)
return launch_code
def launch(shell="best", use_pythonrc=False, use_vi_mode=False, **kwargs):
# Also allowing passing shell='code' to force using code.interact
imported_objects = get_launch_args(**kwargs)
if shell == "best":
shell = detect_best_shell()
if shell == "ptipython":
launch = get_ptipython(options=kwargs, vi_mode=use_vi_mode)
elif shell == "ptpython":
launch = get_ptpython(options=kwargs, vi_mode=use_vi_mode)
elif shell == "ipython":
launch = get_ipython(options=kwargs)
elif shell == "bpython":
launch = get_bpython(options=kwargs)
else:
launch = get_code(use_pythonrc=use_pythonrc, imported_objects=imported_objects)
launch()