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
|
import os
import select
import sys
import traceback
from collections import defaultdict
from django.apps import apps
from django.core.management import BaseCommand, CommandError
from django.utils.datastructures import OrderedSet
class Command(BaseCommand):
help = (
"Runs a Python interactive interpreter. Tries to use IPython or "
"bpython, if one of them is available. Any standard input is executed "
"as code."
)
requires_system_checks = []
shells = ["ipython", "bpython", "python"]
def add_arguments(self, parser):
parser.add_argument(
"--no-startup",
action="store_true",
help=(
"When using plain Python, ignore the PYTHONSTARTUP environment "
"variable and ~/.pythonrc.py script."
),
)
parser.add_argument(
"--no-imports",
action="store_true",
help="Disable automatic imports of models.",
)
parser.add_argument(
"-i",
"--interface",
choices=self.shells,
help=(
"Specify an interactive interpreter interface. Available options: "
'"ipython", "bpython", and "python"'
),
)
parser.add_argument(
"-c",
"--command",
help=(
"Instead of opening an interactive shell, run a command as Django and "
"exit."
),
)
def ipython(self, options):
from IPython import start_ipython
start_ipython(
argv=[],
user_ns=self.get_and_report_namespace(
options["verbosity"], options["no_imports"]
),
)
def bpython(self, options):
import bpython
bpython.embed(
self.get_and_report_namespace(options["verbosity"], options["no_imports"])
)
def python(self, options):
import code
# Set up a dictionary to serve as the environment for the shell.
imported_objects = self.get_and_report_namespace(
options["verbosity"], options["no_imports"]
)
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
if not options["no_startup"]:
for pythonrc in OrderedSet(
[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.
try:
exec(compile(pythonrc_code, pythonrc, "exec"), imported_objects)
except Exception:
traceback.print_exc()
# By default, this will set up readline to do tab completion and to read and
# write history to the .python_history file, but this can be overridden by
# $PYTHONSTARTUP or ~/.pythonrc.py.
try:
hook = sys.__interactivehook__
except AttributeError:
# Match the behavior of the cpython shell where a missing
# sys.__interactivehook__ is ignored.
pass
else:
try:
hook()
except Exception:
# Match the behavior of the cpython shell where an error in
# sys.__interactivehook__ prints a warning and the exception
# and continues.
print("Failed calling sys.__interactivehook__")
traceback.print_exc()
# Set up tab completion for objects imported by $PYTHONSTARTUP or
# ~/.pythonrc.py.
try:
import readline
import rlcompleter
readline.set_completer(rlcompleter.Completer(imported_objects).complete)
except ImportError:
pass
# Start the interactive interpreter.
code.interact(local=imported_objects)
def get_and_report_namespace(self, verbosity, no_imports=False):
if no_imports:
return {}
namespace = self.get_namespace()
if verbosity < 1:
return namespace
amount = len(namespace)
msg = f"{amount} objects imported automatically"
if verbosity < 2:
self.stdout.write(f"{msg} (use -v 2 for details).", self.style.SUCCESS)
return namespace
imports_by_module = defaultdict(list)
for obj_name, obj in namespace.items():
if hasattr(obj, "__module__") and (
(hasattr(obj, "__qualname__") and obj.__qualname__.find(".") == -1)
or not hasattr(obj, "__qualname__")
):
imports_by_module[obj.__module__].append(obj_name)
if not hasattr(obj, "__module__") and hasattr(obj, "__name__"):
tokens = obj.__name__.split(".")
if obj_name in tokens:
module = ".".join(t for t in tokens if t != obj_name)
imports_by_module[module].append(obj_name)
import_string = "\n".join(
[
f" from {module} import {objects}"
for module, imported_objects in imports_by_module.items()
if (objects := ", ".join(imported_objects))
]
)
try:
import isort
except ImportError:
pass
else:
import_string = isort.code(import_string)
self.stdout.write(
f"{msg}, including:\n\n{import_string}", self.style.SUCCESS, ending="\n\n"
)
return namespace
def get_namespace(self):
apps_models = apps.get_models()
namespace = {}
for model in reversed(apps_models):
if model.__module__:
namespace[model.__name__] = model
return namespace
def handle(self, **options):
# Execute the command and exit.
if options["command"]:
exec(options["command"], {**globals(), **self.get_namespace()})
return
# Execute stdin if it has anything to read and exit.
# Not supported on Windows due to select.select() limitations.
if (
sys.platform != "win32"
and not sys.stdin.isatty()
and select.select([sys.stdin], [], [], 0)[0]
):
exec(sys.stdin.read(), {**globals(), **self.get_namespace()})
return
available_shells = (
[options["interface"]] if options["interface"] else self.shells
)
for shell in available_shells:
try:
return getattr(self, shell)(options)
except ImportError:
pass
raise CommandError("Couldn't import {} interface.".format(shell))
|