o
    EDiX                     @  s  U 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	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 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 ddlmZ ddlm Z  ddl!m"Z" e#e$e%e&e&e%ee% f  Z'e(e)Z*ej+hZ,de-d< de-d< ej.d6ddZ/d7d d!Z0d8d%d&Z1G d'd( d(Z2G d)d* d*Z3d9d.d/Z4d:d4d5Z5dS );z$Checker Manager and Checker classes.    )annotationsN)	Generator)Sequence)Any)Optional)defaults)
exceptions)	processor)utils)FSTRING_START)TSTRING_START)expand_paths)
parse_args)Checkers)LoadedPlugin)StyleGuideManagerr   _mp_pluginsargparse.Namespace_mp_optionspluginsoptionsreturnGenerator[None]c                 c  s$    | |a azd V  W b bd S b bw N)r   r   )r   r    r   G/var/www/Datamplify/venv/lib/python3.10/site-packages/flake8/checker.py_mp_prefork4   s
   
r   argvSequence[str]Nonec                 C  sL   t  t jt j zttf W d S  ty%   t| \}}|j|aaY d S w r   )signalSIGINTSIG_IGNr   r   	NameErrorr   checkers)r   r   r   r   r   r   _mp_initA   s   r%   filenamestr#tuple[str, Results, dict[str, int]]c                 C  s   t | ttd S )Nr&   r   r   )FileCheckerr   r   
run_checksr&   r   r   r   _mp_runO   s
   r-   c                   @  st   e Zd ZdZd%d
dZd&ddZd'ddZd(ddZd)ddZd&ddZ	d&ddZ
d&ddZd&d d!Zd&d"d#Zd$S )*ManageraR  Manage the parallelism and checker instances for each plugin and file.

    This class will be responsible for the following:

    - Determining the parallelism of Flake8, e.g.:

      * Do we use :mod:`multiprocessing` or is it unavailable?

      * Do we automatically decide on the number of jobs to use or did the
        user provide that?

    - Falling back to a serial way of processing files if we run into an
      OSError related to :mod:`multiprocessing`

    - Organizing the results of each checker so we can group the output
      together and make our output deterministic.
    style_guider   r   r   r   r   r   r   c                 C  sV   || _ |j| _|| _|  | _ddddd| _g | jj| jjR | _|| _g | _	dS )z Initialize our Manager instance.r   )fileslogical linesphysical linestokensN)
r/   r   r   
_job_countjobs
statisticsexcludeextend_excluder   results)selfr/   r   r   r   r   r   __init__h   s   

zManager.__init__c                 C  sP   | j D ]\}}}tjD ]}| j|  || 7  < qq| jd  t| j7  < d S )Nr0   )r9   r   STATISTIC_NAMESr6   len	filenames)r:   _r6   	statisticr   r   r   _process_statistics}   s
   
zManager._process_statisticsintc                 C  sP   t | jjrtd dS | jj}|jr%zt	 W S  t
y$   Y dS w |jS )Nz]The --jobs option is not compatible with supplying input using - . Ignoring --jobs arguments.r   )r
   is_using_stdinr   r>   LOGwarningr5   is_automultiprocessing	cpu_countNotImplementedErrorn_jobs)r:   r5   r   r   r   r4      s   
zManager._job_countr&   r'   r9   Resultsc           
   
   C  s<   | j }d}|D ]\}}}}}	||j||||||	d7 }q|S )Nr   )coder&   line_numbercolumn_numbertextphysical_line)r/   handle_error)
r:   r&   r9   r/   reported_results_count
error_coderM   columnrO   rP   r   r   r   _handle_results   s   
zManager._handle_resultstuple[int, int]c              	   C  s   d }}| j jtdd | j D ]3\}}}|jtddd | j| || ||7 }W d   n1 s9w   Y  |t|7 }q||fS )a  Report all of the errors found in the managed file checkers.

        This iterates over each of the checkers and reports the errors sorted
        by line number.

        :returns:
            A tuple of the total results found and the results reported.
        r   )key      N)r9   sortoperator
itemgetterr/   processing_filerU   r=   )r:   results_reportedresults_foundr&   r9   r?   r   r   r   report   s   	zManager.reportc                 C  s   t | j| j t| j| j}W d   n1 sw   Y  |du r(|   dS d}z#t|t	| j
| _|  |  d}W |sL|  |  dS dS |sY|  |  w w )zRun the checkers in parallel.NFT)r   r   r   _try_initialize_processpoolr5   r   
run_seriallistimap_unorderedr-   r>   r9   closejoin	terminate)r:   poolpool_closedr   r   r   run_parallel   s&   
zManager.run_parallelc                   s    fdd j D  _dS )zRun the checkers in serial.c                   s"   g | ]}t | j jd  qS )r)   )r*   r   r   r+   ).0r&   r:   r   r   
<listcomp>   s    z&Manager.run_serial.<locals>.<listcomp>N)r>   r9   rl   r   rl   r   rb      s   
zManager.run_serialc                 C  sX   z| j dkrt| jdkr|   W dS |   W dS  ty+   td t	dw )a#  Run all the checkers.

        This will intelligently decide whether to run the checks in parallel
        or whether to run them in serial.

        If running the checks in parallel causes a problem (e.g.,
        :issue:`117`) this also implements fallback to serial processing.
        rX   z"Flake8 was interrupted by the userzEarly quit while running checksN)
