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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import IPython.ipapi as IPython
  5. from IPython.ipapi import UsageError
  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.  
  15. def restore_aliases(self):
  16.     ip = self.getapi()
  17.     staliases = ip.db.get('stored_aliases', { })
  18.     for k, v in staliases.items():
  19.         ip.defalias(k, v)
  20.     
  21.  
  22.  
  23. def refresh_variables(ip):
  24.     db = ip.db
  25.     for key in db.keys('autorestore/*'):
  26.         justkey = os.path.basename(key)
  27.         
  28.         try:
  29.             obj = db[key]
  30.         except KeyError:
  31.             print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
  32.             print 'The error was:', sys.exc_info()[0]
  33.             continue
  34.  
  35.         ip.user_ns[justkey] = obj
  36.     
  37.  
  38.  
  39. def restore_dhist(ip):
  40.     db = ip.db
  41.     ip.user_ns['_dh'] = db.get('dhist', [])
  42.  
  43.  
  44. def restore_data(self):
  45.     ip = self.getapi()
  46.     refresh_variables(ip)
  47.     restore_aliases(self)
  48.     restore_dhist(self)
  49.     raise IPython.ipapi.TryNext
  50.  
  51. ip.set_hook('late_startup_hook', restore_data)
  52.  
  53. def magic_store(self, parameter_s = ''):
  54.     (opts, argsl) = self.parse_options(parameter_s, 'drz', mode = 'string')
  55.     args = argsl.split(None, 1)
  56.     ip = self.getapi()
  57.     db = ip.db
  58.     if opts.has_key('d'):
  59.         
  60.         try:
  61.             todel = args[0]
  62.         except IndexError:
  63.             raise UsageError('You must provide the variable to forget')
  64.  
  65.         
  66.         try:
  67.             del db['autorestore/' + todel]
  68.         raise UsageError("Can't delete variable '%s'" % todel)
  69.  
  70.     elif opts.has_key('z'):
  71.         for k in db.keys('autorestore/*'):
  72.             del db[k]
  73.         
  74.     elif opts.has_key('r'):
  75.         refresh_variables(ip)
  76.     elif not args:
  77.         vars = self.db.keys('autorestore/*')
  78.         vars.sort()
  79.         if vars:
  80.             size = max(map(len, vars))
  81.         else:
  82.             size = 0
  83.         print 'Stored variables and their in-db values:'
  84.         fmt = '%-' + str(size) + 's -> %s'
  85.         get = db.get
  86.         for var in vars:
  87.             justkey = os.path.basename(var)
  88.             print fmt % (justkey, repr(get(var, '<unavailable>'))[:50])
  89.         
  90.     elif len(args) > 1 and args[1].startswith('>'):
  91.         fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
  92.         if args[1].startswith('>>'):
  93.             fil = open(fnam, 'a')
  94.         else:
  95.             fil = open(fnam, 'w')
  96.         obj = ip.ev(args[0])
  97.         print "Writing '%s' (%s) to file '%s'." % (args[0], obj.__class__.__name__, fnam)
  98.         if not isinstance(obj, basestring):
  99.             pprint = pprint
  100.             import pprint
  101.             pprint(obj, fil)
  102.         else:
  103.             fil.write(obj)
  104.             if not obj.endswith('\n'):
  105.                 fil.write('\n')
  106.             
  107.         fil.close()
  108.         return None
  109.     
  110.     try:
  111.         obj = ip.user_ns[args[0]]
  112.     except KeyError:
  113.         if args[0] in self.alias_table:
  114.             staliases = db.get('stored_aliases', { })
  115.             staliases[args[0]] = self.alias_table[args[0]]
  116.             db['stored_aliases'] = staliases
  117.             print 'Alias stored:', args[0], self.alias_table[args[0]]
  118.             return None
  119.         raise UsageError("Unknown variable '%s'" % args[0])
  120.     except:
  121.         args[0] in self.alias_table
  122.  
  123.     if isinstance(inspect.getmodule(obj), FakeModule):
  124.         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))
  125.         return None
  126.     self.db['autorestore/' + args[0]] = obj
  127.     print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
  128.  
  129. ip.expose_magic('store', magic_store)
  130.