o
    Zh                     @   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   sP   | j di }|d u r| D ]\}}| j|| qd S | j|||  d S )Nstored_aliases)dbgetitemsalias_managerZdefine_alias)ipalias	staliaseskv r   T/var/www/html/lang_env/lib/python3.10/site-packages/IPython/extensions/storemagic.pyrestore_aliases   s   r   c              	   C   sn   | j }|dD ],}tj|}z|| }W n ty.   td|  tdt d  Y qw || 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                    s8   t t| j|d | jj|  | jrt| j d S 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|v rLz|d }W n ty/ } ztd|d}~ww z|d| = W dS  tyK } ztd	| |d}~ww d
|v r]|dD ]}	||	= qUdS d|v r|r|D ]1}
z|d|
  }W n! ty   zt	||
d W n ty   t
d|
  Y nw Y qew ||j|
< qedS t| dS |s|d}|  |rttt|}nd}t
d dt| d }|j}|D ]}tj|}t
||t||ddd f  qdS t|dkrr|d drrtj|d d }|d drt|ddd}nt|ddd}|O ||d }t
d|d |jj|f  t|ts?ddlm} ||| n | | |!dsW| d W d   dS W d   dS W d   dS 1 skw   Y  dS |D ]x}
z|j|
 }W nA ty   |
}z|j"#|}W n t$y } ztd| |d}~ww |di }|||< ||d< t
d ||f  Y  dS w t%t&'|d!d"}|d#krt
t()d$|
|f   dS ||d|
 < t
d%|
|jjf  qtd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__r2   __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evr1   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)r2   )r@   
__module____qualname____doc__r   tagr.   r,   r   r   rc   __classcell__r   r   r0   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   ri   r%   )rf   rT   r   r   rV   ZIPython.core.errorr   ZIPython.core.magicr   r   r   ZIPython.testing.skipdoctestr   Z	traitletsr   r   r"   r$   r&   r'   ri   r   r   r   r   <module>   s    
 -