r5   r=   r>   rj   rb   KeyboardInterruptrD   rE   r   	EarlyQuitrl   r   r   r   run   s   	

zManager.runc                 C  sF   t d tt| jj| jj| jj| jd| _t	t
| j| j| _dS )zStart checking files.

        :param paths:
            Path names to check. This is passed directly to
            :meth:`~Manager.make_checkers`.
        zMaking checkers)pathsstdin_display_namefilename_patternsr7   N)rD   infotupler   r   r>   rr   r&   r7   minr=   r5   rl   r   r   r   start   s   
zManager.startc                 C  s   |    dS )zStop checking files.N)rA   rl   r   r   r   stop     zManager.stopN)r/   r   r   r   r   r   r   r   r   r   )r   rB   )r&   r'   r9   rK   r   rB   )r   rV   )__name__
__module____qualname____doc__r;   rA   r4   rU   r`   rj   rb   rp   rw   rx   r   r   r   r   r.   U   s    








r.   c                   @  s   e Zd ZdZd9d
dZd:ddZd;ddZd<ddZd=ddZe	d>d"d#Z
d?d$d%Zd?d&d'Zd@d)d*Zd?d+d,ZdAd.d/ZdBd1d2ZdCd6d7Zd8S )Dr*   z;Manage running checks for a file and aggregate the results.r&   r'   r   r   r   r   r   r   c                C  sx   || _ || _|| _g | _dddd| _|  | _|| _d| _| jdur:| jj| _| j	  | _t
| jj| jd< dS dS )zInitialize our file checker.r   )r3   r1   r2   FNr2   )r   r&   r   r9   r6   _make_processorr	   display_nameshould_processshould_ignore_filer=   lines)r:   r&   r   r   r   r   r   r;     s    


zFileChecker.__init__c                 C  s   d| j  S )z)Provide helpful debugging representation.zFileChecker for r,   rl   r   r   r   __repr__%  ry   zFileChecker.__repr__processor.FileProcessor | Nonec                 C  sZ   z	t | j| jW S  ty, } z| dddt|j d|  W Y d }~d S d }~ww )NE902r   : )r	   FileProcessorr&   r   OSErrorr`   typer{   )r:   er   r   r   r   )  s    zFileChecker._make_processorrS   
str | NonerM   rB   rT   rO   c                 C  sX   |du r| dd\}}t| dr| jdur| j|}nd}| j|||||f |S )z2Report an error by storing it in the results list.N rX   r	   )splithasattrr	   noqa_line_forr9   append)r:   rS   rM   rT   rO   liner   r   r   r`   6  s   zFileChecker.reportpluginr   	argumentsr   c              
   K  s   | j dus
J | jz
| j |j|}W n ty( } ztj|j|dd}~ww z|jdi ||W S  t	yS } zt
jd|jdd tj| j|j|dd}~ww )z!Run the check in a single plugin.N)plugin_name	exceptionz(Plugin %s raised an unexpected exceptionT)exc_info)r&   r   r   r   )r	   r&   keyword_arguments_for
parametersAttributeErrorr    PluginRequestedUnknownParametersr   obj	ExceptionrD   criticalPluginExecutionFailed)r:   r   r   paramsaeall_excr   r   r   	run_checkK  s4   zFileChecker.run_checkr   r   rV   c           
      C  s  t | jdkr#| jd r#t | jd dkr#| jd }|dd \}}n&t| tjrCt | jdkrCt | jd dkrCd}| jd \}}nd}d\}}|dkr|rt| trt |dkrd}d}|d }|d ur|dd}t |d }|d }t |}	||	kr|	}||8 }||8 }||fS )	NrX   rY      r   )rX   r   r      
)r=   args
isinstancetokenize
TokenErrorSyntaxErrorrstripr   )
r   tokenrowrT   column_offset
row_offsetrP   r   logical_linelogical_line_lengthr   r   r   _extract_syntax_informationd  sB   

	z'FileChecker._extract_syntax_informationc           	   	   C  s   | j dus
J | j| j  }| jjD ]-}| j||d}z| }W n ty-   |}Y nw |D ]\}}}}| jd|||d q0qdS )z1Run all checks expecting an abstract syntax tree.N)treerS   rM   rT   rO   )	r	   r&   	build_astr   r   r   rp   r   r`   )	r:   astr   checkerrunnerrM   offsetrO   r?   r   r   r   run_ast_checks  s$   
zFileChecker.run_ast_checksc           
      C  s   | j dusJ | j  \}}}|sdS | j | td|  | jjD ]:}| j | | j	||dp5d}|D ]&\}}t
