Merge pull request #215 from mgrouchy/add-interpreter-options

Adds interpreter option to shell command as per ticket #18639
This commit is contained in:
Alex Gaynor 2012-07-17 11:04:59 -07:00
commit 110c729309
2 changed files with 33 additions and 5 deletions

View File

@ -2,13 +2,19 @@ import os
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from optparse import make_option from optparse import make_option
class Command(NoArgsCommand): class Command(NoArgsCommand):
shells = ['ipython', 'bpython']
option_list = NoArgsCommand.option_list + ( option_list = NoArgsCommand.option_list + (
make_option('--plain', action='store_true', dest='plain', make_option('--plain', action='store_true', dest='plain',
help='Tells Django to use plain Python, not IPython or bpython.'), help='Tells Django to use plain Python, not IPython or bpython.'),
make_option('-i', '--interface', action='store', type='choice', choices=shells,
dest='interface',
help='Specify an interactive interpreter interface. Available options: "ipython" and "bpython"'),
) )
help = "Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available." help = "Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available."
shells = ['ipython', 'bpython']
requires_model_validation = False requires_model_validation = False
def ipython(self): def ipython(self):
@ -31,8 +37,10 @@ class Command(NoArgsCommand):
import bpython import bpython
bpython.embed() bpython.embed()
def run_shell(self): def run_shell(self, shell=None):
for shell in self.shells: available_shells = [shell] if shell else self.shells
for shell in available_shells:
try: try:
return getattr(self, shell)() return getattr(self, shell)()
except ImportError: except ImportError:
@ -46,19 +54,21 @@ class Command(NoArgsCommand):
get_models() get_models()
use_plain = options.get('plain', False) use_plain = options.get('plain', False)
interface = options.get('interface', None)
try: try:
if use_plain: if use_plain:
# Don't bother loading IPython, because the user wants plain Python. # Don't bother loading IPython, because the user wants plain Python.
raise ImportError raise ImportError
self.run_shell()
self.run_shell(shell=interface)
except ImportError: except ImportError:
import code import code
# Set up a dictionary to serve as the environment for the shell, so # Set up a dictionary to serve as the environment for the shell, so
# that tab completion works on objects that are imported at runtime. # that tab completion works on objects that are imported at runtime.
# See ticket 5082. # See ticket 5082.
imported_objects = {} imported_objects = {}
try: # Try activating rlcompleter, because it's handy. try: # Try activating rlcompleter, because it's handy.
import readline import readline
except ImportError: except ImportError:
pass pass

View File

@ -743,6 +743,24 @@ use the ``--plain`` option, like so::
django-admin.py shell --plain django-admin.py shell --plain
.. versionchanged:: 1.5
If you would like to specify either IPython or bpython as your interpreter if
you have both installed you can specify an alternative interpreter interface
with the ``-i`` or ``--interface`` options like so::
IPython::
django-admin.py shell -i ipython
django-admin.py shell --interface ipython
bpython::
django-admin.py shell -i bpython
django-admin.py shell --interface bpython
.. _IPython: http://ipython.scipy.org/ .. _IPython: http://ipython.scipy.org/
.. _bpython: http://bpython-interpreter.org/ .. _bpython: http://bpython-interpreter.org/