o
    û›hè  ã                   @   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   úSc:\Users\shubh\Videos\agent\nova\Lib\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¡}| ¡  |r´ttt|ƒƒ}nd}t
dƒ dt|ƒ d }|j}|D ]}tj |¡}t
||t||dƒƒdd… f ƒ qÇd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�rÛt
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    
 -