Ñò
ÎÑAHc           @   sÍ   d  Z  d d k Z e i i ƒ  Z d d k Z d d k Z d d k Z d d k	 Z	 d d k
 Z
 d d k Z d d k l Z d „  Z d „  Z d „  Z d „  Z e i d e ƒ d	 d
 „ Z e i d e ƒ d S(   s˜   
%store magic for lightweight persistence.

Stores variables, aliases etc. in PickleShare database.

$Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
iÿÿÿÿN(   t
   FakeModulec         C   sR   |  i  ƒ  } | i i d h  ƒ } x* | i ƒ  D] \ } } | i | | ƒ q. Wd  S(   Nt   stored_aliases(   t   getapit   dbt   gett   itemst   defalias(   t   selft   ipt	   staliasest   kt   v(    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   restore_aliases   s
     c         C   s†   |  i  } xv | i d ƒ D]e } t i i | ƒ } y | | } Wn/ t j
 o# d | GHd Gt i ƒ  d GHq X| |  i | <q Wd  S(   Ns   autorestore/*sE   Unable to restore variable '%s', ignoring (use %%store -d to forget!)s   The error was:i    (	   R   t   keyst   ost   patht   basenamet   KeyErrort   syst   exc_infot   user_ns(   R   R   t   keyt   justkeyt   obj(    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   refresh_variables   s    	 	c         C   s&   |  i  } | i d g  ƒ |  i d <d  S(   Nt   dhistt   _dh(   R   R   R   (   R   R   (    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   restore_dhist*   s    	c         C   s:   |  i  ƒ  } t | ƒ t |  ƒ t |  ƒ t i i ‚ d  S(   N(   R   R   R   R   t   IPythont   ipapit   TryNext(   R   R   (    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   restore_data.   s
    


t   late_startup_hookt    c      	   C   sâ  |  i  | d d d ƒ\ } } | i d d ƒ } |  i ƒ  } | i } | i d ƒ oY y | d } Wn t j
 o t d ƒ qÞXy | d | =WqÞt d	 | ƒ qÞXn1| i d
 ƒ o% x| i d ƒ D] } | | =qÎ Wnü| i d ƒ o t	 | ƒ nÞ| p­ |  i i d ƒ }	 |	 i
 ƒ  |	 o t t t |	 ƒ ƒ }
 n d }
 d GHd t |
 ƒ d } | i } xo|	 D]: } t i i | ƒ } | | t | | d ƒ ƒ d  f GHqsWn*t | ƒ d j o| d i d ƒ oø t i i | d i d ƒ i ƒ  ƒ } | d i d ƒ o t | d ƒ } n t | d ƒ } | i | d ƒ } d | d | i i | f GHt | t ƒ p! d d k l } | | | ƒ n/ | i | ƒ | i d ƒ p | i d ƒ n | i  ƒ  d Sy | i! | d } WnŠ t" j
 o~ | d |  i# j oV | i d h  ƒ } |  i# | d | | d <| | d <d G| d G|  i# | d GHd Sd | d GHnj Xt t$ i% | ƒ t& ƒ o! t' i( d | d | f ƒ GHd S| |  i d | d <d | d | i i f GHd S(    s„  Lightweight persistence for python variables.

    Example:
    
    ville@badger[~]|1> A = ['hello',10,'world']\
    ville@badger[~]|2> %store A\
    ville@badger[~]|3> Exit
    
    (IPython session is closed and started again...)
    
    ville@badger:~$ ipython -p pysh\
    ville@badger[~]|1> print A
    
    ['hello', 10, 'world']
    
    Usage:
    
    %store          - Show list of all variables and their current values\
    %store <var>    - Store the *current* value of the variable to disk\
    %store -d <var> - Remove the variable and its value from storage\
    %store -z       - Remove all variables from storage\
    %store -r       - Refresh all variables from store (delete current vals)\
    %store foo >a.txt  - Store value of foo to new file a.txt\
    %store foo >>a.txt - Append value of foo to file a.txt\   
    
    It should be noted that if you change the value of a variable, you
    need to %store it again if you want to persist the new value.
    
    Note also that the variables will need to be pickleable; most basic
    python types can be safely %stored.
    
    Also aliases can be %store'd across sessions.
    t   drzt   modet   stringi   t   di    s'   You must provide the variable to forgets   autorestore/s   Can't delete variable '%s't   zs   autorestore/*t   rs(   Stored variables and their in-db values:s   %-s   s -> %ss   <unavailable>i2   t   >s   >>t   at   ws   Writing '%s' (%s) to file '%s'.iÿÿÿÿ(   t   pprints   
NR   s   Alias stored:s   Error: unknown variable '%s's                  Warning:%s is %s 
                Proper storage of interactively declared classes (or instances
                of those classes) is not possible! Only instances
                of classes in real modules on file system can be %%store'd.
                s   Stored '%s' (%s)()   t   parse_optionst   splitt   NoneR   R   t   has_keyt
   IndexErrort   errorR   R   t   sortt   maxt   mapt   lent   strR   R   R   R   t   reprt
   startswitht
   expandusert   lstript   opent   evt	   __class__t   __name__t
   isinstancet
   basestringR+   t   writet   endswitht   closeR   R   t   alias_tablet   inspectt	   getmoduleR    t   textwrapt   dedent(   R   t   parameter_st   optst   argslt   argsR   R   t   todelR
   t   varst   sizet   fmtR   t   varR   t   fnamt   filR   R+   R	   (    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   magic_store7   s€    #	 
	 *'%


t   store(   t   __doc__t   IPython.ipapiR   R   R   R   t   pickleshareRE   t   pickleR   R   RG   t   IPython.FakeModuleR    R   R   R   R   t   set_hookRT   t   expose_magic(    (    (    sX   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/Extensions/pspersistence.pyt   <module>   s   <					€