ó
æNXc           @  sß   d  Z  d d l m 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 „  Z d	 „  Z d
 „  Z d „  Z e d e
 f d „  ƒ  Yƒ Z d „  Z d S(   sõ   
%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
iÿÿÿÿ(   t   print_functionN(   t
   UsageError(   t   Magicst   magics_classt
   line_magic(   t   Bool(   t   string_typesc         C  sI   |  j  j d i  ƒ } x- | j ƒ  D] \ } } |  j j | | ƒ q" Wd  S(   Nt   stored_aliases(   t   dbt   gett   itemst   alias_managert   define_alias(   t   ipt	   staliasest   kt   v(    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   restore_aliases&   s    c         C  s   |  j  } x} | j d ƒ D]l } t j j | ƒ } y | | } Wn6 t k
 rw t d | ƒ t d t j ƒ  d ƒ q X| |  j	 | <q Wd  S(   Ns   autorestore/*sE   Unable to restore variable '%s', ignoring (use %%store -d to forget!)s   The error was:i    (
   R   t   keyst   ost   patht   basenamet   KeyErrort   printt   syst   exc_infot   user_ns(   R   R   t   keyt   justkeyt   obj(    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   refresh_variables.   s    	c         C  s    |  j  j d g  ƒ |  j d <d  S(   Nt   dhistt   _dh(   R   R	   R   (   R   (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   restore_dhist=   s    c         C  s"   t  |  ƒ t |  ƒ t |  ƒ d  S(   N(   R   R   R!   (   R   (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   restore_dataA   s    

t   StoreMagicsc           B  sA   e  Z d  Z e e d e d d ƒZ d „  Z e d d „ ƒ Z	 RS(   sM   Lightweight persistence for python variables.

    Provides the %store magic.t   configt   helpsd   If True, any %store-d variables will be automatically restored
        when IPython starts.
        c         C  sI   t  t |  ƒ j d | ƒ |  j j j |  ƒ |  j rE t |  j ƒ n  d  S(   Nt   shell(   t   superR#   t   __init__R&   t   configurablest   appendt   autorestoreR"   (   t   selfR&   (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyR(   S   s    	t    c         C  sF  |  j  | d d d ƒ\ } } | j d d ƒ } |  j } | j } d | k r¨ y | d } Wn t k
 r{ t d ƒ ‚ qBXy | d | =WqBt d	 | ƒ ‚ qBXnšd
 | k rØ x‹| j d ƒ D] } | | =qÄ Wnjd | k rL| r?x\ | D]G }	 y | d |	 }
 Wn t k
 r*t	 d |	 ƒ qñ X|
 | j
 |	 <qñ WqBt | ƒ nö| s| j d ƒ } | j ƒ  | r‰t t t | ƒ ƒ } n d } t	 d ƒ d t | ƒ d } | j } x‰| D]? } t j j | ƒ } t	 | | t | | d ƒ ƒ d  f ƒ q½Wn?t | ƒ d k r| d j d ƒ rt j j | d j d ƒ j ƒ  ƒ } | d j d ƒ rrt | d ƒ } n t | d ƒ } | j | d ƒ }
 t	 d | d |
 j j | f ƒ t |
 t ƒ säd d l m } | |
 | ƒ n, | j  |
 ƒ |
 j! d ƒ s| j  d ƒ n  | j" ƒ  d Sy | j
 | d }
 Wn t k
 rÅ| d } y | j# j$ | ƒ } Wn! t% k
 r†t d | ƒ ‚ n X| j d i  ƒ } | | | <| | d <t	 d | | f ƒ d SXt& t' j( |
 ƒ d d  ƒ } | d! k rt	 t) j* d" | d |
 f ƒ ƒ d S|
 | d | d <t	 d# | d |
 j j f ƒ d S($   sù  Lightweight persistence for python variables.

        Example::

          In [1]: l = ['hello',10,'world']
          In [2]: %store l
          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``     - Store the *current* value of the variable spam
                                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 from store (overwrite
                                current vals)
        * ``%store -r spam bar`` - Refresh specified variables 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.
        t   drzt   modet   stringi   t   di    s'   You must provide the variable to forgets   autorestore/s   Can't delete variable '%s't   zs   autorestore/*t   rs   no stored variable %ss(   Stored variables and their in-db values:s   %-s   s -> %ss   <unavailable>i2   t   >s   >>t   at   ws   Writing '%s' (%s) to file '%s'.iÿÿÿÿ(   t   pprints   
Ns   Unknown variable '%s'R   s   Alias stored: %s (%s)t   __name__R-   t   __main__s"                      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.
                    s   Stored '%s' (%s)(+   t   parse_optionst   splitt   NoneR&   R   t
   IndexErrorR   R   R   R   R   R"   t   sortt   maxt   mapt   lent   strR	   R   R   R   t   reprt
   startswitht
   expandusert   lstript   opent   evt	   __class__R8   t
   isinstanceR   R7   t   writet   endswitht   closeR   t   retrieve_aliast
   ValueErrort   getattrt   inspectt	   getmodulet   textwrapt   dedent(   R,   t   parameter_st   optst   argslt   argsR   R   t   todelR   t   argR   t   varst   sizet   fmtR	   t   varR   t   fnamt   filR7   t   namet   cmdR   t   modname(    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   storeY   s’    +		

	.%%



	(
   R8   t
   __module__t   __doc__R   t   Falset   TrueR+   R(   R   Rd   (    (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyR#   G   s   		c         C  s   |  j  t ƒ d S(   s   Load the extension in IPython.N(   t   register_magicsR#   (   R   (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   load_ipython_extensionî   s    (   Rf   t
   __future__R    RQ   R   R   RS   t   IPython.core.errorR   t   IPython.core.magicR   R   R   t	   traitletsR   t   IPython.utils.py3compatR   R   R   R!   R"   R#   Rj   (    (    (    sU   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/extensions/storemagic.pyt   <module>   s   0				¦