
NXc           @  sj  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
 d d l	 m Z d d l	 m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z m Z m Z m Z m Z d d l m Z d d l m Z d d l m Z d d l m  Z  d d l! m" Z" d d l# m$ Z$ d d l% m& Z& e d e f d     Y Z' d S(   s   Implementation of magic functions for interaction with the OS.

Note: this module is named 'osm' instead of 'os' to avoid a collision with the
builtin.
i(   t   print_functionN(   t   pformat(   t   magic_arguments(   t   oinspect(   t   page(   t
   AliasErrort   Alias(   t
   UsageError(   t   Magicst   compress_dhistt   magics_classt
   line_magict
   cell_magict   line_cell_magic(   t   skip_doctest(   t   source_to_unicode(   t   unquote_filename(   t
   abbrev_cwd(   t	   py3compat(   t   unicode_type(   t   set_term_titlet   OSMagicsc        
   B  s  e  Z d  Z e e d d    Z e d d   Z e d d   Z e e d d    Z e e d d    Z	 e d d   Z
 e d    Z e d d	   Z e d d
   Z e d d   Z e d d   Z e e d d    Z e d d d   Z e d  e  Z e d  e  Z e d d   Z e d d   Z e j   e j d d d d d e d d e j d d e d d e d       Z RS(   sJ   Magics to interact with the underlying OS (shell-type functionality).
    t    c         C  s   | j    } | sK t |  j j j  } t d t |   t j j	   | Sy | j
 d d  \ } } Wn( t k
 r t t j |  j   d SXy |  j j j | |  Wn t k
 r } t |  n Xd S(   sv  Define an alias for a system command.

        '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'

        Then, typing 'alias_name params' will execute the system command 'cmd
        params' (from your underlying operating system).

        Aliases have lower precedence than magic functions and Python normal
        variables, so if 'foo' is both a Python variable and an alias, the
        alias can not be executed until 'del foo' removes the Python variable.

        You can use the %l specifier in an alias definition to represent the
        whole line when the alias is called.  For example::

          In [2]: alias bracket echo "Input in brackets: <%l>"
          In [3]: bracket hello world
          Input in brackets: <hello world>

        You can also define aliases with parameters using %s specifiers (one
        per parameter)::

          In [1]: alias parts echo first %s second %s
          In [2]: %parts A B
          first A second B
          In [3]: %parts A
          Incorrect number of arguments: 2 expected.
          parts is an alias to: 'echo first %s second %s'

        Note that %l and %s are mutually exclusive.  You can only use one or
        the other in your aliases.

        Aliases expand Python variables just like system calls using ! or !!
        do: all expressions prefixed with '$' get expanded.  For details of
        the semantic rules, see PEP-215:
        http://www.python.org/peps/pep-0215.html.  This is the library used by
        IPython for variable expansion.  If you want to access a true shell
        variable, an extra $ is necessary to prevent its expansion by
        IPython::

          In [6]: alias show echo
          In [7]: PATH='A Python string'
          In [8]: show $PATH
          A Python string
          In [9]: show $$PATH
          /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...

        You can use the alias facility to acess all of $PATH.  See the %rehash
        and %rehashx functions, which automatically create aliases for the
        contents of your $PATH.

        If called with no parameters, %alias prints the current alias table.s   Total number of aliases:i   N(   t   stript   sortedt   shellt   alias_managert   aliasest   printt   lent   syst   stdoutt   flusht   splitt   Nonet	   TypeErrorR   t   getdoct   aliast   define_aliasR   (   t   selft   parameter_st   parR   R%   t   cmdt   e(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR%   3   s    7c         C  s   | j    } y |  j j j |  Wn t k
 rC } t |  d SX|  j j j d i   } | | k r t d |  | | =| |  j j d <n  d S(   s   Remove an aliasNt   stored_aliasess   Removing %stored alias(   R   R   R   t   undefine_aliast
   ValueErrorR   t   dbt   get(   R'   R(   t   anameR+   t   stored(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   unalias   s    
c           s  d d l  m } |  j j d =g  t j j d d  j t j  D]$ } t j	 j
 t j	 j |   ^ q? } g  } t j d k r d   } n y) t j d j d	 d
  j d d  } Wn t k
 r d } n Xd | k r | d 7} n  t j d | t j      f d   } t j   } zt j d k rx| D] }	 y  t j |	  t j |	  }
 Wn t k
 roq3n Xx| |
 D]t } | |  rwy> |  j j j |  s|  j j j | j d d  |  n  Wn | k
 rqX| j |  qwqwWq3Wnt j } x | D] }	 y  t j |	  t j |	  }
 Wn t k
 rBqn Xx |
 D] } t j	 j |  \ } } | |  rJ| j   | k rJ| j   d k r| } y, |  j j j | j   j d d  |  Wn | k
 rn X| j |  qqJqJWqW| |  j j d <Wd t j |  Xd S(   s  Update the alias table with all executable files in $PATH.

        This version explicitly checks that every entry in $PATH is a file
        with execute access (os.X_OK), so it is much slower than %rehash.

        Under Windows, it checks executability as a match against a
        '|'-separated string of extensions, stored in the IPython config
        variable win_exec_ext.  This defaults to 'exe|com|bat'.

        This function also resets the root module cache of module completer,
        used on slow filesystems.
        i(   t   InvalidAliasErrort   rootmodules_cachet   PATHR   t   posixc         S  s%   t  j j |   o$ t  j |  t  j  S(   N(   t   ost   patht   isfilet   accesst   X_OK(   t   fname(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   <lambda>   s   t   pathextt   ;t   |t   .s   exe|com|bat|pyt   pys   |pys   (.*)\.(%s)$c           s   t  j j |   o   j |   S(   N(   R8   R9   R:   t   match(   R=   (   t   execre(    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR>      s    s   .exet
   syscmdlistN(   t   IPython.core.aliasR4   R   R/   R8   t   environR0   R!   t   pathsepR9   t   abspatht
   expandusert   namet   replacet   KeyErrort   ret   compilet
   IGNORECASER   t   getcwdt   chdirt   listdirt   OSErrorR   t   is_aliasR&   t   appendR   t	   blacklistt   splitextt   lower(   R'   R(   R4   t   pR9   RF   t   isexect   winextt   savedirt   pdirt   dirlistt   fft   no_aliast   baset   ext(    (   RE   sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   rehashx   sj    I)
	 c         C  s
   t  j   S(   s   Return the current working directory path.

        Examples
        --------
        ::

          In [9]: pwd
          Out[9]: '/home/tsuser/sprint/ipython'
        (   R   RR   (   R'   R(   (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   pwd   s    c         C  s  t  j   } t j d |  } | ry t | j d   } y |  j j d | } Wn t k
 ro t	 d  d SXi  } n| j
 d  rdd } d } | d } |  j j d }	 x{ t |	  D]m }
 | t j j |
  k r t j j |
  r |
 } Pn  | d k r | |
 k r t j j |
  r |
 } q q W| d k rA| } n  | d k r[t	 d  d Si  } n3 t j d d	 |  } |  j | d
 d d \ } } | d k ry |  j j d d } Wqjt k
 rt d   qjXn t j j |  sd | k rj|  j j j d i   } | | k rH| | } t	 d | | f  | } qjd | k rjt d |   qjn  t |  } | rHyO t j t j j |   t |  j d  r|  j j rt d t    n  Wn% t k
 rt	 t j   d  qXt  j   } |  j j d } | | k r| j  |  t! |  d |  j j d <qn t j |  j j"  t |  j d  r|  j j rt d  n  t  j   } |  j j d } | | k r| j  |  t! |  d |  j j d <n  d | k r|  j j d rt	 |  j j d d  n  d S(   ss  Change the current working directory.

        This command automatically maintains an internal list of directories
        you visit during your IPython session, in the variable _dh. The
        command %dhist shows this history nicely formatted. You can also
        do 'cd -<tab>' to see directory history conveniently.

        Usage:

          cd 'dir': changes to directory 'dir'.

          cd -: changes to the last visited directory.

          cd -<n>: changes to the n-th directory in the directory history.

          cd --foo: change to directory that matches 'foo' in history

          cd -b <bookmark_name>: jump to a bookmark set by %bookmark
             (note: cd <bookmark_name> is enough if there is no
              directory <bookmark_name>, but a bookmark with the name exists.)
              'cd -b <tab>' allows you to tab-complete bookmark names.

        Options:

        -q: quiet.  Do not print the working directory after the cd command is
        executed.  By default IPython's cd command does print this directory,
        since the default prompts do not display path information.

        Note that !cd doesn't work for this purpose because the shell where
        !command runs is immediately discarded after executing 'command'.

        Examples
        --------
        ::

          In [10]: cd parent/child
          /home/tsuser/parent/child
        s	   (-)(\d+)$i   t   _dhs2   The requested directory does not exist in history.Ns   --s&   No matching entry in directory historys   \\(?! )t   /t   qbt   modet   stringt   -is*   %cd -: No previous directory to change to.t   bt	   bookmarkss   (bookmark:%s) -> %ssD   Bookmark '%s' not found.  Use '%%bookmark -l' to see your bookmarks.t
   term_titles	   IPython: i   it   dhistt   ~t   qis
   IPython: ~(#   R   RR   RO   RD   t   intt   groupR   t   user_nst
   IndexErrorR   t
   startswithR"   t   reversedR8   R9   t   basenamet   isdirt   subt   parse_optionsR   R/   R0   R   RS   RK   t   hasattrRo   R   R   RU   R   t   exc_infoRW   R	   t   home_dir(   R'   R(   t   oldcwdt   numcdt   nnt   pst   optst   fallbackt   patt   dht   entt   bkmst   targett   cwdRp   (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   cd   s    *
	
**	
	
	 c         C  s   | j    r d | k r d n d } | j |  } t |  d k r | j    } | t j k rk t j | Sd j |  } t |   n  t |  d k r |  j |  Sn  t t j  S(   sC  Get, set, or list environment variables.

        Usage:\

          %env: lists all environment variables/values
          %env var: get value for var
          %env var val: set value for var
          %env var=val: set value for var
          %env var=$val: set value for var, using python expansion if possible
        t   =t    i   s"   Environment does not have key: {0}(	   R   R!   R   R8   RH   t   formatR   t   set_envt   dict(   R'   R(   R!   t   bitst   keyt   err(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   env}  s    c         C  s   d | k r d n d } | j  | d  } | j   sI t |  d k  rX t d   n  | d j   } | d j   } t j d |  r d } | j |  } t |   n  t j |  t	 j
 t j |  <t d	 j | |   d
 S(   sg  Set environment variables.  Assumptions are that either "val" is a
        name in the user namespace, or val is something that evaluates to a
        string.

        Usage:\
          %set_env var val: set value for var
          %set_env var=val: set value for var
          %set_env var=$val: set value for var, using python expansion if possible
        R   R   i   i   s   usage is 'set_env var=val'i    s   .*\s.*s.   refusing to set env var with whitespace: '{0}'s   env: {0}={1}N(   R!   R   R   R   RO   RD   R   R   t   cast_bytes_py2R8   RH   R   (   R'   R(   R!   R   t   vart   valR   (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR     s    c         C  sx   |  j  j } t j j t |   } t j   j |  j  j	 d  } | rX |  j
 |  n  | j d |  |  j  j d  S(   sk   Place the current dir on stack and change directory.

        Usage:\
          %pushd ['dirname']
        Rq   i    t   dirs(   R   t	   dir_stackR8   R9   RK   R   R   RR   RM   R   R   t   insertt   magic(   R'   R(   t   dir_st   tgtR   (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   pushd  s    c         C  sN   |  j  j s t d   n  |  j  j j d  } |  j |  t d |  d S(   s=   Change to directory popped off the top of the stack.
        s   %popd on empty stacki    s   popd ->N(   R   R   R   t   popR   R   (   R'   R(   t   top(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   popd  s
    c         C  s
   |  j  j S(   s#   Return the current directory stack.(   R   R   (   R'   R(   (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR     s    c         C  s)  |  j  j d } | r y t t | j    } Wn |  j |  j  d SXt |  d k r t t |  | d d  t |  } } q t |  d k r | \ } } t	 | t |   } q |  j |  j  d Sn d t |  } } t
 d  x/ t | |  D] } t
 d | | | f  qWd S(   s  Print your history of visited directories.

        %dhist       -> print full history\
        %dhist n     -> print last n entries only\
        %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\

        This history is automatically maintained by the %cd command, and
        always available as the global list variable _dh. You can use %cd -<n>
        to go to directory number <n>.

        Note that most of time, you should view directory history by entering
        cd -<TAB>.

        Rg   Ni   i    i   s   Directory history (kept in _dh)s   %d: %s(   R   Ru   t   mapRs   R!   t   arg_errRp   R   t   maxt   minR   t   range(   R'   R(   R   t   argst   init   fint   i(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyRp     s$    -
c   	      C  s   |  j  | d  \ } } y@ | j d d  \ } } | j   } | j d d  \ } } Wn t k
 rw d
 \ } } n Xd | k } |  j j | d | } d | k r t d | t |  f  n  | r |  j j j	 i | | 6 n | Sd	 S(   s  Shell capture - run shell command and capture output (DEPRECATED use !).

        DEPRECATED. Suboptimal, retained for backwards compatibility.

        You should use the form 'var = !command' instead. Example:

         "%sc -l myfiles = ls ~" should now be written as

         "myfiles = !ls ~"

        myfiles.s, myfiles.l and myfiles.n still apply as documented
        below.

        --
        %sc [options] varname=command

        IPython will run the given command using commands.getoutput(), and
        will then update the user's interactive namespace with a variable
        called varname, containing the value of the call.  Your command can
        contain shell wildcards, pipes, etc.

        The '=' sign in the syntax is mandatory, and the variable name you
        supply must follow Python's standard conventions for valid names.

        (A special format without variable name exists for internal use)

        Options:

          -l: list output.  Split the output on newlines into a list before
          assigning it to the given variable.  By default the output is stored
          as a single string.

          -v: verbose.  Print the contents of the variable.

        In most cases you should not need to split as a list, because the
        returned value is a special type of string which can automatically
        provide its contents either as a list (split on newlines) or as a
        space-separated string.  These are convenient, respectively, either
        for sequential processing or to be passed to a shell command.

        For example::

            # Capture into variable a
            In [1]: sc a=ls *py

            # a is a string with embedded newlines
            In [2]: a
            Out[2]: 'setup.py\nwin32_manual_post_install.py'

            # which can be seen as a list:
            In [3]: a.l
            Out[3]: ['setup.py', 'win32_manual_post_install.py']

            # or as a whitespace-separated string:
            In [4]: a.s
            Out[4]: 'setup.py win32_manual_post_install.py'

            # a.s is useful to pass as a single command line:
            In [5]: !wc -l $a.s
              146 setup.py
              130 win32_manual_post_install.py
              276 total

            # while the list form is useful to loop over:
            In [6]: for f in a.l:
              ...:      !wc -l $f
              ...:
            146 setup.py
            130 win32_manual_post_install.py

        Similarly, the lists returned by the -l option are also special, in
        the sense that you can equally invoke the .s attribute on them to
        automatically get a whitespace-separated string from their contents::

            In [7]: sc -l b=ls *py

            In [8]: b
            Out[8]: ['setup.py', 'win32_manual_post_install.py']

            In [9]: b.s
            Out[9]: 'setup.py win32_manual_post_install.py'

        In summary, both the lists and strings used for output capture have
        the following special attributes::

            .l (or .list) : value as list.
            .n (or .nlstr): value as newline-separated string.
            .s (or .spstr): value as space-separated string.
        t   lvR   i   R   t   lR!   t   vs   %s ==
%sN(   R   R   (
   R|   R!   R   R.   R   t	   getoutputR   R   Ru   t   update(	   R'   R(   R   R   R   t   _R*   R!   t   out(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   sc  s    ]c         C  s   | d k r |  j j |  S|  j | d d  \ } } |  j j |  } | j d | j d   } | r} | |  j j | <n | Sd S(   s'  Shell execute - run shell command and capture output (!! is short-hand).

        %sx command

        IPython will run the given command using commands.getoutput(), and
        return the result formatted as a list (split on '\n').  Since the
        output is _returned_, it will be stored in ipython's regular output
        cache Out[N] and in the '_N' automatic variables.

        Notes:

        1) If an input line begins with '!!', then %sx is automatically
        invoked.  That is, while::

          !ls

        causes ipython to simply issue system('ls'), typing::

          !!ls

        is a shorthand equivalent to::

          %sx ls

        2) %sx differs from %sc in that %sx automatically splits into a list,
        like '%sc -l'.  The reason for this is to make it as easy as possible
        to process line-oriented shell output via further python commands.
        %sc is meant to provide much finer control, but requires more
        typing.

        3) Just like %sc -l, this is a list with special attributes:
        ::

          .l (or .list) : value as list.
          .n (or .nlstr): value as newline-separated string.
          .s (or .spstr): value as whitespace-separated string.

        This is very useful when trying to use such lists as arguments to
        system commands.R   s   out=R   t   oN(   R"   R   R   R|   R0   Ru   (   R'   t   linet   cellR   R   t   outputt   out_name(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   sxo  s    *t   systemt   !c   
      C  s  |  j  | d d d \ } } t |  d k r? t d   n  |  j j j d i   } d | k r y | d } Wn t k
 r t d	   qXy | | =Wqt k
 r t d
 |   qXn d | k r i  } n d | k r^t |  } | rt	 t
 t |   } n d } d t |  d } t d  x | D] }	 t | |	 | |	 f  q9Wne | sst d   nP t |  d k rt j   | | d <n' t |  d k r| d | | d <n  | |  j j d <d S(   sh  Manage IPython's bookmark system.

        %bookmark <name>       - set bookmark to current dir
        %bookmark <name> <dir> - set bookmark to <dir>
        %bookmark -l           - list all bookmarks
        %bookmark -d <name>    - remove bookmark
        %bookmark -r           - remove all bookmarks

        You can later on access a bookmarked folder with::

          %cd -b <name>

        or simply '%cd <name>' if there is no directory called <name> AND
        there is such a bookmark defined.

        Your bookmarks persist through IPython sessions, but they are
        associated with each profile.t   drlRj   t   listi   s   %bookmark: too many argumentsRn   t   di    s/   %bookmark -d: must provide a bookmark to deletes)   %%bookmark -d: Can't delete bookmark '%s't   rR   s   %-s   s -> %ss   Current bookmarks:s-   %bookmark: You must specify the bookmark namei   N(   R|   R   R   R   R/   R0   Rv   RN   R   R   R   t   strR   R   RR   (
   R'   R(   R   R   R   t   todelt   bkst   sizet   fmtt   bk(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   bookmark  sB    	
