-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
shell.py
72 lines (50 loc) · 1.7 KB
/
shell.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
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (C) 2017-2024 Vanessa Sochat.
# This Source Code Form is subject to the terms of the
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
def main(args, options, parser):
# If we have options, first is image
image = None
if options:
image = options.pop(0)
lookup = {"ipython": ipython, "python": python, "bpython": run_bpython}
shells = ["ipython", "python", "bpython"]
# Otherwise present order of liklihood to have on system
for shell in shells:
try:
return lookup[shell](image)
except ImportError:
pass
def prepare_client(image):
"""prepare a client to embed in a shell with recipe parsers and writers."""
# The client will announce itself (backend/database) unless it's get
from spython.main import get_client
from spython.main.parse import parsers, writers
client = get_client()
if image:
client.load(image)
# Add recipe parsers
client.parsers = parsers
client.writers = writers
return client
def ipython(image):
"""give the user an ipython shell"""
client = prepare_client(image) # noqa
try:
from IPython import embed
except ImportError:
return python(image)
embed()
def run_bpython(image):
"""give the user a bpython shell"""
client = prepare_client(image)
try:
import bpython
except ImportError:
return python(image)
bpython.embed(locals_={"client": client})
def python(image):
"""give the user a python shell"""
import code
client = prepare_client(image)
code.interact(local={"client": client})