a
    ø£Beá  ã                   @   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_managerZdefine_alias)ÚipÚaliasÚ	staliasesÚkÚv© r   ú`/home/yuhw/wc/wire-cell-python/wcpy/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 )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                    s4   t t| ƒj|d� | jj | ¡ | jr0t| jƒ 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 r²z|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 qè0 ||j|
< qènt|ƒ �nŽ|�sì| d¡}| ¡  |�rŒt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¡�rètj |d  d¡ ¡ ¡}|d  d¡�rDt|ddd�}nt|ddd�}|�| | |d ¡}t
d|d |jj|f ƒ t|tƒ�s¤ddlm} |||ƒ n |  |¡ | !d¡�sÄ|  d¡ W d  ƒ n1 �sÚ0    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�rÈt
t( )d$|
|f ¡ƒ  dS ||d|
 < t
d%|
|jjf ƒ �qì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.
        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__r1   Ú__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evr0   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)r1   )r?   Ú
__module__Ú__qualname__Ú__doc__r   Útagr-   r+   r   r   rb   Ú__classcell__r   r   r/   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    rh   )N)re   rS   r   r   rU   ZIPython.core.errorr   ZIPython.core.magicr   r   r   ZIPython.testing.skipdoctestr   Z	traitletsr   r   r"   r$   r%   r&   rh   r   r   r   r   Ú<module>   s    
 -