B
    \S                 @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ dd Zdd	 Zd
d Zdd Ze	G dd deZdd ZdS )z
%store magic for lightweight persistence.

Stores variables, aliases and macros in IPython's database.

To automatically restore stored variables at startup, add this to your
:file:`ipython_config.py` file::

  c.StoreMagics.autorestore = True
    N)
UsageError)Magicsmagics_class
line_magic)Boolc             C   s6   | j di }x"| D ]\}}| j|| qW d S )Nstored_aliases)dbgetitemsalias_managerZdefine_alias)ip	staliaseskv r   <lib/python3.7/site-packages/IPython/extensions/storemagic.pyrestore_aliases   s    r   c          	   C   st   | j }xh|dD ]Z}tj|}y|| }W n2 tk
r`   td|  tdt d  Y qX || j	|< qW d S )Nzautorestore/*zEUnable to restore variable '%s', ignoring (use %%store -d to forget!)zThe error was:r   )
r   keysospathbasenameKeyErrorprintsysexc_infouser_ns)r   r   keyjustkeyobjr   r   r   refresh_variables   s    r   c             C   s   | j dg | jd< d S )NZdhistZ_dh)r   r	   r   )r   r   r   r   restore_dhist.   s    r    c             C   s   t |  t|  t|  d S )N)r   r   r    )r   r   r   r   restore_data2   s    r!   c                   sB   e Zd ZdZedddjddZ fddZedd
dZ	  Z
S )StoreMagicszMLightweight persistence for python variables.

    Provides the %store magic.FzdIf True, any %store-d variables will be automatically restored
        when IPython starts.
        )helpT)Zconfigc                s4   t t| j|d | jj|  | jr0t| j d S )N)shell)superr"   __init__r$   Zconfigurablesappendautorestorer!   )selfr$   )	__class__r   r   r&   D   s    zStoreMagics.__init__ c             C   s  | j |ddd\}}|dd}| j}|j}d|kry|d }W n tk
r\   tdY n*X y|d	| = W n   td
| Y nX nd|krx|dD ]
}||= qW nd|kr|rxR|D ]@}	y|d	|	  }
W n  tk
r   td|	  Y qX |
|j	|	< qW nt
| nd|s|d}|  |rFttt|}nd}td dt| d }|j}x<|D ]4}tj|}t||t||ddd f  qnW nt|dkr|d drtj|d d }|d drt|d}n
t|d}|r ||d }
td|d |
jj|f  t|
tsZddlm} ||
| n ||
 |
dsz|d W dQ R X dS y|j	|d  }
W n~ tk
r   |d }y|j !|}W n" t"k
r   td| Y nX |di }|||< ||d< td||f  dS X t#t$%|
dd}|d krTtt&'d!|d |
f  dS |
|d	|d  < td"|d |
jjf  dS )#a  Lightweight persistence for python variables.

        Example::

          In [1]: l = ['hello',10,'world']
          In [2]: %store l
          In [3]: exit

          (IPython session is closed and started again...)

          ville@badger:~$ ipython
          In [1]: l
          NameError: name 'l' is not defined
          In [2]: %store -r
          In [3]: l
          Out[3]: ['hello', 10, 'world']

        Usage:

        * ``%store``          - Show list of all variables and their current
                                values
        * ``%store spam``     - Store the *current* value of the variable spam
                                to disk
        * ``%store -d spam``  - Remove the variable and its value from storage
        * ``%store -z``       - Remove all variables from storage
        * ``%store -r``       - Refresh all variables from store (overwrite
                                current vals)
        * ``%store -r spam bar`` - Refresh specified variables from store
                                   (delete current val)
        * ``%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 %store'd.

        Also aliases can be %store'd across sessions.
        Zdrzstring)modeN   dr   z'You must provide the variable to forgetzautorestore/zCan't delete variable '%s'zzautorestore/*rzno stored variable %sz(Stored variables and their in-db values:z%-zs -> %sz<unavailable>2   >z>>awzWriting '%s' (%s) to file '%s'.)pprint
zUnknown variable '%s'r   zAlias stored: %s (%s)__name__r+   __main__a"                      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.
                    zStored '%s' (%s))(Zparse_optionssplitr$   r   
IndexErrorr   r   r   r   r   r!   sortmaxmaplenstrr	   r   r   r   repr
startswith
expanduserlstripopenZevr*   r8   
isinstancer6   writeendswithr   Zretrieve_alias
ValueErrorgetattrinspectZ	getmoduletextwrapdedent)r)   Zparameter_sZoptsZargslargsr   r   Ztodelr   argr   varssizeZfmtr	   varr   ZfnamZfilr6   namecmdr   modnamer   r   r   storeJ   s    +



,



zStoreMagics.store)r+   )r8   
__module____qualname____doc__r   tagr(   r&   r   rV   __classcell__r   r   )r*   r   r"   8   s   r"   c             C   s   |  t dS )zLoad the extension in IPython.N)Zregister_magicsr"   )r   r   r   r   load_ipython_extension   s    r\   )rY   rK   r   r   rL   ZIPython.core.errorr   ZIPython.core.magicr   r   r   Z	traitletsr   r   r   r    r!   r"   r\   r   r   r   r   <module>   s     &