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 | make_option('--bpython', action='store_true', dest='bpython',
|
---|
10 | help='Tells Django to use bpython, not IPython.'),
|
---|
11 | )
|
---|
12 | help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available. If IPython is not available, tries to use bpython. "
|
---|
13 |
|
---|
14 | requires_model_validation = False
|
---|
15 |
|
---|
16 | def handle_noargs(self, **options):
|
---|
17 | # XXX: (Temporary) workaround for ticket #1796: force early loading of all
|
---|
18 | # models from installed apps.
|
---|
19 | from django.db.models.loading import get_models
|
---|
20 | loaded_models = get_models()
|
---|
21 |
|
---|
22 | use_plain = options.get('plain', False)
|
---|
23 | use_bpython = options.get('bpython',False)
|
---|
24 | try:
|
---|
25 | if use_plain or use_bpython:
|
---|
26 | if use_bpython:
|
---|
27 | use_plain = None
|
---|
28 | raise ImportError
|
---|
29 | import IPython
|
---|
30 | shell = IPython.Shell.IPShell(argv=[])
|
---|
31 | shell.mainloop()
|
---|
32 | except ImportError:
|
---|
33 | try:
|
---|
34 | if use_plain:
|
---|
35 | raise ImportError
|
---|
36 | import bpython
|
---|
37 | try:
|
---|
38 | bpython.embed()
|
---|
39 | except AttributeError:
|
---|
40 | if use_bpython:
|
---|
41 | raise AttributeError("Your version of bpython does not support embedding. Please upgrade to the latest version.")
|
---|
42 | else:
|
---|
43 | raise ImportError
|
---|
44 | except ImportError:
|
---|
45 | import code
|
---|
46 | # Set up a dictionary to serve as the environment for the shell, so
|
---|
47 | # that tab completion works on objects that are imported at runtime.
|
---|
48 | # See ticket 5082.
|
---|
49 | imported_objects = {}
|
---|
50 | try: # Try activating rlcompleter, because it's handy.
|
---|
51 | import readline
|
---|
52 | except ImportError:
|
---|
53 | pass
|
---|
54 | else:
|
---|
55 | # We don't have to wrap the following import in a 'try', because
|
---|
56 | # we already know 'readline' was imported successfully.
|
---|
57 | import rlcompleter
|
---|
58 | readline.set_completer(rlcompleter.Completer(imported_objects).complete)
|
---|
59 | readline.parse_and_bind("tab:complete")
|
---|
60 |
|
---|
61 | # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
|
---|
62 | # conventions and get $PYTHONSTARTUP first then import user.
|
---|
63 | if not use_plain:
|
---|
64 | pythonrc = os.environ.get("PYTHONSTARTUP")
|
---|
65 | if pythonrc and os.path.isfile(pythonrc):
|
---|
66 | try:
|
---|
67 | execfile(pythonrc)
|
---|
68 | except NameError:
|
---|
69 | pass
|
---|
70 | # This will import .pythonrc.py as a side-effect
|
---|
71 | import user
|
---|
72 | code.interact(local=imported_objects)
|
---|
73 |
|
---|