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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from datetime import datetime
  5. import IPython.ipapi as IPython
  6. ip = IPython.ipapi.get()
  7. import pickleshare
  8. import inspect
  9. import pickle
  10. import os
  11. import sys
  12. import textwrap
  13. from IPython.FakeModule import FakeModule
  14. from IPython.ipstruct import Struct
  15.  
  16. def refresh_variables(ip, key = None):
  17.     db = ip.db
  18.     if key is None:
  19.         keys = db.keys('jot/*')
  20.     else:
  21.         keys = db.keys('jot/' + key)
  22.     for key in keys:
  23.         justkey = os.path.basename(key)
  24.         print 'Restoring from', justkey, '...'
  25.         
  26.         try:
  27.             obj = db[key]
  28.         except KeyError:
  29.             print "Unable to restore variable '%s', ignoring (use %%jot -d to forget!)" % justkey
  30.             print 'The error was:', sys.exc_info()[0]
  31.             continue
  32.  
  33.         
  34.         try:
  35.             origname = obj.name
  36.         except:
  37.             ip.user_ns[justkey] = obj
  38.             print 'Restored', justkey
  39.             continue
  40.  
  41.         ip.user_ns[origname] = obj['val']
  42.         print 'Restored', origname
  43.     
  44.  
  45.  
  46. def read_variables(ip, key = None):
  47.     db = ip.db
  48.     if key is None:
  49.         return None
  50.     keys = db.keys('jot/' + key)
  51.     for key in keys:
  52.         justkey = os.path.basename(key)
  53.         print 'restoring from ', justkey
  54.         
  55.         try:
  56.             obj = db[key]
  57.         except KeyError:
  58.             print "Unable to read variable '%s', ignoring (use %%jot -d to forget!)" % justkey
  59.             print 'The error was:', sys.exc_info()[0]
  60.             continue
  61.  
  62.         return obj
  63.     
  64.  
  65.  
  66. def detail_variables(ip, key = None):
  67.     db = ip.db
  68.     get = ip.db.get
  69.     if key is None:
  70.         keys = db.keys('jot/*')
  71.     else:
  72.         keys = db.keys('jot/' + key)
  73.     if keys:
  74.         size = max(map(len, keys))
  75.     else:
  76.         size = 0
  77.     fmthead = '%-' + str(size) + 's [%s]'
  78.     fmtbody = 'Comment:\n %s'
  79.     fmtdata = 'Data:\n %s, %s'
  80.     for key in keys:
  81.         v = get(key, '<unavailable>')
  82.         justkey = os.path.basename(key)
  83.         
  84.         try:
  85.             print fmthead % (justkey, datetime.ctime(v.get('time', '<unavailable>')))
  86.             print fmtbody % v.get('comment', '<unavailable>')
  87.             d = v.get('val', 'unavailable')
  88.             print fmtdata % (repr(type(d)), '')
  89.             print repr(d)[0:200]
  90.             print 
  91.             print 
  92.         continue
  93.         except AttributeError:
  94.             print fmt % (justkey, '<unavailable>', '<unavailable>', repr(v)[:50])
  95.             continue
  96.         
  97.  
  98.     
  99.  
  100.  
  101. def intm(n):
  102.     
  103.     try:
  104.         return int(n)
  105.     except:
  106.         return 0
  107.  
  108.  
  109.  
  110. def jot_obj(self, obj, name, comment = ''):
  111.     had = self.db.keys('jot/' + name + '*')
  112.     suffix = ''
  113.     uname = 'jot/' + name + suffix
  114.     all = ip.IP.shell.input_hist
  115.     
  116.     try:
  117.         comment = ip.IP.magic_edit('-x').strip()
  118.     except:
  119.         None if len(had) > 0 else []
  120.         print 'No comment is recorded.'
  121.         comment = ''
  122.  
  123.     self.db[uname] = Struct({
  124.         'val': obj,
  125.         'time': datetime.now(),
  126.         'hist': all,
  127.         'name': name,
  128.         'comment': comment })
  129.     print "Jotted down notes for '%s' (%s)" % (uname, obj.__class__.__name__)
  130.  
  131.  
  132. def magic_jot(self, parameter_s = ''):
  133.     (opts, argsl) = self.parse_options(parameter_s, 'drzl', mode = 'string')
  134.     args = argsl.split(None, 1)
  135.     ip = self.getapi()
  136.     db = ip.db
  137.     if opts.has_key('d'):
  138.         
  139.         try:
  140.             todel = args[0]
  141.         except IndexError:
  142.             error('You must provide the variable to forget')
  143.  
  144.         
  145.         try:
  146.             del db['jot/' + todel]
  147.         error("Can't delete variable '%s'" % todel)
  148.  
  149.     elif opts.has_key('z'):
  150.         print 'reseting the whole database has been disabled.'
  151.     elif opts.has_key('r'):
  152.         
  153.         try:
  154.             toret = args[0]
  155.         except:
  156.             print 'restoring all the variables jotted down...'
  157.             refresh_variables(ip)
  158.  
  159.         refresh_variables(ip, toret)
  160.     elif opts.has_key('l'):
  161.         
  162.         try:
  163.             tolist = args[0]
  164.         except:
  165.             print 'List details for all the items.'
  166.             detail_variables(ip)
  167.  
  168.         print 'Details for', tolist, ':'
  169.         detail_variables(ip, tolist)
  170.     elif not args:
  171.         vars = self.db.keys('jot/*')
  172.         vars.sort()
  173.         if vars:
  174.             size = max(map(len, vars)) - 4
  175.         else:
  176.             size = 0
  177.         print 'Variables and their in-db values:'
  178.         fmt = '%-' + str(size) + 's [%s] -> %s'
  179.         get = db.get
  180.         for var in vars:
  181.             justkey = os.path.basename(var)
  182.             v = get(var, '<unavailable>')
  183.             
  184.             try:
  185.                 print fmt % (justkey, datetime.ctime(v.get('time', '<unavailable>')), v.get('comment', '<unavailable>')[:70].replace('\n', ' '))
  186.             continue
  187.             except AttributeError:
  188.                 print fmt % (justkey, '<unavailable>', '<unavailable>', repr(v)[:50])
  189.                 continue
  190.             
  191.  
  192.         
  193.     elif len(args) > 1 and args[1].startswith('>'):
  194.         fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
  195.         if args[1].startswith('>>'):
  196.             fil = open(fnam, 'a')
  197.         else:
  198.             fil = open(fnam, 'w')
  199.         obj = ip.ev(args[0])
  200.         print "Writing '%s' (%s) to file '%s'." % (args[0], obj.__class__.__name__, fnam)
  201.         if not isinstance(obj, basestring):
  202.             pprint = pprint
  203.             import pprint
  204.             pprint(obj, fil)
  205.         else:
  206.             fil.write(obj)
  207.             if not obj.endswith('\n'):
  208.                 fil.write('\n')
  209.             
  210.         fil.close()
  211.         return None
  212.     
  213.     try:
  214.         obj = ip.user_ns[args[0]]
  215.     except KeyError:
  216.         print 
  217.         print "Error: %s doesn't exist." % args[0]
  218.         print 
  219.         print 'Use %note -r <var> to retrieve variables. This should not be used ' + 'to store alias, for saving aliases, use %store'
  220.         return None
  221.  
  222.     if isinstance(inspect.getmodule(obj), FakeModule):
  223.         print textwrap.dedent("                Warning:%s is %s \n                Proper storage of interactively declared classes (or instances\n                of those classes) is not possible! Only instances\n                of classes in real modules on file system can be %%store'd.\n                " % (args[0], obj))
  224.         return None
  225.     jot_obj(self, obj, args[0])
  226.  
  227.  
  228. def magic_read(self, parameter_s = ''):
  229.     (opts, argsl) = self.parse_options(parameter_s, 'drzl', mode = 'string')
  230.     args = argsl.split(None, 1)
  231.     ip = self.getapi()
  232.     db = ip.db
  233.     
  234.     try:
  235.         toret = args[0]
  236.     except:
  237.         print 'which record do you want to read out?'
  238.         return None
  239.  
  240.     return read_variables(ip, toret)
  241.  
  242. ip.expose_magic('jot', magic_jot)
  243. ip.expose_magic('read', magic_read)
  244.