|
0
|
1 |
import os |
|
|
2 |
from django.core.management.base import NoArgsCommand |
|
|
3 |
from optparse import make_option |
|
|
4 |
|
|
|
5 |
class Command(NoArgsCommand): |
|
|
6 |
option_list = NoArgsCommand.option_list + ( |
|
|
7 |
make_option('--plain', action='store_true', dest='plain', |
|
|
8 |
help='Tells Django to use plain Python, not IPython.'), |
|
|
9 |
) |
|
|
10 |
help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available." |
|
|
11 |
|
|
|
12 |
requires_model_validation = False |
|
|
13 |
|
|
|
14 |
def handle_noargs(self, **options): |
|
|
15 |
# XXX: (Temporary) workaround for ticket #1796: force early loading of all |
|
|
16 |
# models from installed apps. |
|
|
17 |
from django.db.models.loading import get_models |
|
|
18 |
loaded_models = get_models() |
|
|
19 |
|
|
|
20 |
use_plain = options.get('plain', False) |
|
|
21 |
|
|
|
22 |
try: |
|
|
23 |
if use_plain: |
|
|
24 |
# Don't bother loading IPython, because the user wants plain Python. |
|
|
25 |
raise ImportError |
|
|
26 |
import IPython |
|
|
27 |
# Explicitly pass an empty list as arguments, because otherwise IPython |
|
|
28 |
# would use sys.argv from this script. |
|
|
29 |
shell = IPython.Shell.IPShell(argv=[]) |
|
|
30 |
shell.mainloop() |
|
|
31 |
except ImportError: |
|
|
32 |
import code |
|
|
33 |
# Set up a dictionary to serve as the environment for the shell, so |
|
|
34 |
# that tab completion works on objects that are imported at runtime. |
|
|
35 |
# See ticket 5082. |
|
|
36 |
imported_objects = {} |
|
|
37 |
try: # Try activating rlcompleter, because it's handy. |
|
|
38 |
import readline |
|
|
39 |
except ImportError: |
|
|
40 |
pass |
|
|
41 |
else: |
|
|
42 |
# We don't have to wrap the following import in a 'try', because |
|
|
43 |
# we already know 'readline' was imported successfully. |
|
|
44 |
import rlcompleter |
|
|
45 |
readline.set_completer(rlcompleter.Completer(imported_objects).complete) |
|
|
46 |
readline.parse_and_bind("tab:complete") |
|
|
47 |
|
|
|
48 |
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system |
|
|
49 |
# conventions and get $PYTHONSTARTUP first then import user. |
|
|
50 |
if not use_plain: |
|
|
51 |
pythonrc = os.environ.get("PYTHONSTARTUP") |
|
|
52 |
if pythonrc and os.path.isfile(pythonrc): |
|
|
53 |
try: |
|
|
54 |
execfile(pythonrc) |
|
|
55 |
except NameError: |
|
|
56 |
pass |
|
|
57 |
# This will import .pythonrc.py as a side-effect |
|
|
58 |
import user |
|
|
59 |
code.interact(local=imported_objects) |