
    gJX                       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  ddl!m"Z" eee#e$e$e#ee#   f      Z% ejL                  e'      Z(ejR                  hZ*de+d<   de+d<   ejX                  	 	 	 	 	 	 d!d       Z-d"dZ.d#dZ/ G d d      Z0 G d d      Z1	 	 	 	 	 	 d$dZ2	 	 	 	 	 	 d%d Z3y)&z$Checker Manager and Checker classes.    )annotationsN)Any)	Generator)List)Optional)Sequence)Tuple)defaults)
exceptions)	processor)utils)FSTRING_START)expand_paths)
parse_args)Checkers)LoadedPlugin)StyleGuideManagerr   _mp_pluginsargparse.Namespace_mp_optionsc              #  2   K   | |ca a	 d  b by # b bw xY wwN)r   r   )pluginsoptionss     Q/var/www/html/brdwt/brdwt/brdwtenv/lib/python3.12/site-packages/flake8/checker.py_mp_preforkr   5   s&       'K%Ks    c                    t        j                   t         j                  t         j                         	 t        t        f y # t
        $ r  t        |       \  }}|j                  |caaY y w xY wr   )signalSIGINTSIG_IGNr   r   	NameErrorr   checkers)argvr   r   s      r   _mp_initr$   B   sW     MM&--0=[  =%d+#*#3#3W [=s   A &A*)A*c                J    t        | t        t              j                         S )Nfilenamer   r   )FileCheckerr   r   
run_checksr'   s    r   _mp_runr+   P   s    ;jl    c                  p    e Zd ZdZ	 	 	 	 	 	 	 	 ddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZy)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.
    c                   || _         |j                  | _        || _        | j                         | _        ddddd| _        g | j                  j                  | j                  j                  | _        || _        g | _	        y)z Initialize our Manager instance.r   )fileslogical linesphysical linestokensN)
style_guider   r   
_job_countjobs
statisticsexcludeextend_excluder#   results)selfr4   r   r#   s       r   __init__zManager.__init__i   sy     '"**OO%		
 M--L0K0KL	BDr,   c                    | j                   D ]5  \  }}}t        j                  D ]  }| j                  |xx   ||   z  cc<    7 | j                  dxx   t	        | j
                        z  cc<   y )Nr0   )r:   r
   STATISTIC_NAMESr7   len	filenames)r;   _r7   	statistics       r   _process_statisticszManager._process_statistics~   sm     $ 	DAq*%55 D		*j.CC*D	D 	 C$77 r,   c                $   t        j                  | j                  j                        rt        j                  d       y| j                  j                  }|j                  r	 t        j                         S |j                  S # t        $ r Y yw xY w)Nz]The --jobs option is not compatible with supplying input using - . Ignoring --jobs arguments.r   )r   is_using_stdinr   r@   LOGwarningr6   is_automultiprocessing	cpu_countNotImplementedErrorn_jobs)r;   r6   s     r   r5   zManager._job_count   s~      6 67KK= ||   <<&0022 {{ ' s   #B 	BBc           
     p    | j                   }d}|D ]"  \  }}}}}	||j                  ||||||	      z  }$ |S )Nr   )coder'   line_numbercolumn_numbertextphysical_line)r4   handle_error)
r;   r'   r:   r4   reported_results_count
error_coderO   columnrQ   rR   s
             r   _handle_resultszManager._handle_results   sd    &&!"DK 	@JVT="k&>&>!'$+ '? ' "	 &%r,   c                   dx}}| j                   j                  t        j                  d             | j                   D ]s  \  }}}|j                  t        j                  dd             | j                  j                  |      5  || j                  ||      z  }ddd       |t        |      z  }u ||fS # 1 sw Y   xY w)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)r:   sortoperator
itemgetterr4   processing_filerW   r?   )r;   results_reportedresults_foundr'   r:   rA   s         r   reportzManager.report   s     ,-,=h11!45$(LL 	* HgqLLX00A6L7!!11(; L D$8$87$KK LS\)M		*
 /00L Ls   B::C	c                   t        | j                  | j                        5  t        | j                  | j
                        }ddd       | j                          yd}	 t        |j                  t        | j                              | _        |j                          |j                          d}|s!|j                          |j                          yy# 1 sw Y   xY w# |s!|j                          |j                          w w xY w)zRun the checkers in parallel.NFT)r   r   r   _try_initialize_processpoolr6   r#   
