
    Mh                         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Zd Zd	 Zd
 Ze	 G d de                      Z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                     | j                             di           }|7|                                D ] \  }}| j                            ||           !d S | j                            |||                    d S )Nstored_aliases)dbgetitemsalias_managerdefine_alias)ipalias	staliaseskvs        ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/extensions/storemagic.pyrestore_aliasesr      s    		*B//I}??$$ 	/ 	/CAa ))!A....	/ 	/
 	%%eYu-=>>>>>    c                 :   | j         }|                    d          D ]}}t          j                            |          }	 ||         }|| j        |<   5# t          $ r< t          d|z             t          dt          j	                    d                    Y zw xY w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user_nsKeyErrorprintsysexc_info)r   r   keyjustkeyobjs        r   refresh_variablesr&   #   s    	Bww'' 
& 
&'""3''	&S'C #&BJw  	7 	7 	7Y\ccddd"CLNN1$566666	7
& 
&s   AABBc                 L    | j                             dg           | j        d<   d S )Ndhist_dh)r   r   r   r   s    r   restore_dhistr+   2   s$    		'"--BJur   c                 `    t          |            t          |            t          |            d S N)r&   r   r+   r*   s    r   restore_datar.   6   s2    bB"r   c                        e Zd ZdZ edd                              d          Z fdZee	d
d	                        Z
 x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                     t          t          |                               |           | j        j                            |            | j        rt          | j                   d S d S )N)shell)superr0   __init__r4   configurablesappendautorestorer.   )selfr4   	__class__s     r   r6   zStoreMagics.__init__H   sh    k4  )))666
 ''--- 	%$$$$$	% 	%r    c                 R	   |                      |dd          \  }}|                                }| j        }|j        }d|v rY	 |d         }	 |d|z   = dS # t          $ r}t          d|z            |d}~ww xY w# t          $ r}t          d	          |d}~ww xY wd
|v r|                    d          D ]}	||	= dS d|v ru|rb|D ]]}
	 |d|
z            }||j        |
<   # t          $ r8 	 t          ||
           n"# t          $ r t          d|
z             Y nw xY wY Zw xY wdS t          |           dS |s|                    d          }|                                 |r#t          t          t           |                    }nd}t          d           dt#          |          z   dz   }|j        }|D ]T}t&          j                            |          }t          ||t-           ||d                    dd         fz             UdS t!          |          dk    rm|d                             d          rQt&          j                            |d                             d                                                    }|d                             d          rt5          |dd          }nt5          |dd          }|5  |                    |d                   }t          d|d         d|j        j        d|d           t=          |t"                    sddlm}  |||           n?|                     |           |!                    d           s|                     d            ddd           n# 1 swxY w Y   dS |D ]}
	 |j        |
         }tE          tG          j$        |          d!d"          }|d#k    r+t          tK          j&        d$|
d%|d&                      dS ||d|
z   <   t          d'|
d|j        j        d(           # t          $ r~ |
}	 |j'        (                    |          }n%# tR          $ r}t          d)|z            |d}~ww xY w|                    d*i           }|||<   ||d*<   t          d+|d,|d(           Y  dS w xY wd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.
        drzstring)modedr   zautorestore/zCan't delete variable '%s'Nz'You must provide the variable to forget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 'z' (z) to file 'z'.)pprint
__name__r<   __main__z                         Warning:z is a  
                        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 ')zUnknown variable '%s'r
   zAlias stored: z ()*parse_optionssplitr4   r   BaseExceptionr   
IndexErrorr   r   r   r   r    r.   sortmaxmaplenstrr   r   r   r   repr
startswith
expanduserlstripopenevr;   rL   
isinstancerJ   writeendswithgetattrinspect	getmoduletextwrapdedentr   retrieve_alias
ValueError)r:   parameter_soptsargslargsr   r   todeler   argr%   varssizefmtr   varr$   fnamfilrJ   modnamenamecmdr   s                            r   storezStoreMagics.storeN   s$   \ ''Ex'HH
U{{}}ZU$;;RQR>E1222$ R R R$%AE%IJJPQQR  S S S !JKKQRRS D[[WW_--  qEE  D[[ ! 	. 	.C. #!56 +.
3 $ J J JJ+Bc:::::' J J J!"BS"HIIIIIJJ	. 	. R       F	N77?++DIIKKK 3sD>>**<===s4yy.*C&C M M'**3//cWd33sO+D+D&E&Ecrc&JKKLLLLM M 4yy1}}a!3!3C!8!8}w))$q'..*=*=*D*D*F*FGG7%%d++ <tS7;;;CCtS7;;;C , ,%%Q..CEtAwww...6 7 7 7 &sC00 ,111111sC((((		#"||D11 ,IIdOOO, , , , , , , , , , , , , , ,   N NN*S/C  &g&7&<&<j"MMG*,,hoo
  #ssCCC/) + + , , , 14B,.ES]5K5K5KLMMMM5     DP .==dCC% P P P()@4)GHHaOP !#'7 ; ;I&)IdO+4B'(ETTT333?@@@FFFN Ns   A9 A 
A6A11A69
BBBC
D(C:9D:DDDDDB,M??NNPR$)QR$
Q&Q!!Q&&9R$#R$)r<   )rL   
__module____qualname____doc__r   tagr9   r6   r   r   rx   __classcell__)r;   s   @r   r0   r0   <   s        " " $u	   
cc	 % % % % % VN VN VN Z \VN VN VN VN VNr   r0   c                 :    |                      t                     dS )zLoad the extension in IPython.N)register_magicsr0   r*   s    r   load_ipython_extensionr      s    {#####r   r-   )r{   rb   r   r!   rd   IPython.core.errorr   IPython.core.magicr   r   r   IPython.testing.skipdoctestr   	traitletsr   r   r&   r+   r.   r0   r    r   r   <module>r      s6  	 	 " ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ) ) ) ) ) ) ? ? ? ? ? ? ? ? ? ? 4 4 4 4 4 4      ? ? ? ?& & &. . .   iN iN iN iN iN& iN iN iNX$ $ $ $ $r   