Ticket #11542: shell.py

File shell.py, 3.6 KB (added by Seamus, 15 years ago)

Improvements to the manage.py shell command. (BUGFIXES; IMPROVED COMMENTING; USE THIS ATTACHMENT)

Line 
1import os
2from django.core.management.base import NoArgsCommand
3from optparse import make_option
4
5class 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 # Older versions of bpython do not support embed(). When
38 # specifying the --bpython flag, an attributeError is raised
39 # telling the user to upgrade. If --bpython is not specified
40 # fall back to the plain interpreter.
41 try:
42 bpython.embed()
43 except AttributeError:
44 if use_bpython:
45 raise AttributeError("Your version of bpython does not support embedding. Please upgrade to the latest version.")
46 else:
47 raise ImportError
48 except ImportError:
49 # When specifying the --bpython flag and the import fails,
50 # do not fall back to the plain interpreter.
51 if use_bpython:
52 raise ImportError("You do not have bpython installed")
53 import code
54 # Set up a dictionary to serve as the environment for the shell, so
55 # that tab completion works on objects that are imported at runtime.
56 # See ticket 5082.
57 imported_objects = {}
58 try: # Try activating rlcompleter, because it's handy.
59 import readline
60 except ImportError:
61 pass
62 else:
63 # We don't have to wrap the following import in a 'try', because
64 # we already know 'readline' was imported successfully.
65 import rlcompleter
66 readline.set_completer(rlcompleter.Completer(imported_objects).complete)
67 readline.parse_and_bind("tab:complete")
68
69 # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
70 # conventions and get $PYTHONSTARTUP first then import user.
71 if not use_plain:
72 pythonrc = os.environ.get("PYTHONSTARTUP")
73 if pythonrc and os.path.isfile(pythonrc):
74 try:
75 execfile(pythonrc)
76 except NameError:
77 pass
78 # This will import .pythonrc.py as a side-effect
79 import user
80 code.interact(local=imported_objects)
81
Back to Top