-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathconsole.py
More file actions
72 lines (54 loc) · 1.88 KB
/
console.py
File metadata and controls
72 lines (54 loc) · 1.88 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
# Copyright 2018 ACSONE SA/NV (<http://acsone.eu>)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
import code
import logging
import os
_logger = logging.getLogger(__name__)
class Shell:
shells = ["ipython", "ptpython", "bpython", "python"]
@classmethod
def python(cls, local_vars):
if os.getenv("PYTHONSTARTUP"):
# If PYTHONSTARTUP is defined, run it.
startup_file = os.getenv("PYTHONSTARTUP")
if os.path.isfile(startup_file):
with open(startup_file) as f:
startup = f.read()
exec(startup, local_vars)
console = code.InteractiveConsole(locals=local_vars)
import readline
import rlcompleter # noqa
readline.parse_and_bind("tab: complete")
console.interact()
@classmethod
def ipython(cls, local_vars):
from IPython import start_ipython
start_ipython(argv=[], user_ns=local_vars)
@classmethod
def ptpython(cls, local_vars):
from ptpython.repl import embed
embed({}, local_vars)
@classmethod
def bpython(cls, local_vars):
from bpython import embed
embed(local_vars)
@classmethod
def interact(cls, local_vars, preferred_shell=None):
if preferred_shell:
shells_to_try = [preferred_shell] + Shell.shells
else:
shells_to_try = Shell.shells
for shell in shells_to_try:
try:
return getattr(cls, shell)(local_vars)
except ImportError:
pass
except Exception:
_logger.error("Could not start '%s' shell.", shell)
_logger.debug("Shell error:", exc_info=True)
_logger.error("Could not start any shell.")
def _isatty(stream):
try:
return os.isatty(stream.fileno())
except Exception:
return False