a
    lc                     @   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 u r8| D ]\}}| j|| qn| j|||  d S )Nstored_aliases)dbgetitemsalias_managerdefine_alias)ipalias	staliaseskv r   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/extensions/storemagic.pyrestore_aliases   s
    r   c              	   C   sn   | j }|dD ]X}tj|}z|| }W n0 ty\   td|  tdt d  Y q0 || 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 )Ndhist_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,   configurablesappendautorestorer(   )selfr,   	__class__r   r   r.   H   s    zStoreMagics.__init__ c                 C   s  | j |ddd\}}| }| j}|j}d|v rz|d }W n. tyj } ztd|W Y d}~nLd}~0 0 z|d| = W n2 ty } ztd	| |W Y d}~n
d}~0 0 n:d
|v r|dD ]
}	||	= qĐnd|v r^|rR|D ]f}
z|d|
  }W nF tyB   zt	||
d W n  ty<   t
d|
  Y n0 Y q0 ||j|
< qnt| n|s|d}|  |rttt|}nd}t
d dt| d }|j}|D ]4}tj|}t
||t||ddd f  qn t|dkr|d drtj|d d }|d drDt|ddd}nt|ddd}|| ||d }t
d|d |jj|f  t|tsddlm} ||| n | | |!ds| d W d   n1 s0    Y  dS |D  ]}
z|j|
 }W n ty   |
}z|j"#|}W n4 t$yX } ztd| |W Y d}~n
d}~0 0 |di }|||< ||d< t
d ||f  Y  dS 0 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__r5   __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))*parse_optionssplitr,   r	   
IndexErrorr   BaseExceptionr   r   r   r   r   r(   sortmaxmaplenstrr
   r   r   r   repr
startswith
expanduserlstripopenevr4   rC   
isinstancerA   writeendswithr   retrieve_alias
ValueErrorgetattrinspect	getmoduletextwrapdedent)r2   parameter_soptsZargslargsr   r	   todeler   argr"   varssizefmtr
   varr!   ZfnamfilrA   namecmdr   modnamer   r   r   storeN   s    . (

*

*
$

zStoreMagics.store)r5   )rC   
__module____qualname____doc__r   tagr1   r.   r   r   rl   __classcell__r   r   r3   r   r)   <   s   r)   c                 C   s   |  t dS )zLoad the extension in IPython.N)register_magicsr)   r&   r   r   r   load_ipython_extension   s    rs   )N)ro   rZ   r   r   r\   IPython.core.errorr   IPython.core.magicr   r   r   IPython.testing.skipdoctestr   	traitletsr   r   r#   r'   r(   r)   rs   r   r   r   r   <module>   s    
 -