home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import fnmatch
- import os
- from IPython.genutils import Term, ask_yes_no, warn
- import IPython.ipapi as IPython
-
- def magic_history(self, parameter_s = ''):
- 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 None
- (opts, args) = self.parse_options(parameter_s, 'gntsrf:', mode = 'list')
-
- try:
- outfname = opts['f']
- except KeyError:
- shell.outputcache.do_full_cache
- shell.outputcache.do_full_cache
- outfile = Term.cout
- close_at_end = False
- except:
- shell.outputcache.do_full_cache
-
- 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 None
- width = 'g' in opts(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
- continue
-
-
- 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_hist(self, parameter_s = ''):
- return self.magic_history(parameter_s)
-
-
- def rep_f(self, arg):
- (opts, args) = self.parse_options(arg, '', mode = 'list')
- ip = self.api
- if not args:
- ip.set_next_input(str(ip.user_ns['_']))
- return None
-
- try:
- lines = self.extract_input_slices(args, True)
- print 'lines', lines
- ip.runlines(lines)
- except ValueError:
- None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
- None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
- print 'Not found in recent history:', args
- except:
- None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
-
-
- _sentinel = object()
-
- class ShadowHist:
-
- def __init__(self, db):
- self.curidx = 0
- self.db = db
- self.disabled = False
-
-
- def inc_idx(self):
- idx = self.db.get('shadowhist_idx', 1)
- self.db['shadowhist_idx'] = idx + 1
- return idx
-
-
- def add(self, ent):
- if self.disabled:
- return None
-
- try:
- old = self.db.hget('shadowhist', ent, _sentinel)
- if old is not _sentinel:
- return None
- newidx = self.inc_idx()
- self.db.hset('shadowhist', ent, newidx)
- except:
- self.disabled
- IPython.ipapi.get().IP.showtraceback()
- print 'WARNING: disabling shadow history'
- self.disabled = True
-
-
-
- def all(self):
- d = self.db.hdict('shadowhist')
- items = [ (i, s) for s, i in d.items() ]
- items.sort()
- return items
-
-
- def get(self, idx):
- all = self.all()
- for k, v in all:
- if k == idx:
- return v
-
-
-
-
- def init_ipython(ip):
- import ipy_completers
- ip.expose_magic('rep', rep_f)
- ip.expose_magic('hist', magic_hist)
- ip.expose_magic('history', magic_history)
- ipy_completers.quick_completer('%hist', '-g -t -r -n')
-
-