diff --git a/IPython/Extensions/pickleshare.py b/IPython/Extensions/pickleshare.py index 4037082..094d787 100644 --- a/IPython/Extensions/pickleshare.py +++ b/IPython/Extensions/pickleshare.py @@ -40,6 +40,8 @@ import UserDict import warnings import glob +from sets import Set as set + def gethashfile(key): return ("%02x" % abs(hash(key) % 256))[-2:] @@ -52,6 +54,7 @@ class PickleShareDB(UserDict.DictMixin): self.root.makedirs() # cache has { 'key' : (obj, orig_mod_time) } self.cache = {} + def __getitem__(self,key): """ db['key'] reading """ diff --git a/IPython/history.py b/IPython/history.py index 543d898..2acb6ee 100644 --- a/IPython/history.py +++ b/IPython/history.py @@ -33,13 +33,15 @@ def magic_history(self, parameter_s = ''): -g: treat the arg as a pattern to grep for in (full) history + -s: show "shadow" history """ + 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,'gnt',mode='list') + opts,args = self.parse_options(parameter_s,'gnts',mode='list') if not opts.has_key('t'): input_hist = shell.input_hist_raw @@ -68,6 +70,20 @@ def magic_history(self, parameter_s = ''): 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 (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): @@ -119,6 +135,13 @@ def rep_f(self, arg): return if len(args) == 1: + arg = args[0] + if len(arg) > 1 and arg.startswith('0'): + # get from shadow hist + num = int(arg[1:]) + line = self.shadowhist.get(num) + ip.set_next_input(str(line)) + return try: num = int(args[0]) ip.set_next_input(str(ip.IP.input_hist_raw[num]).rstrip()) @@ -150,7 +173,7 @@ class ShadowHist: if old is not _sentinel: return newidx = self.inc_idx() - print "new",newidx + #print "new",newidx # dbg self.db.hset('shadowhist',ent, newidx) def all(self): @@ -159,11 +182,25 @@ class ShadowHist: items.sort() return items + def get(self, idx): + all = self.all() + + for k, v in all: + print k,v + if k == idx: + return v + def test_shist(): - s = ShadowHist(ip.db) + from IPython.Extensions import pickleshare + db = pickleshare.PickleShareDB('~/shist') + s = ShadowHist(db) s.add('hello') s.add('world') + s.add('hello') + s.add('hello') + s.add('karhu') print "all",s.all() + print s.get(2) def init_ipython(ip): ip.expose_magic("rep",rep_f) diff --git a/IPython/iplib.py b/IPython/iplib.py index 29c8c81..b031cb4 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 2430 2007-06-14 15:59:06Z vivainio $ +$Id: iplib.py 2440 2007-06-14 19:31:36Z vivainio $ """ #***************************************************************************** @@ -60,7 +60,7 @@ from sets import Set from pprint import pprint, pformat # IPython's own modules -import IPython +#import IPython from IPython import Debugger,OInspect,PyColorize,ultraTB from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names from IPython.FakeModule import FakeModule @@ -74,7 +74,7 @@ from IPython.usage import cmd_line_usage,interactive_usage from IPython.genutils import * from IPython.strdispatch import StrDispatch import IPython.ipapi - +import IPython.history import IPython.prefilter as prefilter # Globals @@ -620,6 +620,7 @@ class InteractiveShell(object,Magic): print r"only has ASCII characters, e.g. c:\home" print "Now it is",rc.ipythondir sys.exit() + self.shadowhist = IPython.history.ShadowHist(self.db) def post_config_initialization(self): @@ -2014,8 +2015,9 @@ want to merge them back into the new files.""" % locals() except AttributeError: pass # re{move,place}_history_item are new in 2.4. else: - self.input_hist_raw.append('%s\n' % line) + self.input_hist_raw.append('%s\n' % line) + self.shadowhist.add(line) try: lineout = self.prefilter(line,continue_prompt) except: