
]Lc        
   @   s`  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d Z d Z d Z d Z d j e e d d d	 d
 d d d g e d d    Z d j e e e d    Z d d d d d d d d d g	 Z i e d d d e  d 6e d d d d  d 6Z d Z d d  d  Z d!   Z d"   Z e e e e e d#  Z d$ e f d%     YZ  d&   Z! d' e" f d(     YZ# e#   Z$ d) e" f d*     YZ% d+ e" f d,     YZ& e d-  Z' e d.  Z( d/   Z) d0   Z* d1   Z+ e d2  Z, d3   Z- d4   Z. e d5  Z/ e0 d6 k r\e,   n  d S(7   s    grin searches text files.
iNs   1.2.1i    i   t    i   i   i	   i
   i   i   i   i    i   t   blackt   redt   greent   yellowt   bluet   magentat   cyant   whitet   defaultt   fgt   boldt   filenamet   bgt
   searchterms   i   i   c         C   s   |  j  t t  } t |  S(   s    Determine if a string is classified as binary rather than text.

    Parameters
    ----------
    bytes : str

    Returns
    -------
    is_binary : bool
    (   t	   translatet   ALLBYTESt	   TEXTCHARSt   bool(   t   bytest   nontext(    (    s#   lib/python2.7/site-packages/grin.pyt   is_binary_string,   s    c         C   s   d g } d } |  j  } x t r | j d | d  } | d k  ry | d t |  k  ro | j t |   n  | | f S| j | d  | |  j k r | |  j k  r | d 7} q q Wd S(   s    Compute the list of offsets in DataBlock 'block' which correspond to
    the beginnings of new lines.

    Returns: (offset list, count of lines in "current block")
    i    s   
ii   N(   t   datat   Truet   findt   lent   appendt   startt   end(   t   blockt   line_offsetst
   line_countt   st   next_newline(    (    s#   lib/python2.7/site-packages/grin.pyt   get_line_offsets:   s    			
c   	      C   s   g  } | t  k r/ | j t  j |  d  n  | t  k rX | j t  j |  d  n  | rn | j d  n  | r | j d  n  | r | j d  n  d d j t t |   d } d	 } | |  | S(
   s   Wraps a string with ANSI color escape sequences corresponding to the
    style parameters given.
    
    All of the color and style parameters are optional.
    
    Parameters
    ----------
    s : str
    fg : str
        Foreground color of the text.  One of (black, red, green, yellow, blue, 
        magenta, cyan, white, default)
    bg : str
        Background color of the text.  Color choices are the same as for fg.
    bold : bool
        Whether or not to display the text in bold.
    underline : bool
        Whether or not to underline the text.
    reverse : bool
        Whether or not to show the text in reverse video.

    Returns
    -------
    A string with embedded color escape sequences.
    i   i(   i   i   i   s   [t   ;t   ms   [0m(   t   COLOR_TABLER   t   indext   joint   mapt   str(	   R    R
   R   R   t	   underlinet   reverset   style_fragmentst   style_startt	   style_end(    (    s#   lib/python2.7/site-packages/grin.pyt   colorizeT   s     t   Optionsc           B   s   e  Z d  Z d   Z RS(   s    Simple options.
    c         O   s    t  j |  | |  |  |  _ d  S(   N(   t   dictt   __init__t   __dict__(   t   selft   argst   kwds(    (    s#   lib/python2.7/site-packages/grin.pyR2      s    (   t   __name__t
   __module__t   __doc__R2   (    (    (    s#   lib/python2.7/site-packages/grin.pyR0      s   c          C   sg   t  d d d d d t d t d t d t d t d	 t d
 t d t   d t   d t d t d d  }  |  S(   s#    Populate the default options.
    t   before_contexti    t   after_contextt   show_line_numberst
   show_matcht   show_filenamet
   show_emacst   skip_hidden_dirst   skip_hidden_filest   skip_backup_filest	   skip_dirst	   skip_extst   skip_symlink_dirst   skip_symlink_filest   binary_bytesi   (   R0   R   t   Falset   set(   t   opt(    (    s#   lib/python2.7/site-packages/grin.pyt   default_options   s     			t	   DataBlockc           B   s&   e  Z d  Z d d d d e d  Z RS(   s;   This class holds a block of data read from a file, along with
    some preceding and trailing context.

    Attributes
    ----------
    data  : byte string
    start : int
        Offset into 'data' where the "current block" begins; everything
        prior to this is 'before' context bytes
    end : int
        Offset into 'data' where the "current block" ends; everything
        after this is 'after' context bytes
    before_count : int
        Number of lines contained in data[:start]
    is_last : bool
        True if this is the final block in the file
    R    i    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   R   R   R   t   before_countt   is_last(   R4   R   R   R   RM   RN   (    (    s#   lib/python2.7/site-packages/grin.pyR2      s
    				(   R7   R8   R9   RH   R2   (    (    (    s#   lib/python2.7/site-packages/grin.pyRL      s   t   GrepTextc           B   sV   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d d  Z	 e
 d  Z RS(	   s    Grep a single file for a regex by iterating over the lines in a file.

    Attributes
    ----------
    regex : compiled regex
    options : Options or similar
    c         C   sP   | |  _  t j | j | j t j B |  _ | d  k rC t   } n  | |  _	 d  S(   N(
   t   regext   ret   compilet   patternt   flagst	   MULTILINEt   regex_mt   NoneRK   t   options(   R4   RP   RX   (    (    s#   lib/python2.7/site-packages/grin.pyR2      s
    	"c         C   s$  | d
 k r6 t } | j |  } t |  | k  } nC t | | j   d  } t t |  } | j |  } | | k } | d
 k r | r t d | d d d t |  d d d t  } | St	 } n  | j
 d k r d }	 d }
 nr | j
 d }	 d }
 xR t |  j j  D]> } | j j d d |	  } | }	 |
 d 7}
 | d k  r	Pq	q	W|	 d 7}	 | j |	 | j
 !} | j | j
 | | rd	 n	 | j   } | rd	 } n: g  t |  j j  D] } | j   ^ q} d	 j |  } t d | | | d t |  d t |  t |  d |
 d |  } | S(   si   Read a block of data from the file, along with some surrounding
        context.
        
        Parameters
        ----------
        prev : DataBlock, or None
            The result of the previous application of read_block_with_context(),
            or None if this is the first block.

        fp : filelike object
            The source of block data.
        
        fp_size : int or None
            Size of the file in bytes, or None if the size could not be
            determined.
        
        Returns
        -------
        A DataBlock representing the "current" block along with context.
        i    R   R   R   RM   RN   i   s   
R    N(   RW   t   READ_BLOCKSIZEt   readR   t   maxt   tellt   minRL   R   t   EMPTY_DATABLOCKR   t   rangeRX   R:   R   t   rfindt   readlineR;   R'   (   R4   t   prevt   fpt   fp_sizet   target_io_sizet
   block_maint   is_last_blockt	   remainingt   resultt   before_startRM   t   it   ofst   before_linest
   curr_blockt   after_linest   after_lines_list(    (    s#   lib/python2.7/site-packages/grin.pyt   read_block_with_context   sX    			

	+	c         C   sP  g  } d } t  | t j  r' d } nW y= t j | j    } t j | j	  r] | j
 } n d } Wn t k
 r} d } n X|  j d | |  } x | j | j k r<|  j | | | j  \ } } | | 7} | j r Pn  |  j | | |  }	 |	 j |	 j k r3| d k r&t |  \ }
 } n  | | 7} n  |	 } q W|  j |  } | S(   sp   Do a full grep.

        Parameters
        ----------
        fp : filelike object
            An open filelike object.

        Returns
        -------
        A list of 4-tuples (lineno, type (POST/PRE/MATCH), line, spans).  For
        each tuple of type MATCH, **spans** is a list of (start,end) positions
        of substrings that matched the pattern.
        i    N(   t
   isinstancet   gzipt   GzipFileRW   t   ost   fstatt   filenot   statt   S_ISREGt   st_modet   st_sizet   AttributeErrorRq   R   R   t   do_grep_blockRM   RN   R"   t   uniquify_context(   R4   Rc   t   contextR   Rd   t   statusR   t   block_line_countt   block_contextt
   next_blockt   _t   unique_context(    (    s#   lib/python2.7/site-packages/grin.pyt   do_grep#  s4    	

	
	
c            s    j  j   j  j   g  } d  d }        f d   } x[  j j  j  j  j ! D]7 }  d k r t	   \  } n  | | |  7} qk W| | f S(   sj   Grep a single block of file content.

        Parameters
        ----------
        block : DataBlock
            A chunk of file data.

        line_num_offset: int
            The number of lines preceding block.data.

        Returns
        -------
        Tuple of the form
            (line_count, list of (lineno, type (POST/PRE/MATCH), line, spans).
        'line_count' is either the number of lines in the block, or None if
        the line_count was not computed.  For each 4-tuple of type MATCH,
        **spans** is a list of (start,end) positions of substrings that matched
        the pattern.
        c      	      s_  t  j   |  j    j  d } t  |  } t   t   d | d  }  j  |  | d !} g   j j |  D] } | j   ^ q } g  t | | |  D]2 } |  t	  j  |  | d !d  f ^ q } g  t | d | | d  D]2 } |  t  j  |  | d !d  f ^ q}	 |  t | | f g }
 | |
 |	 S(   Ni   (   t   bisectR   R]   R   R   RP   t   finditert   spanR_   t   PRERW   t   POSTt   MATCH(   t   matcht   match_line_numRM   t   after_countt
   match_lineR$   t   spansRk   t
   before_ctxt	   after_ctxt	   match_ctx(   t   aftert   beforeR   t   line_num_offsetR   R4   (    s#   lib/python2.7/site-packages/grin.pyt   build_match_contextp  s    #!+IQN(
   RX   R:   R;   RW   RV   R   R   R   R   R"   (   R4   R   R   R   R   R   R   (    (   R   R   R   R   R   R4   s#   lib/python2.7/site-packages/grin.pyR}   V  s    )c         C   s   | j    g  } x t j | d    D]i } x` | d D]; \ } } } } | t k r7 | j | | | | f  Pq7 q7 W| j | | | | f  q& W| S(   s@    Remove duplicate lines from the list of context lines.
        c         S   s   |  d S(   Ni    (    (   t   ikl(    (    s#   lib/python2.7/site-packages/grin.pyt   <lambda>  s    i   (   t   sortt	   itertoolst   groupbyR   R   (   R4   R   R   t   groupRk   t   kindt   linet   matches(    (    s#   lib/python2.7/site-packages/grin.pyR~     s    
c      
   C   s4  t  |  d k r d Sg  } |  j j sB d | } | j |  n|  j j r | d k	 r |  j j r d | } |  j j r t | t	 j
 d i    } n  | j |  n  |  j j r d } n |  j j r d } n d } xC| D];\ } } } } |  j j r| t k rd	 t	 k rt	 d	 }	 | }
 d } xw | D]l \ } } |
 | | !} | | 7} | | 7} t | |	  } | |  | | | } | t  |  t  |  7} q6Wn  t d
 | d d i d t 6d t 6d t 6| d | d |  } | | } | j |  | j d  s | j d  q q Wd j |  } | S(   s2   Return a string showing the results.

        Parameters
        ----------
        context_lines : list of tuples of (int, PRE/MATCH/POST, str, spans)
            The lines of matches and context.
        filename : str, optional
            The name of the file being grepped, if one exists. If not provided,
            the filename may not be printed out.

        Returns
        -------
        text : str
            This will end in a newline character if there is any text. Otherwise, it
            might be an empty string without a newline.
        i    R    s   %s
s   %s:
R   s!   %(filename)s:%(lineno)s: %(line)ss   %(lineno)5s %(sep)s %(line)ss   %(line)sR   t   linenoi   t   sept   -t   +t   :R   s   
N(   R   RX   R=   R   R>   RW   R?   t	   use_colorR/   t   COLOR_STYLEt   getR<   R   R1   R   R   t   endswithR'   (   R4   t   context_linesR   t   linesR   t   templateRk   R   R   t   stylet	   orig_linet   total_offsetR   R   t   old_substringt   color_substringt   nst   text(    (    s#   lib/python2.7/site-packages/grin.pyt   report  sN    
%
		$


!
	
c         C   ss   | d k r t  j } d } n | | d  } z |  j |  } Wd | d k r\ | j   n  X|  j | |  } | S(   s   Grep a single file that actually exists on the file system.

        Parameters
        ----------
        filename : str
            The file to open.
        opener : callable
            A function to call which creates a file-like object. It should
            accept a filename and a mode argument like the builtin open()
            function which is the default.

        Returns
        -------
        report : str
            The grep results as text.
        R   s   <STDIN>t   rN(   t   syst   stdinR   t   closeR   (   R4   R   t   openert   fR   R   (    (    s#   lib/python2.7/site-packages/grin.pyt   grep_a_file  s    		N(   R7   R8   R9   RW   R2   Rq   R   R}   R~   R   t   openR   (    (    (    s#   lib/python2.7/site-packages/grin.pyRO      s   	T	3	4	@t   FileRecognizerc        	   B   st   e  Z d  Z e e e e   e   e e d d  Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z RS(
   s   Configurable way to determine what kind of file something is.

    Attributes
    ----------
    skip_hidden_dirs : bool
        Whether to skip recursing into hidden directories, i.e. those starting
        with a "." character.
    skip_hidden_files : bool
        Whether to skip hidden files.
    skip_backup_files : bool
        Whether to skip backup files.
    skip_dirs : container of str
        A list of directory names to skip. For example, one might want to skip
        directories named "CVS".
    skip_exts : container of str
        A list of file extensions to skip. For example, some file names like
        ".so" are known to be binary and one may want to always skip them.
    skip_symlink_dirs : bool
        Whether to skip symlinked directories.
    skip_symlink_files : bool
        Whether to skip symlinked files.
    binary_bytes : int
        The number of bytes to check at the beginning and end of a file for
        binary characters.
    i   c	   
      C   s   | |  _  | |  _ | |  _ | |  _ t   |  _ t   |  _ xQ | D]I }	 t j	 j
 d |	  d |	 k r| |  j j |	  qC |  j j |	  qC W| |  _ | |  _ | |  _ d  S(   Ns   foo.bari   (   R@   RA   RB   RC   RI   t   skip_exts_simplet   listt   skip_exts_endswithRu   t   patht   splitextt   addR   RE   RF   RG   (
   R4   R@   RA   RB   RC   RD   RE   RF   RG   t   ext(    (    s#   lib/python2.7/site-packages/grin.pyR2     s    				 		c         C   s,   t  | d  } |  j |  } | j   | S(   s    Determine if a given file is binary or not.

        Parameters
        ----------
        filename : str

        Returns
        -------
        is_binary : bool
        t   rb(   R   t   _is_binary_fileR   (   R4   R   R   t	   is_binary(    (    s#   lib/python2.7/site-packages/grin.pyR   1  s    
c         C   s7   y | j  |  j  } Wn t k
 r, } t SXt |  S(   s    Determine if a given filelike object has binary data or not.

        Parameters
        ----------
        f : filelike object

        Returns
        -------
        is_binary : bool
        (   RZ   RG   t	   ExceptionR   R   (   R4   R   R   t   e(    (    s#   lib/python2.7/site-packages/grin.pyR   A  s
    c         C   s   t  } t | d  } | j d  } | j   | t k r t j |  } z2 y |  j |  } Wn t k
 ry t  } n XWd | j   Xn  | S(   s'   Determine if a given file is a gzip-compressed text file or not.

        If the uncompressed file is binary and not text, then this will return
        False.

        Parameters
        ----------
        filename : str

        Returns
        -------
        is_gzipped_text : bool
        R   i   N(   RH   R   RZ   R   t
   GZIP_MAGICRs   R   t   IOError(   R4   R   t   is_gzipped_textR   t   markerRc   (    (    s#   lib/python2.7/site-packages/grin.pyR   T  s    
c         C   sk   yR t  j |  j } t j |  r1 |  j |  St j |  rM |  j |  Sd SWn t k
 rf d SXd S(   s   Determine what kind of thing a filename represents.

        It will also determine what a directory walker should do with the
        file:

            'text' :
                It should should be grepped for the pattern and the matching
                lines displayed. 
            'binary' : 
                The file is binary and should be either ignored or grepped
                without displaying the matching lines depending on the
                configuration.
            'gzip' :
                The file is gzip-compressed and should be grepped while
                uncompressing.
            'directory' :
                The filename refers to a readable and executable directory that
                should be recursed into if we are configured to do so.
            'link' :
                The filename refers to a symlink that should be skipped.
            'unreadable' :
                The filename cannot be read (and also, in the case of
                directories, is not executable either).
            'skip' :
                The filename, whether a directory or a file, should be skipped
                for any other reason.

        Parameters
        ----------
        filename : str

        Returns
        -------
        kind : str
        t   skipt
   unreadableN(   Ru   Rx   Rz   Ry   t   recognize_filet   S_ISDIRt   recognize_directoryt   OSError(   R4   R   Rz   (    (    s#   lib/python2.7/site-packages/grin.pyt	   recognizes  s    $c         C   st   t  j j |  d } |  j r> | j d  r> | d k r> d S|  j r] t  j j |  r] d S| |  j k rp d Sd S(   s0    Determine what to do with a directory.
        it   .s   ..R   t   linkt	   directory(   R   s   ..(   Ru   R   t   splitR@   t
   startswithRE   t   islinkRC   (   R4   R   t   basename(    (    s#   lib/python2.7/site-packages/grin.pyR     s    c         C   s.  t  j j |  d } |  j r2 | j d  r2 d S|  j rN | j d  rN d S|  j rm t  j j |  rm d St  j j	 |  } t  j j
 |  d } | |  j k s | j d  r d Sx$ |  j D] } | j |  r d Sq Wy1 |  j |  r
|  j |  rd Sd	 Sn d
 SWn t t f k
 r)d SXd S(   s+    Determine what to do with a file.
        iR   R   t   ~R   i   s   .~Rs   t   binaryR   R   N(   Ru   R   R   RA   R   RB   R   RF   R   t   normcaseR   R   R   R   R   R   R   (   R4   R   R   t   filename_ncR   (    (    s#   lib/python2.7/site-packages/grin.pyR     s,    c         c   s   |  j  |  } | d k r* | | f Vd S| d k r y t j |  } Wn t k
 r] d SXxW t |  D]F } t j j | |  } x( |  j |  D] \ } } | | f Vq Wqk Wn  d S(   s   Walk the tree from a given start path yielding all of the files (not
        directories) and their kinds underneath it depth first.

        Paths which are recognized as 'skip', 'link', or 'unreadable' will
        simply be passed over without comment.

        Parameters
        ----------
        startpath : str

        Yields
        ------
        filename : str
        kind : str
        R   R   Rs   NR   (   R   R   Rs   (   R   Ru   t   listdirR   t   sortedR   R'   t   walk(   R4   t	   startpathR   t	   basenamesR   R   t   fnt   k(    (    s#   lib/python2.7/site-packages/grin.pyR     s    (   R7   R8   R9   RH   RI   R   R2   R   R   R   R   R   R   R   (    (    (    s#   lib/python2.7/site-packages/grin.pyR     s   				2		c         C   s  |  d} k r0 t j d d d d d t j  }  n  |  j d d d d	 d	 d
 t d d |  j d d d d d d d t j d g  d d |  j d d d d d t d d |  j d d d d d t d d |  j d d d t d d |  j d  d! d d" d d# |  j d$ d% d d& d d' d t	 d d( |  j d) d* d d+ d d' d d, |  j d- d. d d& d d/ d t	 d d0 |  j d1 d d+ d d/ d d2 |  j d3 d d& d d4 d d5 |  j d6 d7 d d+ d d8 d d9 |  j d: d; d d& d d8 d t
 d d< |  j d= d d& d t j d> k d d? |  j d@ d d+ d dA d dB |  j dC d d& d dD |  j dE dF d dG d d+ d dH |  j dI d dG d d& d t	 d dJ |  j dK dL d dM d d+ d dN |  j dO d dM d d& d t	 d dP |  j dQ dR d dS d d+ d dT |  j dU d dS d t	 d d& d dV |  j dW dX d dY d dZ |  j d[ d\ d d] d d^ d d_ d d` |  j da db d dc d dd |  j de df d dg d d^ d d_ d dh |  j di d d+ d dj d t
 d dk |  j dl d d& d dj d dm |  j dn do dp dq d dr |  j ds dt d d& d du |  j dv d d& d dw |  j dx d dy |  j dz d{ d" d d| |  S(~   s%    Create the command-line parser.
    t   descriptions,   Search text files for a given regex pattern.t   epilogs2   Bug reports to <enthought-dev@mail.enthought.com>.t   formatter_classs   -vs	   --versiont   actiont   versions   grin %st   helps&   show program's version number and exits   -is   --ignore-caset   append_constt   destt   re_flagst   constR	   s   ignore case in the regexs   -As   --after-contexti    t   typesL   the number of lines of context to show after the match [default=%(default)r]s   -Bs   --before-contextsM   the number of lines of context to show before the match [default=%(default)r]s   -Cs	   --contextsB   the number of lines of context to show on either side of the matchs   -Is	   --includet   *s=   only search in files matching this glob [default=%(default)r]s   -ns   --line-numbert
   store_trueR<   s   show the line numbers [default]s   -Ns   --no-line-numbert   store_falses   do not show the line numberss   -Hs   --with-filenameR>   s0   show the filenames of files that match [default]s   --without-filenames-   do not show the filenames of files that matchs   --emacsR?   sD   print the filename with every match for easier parsing by e.g. Emacss   -ls   --files-with-matchesR=   s8   show only the filenames and not the texts of the matchess   -Ls   --files-without-matchess#   show the matches with the filenamess
   --no-colort   win32s:   do not use colorized output [default if piping the output]s   --use-colort   no_colors:   use colorized output [default if outputting to a terminal]s   --force-colors[   always use colorized output even when piping to something that may not be able to handle its   -ss   --no-skip-hidden-filesRA   s   do not skip .hidden filess   --skip-hidden-filess   do skip .hidden files [default]s   -bs   --no-skip-backup-filesRB   s8   do not skip backup~ files [deprecated; edit --skip-exts]s   --skip-backup-filess>   do skip backup~ files [default] [deprecated; edit --skip-exts]s   -Ss   --no-skip-hidden-dirsR@   s   do not skip .hidden directoriess   --skip-hidden-dirss%   do skip .hidden directories [default]s   -ds   --skip-dirss    CVS,RCS,.svn,.hg,.bzr,build,distsE   comma-separated list of directory names to skip [default=%(default)r]s   -Ds   --no-skip-dirsRC   t   store_constR    s   do not skip any directoriess   -es   --skip-extssk   .pyc,.pyo,.so,.o,.a,.tgz,.tar.gz,.rar,.zip,~,#,.bak,.png,.jpg,.gif,.bmp,.tif,.tiff,.pyd,.dll,.exe,.obj,.libsE   comma-separated list of file extensions to skip [default=%(default)r]s   -Es   --no-skip-extsRD   s   do not skip any file extensionss   --no-followt   follow_symlinkss9   do not follow symlinks to directories and files [default]s   --follows(   follow symlinks to directories and filess   -fs   --files-from-filet   metavart   FILEs;   read files to search from a file, one per line; - for stdins   -0s   --null-separateds>   filenames specified in --files-from-file are separated by NULss
   --sys-paths"   search the directories on sys.pathRP   s$   the regular expression to search fort   filest   nargss   the files to searchN(   RW   t   argparset   ArgumentParsert   RawDescriptionHelpFormattert   add_argumentt   __version__RQ   t   It   intR   RH   R   t   platform(   t   parser(    (    s#   lib/python2.7/site-packages/grin.pyt   get_grin_arg_parser  s    	!c         C   sh  |  dH k r' t j d d d d  }  n  |  j d d d d d d	 t d
 d |  j d d d d d d d
 d |  j d d d d d d t d
 d |  j d d d d d d d
 d |  j d d d d d d t d
 d |  j d d d d d d d
 d |  j d  d d d t d d d
 d! |  j d" d# d d$ d
 d% |  j d& d' d d( d d) d* d+ d
 d, |  j d- d. d d/ d
 d0 |  j d1 d2 d d3 d d) d* d+ d
 d4 |  j d5 d d d d6 d t d
 d7 |  j d8 d d d d6 d
 d9 |  j d: d; d d d
 d< |  j d= d> d? d d@ g d
 dA |  j dB d d d
 dC |  j dD d dE d> dF d
 dG |  S(I   sI    Create the command-line parser for the find-like companion program.
    R   s7   Find text and binary files using similar rules as grin.R   s2   Bug reports to <enthought-dev@mail.enthought.com>.s   -vs	   --versionR   R   s   grin %sR   s&   show program's version number and exits   -ss   --no-skip-hidden-filesR   RA   R   s   do not skip .hidden filess   --skip-hidden-filesR   R	   s   do skip .hidden filess   -bs   --no-skip-backup-filesRB   s8   do not skip backup~ files [deprecated; edit --skip-exts]s   --skip-backup-filess>   do skip backup~ files [default] [deprecated; edit --skip-exts]s   -Ss   --no-skip-hidden-dirsR@   s   do not skip .hidden directoriess   --skip-hidden-dirss   do skip .hidden directoriess   -ds   --skip-dirss    CVS,RCS,.svn,.hg,.bzr,build,distsE   comma-separated list of directory names to skip [default=%(default)r]s   -Ds   --no-skip-dirsRC   R   R   R    s   do not skip any directoriess   -es   --skip-extssk   .pyc,.pyo,.so,.o,.a,.tgz,.tar.gz,.rar,.zip,~,#,.bak,.png,.jpg,.gif,.bmp,.tif,.tiff,.pyd,.dll,.exe,.obj,.libsE   comma-separated list of file extensions to skip [default=%(default)r]s   -Es   --no-skip-extsRD   s   do not skip any file extensionss   --no-followR   s9   do not follow symlinks to directories and files [default]s   --follows(   follow symlinks to directories and filess   -0s   --null-separateds%   print the filenames separated by NULss   --dirsR   R   R   s   the directories to start froms
   --sys-paths"   search the directories on sys.patht   globR   t   ?sy   the glob pattern to match; you may need to quote this to prevent the shell from trying to expand it [default=%(default)r]N(   RW   R   R   R  R  R   RH   (   R  (    (    s#   lib/python2.7/site-packages/grin.pyt   get_grind_arg_parserO  sd    	c         C   sl   t  d |  j d |  j d |  j d t |  j j d   d t |  j j d   d |  j d |  j  } | S(	   sA    Get the file recognizer object from the configured options.
    RA   RB   R@   RC   t   ,RD   RF   RE   (	   R   RA   RB   R@   RI   RC   R   RD   R   (   R5   t   fr(    (    s#   lib/python2.7/site-packages/grin.pyt   get_recognizer  s    			
c         c   s  g  } |  j  d k	 r |  j  d k r6 t j } t } nC t j j |  j   rc t |  j   } t	 } n t
 d d |  j    zk |  j r | j g  | j   j d  D] } | j   ^ q  n& | j g  | D] } | j   ^ q  Wd | r | j   n  Xn  | j |  j  |  j r*| j t j  n  t j d k r]t d d g  } t d	 g  } n t   } t d	 d
 g  } g  | D]* } | | k r| j   | k r| ^ q} t |  d k rd g } n  t |   } x | D] } | d k r| d f Vqn  | j |  }	 |	 d k rMt j t j j |  |  j  rM| |	 f Vq|	 d k rx[ | j |  D]G \ }
 } | d k rit j t j j |
  |  j  ri|
 | f VqiqiWqqWd S(   s9   Generate the filenames to grep.

    Parameters
    ----------
    args : Namespace
        The commandline arguments object.

    Yields
    ------
    filename : str
    kind : either 'text' or 'gzip'
        What kind of file it is.

    Raises
    ------
    IOError if a requested file cannot be found.
    R   i   s   No such file: %rs    NR   s   NUL:t   NULR    s	   /dev/nulli    R   R   Rs   R   (   R   Rs   (   R   Rs   (   t   files_from_fileRW   R   R   RH   Ru   R   t   existsR   R   R   t   null_separatedt   extendRZ   R   t   stripR   R   t   sys_pathR  RI   t   upperR   R  R   t   fnmatchR   t   includeR   (   R5   R   t
   files_filet   should_closet   xt	   upper_badt   raw_badR   R  R   R   R   (    (    s#   lib/python2.7/site-packages/grin.pyt   get_filenames  sL    				8*		7--c         C   s7   d } x |  j  D] } | | O} q Wt j |  j |  S(   s3    Get the compiled regex object to search with.
    i    (   R   RQ   RR   RP   (   R5   RT   t   flag(    (    s#   lib/python2.7/site-packages/grin.pyt	   get_regex  s    c         C   s  yI|  d  k rL t j t j d d   } t j d g | t j d }  n  t   } | j |  d  } | j	 d  k	 r | j	 | _
 | j	 | _ n  | j p | j o t j j   o t j j d  d k | _ t |  } t | |  } t d t d t j  } xC t |  D]5 \ } } | j | d	 | | }	 t j j |	  qWWnS t k
 rht d   n7 t k
 r}
 d
 t |
  k rt d   n    n Xd  S(   Nt	   GRIN_ARGSR    i    i   t   TERMt   dumbR   Rs   R   s   Broken pipe(   RW   t   shlexR   Ru   t   getenvR   t   argvR  t
   parse_argsR   R:   R;   t   force_colorR   t   stdoutt   isattyt   environR   R   R  RO   R1   R   Rs   R  R   t   writet   KeyboardInterruptt
   SystemExitR   R)   (   R%  t   env_argsR  R5   RP   t   gt   openersR   R   R   R   (    (    s#   lib/python2.7/site-packages/grin.pyt	   grin_main  s0    "	c         C   s	   |  GHd  S(   N(    (   R   (    (    s#   lib/python2.7/site-packages/grin.pyt
   print_line  s    c         C   s$   t  j j |   t  j j d  d  S(   Ns    (   R   R(  R+  (   R   (    (    s#   lib/python2.7/site-packages/grin.pyt
   print_null  s    c   
      C   sh  y|  d  k rL t j t j d d   } t j d g | t j d }  n  t   } | j |  d  } | j	 rz t
 } n t } | j r | j j t j  n  t |  } x_ | j D]T } xK | j |  D]: \ } } t j t j j |  | j  r | |  q q Wq WWnS t k
 r-t d   n7 t k
 rc}	 d t |	  k r]t d   n    n Xd  S(   Nt
   GRIND_ARGSR    i    i   s   Broken pipe(   RW   R#  R   Ru   R$  R   R%  R
  R&  R  R3  R2  R  t   dirsR  R   R  R   R  R   R  R,  R-  R   R)   (
   R%  R.  R  R5   t   outputR  t   dirR   R   R   (    (    s#   lib/python2.7/site-packages/grin.pyt
   grind_main  s,    "				!t   __main__(1   R9   R   R  Rs   R   Ru   RQ   R#  Rx   R   R   R  R   R   R   R'   R(   t   chrR_   R   R   R%   R1   R   R   R   RY   R   R"   RW   RH   R/   R0   RK   t   objectRL   R^   RO   R   R  R
  R  R  R  R1  R2  R3  R8  R7   (    (    (    s#   lib/python2.7/site-packages/grin.pyt   <module>   sX   :		,			 C`;		O	
		 