||\}}	||	  krMdkrUn ntd| | jd||	|d q8q%| j   dS )z(Run all checks expecting a logical line.NzLogical line: "%s")r   r   r   z#position of error out of bounds: %sr   )r	   build_logical_lineupdate_staterD   debugr   r   r   update_checker_state_forr   find_offsetrE   r`   next_logical_line)
r:   commentsr   mappingr   r9   r   rO   rM   r   r   r   r   run_logical_checks  s*   zFileChecker.run_logical_checksrP   c              
   C  s   | j dusJ | jjD ]C}| j | | j||d}|durNd}z|d }W n ttfy1   Y nw t|tr:|f}|D ]}|\}}| j	d| j j
||d q<qdS )zoRun all checks for a given physical line.

        A single physical check may return multiple errors.
        N)rP   r   r   )r	   r   rP   r   r   
IndexError	TypeErrorr   rB   r`   rM   )r:   rP   r   resultr   result_singlerO   r   r   r   run_physical_checks  s.   
zFileChecker.run_physical_checksc                 C  s   | j dusJ d}| j}| j }d}| D ]6}|d  d7  < | || |dd \}}|tjkr9t ||}n|dkrGt |rG| | |d }q|j	r]| 
|jd  |   dS dS )	zProcess tokens and trigger checks.

        Instead of using this directly, you should use
        :meth:`flake8.checker.FileChecker.run_checks`.
        Nr    r3   rX   rY   r   )r	   r6   generate_tokenscheck_physical_eolr   OPcount_parenthesestoken_is_newlinehandle_newliner3   r   r   r   )r:   parensr6   file_processorprev_physicalr   
token_typerO   r   r   r   process_tokens  s&   



zFileChecker.process_tokensr(   c                 C  s   | j du s| js| j| j| jfS z
|   |   W nA ttj	fy[ } z2t
|tj	r-dnd}| |\}}| |||t|j d|jd   | j| j| jfW  Y d}~S d}~ww | j jd }|| jd< | j| j| jfS )zRun checks against the file.Nr   E999r   r   r1   )r	   r   r   r9   r6   r   r   r   r   r   r   r   r`   r   r{   r   )r:   r   rL   r   rT   logical_linesr   r   r   r+     s   &
zFileChecker.run_checksr   c                 C  sb   | j dusJ |tjkr|   | j   dS t| j jdkr+| j   | j   dS |   dS )z3Handle the logic when encountering a newline token.NrX   )	r	   r   NEWLINEr   reset_blank_beforer=   r3   visited_new_blank_linedelete_first_token)r:   r   r   r   r   r     s   

zFileChecker.handle_newliner   tokenize.TokenInfor   c                 C  s   | j dusJ |jtkr| j |jd  dS |jtkr'| j |jd  dS t |r@|jdkr8| 	| dS | 	|j dS t 
|rS| j |D ]	}| 	| qKdS dS )z@Run physical checks if and only if it is at the end of the line.Nr   r   )r	   r   r   fstring_startrw   r   tstring_startis_eol_tokenr   r   is_multiline_stringmultiline_string)r:   r   r   r   r   r   r   r   '  s   




zFileChecker.check_physical_eolN)r&   r'   r   r   r   r   r   r   )r   r'   )r   r   )
rS   r   rM   rB   rT   rB   rO   r'   r   r'   )r   r   r   r   r   r   )r   r   r   rV   rz   )rP   r'   r   r   )r   r(   )r   rB   r   r   )r   r   r   r'   r   r   )r{   r|   r}   r~   r;   r   r   r`   r   staticmethodr   r   r   r   r   r+   r   r   r   r   r   r   r*   	  s     





:




r*   	job_countrB    multiprocessing.pool.Pool | Nonec              
   C  sZ   z
t j| t|fdW S  ty# } z|jtvr W Y d}~dS d}~w ty,   Y dS w )z@Return a new process pool instance if we are able to create one.)initargsN)rG   Poolr%   r   errnoSERIAL_RETRY_ERRNOSImportError)r   r   errr   r   r   ra   I  s   

ra   r   r   processor._LogicalMappingrV   c                 C  sX   t | tr| S |D ]}|d }| |kr|d } nq	d}d } }|d |d |  | fS )z*Find the offset tuple for a single offset.r   rX   )r   r   )r   ru   )r   r   r   token_offsetpositionr   r   r   r   Y  s   
r   )r   r   r   r   r   r   )r   r   r   r   )r&   r'   r   r(   )r   rB   r   r   r   r   )r   rB   r   r   r   rV   )6r~   
__future__r   argparse
contextlibr   loggingmultiprocessing.poolrG   r[   r    r   collections.abcr   r   typingr   r   flake8r   r   r	   r
   flake8._compatr   r   flake8.discover_filesr   flake8.options.parse_argsr   flake8.plugins.finderr   r   flake8.style_guider   rc   ru   r'   rB   rK   	getLoggerr{   rD   ENOSPCr   __annotations__contextmanagerr   r%   r-   r.   r*   ra   r   r   r   r   r   <module>   sT    



 5  
B