home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1825 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.5 KB  |  174 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import fnmatch
  5. import os
  6. from IPython.genutils import Term, ask_yes_no, warn
  7. import IPython.ipapi as IPython
  8.  
  9. def magic_history(self, parameter_s = ''):
  10.     ip = self.api
  11.     shell = self.shell
  12.     if not shell.outputcache.do_full_cache:
  13.         print 'This feature is only available if numbered prompts are in use.'
  14.         return None
  15.     (opts, args) = self.parse_options(parameter_s, 'gntsrf:', mode = 'list')
  16.     
  17.     try:
  18.         outfname = opts['f']
  19.     except KeyError:
  20.         shell.outputcache.do_full_cache
  21.         shell.outputcache.do_full_cache
  22.         outfile = Term.cout
  23.         close_at_end = False
  24.     except:
  25.         shell.outputcache.do_full_cache
  26.  
  27.     outfile = open(outfname, 'w')
  28.     close_at_end = True
  29.     if 't' in opts:
  30.         input_hist = shell.input_hist
  31.     elif 'r' in opts:
  32.         input_hist = shell.input_hist_raw
  33.     else:
  34.         input_hist = shell.input_hist
  35.     default_length = 40
  36.     pattern = None
  37.     if 'g' in opts:
  38.         init = 1
  39.         final = len(input_hist)
  40.         parts = parameter_s.split(None, 1)
  41.         if len(parts) == 1:
  42.             parts += '*'
  43.         
  44.         (head, pattern) = parts
  45.         pattern = '*' + pattern + '*'
  46.     elif len(args) == 0:
  47.         final = len(input_hist)
  48.         init = max(1, final - default_length)
  49.     elif len(args) == 1:
  50.         final = len(input_hist)
  51.         init = max(1, final - int(args[0]))
  52.     elif len(args) == 2:
  53.         (init, final) = map(int, args)
  54.     else:
  55.         warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
  56.         print self.magic_hist.__doc__
  57.         return None
  58.     width = 'g' in opts(str(final))
  59.     line_sep = [
  60.         '',
  61.         '\n']
  62.     print_nums = not opts.has_key('n')
  63.     found = False
  64.     if pattern is not None:
  65.         sh = ip.IP.shadowhist.all()
  66.         for idx, s in sh:
  67.             if fnmatch.fnmatch(s, pattern):
  68.                 print '0%d: %s' % (idx, s)
  69.                 found = True
  70.                 continue
  71.         
  72.     
  73.     if found:
  74.         print '==='
  75.         print 'shadow history ends, fetch by %rep <number> (must start with 0)'
  76.         print '=== start of normal history ==='
  77.     
  78.     for in_num in range(init, final):
  79.         inline = input_hist[in_num]
  80.         if pattern is not None and not fnmatch.fnmatch(inline, pattern):
  81.             continue
  82.         
  83.         multiline = int(inline.count('\n') > 1)
  84.         if print_nums:
  85.             print >>outfile, '%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
  86.         
  87.         print >>outfile, inline,
  88.     
  89.     if close_at_end:
  90.         outfile.close()
  91.     
  92.  
  93.  
  94. def magic_hist(self, parameter_s = ''):
  95.     return self.magic_history(parameter_s)
  96.  
  97.  
  98. def rep_f(self, arg):
  99.     (opts, args) = self.parse_options(arg, '', mode = 'list')
  100.     ip = self.api
  101.     if not args:
  102.         ip.set_next_input(str(ip.user_ns['_']))
  103.         return None
  104.     
  105.     try:
  106.         lines = self.extract_input_slices(args, True)
  107.         print 'lines', lines
  108.         ip.runlines(lines)
  109.     except ValueError:
  110.         None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
  111.         None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
  112.         print 'Not found in recent history:', args
  113.     except:
  114.         None if len(args) == 1 and '-' not in args[0] else None if 'rep' in h else args
  115.  
  116.  
  117. _sentinel = object()
  118.  
  119. class ShadowHist:
  120.     
  121.     def __init__(self, db):
  122.         self.curidx = 0
  123.         self.db = db
  124.         self.disabled = False
  125.  
  126.     
  127.     def inc_idx(self):
  128.         idx = self.db.get('shadowhist_idx', 1)
  129.         self.db['shadowhist_idx'] = idx + 1
  130.         return idx
  131.  
  132.     
  133.     def add(self, ent):
  134.         if self.disabled:
  135.             return None
  136.         
  137.         try:
  138.             old = self.db.hget('shadowhist', ent, _sentinel)
  139.             if old is not _sentinel:
  140.                 return None
  141.             newidx = self.inc_idx()
  142.             self.db.hset('shadowhist', ent, newidx)
  143.         except:
  144.             self.disabled
  145.             IPython.ipapi.get().IP.showtraceback()
  146.             print 'WARNING: disabling shadow history'
  147.             self.disabled = True
  148.  
  149.  
  150.     
  151.     def all(self):
  152.         d = self.db.hdict('shadowhist')
  153.         items = [ (i, s) for s, i in d.items() ]
  154.         items.sort()
  155.         return items
  156.  
  157.     
  158.     def get(self, idx):
  159.         all = self.all()
  160.         for k, v in all:
  161.             if k == idx:
  162.                 return v
  163.         
  164.  
  165.  
  166.  
  167. def init_ipython(ip):
  168.     import ipy_completers
  169.     ip.expose_magic('rep', rep_f)
  170.     ip.expose_magic('hist', magic_hist)
  171.     ip.expose_magic('history', magic_history)
  172.     ipy_completers.quick_completer('%hist', '-g -t -r -n')
  173.  
  174.