def construct(self): # I am a little hesitant to put these into InteractiveShell itself. # But that might be the place for them sys.path.insert(0, '') # Create an InteractiveShell instance self.shell = InteractiveShell( parent=None, config=self.master_config )
def __init__(self, ipython0=None, *args, **kwargs): """ Parameters ---------- ipython0: an optional ipython0 instance to use for command prefiltering and completion. """ LineFrontEndBase.__init__(self, *args, **kwargs) self.shell.output_trap = RedirectorOutputTrap( out_callback=self.write, err_callback=self.write, ) self.shell.traceback_trap = SyncTracebackTrap( formatters=self.shell.traceback_trap.formatters, ) # Start the ipython0 instance: self.save_output_hooks() if ipython0 is None: # Instanciate an IPython0 InteractiveShell to be able to use the # prefiltering. # Suppress all key input, to avoid waiting def my_rawinput(x=None): return '\n' old_rawinput = __builtin__.raw_input __builtin__.raw_input = my_rawinput ipython0 = InteractiveShell( parent=None, user_ns=self.shell.user_ns, user_global_ns=self.shell.user_global_ns) __builtin__.raw_input = old_rawinput self.ipython0 = ipython0 # Set the pager: self.ipython0.set_hook('show_in_pager', lambda s, string: self.write("\n" + string)) self.ipython0.write = self.write self._ip = _ip = self.ipython0 # Make sure the raw system call doesn't get called, as we don't # have a stdin accessible. self._ip.system = self.system_call # XXX: Muck around with magics so that they work better # in our environment if not sys.platform.startswith('win'): self.ipython0.magic_ls = mk_system_call(self.system_call, 'ls -CF') # And now clean up the mess created by ipython0 self.release_output() if not 'banner' in kwargs and self.banner is None: self.banner = self.ipython0.banner # FIXME: __init__ and start should be two different steps self.start()
class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option('--ipython', action='store_true', dest='ipython', help='Tells Django to use IPython, not BPython.'), make_option( '--plain', action='store_true', dest='plain', help='Tells Django to use plain Python, not BPython nor IPython.'), make_option('--no-pythonrc', action='store_true', dest='no_pythonrc', help='Tells Django to use plain Python, not IPython.'), make_option('--print-sql', action='store_true', default=False, help="Print SQL queries as they're executed"), ) help = "Like the 'shell' command but autoloads the models of all installed Django apps." requires_model_validation = True def handle_noargs(self, **options): # XXX: (Temporary) workaround for ticket #1796: force early loading of all # models from installed apps. (this is fixed by now, but leaving it here # for people using 0.96 or older trunk (pre [5919]) versions. from django.db.models.loading import get_models, get_apps loaded_models = get_models() use_ipython = options.get('ipython', False) use_plain = options.get('plain', False) use_pythonrc = not options.get('no_pythonrc', True) if options.get("print_sql", False): # Code from http://gist.github.com/118990 from django.db.backends import util try: import sqlparse except ImportError: sqlparse = None class PrintQueryWrapper(util.CursorDebugWrapper): def execute(self, sql, params=()): try: return self.cursor.execute(sql, params) finally: raw_sql = self.db.ops.last_executed_query( self.cursor, sql, params) if sqlparse: print sqlparse.format(raw_sql, reindent=True) else: print raw_sql print util.CursorDebugWrapper = PrintQueryWrapper # Set up a dictionary to serve as the environment for the shell, so # that tab completion works on objects that are imported at runtime. # See ticket 5082. from django.conf import settings imported_objects = {'settings': settings} for app_mod in get_apps(): app_models = get_models(app_mod) if not app_models: continue model_labels = ", ".join([model.__name__ for model in app_models]) print self.style.SQL_COLTYPE( "From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], model_labels)) for model in app_models: try: imported_objects[model.__name__] = getattr( __import__(app_mod.__name__, {}, {}, model.__name__), model.__name__) except AttributeError, e: print self.style.ERROR( "Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_mod.__name__.split('.')[-2], str(e))) continue try: if use_plain: # Don't bother loading B/IPython, because the user wants plain Python. raise ImportError try: if use_ipython: # User wants IPython raise ImportError from bpython import embed embed(imported_objects) except ImportError: # Explicitly pass an empty list as arguments, because otherwise IPython # would use sys.argv from this script. try: from IPython.core.iplib import InteractiveShell shell = InteractiveShell(user_ns=imported_objects) except ImportError: import IPython shell = IPython.Shell.IPShell(argv=[], user_ns=imported_objects) shell.mainloop() except ImportError: # Using normal Python shell import code try: # Try activating rlcompleter, because it's handy. import readline except ImportError: pass else: # We don't have to wrap the following import in a 'try', because # we already know 'readline' was imported successfully. import rlcompleter readline.set_completer( rlcompleter.Completer(imported_objects).complete) readline.parse_and_bind("tab:complete") # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system # conventions and get $PYTHONSTARTUP first then import user. if use_pythonrc: pythonrc = os.environ.get("PYTHONSTARTUP") if pythonrc and os.path.isfile(pythonrc): try: execfile(pythonrc) except NameError: pass # This will import .pythonrc.py as a side-effect import user code.interact(local=imported_objects)