U
    puf                     @   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lmZ d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)skip_doctest)Boolc                 C   sN   | j di }|d kr8| D ]\}}| j|| qn| j|||  d S )Nstored_aliases)dbgetitemsalias_managerZdefine_alias)ipalias	staliaseskv r   A/tmp/pip-unpacked-wheel-bxwx4lqe/IPython/extensions/storemagic.pyrestore_aliases   s
    r   c              	   C   sp   | j }|dD ]Z}tj|}z|| }W n2 tk
r^   td|  tdt d  Y qX || j	|< qd S )N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_dhist2   s    r$   c                 C   s   t |  t|  t|  d S )N)r"   r   r$   r#   r   r   r   restore_data6   s    r%   c                       sF   e Zd ZdZedddjddZ fddZe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)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+   H   s    zStoreMagics.__init__ c                 C   s  | j |ddd\}}| }| j}|j}d|krz|d }W n, tk
rh } ztd|W 5 d}~X Y nBX z|d| = W n0 tk
r } ztd	| |W 5 d}~X Y nX n(d
|kr|dD ]
}	||	= qnd|kr^|rR|D ]j}
z|d|
  }W nJ tk
rB   zt	||
d W n" tk
r<   t
d|
  Y nX Y qX ||j|
< qnt| nx|s|d}|  |rttt|}nd}t
d dt| d }|j}|D ]4}tj|}t
||t||ddd f  qnt|dkr|d drtj|d d }|d drDt|ddd}nt|ddd}|r ||d }t
d|d |jj|f  t|tsddlm} ||| n | | |!ds| d W 5 Q R X dS |D  ]}
z|j|
 }W n tk
rx   |
}z|j"#|}W n2 t$k
rB } ztd| |W 5 d}~X Y nX |di }|||< ||d< t
d ||f  Y  dS X t%t&'|d!d"}|d#krt
t()d$|
|f   dS ||d|
 < t
d%|
|jjf  qdS )&a  Lightweight persistence for python variables.

        Example::

          In [1]: l = ['hello',10,'world']
          In [2]: %store l
          Stored 'l' (list)
          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 bar`` - Store the *current* value of the variables spam
                                and bar 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, aliases and directory history
                                from store (overwrite current vals)
        * ``%store -r spam bar`` - Refresh specified variables and aliases 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.
        To remove an alias from the storage, use the %unalias magic.
        Zdrzstring)modedr   z'You must provide the variable to forgetNzautorestore/zCan't delete variable '%s'zr   r)r   zno stored variable or alias %sz(Stored variables and their in-db values:z%-zs -> %sz<unavailable>2      >z>>azutf-8)encodingwzWriting '%s' (%s) to file '%s'.)pprint
zUnknown variable '%s'r   zAlias stored: %s (%s)__name__r1   __main__a6                          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   BaseExceptionr   r   r   r   r   r%   sortmaxmaplenstrr
   r   r   r   repr
startswith
expanduserlstripopenZevr0   r?   
isinstancer=   writeendswithr   Zretrieve_alias
ValueErrorgetattrinspect	getmoduletextwrapdedent)r.   Zparameter_soptsZargslargsr   r	   Ztodeler   argr!   varssizefmtr
   varr    ZfnamZfilr=   namecmdr   modnamer   r   r   storeN   s    .$

*
 

 

zStoreMagics.store)r1   )r?   
__module____qualname____doc__r   tagr-   r+   r   r   rb   __classcell__r   r   r/   r   r&   <   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    rh   )N)re   rS   r   r   rU   ZIPython.core.errorr   ZIPython.core.magicr   r   r   ZIPython.testing.skipdoctestr   Z	traitletsr   r   r"   r$   r%   r&   rh   r   r   r   r   <module>   s    
 -