run_seriallistimap_unorderedr+   r@   r:   closejoin	terminate)r;   poolpool_closeds      r   run_parallelzManager.run_parallel   s    t||4 	E.tyy$))DD	E <OO	 3 3GT^^ LMDLJJLIIKK 		 	E 	E  		 s   !C AC  C %Dc                    | j                   D cg c]2  }t        || j                  | j                        j	                         4 c}| _        yc c}w )zRun the checkers in serial.r&   N)r@   r(   r   r   r)   r:   )r;   r'   s     r   re   zManager.run_serial   sM     !NN
  ! jl	
 
s   7Ac                   	 | j                   dkD  r)t        | j                        dkD  r| j                          y| j	                          y# t
        $ r+ t        j                  d       t        j                  d      w xY 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.
        rZ   z"Flake8 was interrupted by the userzEarly quit while running checksN)
r6   r?   r@   rm   re   KeyboardInterruptrF   rG   r   	EarlyQuitr;   s    r   runzManager.run   si    	Jyy1}T^^!4q!8!!#!  	JKK<=&&'HII	Js   7A A 4A?c                    t         j                  d       t        t        | j                  j
                  | j                  j                  | j                  j                  | j                              | _        y)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_patternsr8   N)	rF   infotupler   r   r@   rv   r'   r8   rr   s    r   startzManager.start   sS     	"#ll,,#'<<#B#B"&,,"7"7	
r,   c                $    | j                          y)zStop checking files.N)rC   rr   s    r   stopzManager.stop  s      "r,   N)r4   r   r   r   r#   Sequence[str]returnNoner~   r   )r~   int)r'   strr:   Resultsr~   r   )r~   tuple[int, int])__name__
__module____qualname____doc__r<   rC   r5   rW   rb   rm   re   rs   rz   r|    r,   r   r.   r.   V   se    $E&E E 	E
 
E*8<&1$(	
J$
"#r,   r.   c                      e Zd ZdZ	 	 	 	 	 	 	 	 ddZddZddZ	 	 	 	 	 	 	 	 	 	 ddZddZe	dd       Z
ddZdd	Zdd
ZddZddZddZ	 	 	 	 	 	 ddZy)r(   z;Manage running checks for a file and aggregate the results.c                  || _         || _        || _        g | _        dddd| _        | j                         | _        || _        d| _        | j                  h| j                  j                  | _        | j                  j                          | _        t        | j                  j                        | j                  d<   yy)zInitialize our file checker.r   )r3   r1   r2   FNr2   )r   r'   r   r:   r7   _make_processorr   display_nameshould_processshould_ignore_filer?   lines)r;   r'   r   r   s       r   r<   zFileChecker.__init__  s       "

 --/$#>>% $ 7 7D&*nn&G&G&I"ID03DNN4H4H0IDOO,- &r,   c                     d| j                    S )z)Provide helpful debugging representation.zFileChecker for r*   rr   s    r   __repr__zFileChecker.__repr__%  s    !$--11r,   c           	         	 t        j                  | j                  | j                        S # t        $ r6}| j                  dddt        |      j                   d|        Y d }~y d }~ww xY w)NE902r   : )r   FileProcessorr'   r   OSErrorrb   typer   )r;   es     r   r   zFileChecker._make_processor)  s_    
	**4==$,,GG 	 KK1a)9)9(:"QC&@A	s   ), 	A+,A&&A+c                    ||j                  dd      \  }}t        | d      r(| j                  | j                  j                  |      }nd}| j                  j                  |||||f       |S )z2Report an error by storing it in the results list.N rZ   r   )splithasattrr   noqa_line_forr:   append)r;   rU   rO   rV   rQ   lines         r   rb   zFileChecker.report6  sp     #zz#q1J 4%$..*D>>//<DDZfdDIJr,   c                   | j                   J | j                         	 | j                   j                  |j                  |      }	  |j                  di ||S # t        $ r&}t        j                  |j                  |      d}~ww xY w# t        $ rS}t        j                  d|j                  d       t        j                  | j                  |j                  |      d}~ww xY 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	ExceptionrF   criticalPluginExecutionFailed)r;   plugin	argumentsparamsaeall_excs         r   	run_checkzFileChecker.run_checkK  s    ~~)84==8)	^^99!!9F	6::4	4V44  	=="//2 	  
	LL:##  
 22"//! 
	s0   &A B 	B	#!BB		C(AC##C(c                   t        | j                        dkD  rB| j                  d   r3t        | j                  d         dkD  r| j                  d   }|dd \  }}nit        | t        j                        rHt        | j                        dk(  r0t        | j                  d         dk(  rd}| j                  d   \  }}nd}d\  }}|dkD  rz|rxt        | t
              rht        |      dk(  rZd}d}|d   }|E|j                  d      j                  d      }t        |      dz
  }|d   }t        |      }	||	kD  r|	}||z  }||z  }||fS )	NrZ   r[      r   )rZ   r   r      
)r?   args
isinstancetokenize
TokenErrorSyntaxErrorrstripr   )
r   tokenrowrV   column_offset
row_offsetrR   r   logical_linelogical_line_lengths
             r   _extract_syntax_informationz'FileChecker._extract_syntax_informationd  sZ    	!#q!INN1%&*NN1%E!*KCy("5"56INN#q(INN1%&!+E#..+KCE KC QJ9k2E
