ó
ďĘSc           @   s¸   d  Z  d d l Z d d l Z d d l Z d   Z d   Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d S(   s                  file.py   
 
 Useful utilities for file editing/manipulation

              by Andrea Mignone 

 Last modified Feb 2nd, 2008
i˙˙˙˙Nc         C   sB   y t  |  d  } Wn( t k
 r= d |  d GHt j   n X| S(   Nt   rs!    ! file.openr: Cannot open file 't   '(   t   opent   IOErrort   syst   exit(   t   fnamet   fl(    (    s   ./PLUTO//Tools/Python/file.pyt   openr   s    c         C   s&   t  |   } | j   } | j   | S(   N(   R   t	   readlinest   close(   R   R   t   lines(    (    s   ./PLUTO//Tools/Python/file.pyt   read   s    
c         C   s;   t  |  d  } x | D] } | j |  q W| j   d  S(   Nt   w(   R   t   writeR
   (   t   f_nameR   R   t   tmp(    (    s   ./PLUTO//Tools/Python/file.pyt   create_file*   s     c         C   s+   t  |   } | | | d 5t |  |  d  S(   Ni   (   R   R   (   R   t   lbegt   lendt   scrh(    (    s   ./PLUTO//Tools/Python/file.pyt   delete_lines7   s    c         C   s8   t  |   } t |  } | | k r- | } n  | | | !S(   N(   R   t   len(   R   R   R   R   t   lscrh(    (    s   ./PLUTO//Tools/Python/file.pyt
   read_linesD   s
     	c         C   s-   t  |   } | g | | | +t |  |  d  S(   N(   R   R   (   R   t   stringR   R   (    (    s   ./PLUTO//Tools/Python/file.pyt   insertR   s    c         C   s4   t  |   } | | =| g | | d +t |  |  d  S(   Ni   (   R   R   (   R   t	   newstringt   nlineR   (    (    s   ./PLUTO//Tools/Python/file.pyt   replace_line^   s    c         C   s   t  |  | d d d d S(   Nt   actionR   t   wantt   list(   t   find(   R   t   str(    (    s   ./PLUTO//Tools/Python/file.pyt   string_listk   s    c         C   s   t  |  | d d d d S(   NR   R   R   t   enum(   R!   (   R   R"   (    (    s   ./PLUTO//Tools/Python/file.pyt   string_findu   s    c         C   s   t  |  | d d d d S(   NR   t   wordR   R    (   R!   (   R   R"   (    (    s   ./PLUTO//Tools/Python/file.pyt	   word_list   s    c         C   s   t  |  | d d d d S(   NR   R&   R   R$   (   R!   (   R   R"   (    (    s   ./PLUTO//Tools/Python/file.pyt	   word_find   s    c   
      C   sń   t  |   } g  } g  } d } xŹ | D]¤ } | d } t j |  }	 | d k r t j | |  d k rÉ | j |  | j |  qÉ q% | d k r% |	 j |  d k rÉ | j |  | j |  qÉ q% q% W| d k rÝ | S| d k rí | Sd  S(   Ni˙˙˙˙i   R   i    R&   R    R$   (   R   R   t   splitR!   t   appendt   count(
   R   R"   R   R   R   t   line_strt   line_numt   iposR   t   x(    (    s   ./PLUTO//Tools/Python/file.pyR!      s&    
c         C   sO   t  |   } d } x6 | D]. } t t j |   d k r | d } q q W| S(   Ni    i   (   R   R   R   R)   (   R   R   t   nlinesR/   (    (    s   ./PLUTO//Tools/Python/file.pyt   count_linesł   s    c   
      C   s  y t  |  d  } Wn- t k
 rB d GHd |  d GHt j   n Xg  } x t |  D] } | j   qV W| d k  r | j   } n2 x/ t | | d  D] } | j | j    q Wg  } xP | D]H } t j	 |  } t
 |  d k rÄ x | D] }	 | j |	  qň WqÄ qÄ W| S(   NR    s   Error in count_words [file.py]:s   Cannot open file 'R   i    i   (   R   R   R   R   t   ranget   readlineR	   R*   R   R)   R   (
   R   t   i_linet   f_lineR   R   t   iR   t   yyt	   this_lineR/   (    (    s   ./PLUTO//Tools/Python/file.pyt   count_wordsÂ   s(    	 c         C   sD   t  |   } | | } | | } t |  | |  t |  | |  d  S(   N(   R   R   (   R   t   l1t   l2R   t   line_1t   line_2(    (    s   ./PLUTO//Tools/Python/file.pyt
   swap_linesę   s
    

(   t   __doc__R   t   osR   R   R   R   R   R   R   R   R#   R%   R'   R(   R!   R1   R9   R>   (    (    (    s   ./PLUTO//Tools/Python/file.pyt   <module>   s$   								
	
				(