Format: application/x-python-code
⬇ Download File (6.7 KB)
   1  o
   2  
   3  ��h��@s�dZddlZddlZddlZddlZddlmZddlmZm	Z	m
   4  Z
   5  ddlmZddl
   6  mZddd�Zd	d
   7  �Zdd�Zd
   8  d�Ze	Gdd�de��Zdd�ZdS)z�
   9  %store magic for lightweight persistence.
  10  
  11  Stores variables, aliases and macros in IPython's database.
  12  
  13  To automatically restore stored variables at startup, add this to your
  14  :file:`ipython_config.py` file::
  15  
  16    c.StoreMagics.autorestore = True
  17  �N)�
  18  UsageError)�Magics�magics_class�
  19  line_magic)�skip_doctest)�BoolcCsP|j�di�}|dur|��D]\}}|j�||�qdS|j�|||�dS)N�stored_aliases)�db�get�items�
  20  alias_managerZdefine_alias)�ip�alias�	staliases�k�v�r�Sc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/extensions/storemagic.py�restore_aliasess�rc	Csn|j}|�d�D],}tj�|�}z||}Wnty.td|�tdt��d�Yqw||j	|<qdS)N�
  21  autorestore/*zEUnable to restore variable '%s', ignoring (use %%store -d to forget!)zThe error was:r)
  22  r	�keys�os�path�basename�KeyError�print�sys�exc_info�user_ns)r
  23  r	�key�justkey�objrrr�refresh_variables#s��r"cCs|j�dg�|jd<dS)NZdhistZ_dh)r	r
  24  r�r
  25  rrr�
  26  restore_dhist2sr$cCst|�t|�t|�dS�N)r"rr$r#rrr�restore_data6sr&csFeZdZdZeddd�jdd�Z�fdd�Zee	dd
  27  d���Z
  28  �ZS)
  29  �StoreMagicszMLightweight persistence for python variables.
  30  
  31      Provides the %store magic.FzdIf True, any %store-d variables will be automatically restored
  32          when IPython starts.
  33          )�helpT)�configcs8tt|�j|d�|jj�|�|jrt|j�dSdS)N)�shell)�superr'�__init__r*Z
  34  configurables�append�autorestorer&)�selfr*��	__class__rrr,Hs
  35  �zStoreMagics.__init__�cCs�|j|ddd�\}}|��}|j}|j}d|vrLz|d}Wnty/}ztd�|�d}~wwz|d|=WdStyK}ztd	|�|�d}~wwd
  36  |vr]|�d�D]}	||	=qUdSd|vr�|r�|D]1}
  37  z|d|
  38  }Wn!ty�zt	||
  39  d
  40  �Wnty�t
  41  d|
  42  �YnwYqew||j|
  43  <qedSt|�dS|s�|�d�}|�
  44  �|r�ttt|��}
  45  nd}
  46  t
  47  d�dt|
  48  �d}|j}|D]}tj�|�}t
  49  ||t||d��dd�f�q�dSt|�dk�rr|d�d��rrtj�|d�d����}|d�d��rt|ddd�}nt|ddd�}|�O|�|d�}t
  50  d|d|jj|f�t|t��s?ddlm}|||�n |� |�|�!d��sW|� d�Wd�dSWd�dSWd�dS1�skwYdS|D]x}
  51  z|j|
  52  }WnAt�y�|
  53  }z|j"�#|�}Wnt$�y�}ztd|�|�d}~ww|�di�}|||<||d<t
  54  d ||f�YdSwt%t&�'|�d!d"�}|d#k�r�t
  55  t(�)d$|
  56  |f��dS||d|
  57  <t
  58  d%|
  59  |jjf��qtdS)&a�Lightweight persistence for python variables.
  60  
  61          Example::
  62  
  63            In [1]: l = ['hello',10,'world']
  64            In [2]: %store l
  65            Stored 'l' (list)
  66            In [3]: exit
  67  
  68            (IPython session is closed and started again...)
  69  
  70            ville@badger:~$ ipython
  71            In [1]: l
  72            NameError: name 'l' is not defined
  73            In [2]: %store -r
  74            In [3]: l
  75            Out[3]: ['hello', 10, 'world']
  76  
  77          Usage:
  78  
  79          * ``%store``          - Show list of all variables and their current
  80                                  values
  81          * ``%store spam bar`` - Store the *current* value of the variables spam
  82                                  and bar to disk
  83          * ``%store -d spam``  - Remove the variable and its value from storage
  84          * ``%store -z``       - Remove all variables from storage
  85          * ``%store -r``       - Refresh all variables, aliases and directory history
  86                                  from store (overwrite current vals)
  87          * ``%store -r spam bar`` - Refresh specified variables and aliases from store
  88                                     (delete current val)
  89          * ``%store foo >a.txt``  - Store value of foo to new file a.txt
  90          * ``%store foo >>a.txt`` - Append value of foo to file a.txt
  91  
  92          It should be noted that if you change the value of a variable, you
  93          need to %store it again if you want to persist the new value.
  94  
  95          Note also that the variables will need to be pickleable; most basic
  96          python types can be safely %store'd.
  97  
  98          Also aliases can be %store'd across sessions.
  99          To remove an alias from the storage, use the %unalias magic.
 100          Zdrz�string)�mode�drz'You must provide the variable to forgetNzautorestore/zCan't delete variable '%s'�zr�r)rzno stored variable or alias %sz(Stored variables and their in-db values:z%-zs -> %sz
 101  <unavailable>�2��>z>>�azutf-8)�encoding�wzWriting '%s' (%s) to file '%s'.)�pprint�
 102  zUnknown variable '%s'rzAlias stored: %s (%s)�__name__r2�__main__a6                        Warning:%s is %s
 103                          Proper storage of interactively declared classes (or instances
 104                          of those classes) is not possible! Only instances
 105                          of classes in real modules on file system can be %%store'd.
 106                          zStored '%s' (%s))*Z
 107  parse_options�splitr*r	�
 108  IndexErrorr�
 109  BaseExceptionrrrrrr&�sort�max�map�len�strr
 110  rrr�repr�
 111  startswith�
 112  expanduser�lstrip�openZevr1r@�
 113  isinstancer>�write�endswithrZretrieve_alias�
 114  ValueError�getattr�inspect�	getmodule�textwrap�dedent)r/Zparameter_s�optsZargsl�argsr
 115  r	Ztodel�er�argr!�vars�size�fmtr
 116  �varr ZfnamZfilr>�name�cmdr�modnamerrr�storeNs�.
 117  ���������
 118  $�
 119  �
 120  
 121  �
 122  �
 123  �
 124  �
 125  �
 126  �
 127  ���
 128  ��zStoreMagics.store)r2)r@�
 129  __module__�__qualname__�__doc__r�tagr.r,rrrc�
 130  __classcell__rrr0rr'<s��r'cCs|�t�dS)zLoad the extension in IPython.N)Zregister_magicsr'r#rrr�load_ipython_extension�srir%)rfrTrrrVZIPython.core.errorrZIPython.core.magicrrrZIPython.testing.skipdoctestrZ	traitletsrrr"r$r&r'rirrrr�<module>s 
 131  -