Created
February 10, 2011 19:59
-
-
Save EBNull/821215 to your computer and use it in GitHub Desktop.
Version of Python's code.interact() that takes a `globals` argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import code, sys | |
from code import softspace | |
class GlobalInteractiveConsole(code.InteractiveConsole): | |
"""Interactive console that also takes a globals argument""" | |
def __init__(self, locals=None, filename="<console>", globals=None): | |
try: | |
super(GlobalInteractiveConsole, self).__init__(locals=locals, filename=filename) | |
except TypeError: | |
code.InteractiveConsole.__init__(self, locals=locals, filename=filename) | |
if globals is None: | |
globals = {} | |
self.globals = globals | |
def runcode(self, code): | |
try: | |
exec code in self.globals, self.locals | |
except SystemExit: | |
raise | |
except: | |
self.showtraceback() | |
else: | |
if softspace(sys.stdout, 0): | |
class DjangoGlobalInteractiveConsole(GlobalInteractiveConsole): | |
"""As above, but with auto-loading django modules. Mostly copied from http://code.google.com/p/django-command-extensions/source/browse/trunk/django_extensions/management/commands/shell_plus.py""" | |
def __init__(self, locals=None, filename="<console>", globals=None, noisy=False): | |
try: | |
super(DjangoGlobalInteractiveConsole, self).__init__(locals=locals, filename=filename, globals=globals) | |
except TypeError: | |
GlobalInteractiveConsole.__init__(self, locals=locals, filename=filename, globals=globals) | |
from django.db.models.loading import get_models, get_apps | |
loaded_models = get_models() | |
imported_objects = {} | |
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]) | |
if noisy: | |
print "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 "Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_mod.__name__.split('.')[-2], str(e)) | |
continue | |
self.locals = dict(imported_objects, **self.locals) | |
try: # Try activating rlcompleter, because it's handy. | |
import readline | |
except ImportError: | |
pass | |
else: | |
import rlcompleter | |
readline.set_completer(rlcompleter.Completer(self.locals).complete) | |
readline.parse_and_bind("tab:complete") | |
def globalinteract(banner=None, readfunc=None, local=None, globals=None, locals=None): | |
if locals is not None and local is not None: | |
raise ValueError("local and locals are synonymous; use one or the other") | |
if local is None and locals is not None: | |
local = locals | |
try: | |
from django.conf import settings | |
klass = DjangoGlobalInteractiveConsole | |
except Exception: | |
klass = GlobalInteractiveConsole | |
console = klass(local, globals=globals) | |
if readfunc is not None: | |
console.raw_input = readfunc | |
else: | |
try: | |
import readline | |
except ImportError: | |
pass | |
console.interact(banner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import code, sys | |
from code import softspace | |
class GlobalInteractiveConsole(code.InteractiveConsole): | |
"""Interactive console that also takes a globals argument""" | |
def __init__(self, locals=None, filename="<console>", globals=None): | |
try: | |
super(GlobalInteractiveConsole, self).__init__(locals=locals, filename=filename) | |
except TypeError: | |
code.InteractiveConsole.__init__(self, locals=locals, filename=filename) | |
if globals is None: | |
globals = {} | |
self.globals = globals | |
def runcode(self, code): | |
try: | |
exec code in self.globals, self.locals | |
except SystemExit: | |
raise | |
except: | |
self.showtraceback() | |
else: | |
if softspace(sys.stdout, 0): | |
def globalinteract(banner=None, readfunc=None, local=None, globals=None, locals=None): | |
if locals is not None and local is not None: | |
raise ValueError("local and locals are synonymous; use one or the other") | |
if local is None and locals is not None: | |
local = locals | |
console = GlobalInteractiveConsole(local, globals=globals) | |
if readfunc is not None: | |
console.raw_input = readfunc | |
else: | |
try: | |
import readline | |
except ImportError: | |
pass | |
console.interact(banner) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment