-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathshell.py
More file actions
322 lines (262 loc) · 9.46 KB
/
shell.py
File metadata and controls
322 lines (262 loc) · 9.46 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# mypy: allow-untyped-calls
"""Utility and helper methods for tmuxp."""
from __future__ import annotations
import logging
import os
import pathlib
import typing as t
logger = logging.getLogger(__name__)
if t.TYPE_CHECKING:
from collections.abc import Callable
from types import ModuleType
from typing import TypeAlias
from libtmux.pane import Pane
from libtmux.server import Server
from libtmux.session import Session
from libtmux.window import Window
from typing_extensions import NotRequired, TypedDict, Unpack
CLIShellLiteral: TypeAlias = t.Literal[
"best",
"pdb",
"code",
"ptipython",
"ptpython",
"ipython",
"bpython",
]
class LaunchOptionalImports(TypedDict):
"""tmuxp shell optional imports."""
server: NotRequired[Server]
session: NotRequired[Session]
window: NotRequired[Window]
pane: NotRequired[Pane]
class LaunchImports(t.TypedDict):
"""tmuxp shell launch import mapping."""
libtmux: ModuleType
Server: type[Server]
Session: type[Session]
Window: type[Window]
Pane: type[Pane]
server: Server | None
session: Session | None
window: Window | None
pane: Pane | None
def has_ipython() -> bool:
"""Return True if ipython is installed."""
try:
from IPython import start_ipython # NOQA: F401
except ImportError:
try:
from IPython.Shell import IPShell # NOQA: F401
except ImportError:
return False
return True
def has_ptpython() -> bool:
"""Return True if ptpython is installed."""
try:
from ptpython.repl import embed, run_config # F401
except ImportError:
try:
from prompt_toolkit.contrib.repl import embed, run_config # NOQA: F401
except ImportError:
return False
return True
def has_ptipython() -> bool:
"""Return True if ptpython + ipython are both installed."""
try:
from ptpython.ipython import embed # F401
from ptpython.repl import run_config # F401
except ImportError:
try:
from prompt_toolkit.contrib.ipython import embed # NOQA: F401
from prompt_toolkit.contrib.repl import run_config # NOQA: F401
except ImportError:
return False
return True
def has_bpython() -> bool:
"""Return True if bpython is installed."""
try:
from bpython import embed # NOQA: F401
except ImportError:
return False
return True
def detect_best_shell() -> CLIShellLiteral:
"""Return the best, most feature-rich shell available."""
if has_ptipython():
shell: CLIShellLiteral = "ptipython"
elif has_ptpython():
shell = "ptpython"
elif has_ipython():
shell = "ipython"
elif has_bpython():
shell = "bpython"
else:
shell = "code"
logger.debug("detected shell: %s", shell)
return shell
def get_bpython(
options: LaunchOptionalImports,
extra_args: dict[str, t.Any] | None = None,
) -> Callable[[], None]:
"""Return bpython shell."""
if extra_args is None:
extra_args = {}
from bpython import embed
def launch_bpython() -> None:
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() -> list[str]:
"""Return ipython shell args via ``IPYTHON_ARGUMENTS`` environment variables."""
ipython_args = "IPYTHON_ARGUMENTS"
return os.environ.get(ipython_args, "").split()
def get_ipython(
options: LaunchOptionalImports,
**extra_args: dict[str, t.Any],
) -> t.Any:
"""Return ipython shell."""
try:
from IPython import start_ipython
def launch_ipython() -> None:
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 # NOQA: TRY300
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() -> None:
imported_objects = get_launch_args(**options)
shell = IPShell(argv=[], user_ns=imported_objects)
shell.mainloop()
return launch_ipython
def get_ptpython(options: LaunchOptionalImports, vi_mode: bool = False) -> t.Any:
"""Return ptpython shell."""
try:
from ptpython.repl import embed, run_config
except ImportError:
from prompt_toolkit.contrib.repl import embed, run_config
def launch_ptpython() -> None:
imported_objects = get_launch_args(**options)
history_filename = str(pathlib.Path("~/.ptpython_history").expanduser())
embed(
globals=imported_objects,
history_filename=history_filename,
vi_mode=vi_mode,
configure=run_config,
)
return launch_ptpython
def get_ptipython(options: LaunchOptionalImports, vi_mode: bool = False) -> t.Any:
"""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() -> None:
imported_objects = get_launch_args(**options)
history_filename = str(pathlib.Path("~/.ptpython_history").expanduser())
embed(
user_ns=imported_objects,
history_filename=history_filename,
vi_mode=vi_mode,
configure=run_config,
)
return launch_ptipython
def get_launch_args(**kwargs: Unpack[LaunchOptionalImports]) -> LaunchImports:
"""Return tmuxp shell launch arguments, counting for overrides."""
import libtmux
from libtmux.pane import Pane
from libtmux.server import Server
from libtmux.session import Session
from libtmux.window import Window
return {
"libtmux": libtmux,
"Server": Server,
"Session": Session,
"Window": Window,
"Pane": Pane,
"server": kwargs.get("server"),
"session": kwargs.get("session"),
"window": kwargs.get("window"),
"pane": kwargs.get("pane"),
}
def get_code(use_pythonrc: bool, imported_objects: LaunchImports) -> t.Any:
"""Launch basic python shell via :mod:`code`."""
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, # type:ignore
).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:
PYTHONSTARTUP = os.environ.get("PYTHONSTARTUP")
for pythonrc in {
*([pathlib.Path(PYTHONSTARTUP)] if PYTHONSTARTUP is not None else []),
pathlib.Path("~/.pythonrc.py").expanduser(),
}:
if not pythonrc:
continue
if not pythonrc.is_file():
continue
with pythonrc.open() 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, # type:ignore
)
def launch_code() -> None:
code.interact(local=imported_objects) # type:ignore
return launch_code
def launch(
shell: CLIShellLiteral | None = "best",
use_pythonrc: bool = False,
use_vi_mode: bool = False,
**kwargs: Unpack[LaunchOptionalImports],
) -> None:
"""Launch interactive libtmux shell for tmuxp shell."""
# 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()