def hijack_wx(): """Modifies wxPython's MainLoop with a dummy so user code does not block IPython. The hijacked mainloop function is returned. """ def dummy_mainloop(*args, **kw): pass try: import wx except ImportError: # For very old versions of WX import wxPython as wx ver = wx.__version__ orig_mainloop = None if ver[:3] >= '2.5': import wx if hasattr(wx, '_core_'): core = getattr(wx, '_core_') elif hasattr(wx, '_core'): core = getattr(wx, '_core') else: raise AttributeError('Could not find wx core module') orig_mainloop = core.PyApp_MainLoop core.PyApp_MainLoop = dummy_mainloop elif ver[:3] == '2.4': orig_mainloop = wx.wxc.wxPyApp_MainLoop wx.wxc.wxPyApp_MainLoop = dummy_mainloop else: warn("Unable to find either wxPython version 2.4 or >= 2.5.") return orig_mainloop
def __init__(self, argv=None, user_ns=None, user_global_ns=None, debug=0, shell_class=MTInteractiveShell): from PyQt4 import QtCore, QtGui try: # present in PyQt4-4.2.1 or later QtCore.pyqtRemoveInputHook() except AttributeError: pass if QtCore.PYQT_VERSION_STR == '4.3': warn('''PyQt4 version 4.3 detected. If you experience repeated threading warnings, please update PyQt4. ''') self.exec_ = hijack_qt4() # Allows us to use both Tk and QT. self.tk = get_tk() self.IP = make_IPython(argv, user_ns=user_ns, user_global_ns=user_global_ns, debug=debug, shell_class=shell_class, on_kill=[QtGui.qApp.exit]) # HACK: slot for banner in self; it will be passed to the mainloop # method only and .run() needs it. The actual value will be set by # .mainloop(). self._banner = None threading.Thread.__init__(self)
def _dummy_warn(self, *args, **kw): """Dummy function, which doesn't do anything but warn.""" warn("IPython is not running, this is a dummy no-op function")
def magic_history(self, parameter_s=''): """Print input history (_i<n> variables), with most recent last. %history -> print at most 40 inputs (some may be multi-line)\\ %history n -> print at most n inputs\\ %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ Each input's number <n> is shown, and is accessible as the automatically generated variable _i<n>. Multi-line statements are printed starting at a new line for easy copy/paste. Options: -n: do NOT print line numbers. This is useful if you want to get a printout of many lines which can be directly pasted into a text editor. This feature is only available if numbered prompts are in use. -t: (default) print the 'translated' history, as IPython understands it. IPython filters your input and converts it all into valid Python source before executing it (things like magics or aliases are turned into function calls, for example). With this option, you'll see the native history instead of the user-entered version: '%cd /' will be seen as '_ip.magic("%cd /")' instead of '%cd /'. -r: print the 'raw' history, i.e. the actual commands you typed. -g: treat the arg as a pattern to grep for in (full) history. This includes the "shadow history" (almost all commands ever written). Use '%hist -g' to show full shadow history (may be very long). In shadow history, every index nuwber starts with 0. -f FILENAME: instead of printing the output to the screen, redirect it to the given file. The file is always overwritten, though IPython asks for confirmation first if it already exists. """ ip = self.api shell = self.shell if not shell.outputcache.do_full_cache: print 'This feature is only available if numbered prompts are in use.' return opts, args = self.parse_options(parameter_s, 'gntsrf:', mode='list') # Check if output to specific file was requested. try: outfname = opts['f'] except KeyError: outfile = Term.cout # default # We don't want to close stdout at the end! close_at_end = False else: if os.path.exists(outfname): if not ask_yes_no("File %r exists. Overwrite?" % outfname): print 'Aborting.' return outfile = open(outfname, 'w') close_at_end = True if 't' in opts: input_hist = shell.input_hist elif 'r' in opts: input_hist = shell.input_hist_raw else: input_hist = shell.input_hist default_length = 40 pattern = None if 'g' in opts: init = 1 final = len(input_hist) parts = parameter_s.split(None, 1) if len(parts) == 1: parts += '*' head, pattern = parts pattern = "*" + pattern + "*" elif len(args) == 0: final = len(input_hist) init = max(1, final - default_length) elif len(args) == 1: final = len(input_hist) init = max(1, final - int(args[0])) elif len(args) == 2: init, final = map(int, args) else: warn('%hist takes 0, 1 or 2 arguments separated by spaces.') print self.magic_hist.__doc__ return width = len(str(final)) line_sep = ['', '\n'] print_nums = not opts.has_key('n') found = False if pattern is not None: sh = ip.IP.shadowhist.all() for idx, s in sh: if fnmatch.fnmatch(s, pattern): print "0%d: %s" % (idx, s) found = True if found: print "===" print "shadow history ends, fetch by %rep <number> (must start with 0)" print "=== start of normal history ===" for in_num in range(init, final): inline = input_hist[in_num] if pattern is not None and not fnmatch.fnmatch(inline, pattern): continue multiline = int(inline.count('\n') > 1) if print_nums: print >> outfile, \ '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), print >> outfile, inline, if close_at_end: outfile.close()
def magic_history(self, parameter_s = ''): """Print input history (_i<n> variables), with most recent last. %history -> print at most 40 inputs (some may be multi-line)\\ %history n -> print at most n inputs\\ %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ Each input's number <n> is shown, and is accessible as the automatically generated variable _i<n>. Multi-line statements are printed starting at a new line for easy copy/paste. Options: -n: do NOT print line numbers. This is useful if you want to get a printout of many lines which can be directly pasted into a text editor. This feature is only available if numbered prompts are in use. -t: (default) print the 'translated' history, as IPython understands it. IPython filters your input and converts it all into valid Python source before executing it (things like magics or aliases are turned into function calls, for example). With this option, you'll see the native history instead of the user-entered version: '%cd /' will be seen as '_ip.magic("%cd /")' instead of '%cd /'. -r: print the 'raw' history, i.e. the actual commands you typed. -g: treat the arg as a pattern to grep for in (full) history. This includes the "shadow history" (almost all commands ever written). Use '%hist -g' to show full shadow history (may be very long). In shadow history, every index nuwber starts with 0. -f FILENAME: instead of printing the output to the screen, redirect it to the given file. The file is always overwritten, though IPython asks for confirmation first if it already exists. """ ip = self.api shell = self.shell if not shell.outputcache.do_full_cache: print 'This feature is only available if numbered prompts are in use.' return opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list') # Check if output to specific file was requested. try: outfname = opts['f'] except KeyError: outfile = Term.cout # default # We don't want to close stdout at the end! close_at_end = False else: if os.path.exists(outfname): if not ask_yes_no("File %r exists. Overwrite?" % outfname): print 'Aborting.' return outfile = open(outfname,'w') close_at_end = True if 't' in opts: input_hist = shell.input_hist elif 'r' in opts: input_hist = shell.input_hist_raw else: input_hist = shell.input_hist default_length = 40 pattern = None if 'g' in opts: init = 1 final = len(input_hist) parts = parameter_s.split(None,1) if len(parts) == 1: parts += '*' head, pattern = parts pattern = "*" + pattern + "*" elif len(args) == 0: final = len(input_hist) init = max(1,final-default_length) elif len(args) == 1: final = len(input_hist) init = max(1,final-int(args[0])) elif len(args) == 2: init,final = map(int,args) else: warn('%hist takes 0, 1 or 2 arguments separated by spaces.') print self.magic_hist.__doc__ return width = len(str(final)) line_sep = ['','\n'] print_nums = not opts.has_key('n') found = False if pattern is not None: sh = ip.IP.shadowhist.all() for idx, s in sh: if fnmatch.fnmatch(s, pattern): print "0%d: %s" %(idx, s) found = True if found: print "===" print "shadow history ends, fetch by %rep <number> (must start with 0)" print "=== start of normal history ===" for in_num in range(init,final): inline = input_hist[in_num] if pattern is not None and not fnmatch.fnmatch(inline, pattern): continue multiline = int(inline.count('\n') > 1) if print_nums: print >> outfile, \ '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), print >> outfile, inline, if close_at_end: outfile.close()