c         C  sy   | s t  d   n  y |  j j | d t } Wn" t t f k
 rU t d  d SXt j |  j j t	 |    d S(   s  Show a syntax-highlighted file through a pager.

        This magic is similar to the cat utility, but it will assume the file
        to be Python source and will show it with syntax highlighting.

        This magic command can either take a local filename, an url,
        an history range (see %history) or a macro as argument ::

        %pycat myscript.py
        %pycat 7-27
        %pycat myMacro
        %pycat http://www.example.com/myscript.py
        s5   Missing filename, URL, input history range, or macro.t   skip_encoding_cookies:   Error: no such file, variable, URL, history range or macroN(
   R   R   t   find_user_codet   FalseR.   t   IOErrorR   R   t
   pycolorizeR   (   R'   R(   t   cont(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   pycat  s    
s   -as   --appendt   actiont
   store_truet   defaultt   helps_   Append contents of the cell to an existing file. The file will be created if it does not exist.t   filenamet   types   file to writec         C  s   t  j |  j |  } t j j t | j   } t j j |  rm | j	 r\ t
 d |  q{ t
 d |  n t
 d |  | j	 r d n d } t j | | d d  } | j |  Wd QXd S(	   s   Write the contents of the cell to a file.
        
        The file will be overwritten unless the -a (--append) flag is specified.
        s   Appending to %ss   Overwriting %ss
   Writing %st   at   wt   encodings   utf-8N(   R   t   parse_argstringt	   writefileR8   R9   RK   R   R   t   existsRW   R   t   iot   opent   write(   R'   R   R   R   R   Rj   t   f(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR     s    	N(   t   __name__t
   __module__t   __doc__R   R   R%   R3   Re   Rf   R   R   R   R   R   R   Rp   R   R   R"   R   R   R   t   bangR   R   R   t   argumentR   R   R   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyR   .   sX   MU	%r5;	((   R   t
   __future__R    R   R8   RO   R   t   pprintR   t   IPython.coreR   R   R   RG   R   R   t   IPython.core.errorR   t   IPython.core.magicR   R	   R
   R   R   R   t   IPython.testing.skipdoctestR   t   IPython.utils.openpyR   t   IPython.utils.pathR   t   IPython.utils.processR   t   IPython.utilsR   t   IPython.utils.py3compatR   t   IPython.utils.terminalR   R   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/osm.pyt   <module>   s*   .