
    f                         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y)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                   j                  di       }|5|j                         D ]!  \  }}| j                  j	                  ||       # y | j                  j	                  |||          y )Nstored_aliases)dbgetitemsalias_managerdefine_alias)ipalias	staliaseskvs        X/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/extensions/storemagic.pyrestore_aliasesr      sk    		*B/I}??$ 	/CAa ))!A.	/
 	%%eYu-=>    c                 ,   | j                   }|j                  d      D ]6  }t        j                  j	                  |      }	 ||   }|| j
                  |<   8 y # t        $ r2 t        d|z         t        dt        j                         d          Y tw xY w)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 #&BJJw
&
  	7Y\ccd"CLLN1$56	7s   A8BBc                 V    | j                   j                  dg       | j                  d<   y )Ndhist_dh)r   r   r   r   s    r   restore_dhistr+   2   s    		'"-BJJur   c                 F    t        |        t        |        t        |        y N)r&   r   r+   r*   s    r   restore_datar.   6   s    bB"r   c                   r     e Zd ZdZ edd      j                  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                  |        | j                  rt        | j                         y y )N)shell)superr0   __init__r4   configurablesappendautorestorer.   )selfr4   	__class__s     r   r6   zStoreMagics.__init__H   sG    k4))6

  ''-$ r   c                 8   | j                  |dd      \  }}|j                         }| j                  }|j                  }d|v r	 |d   }	 |d|z   = yd
|v r|j                  d      D ]  }	||	=  yd|v r.|r |D ]  }
	 |d|
z      }||j                  |
<    yt        |       y|s|j                  d      }|j                          |rt        t        t         |            }nd}t        d       dt#        |      z   dz   }|j$                  }|D ]D  }t&        j(                  j+                  |      }t        ||t-         ||d            dd fz         F yt!        |      dkD  r$|d   j/                  d      rt&        j(                  j1                  |d   j3                  d      j3                               }|d   j/                  d      rt5        |dd      }nt5        |dd      }|5  |j7                  |d         }t        d|d   d|j8                  j:                  d|d       t=        |t"              sddlm}  |||       n3|jA                  |       |jC                  d       s|jA                  d        ddd       y|D ]  }
	 |j                  |
   }tE        tG        jH                  |      d!d"      }|d#k(  r't        tK        jL                  d$|
d%|d&              y||d|
z   <   t        d'|
d|j8                  j:                  d(        y# t        $ r}t        d|z        |d}~ww xY w# t        $ r}t        d	      |d}~ww xY w# t        $ r0 	 t        ||
       n# t        $ r t        d|
z         Y nw xY wY w xY w# 1 sw Y   yxY w# t        $ rq |
}	 |jN                  jQ                  |      }n # tR        $ r}t        d)|z        |d}~ww xY w|j%                  d*i       }|||<   ||d*<   t        d+|d,|d(       Y  yw xY w)-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__ __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;   rK   
isinstancerI   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filrI   modnamenamecmdr   s                            r   storezStoreMagics.storeN   s&   \ ''Ex'H
U{{}ZZUU$;RQR>E12 D[WW_- qE D[ 	.C. #!56 +.

3	. R  77?+DIIK3sD>*<=s4y.*C&&C M''**3/cWd3sO+D&Ecr&JKKLM 4y1}a!3!3C!8ww))$q'..*=*D*D*FG7%%d+tS7;CtS7;C ,%%Q.CtAw..6 7 &sC01sC(		#"||D1IIdO,   NN**S/C  &g&7&7&<j"MG*,hoo
  #C/) + , 14B,.S]]5K5KLM;NA % R$%AE%IJPQQR  S !JKQRRS" $ JJ+Bc:' J!"BS"HIJJH,    DP ..==dC% P()@4)GHaOP !#'7 ;I&)IdO+4B'(T3?@s   L: L <M+BNN	L7#L22L7:	MMM	N!M/.N/N	NN		NNNP+OP	O$OO$$1PP)rL   )rK   
__module____qualname____doc__r   tagr9   r6   r   r   rx   __classcell__)r;   s   @r   r0   r0   <   sQ    " u	 
cc	 % VN  VNr   r0   c                 .    | j                  t               y)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      s\   	 " ! ) ? ? 4 ?&. iN& iN iNX$r   