a
 MJ "!HM
 (
 &,,T288> Z!^
$Qx&),&7#//0F:Cm#FF{r,   c                T   | j                   J | j                         | j                   j                         }| j                  j                  D ]G  }| j                  ||      }	 |j                         }|D ]  \  }}}}| j                  d|||        I y# t        $ r |}Y 1w xY w)z1Run all checks expecting an abstract syntax tree.N)treerU   rO   rV   rQ   )	r   r'   	build_astr   r   r   rs   r   rb   )	r;   astr   checkerrunnerrO   offsetrQ   rA   s	            r   run_ast_checkszFileChecker.run_ast_checks  s    ~~)84==8)nn&&(ll'' 	FnnV#n6G!  17 ,VT1# +!	  	 " ! !s   %BB'&B'c                R   | j                   J | j                   j                         \  }}}|sy| j                   j                  |       t        j	                  d|j                                | j                  j                  D ]  }| j                   j                  |       | j                  ||      xs d}|D ]M  \  }}t        ||      \  }}	||	cxk(  rdk(  rn nt        j                  d|       | j                  d||	|       O  | j                   j                          y)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_staterF   debugr   r   r   update_checker_state_forr   find_offsetrG   rb   next_logical_line)
r;   commentsr   mappingr   r:   r   rQ   rO   r   s
             r   run_logical_checkszFileChecker.run_logical_checks  s   ~~)))*...*K*K*M',##G,		&(;(;(=>ll// 	FNN33F;nnV,nGM2G ' 	-8-I*]-414KK EvN# +(	  			 	((*r,   c                   | j                   J | j                  j                  D ]  }| j                   j                  |       | j	                  ||      }|4d}	 |d   }t        |t              r|f}|D ]0  }|\  }}| j                  d| j                   j                  ||       2  y# t
        t        f$ r Y \w xY w)zoRun all checks for a given physical line.

        A single physical check may return multiple errors.
        N)rR   r   r   )r   r   rR   r   r   
IndexError	TypeErrorr   r   rb   rO   )r;   rR   r   resultr   result_singlerQ   s          r   run_physical_checkszFileChecker.run_physical_checks  s    
 ~~)))ll00 	FNN33F;^^F-^HF! $$*1IM mS1$YF%+ M*7'M4KK#'$(NN$>$>,!	   !	 #I. s   B,,B>=B>c                   | j                   J d}| j                  }| j                   }d}|j                         D ]  }|dxx   dz  cc<   | j                  ||       |dd \  }}|t        j
                  k(  rt        j                  ||      }n+|dk(  r&t        j                  |      r| j                  |       |d   } |j                  r/| j                  |j                  d          | j                          yy)	zProcess tokens and trigger checks.

        Instead of using this directly, you should use
        :meth:`flake8.checker.FileChecker.run_checks`.
        Nr    r3   rZ   r[   r   )r   r7   generate_tokenscheck_physical_eolr   OPcount_parenthesestoken_is_newlinehandle_newliner3   r   r   r   )r;   parensr7   file_processorprev_physicalr   
