B
    >\                 @   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Zddlm	Z	m
Z
 ddlmZ dddd	d
gZedZdddZdddZdd Zdd	 Zdd Zdd Zdd Zddd
ZdS )z,
API for the command-line I{pyflakes} tool.
    )with_statementN)checker__version__)reportercheck	checkPathcheckRecursiveiterSourceCodemains   ^#!.*\bpython[23w]?\b\s*$c             C   st  |dkrt  }ytj| |d}W  n tk
r    t d }|jd }|j|j	|j
  }}}tjr|dkr|  }	t|	|kr|	|d  }tjdkrt|try|d}W n tk
r   d}Y nX |d8 }|dkr||d n|||||| dS  tk
r    ||d dS X t| }
tj||
|d}|jjd	d
 d x|jD ]}|| qVW t|jS )a  
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: The number of warnings emitted.
    @rtype: C{int}
    N)filename   r   )   asciizproblem decoding source)file_tokensr   c             S   s   | j S )N)lineno)m r   +lib/python3.7/site-packages/pyflakes/api.py<lambda>K   s    zcheck.<locals>.<lambda>)key)modReporter_makeDefaultReporterastparseSyntaxErrorsysexc_infoargsr   offsettextr   ZPYPY
splitlineslenversion_info
isinstancebytesdecodeUnicodeDecodeErrorunexpectedErrorZsyntaxError	ExceptionZmake_tokensZCheckerZmessagessortZflake)Z
codeStringr   r   Ztreevaluemsgr   r   r   linesr   wZwarningr   r   r   r      s@    


c          	   C   sr   |dkrt  }y"t| d}| }W dQ R X W n2 tk
rd   t d }|| |jd  dS X t	|| |S )z
    Check the given path, printing out any warnings detected.

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: the number of warnings printed
    Nrbr   )
r   r   openreadIOErrorr   r   r'   r   r   )r   r   fZcodestrr+   r   r   r   r   Q   s    	c          	   C   sx   |  drdS |  drdS d}y,t| d}||}|s@dS W dQ R X W n tk
r`   dS X | d }t|S )	z0Return True if filename points to a Python file.z.pyT~F   r.   Nr   )endswithr/   r0   r1   r    PYTHON_SHEBANG_REGEXmatch)r   Z	max_bytesr2   r   Z
first_liner   r   r   isPythonFilef   s    


r8   c             c   sj   xd| D ]\}t j|r\xJt |D ]4\}}}x(|D ] }t j||}t|r2|V  q2W q"W q|V  qW dS )z
    Iterate over all Python source files in C{paths}.

    @param paths: A list of paths.  Directories will be recursed into and
        any .py files found will be yielded.  Any non-directories will be
        yielded as-is.
    N)ospathisdirwalkjoinr8   )pathsr:   dirpathZdirnames	filenamesr   	full_pathr   r   r   r	   }   s    

c             C   s(   d}xt | D ]}|t||7 }qW |S )a;  
    Recursively check all source files in C{paths}.

    @param paths: A list of paths to Python source files and directories
        containing Python source files.
    @param reporter: A L{Reporter} where all of the warnings and errors
        will be reported to.
    @return: The number of warnings found.
    r   )r	   r   )r>   r   warningsZ
sourcePathr   r   r   r      s    
c                sb   ddl }yt|| }W n tk
r*   dS X  fdd}y| || W n tk
r\   Y nX dS )zHandles a signal with sys.exit.

    Some of these signals (SIGPIPE, for example) don't exist or are invalid on
    Windows. So, ignore errors that might arise.
    r   Nc                s   t   d S )N)r   exit)Zsigr2   )messager   r   handler   s    z_exitOnSignal.<locals>.handler)signalgetattrAttributeError
ValueError)ZsigNamerD   rF   Z	sigNumberrE   r   )rD   r   _exitOnSignal   s    rJ   c               C   s   dt t t f S )zQ
    Retrieve and format package version along with python version & OS used
    z%s Python %s on %s)r   platformZpython_versionsystemr   r   r   r   _get_version   s    rM   c             C   sv   ddl }tdd tdd |j| t d}|j|d\}}t }|rTt||}ntt	j
 d	|}t|dkdS )
z&Entry point for the script "pyflakes".r   NSIGINTz... stoppedSIGPIPEr   )progversion)r   z<stdin>)optparserJ   ZOptionParserrM   
parse_argsr   r   r   r   r   stdinr0   
SystemExit)rP   r   rR   parser__r   rB   r   r   r   r
      s    

)N)N)NN)__doc__Z
__future__r   r   r9   rK   rer   Zpyflakesr   r   r   r   __all__compiler6   r   r   r8   r	   r   rJ   rM   r
   r   r   r   r   <module>   s$   

=