token_typerQ   s           r   process_tokenszFileChecker.process_tokens  s     ~~)))__
#335 		%Ex A% ##E=9$QqzJX[[("44VTB1--e4''
3!!HM		%   $$^%9%9"%=>##% !r,   c           
        | j                   | j                  s#| j                  | j                  | j                  fS 	 | j                          | j                          | j                   j                  d   }|| j                  d<   | j                  | j                  | j                  fS # t        t        j                  f$ r}t        |t        j                        rdnd}| j                  |      \  }}| j                  |||t        |      j                   d|j                  d           | j                  | j                  | j                  fcY d}~S d}~ww xY w)zRun checks against the file.Nr   E999r   r   r1   )r   r   r   r:   r7   r   r   r   r   r   r   r   rb   r   r   r   )r;   r   rN   r   rV   logical_liness         r   r)   zFileChecker.run_checks  s    >>!)<)<$$dllDOOCC	D!! 11/B+8(  $,,?? X001 	D'8+>+>?6VD::1=KCKKc6d1g.>.>-?r!&&)+MN$$dllDOOCC		Ds    B( (EBEEEc                j   | j                   J |t        j                  k(  r+| j                          | j                   j	                          yt        | j                   j                        dk(  r5| j                   j                          | j                   j                          y| j                          y)z3Handle the logic when encountering a newline token.NrZ   )	r   r   NEWLINEr   reset_blank_beforer?   r3   visited_new_blank_linedelete_first_token)r;   r   s     r   r   zFileChecker.handle_newline  s    ~~))))))##%NN--/&&'1,NN113NN--/##%r,   c                   | j                   J |j                  t        k(  r)| j                   j                  |j                  d          yt        j
                  |      r=|j                  dk(  r| j                  |       y| j                  |j                         yt        j                  |      r2| j                   j                  |      D ]  }| j                  |        yy)z@Run physical checks if and only if it is at the end of the line.Nr   r   )
r   r   r   fstring_startrz   is_eol_tokenr   r   is_multiline_stringmultiline_string)r;   r   r   r   s       r   r   zFileChecker.check_physical_eol'  s     ~~)))::&NN((Q8##E* zzR((7((4**51 77> /((./ 2r,   N)r'   r   r   r   r   r   r~   r   )r~   r   )r~   zprocessor.FileProcessor | None)
rU   z
str | NonerO   r   rV   r   rQ   r   r~   r   )r   r   r   r   r~   r   )r   r   r~   r   r   )rR   r   r~   r   )r~   #tuple[str, Results, dict[str, int]])r   r   r~   r   )r   ztokenize.TokenInfor   r   r~   r   )r   r   r   r   r<   r   r   rb   r   staticmethodr   r   r   r   r   r)   r   r   r   r,   r   r(   r(   	  s    EJ J 	J
 $J 
J22  	
  
*2 8 8t*+4>&6@$&/'/8;/	/r,   r(   c                    	 t        j                  | t        |f      S # t        $ r}|j                  t
        vr Y d}~yd}~wt        $ r Y yw xY w)z@Return a new process pool instance if we are able to create one.)initargsN)rI   Poolr$   r   errnoSERIAL_RETRY_ERRNOSImportError)	job_countr#   errs      r   rd   rd   G  sX    
##Ix4'JJ 99// 0
   s    	AA  AAc                    t        | t              r| S |D ]  }|d   }| |k  s|d   } n d}dx} }|d   |d   | z   |z
  fS )z*Find the offset tuple for a single offset.r   rZ   )r   r   )r   ry   )r   r   r   token_offsetpositions        r   r   r   W  sm     &%  "Qx\!QxH	"  !!QK!v-<==r,   )r   r   r   r   r~   zGenerator[None, None, None])r#   r}   r~   r   )r'   r   r~   r   )r   r   r#   r}   r~   z multiprocessing.pool.Pool | None)r   r   r   zprocessor._LogicalMappingr~   r   )4r   
__future__r   argparse
contextlibr   loggingmultiprocessing.poolrI   r]   r   r   typingr   r   r   r   r   r	   flake8r
   r   r   r   flake8._compatr   flake8.discover_filesr   flake8.options.parse_argsr   flake8.plugins.finderr   r   flake8.style_guider   r   r   r   	getLoggerr   rF   ENOSPCr   __annotations__contextmanagerr   r$   r+   r.   r(   rd   r   r   r,   r   <module>r     s4   * "                   ( . 0 * . 0
uS#sC#67
8g! 
LL     	%	% 2	% 	% 	%=p# p#f{/ {/|	
 & >>3>>r,   