
    yIfJX                       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j&        e'          Z(ej)        hZ*de+d<   de+d<   ej,        d2d            Z-d3d Z.d4d$Z/ G d% d&          Z0 G d' d(          Z1d5d,Z2d6d1Z3dS )7z$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_optionspluginsoptionsreturnGenerator[None, None, None]c              #  0   K   | |ca a	 d V  b bd S # b bw xY wN)r   r   )r   r   s     B/var/www/piapp/venv/lib/python3.11/site-packages/flake8/checker.py_mp_preforkr   5   s<        'K%K$$$$s    argvSequence[str]Nonec                    t          j         t           j        t           j                   	 t          t          f d S # t
          $ r  t          |           \  }}|j        |caaY d S w xY wr   )signalSIGINTSIG_IGNr   r   	NameErrorr   checkers)r   r   r   s      r   _mp_initr(   B   sr     M&-000=[     = = =%d++#*#3W [[[[=s   ; &A%$A%filenamestr#tuple[str, Results, dict[str, int]]c                ^    t          | t          t                                                    S )Nr)   r   r   )FileCheckerr   r   
run_checksr)   s    r   _mp_runr1   P   s)    ;  jll    c                  b    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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                    || _         |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)
r5   r   r   
_job_countjobs
statisticsexcludeextend_excluder   results)selfr5   r   r   s       r   __init__zManager.__init__i   s{     '"*OO%%		
 
 M-L0KLL	BDr2   c                    | j         D ]0\  }}}t          j        D ]}| j        |xx         ||         z  cc<   1| j        dxx         t	          | j                  z  cc<   d S )Nr7   )r@   r
   STATISTIC_NAMESr=   len	filenames)rA   _r=   	statistics       r   _process_statisticszManager._process_statistics~   s     $ 	D 	DAq*%5 D D		***j.CC****D   C$7$77     r2   intc                    t          j        | j        j                  rt                              d           dS | j        j        }|j        r%	 t          j	                    S # t          $ r Y dS w xY w|j        S )Nz]The --jobs option is not compatible with supplying input using - . Ignoring --jobs arguments.r   )r   is_using_stdinr   rF   LOGwarningr<   is_automultiprocessing	cpu_countNotImplementedErrorn_jobs)rA   r<   s     r   r;   zManager._job_count   s      677 	KK=   1|  < 	&0222&   qq
 {s   A" "
A0/A0r)   r*   r@   Resultsc           
     j    | j         }d}|D ]&\  }}}}}	||                    ||||||	          z  }'|S )Nr   )coder)   line_numbercolumn_numbertextphysical_line)r5   handle_error)
rA   r)   r@   r5   reported_results_count
error_coderW   columnrY   rZ   s
             r   _handle_resultszManager._handle_results   si    &!"DK 	 	@JVT="k&>&>!'$+ '? ' ' "" &%r2   tuple[int, int]c                   dx}}| j                             t          j        d                     | j         D ]\  }}}|                    t          j        dd                     | j                            |          5  ||                     ||          z  }ddd           n# 1 swxY w Y   |t          |          z  }||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)r@   sortoperator
itemgetterr5   processing_filer_   rE   )rA   results_reportedresults_foundr)   r@   rG   s         r   reportzManager.report   s"    ,-,=h1!44555$(L 	* 	* HgqLLX0A66L777!11(;; L L D$8$87$K$KK L L L L L L L L L L L L L L LS\\)MM/00s   B((B,	/B,	c                D   t          | j        | j                  5  t          | j        | j                  }ddd           n# 1 swxY w Y   ||                                  dS d}	 t          |                    t          | j
                            | _        |                                 |                                 d}|s*|                                 |                                 dS dS # |s)|                                 |                                 w w xY w)zRun the checkers in parallel.NFT)r   r   r   _try_initialize_processpoolr<   r   
run_seriallistimap_unorderedr1   rF   r@   closejoin	terminate)rA   poolpool_closeds      r   run_parallelzManager.run_parallel   sW   t|44 	E 	E.ty$)DDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E <OOF	 3 3GT^ L LMMDLJJLLLIIKKKK    		 ;    		s   AA	A(AC2 2-Dc                8      fd j         D              _        dS )zRun the checkers in serial.c                l    g | ]0}t          |j        j                                                   1S )r-   )r.   r   r   r/   ).0r)   rA   s     r   
<listcomp>z&Manager.run_serial.<locals>.<listcomp>   sR     
 
 
  !   jll
 
 
r2   N)rF   r@   rA   s   `r   rn   zManager.run_serial   s5    
 
 
 
 !N
 
 
r2   c                   	 | j         dk    r.t          | j                  dk    r|                                  dS |                                  dS # t
          $ r/ t                              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.
        rc   z"Flake8 was interrupted by the userzEarly quit while running checksN)
r<   rE   rF   rv   rn   KeyboardInterruptrM   rN   r   	EarlyQuitr{   s    r   runzManager.run   s    	Jy1}}T^!4!4q!8!8!!#####!!!!!  	J 	J 	JKK<===&'HIII	Js   7A A 9B
c                    t                               d           t          t          | j        j        | j        j        | j        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_patternsr>   N)	rM   infotupler   r   rF   r   r)   r>   r{   s    r   startzManager.start   s[     	"###l,#'<#B"&,"7	  
 
r2   c                .    |                                   dS )zStop checking files.N)rI   r{   s    r   stopzManager.stop  s      """""r2   N)r5   r   r   r   r   r    r   r!   r   r!   )r   rJ   )r)   r*   r@   rT   r   rJ   )r   r`   )__name__
__module____qualname____doc__rB   rI   r;   r_   rk   rv   rn   r   r   r    r2   r   r4   r4   V   s         $E E E E*8 8 8 8   <& & & &1 1 1 1$   (	
 	
 	
 	
J J J J$
 
 
 
"# # # # # #r2   r4   c                      e Zd ZdZd,d
Zd-dZd.dZd/dZd0dZe	d1d            Z
d2dZd2dZd3d!Zd2d"Zd4d$Zd5d&Zd6d*Zd+S )7r.   z;Manage running checks for a file and aggregate the results.r)   r*   r   r   r   r   r   r!   c               V   || _         || _        || _        g | _        dddd| _        |                                 | _        || _        d| _        | j        S| j        j        | _        | j        	                                 | _        t          | j        j                  | j        d<   dS dS )zInitialize our file checker.r   )r:   r8   r9   FNr9   )r   r)   r   r@   r=   _make_processorr   display_nameshould_processshould_ignore_filerE   lines)rA   r)   r   r   s       r   rB   zFileChecker.__init__  s       "
 

 --//$#>% $ 7D&*n&G&G&I&I"ID03DN4H0I0IDO,--- &%r2   c                    d| j          S )z)Provide helpful debugging representation.zFileChecker for r0   r{   s    r   __repr__zFileChecker.__repr__%  s    1$-111r2   processor.FileProcessor | Nonec           	         	 t          j        | j        | j                  S # t          $ r:}|                     dddt          |          j         d|            Y d }~d S d }~ww xY w)NE902r   : )r   FileProcessorr)   r   OSErrorrk   typer   )rA   es     r   r   zFileChecker._make_processor)  s{    
	*4=$,GGG 	 	 	 KK1a)9&@&@Q&@&@AAA44444	s   ! 
A%/A  A%r]   
str | NonerW   rJ   r^   rY   c                    ||                     dd          \  }}t          | d          r"| j        | j                            |          }nd}| j                            |||||f           |S )z2Report an error by storing it in the results list.N rc   r   )splithasattrr   noqa_line_forr@   append)rA   r]   rW   r^   rY   lines         r   rk   zFileChecker.report6  s     #zz#q11J 4%% 	$.*D>//<<DDDZfdDIJJJr2   pluginr   	argumentsr   c                   | j         J | j                    	 | j                             |j        |          }n-# t          $ r }t          j        |j        |          d}~ww xY w	  |j        di ||S # t          $ rH}t                              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	ExceptionrM   criticalPluginExecutionFailed)rA   r   r   paramsaeall_excs         r   	run_checkzFileChecker.run_checkK  s   ~))4=)))	^99!9 FF  	 	 	="/2   		6:44	4V444 
	 
	 
	LL:#    
 2"/!   
	s.    9 
A#AA#'A6 6
C ACCr   r   r`   c                   t          | j                  dk    rF| j        d         r9t          | j        d                   dk    r| j        d         }|dd         \  }}njt          | t          j                  rIt          | j                  dk    r1t          | j        d                   dk    rd}| j        d         \  }}nd}d\  }}|dk    r|rt          | t
                    rt          |          dk    rqd}d}|d         }|Y|                    d                              d          }t          |          dz
  }|d         }t          |          }	||	k    r|	}||z  }||z  }||fS )	Nrc   rd      r   )rc   r   r      
)rE   args
isinstancetokenize
TokenErrorSyntaxErrorrstripr   )
r   tokenrowr^   column_offset
row_offsetrZ   r   logical_linelogical_line_lengths
             r   _extract_syntax_informationz'FileChecker._extract_syntax_informationd  s    	!##q! $IN1%&&**N1%E!*KCy("566		!IN##q((IN1%&&!++E#.+KCE KC QJJ 9k22  E

a
 MJ "!HM
 (
 &,,T2288>> ZZ!^
$Qx&),&7&7#///0F:Cm#FF{r2   c                F   | j         J | j                    | j                                         }| j        j        D ]d}|                     ||          }	 |                                }n# t          $ r |}Y nw xY w|D ] \  }}}}|                     d|||           !edS )z1Run all checks expecting an abstract syntax tree.N)treer]   rW   r^   rY   )	r   r)   	build_astr   r   r   r   r   rk   )	rA   astr   checkerrunnerrW   offsetrY   rG   s	            r   run_ast_checkszFileChecker.run_ast_checks  s    ~))4=)))n&&((l' 	 	FnnV#n66G! ! ! ! ! !06  ,VT1# +!	     	 	s   A++A:9A:c                \   | j         J | j                                         \  }}}|sdS | j                             |           t                              d|                                           | j        j        D ]}| j                             |           | 	                    ||          pd}|D ]\\  }}t          ||          \  }}	||	cxk    rdk    rn nt                              d|           |                     d||	|           ]| 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_staterM   debugr   r   r   update_checker_state_forr   find_offsetrN   rk   next_logical_line)
rA   commentsr   mappingr   r@   r   rY   rW   r   s
             r   run_logical_checkszFileChecker.run_logical_checks  s]   ~)))*..*K*K*M*M', 	F##G,,,		&(;(;(=(=>>>l/ 	 	FN33F;;;nnV,nGGM2G ' 	 	-8-I-I*]-4444144444KK EvNNN# +(	     		 	((*****r2   rZ   c                l   | j         J | j        j        D ]}| j                             |           |                     ||          }|hd}	 |d         }n# t
          t          f$ r Y nw xY wt          |t                    r|f}|D ]*}|\  }}| 	                    d| j         j
        ||           +dS )zoRun all checks for a given physical line.

        A single physical check may return multiple errors.
        N)rZ   r   r   )r   r   rZ   r   r   
IndexError	TypeErrorr   rJ   rk   rW   )rA   rZ   r   resultr   result_singlerY   s          r   run_physical_checkszFileChecker.run_physical_checks  s   
 ~)))l0 	 	FN33F;;;^^F-^HHF! $$*1IMM"I.   D mS11 '$YF%+  M*7'M4KK#'$(N$>,!	      %	 	s   AA+*A+c                   | j         J d}| j        }| j         }d}|                                D ]}|dxx         dz  cc<   |                     ||           |dd         \  }}|t          j        k    rt          j        ||          }n/|dk    r)t          j        |          r|                     |           |d         }|j	        r6| 
                    |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    r:   rc   rd   r   )r   r=   generate_tokenscheck_physical_eolr   OPcount_parenthesestoken_is_newlinehandle_newliner:   r   r   r   )rA   parensr=   file_processorprev_physicalr   
token_typerY   s           r   process_tokenszFileChecker.process_tokens  s.    ~)))_
#3355 		% 		%Ex   A%   ##E=999$QqSzJX[(("4VTBB1-e44 4''
333!!HMM  	&$$^%9"%=>>>##%%%%%	& 	&r2   r+   c           
     H   | j         | j        s| j        | j        | j        fS 	 |                                  |                                  n# t          t          j	        f$ r}t          |t          j	                  rdnd}|                     |          \  }}|                     |||t          |          j         d|j        d                     | j        | j        | j        fcY d}~S d}~ww xY w| j         j        d         }|| j        d<   | j        | j        | j        fS )zRun checks against the file.Nr   E999r   r   r8   )r   r   r   r@   r=   r   r   r   r   r   r   r   rk   r   r   r   )rA   r   rV   r   r^   logical_liness         r   r/   zFileChecker.run_checks  s9   >!)<!$dlDOCC	D!!!!!!!X01 	D 	D 	D'8+>??K66VD::1==KCKKc6d1gg.>+M+M!&)+M+MNNN$dlDOCCCCCCC		D 1/B+8( $,??s   (A C1#BC,&C1,C1r   c                `   | j         J |t          j        k    r/|                                  | j                                          dS t          | j         j                  dk    r4| j                                          | j                                          dS |                                  dS )z3Handle the logic when encountering a newline token.Nrc   )	r   r   NEWLINEr   reset_blank_beforerE   r:   visited_new_blank_linedelete_first_token)rA   r   s     r   r   zFileChecker.handle_newline  s    ~))))))##%%%N--/////&''1,,N11333N--/////##%%%%%r2   r   tokenize.TokenInfor   c                   | j         J |j        t          k    r'| j                             |j        d                    dS t          j        |          r>|j        dk    r|                     |           dS |                     |j                   dS t          j        |          r2| j         	                    |          D ]}|                     |           d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_startr   is_eol_tokenr   r   is_multiline_stringmultiline_string)rA   r   r   r   s       r   r   zFileChecker.check_physical_eol'  s     ~))):&&N((Q88888#E** 	/ zR((77777((44444*511 	/ 77>> / /((....	/ 	// /r2   N)r)   r*   r   r   r   r   r   r!   )r   r*   )r   r   )
r]   r   rW   rJ   r^   rJ   rY   r*   r   r*   )r   r   r   r   r   r   )r   r   r   r`   r   )rZ   r*   r   r!   )r   r+   )r   rJ   r   r!   )r   r   r   r*   r   r!   )r   r   r   r   rB   r   r   rk   r   staticmethodr   r   r   r   r   r/   r   r   r   r2   r   r.   r.   	  s3       EEJ J J J22 2 2 2      *   2 8 8 8 \8t   *+ + + +4   >& & & &6@ @ @ @$& & & &/ / / / / /r2   r.   	job_countrJ    multiprocessing.pool.Pool | Nonec                    	 t          j        | t          |f          S # t          $ r}|j        t
          vr Y d}~nd}~wt          $ r Y nw xY wdS )z@Return a new process pool instance if we are able to create one.)initargsN)rP   Poolr(   r   errnoSERIAL_RETRY_ERRNOSImportError)r  r   errs      r   rm   rm   G  s    
#Ix4'JJJJ   9/// 0////    4s    
A=AAr   r   processor._LogicalMappingr`   c                    t          | t                    r| S |D ]}|d         }| |k    r
|d         } nd}dx} }|d         |d         | z   |z
  fS )z*Find the offset tuple for a single offset.r   rc   )r   r   )r   r   )r   r   r   token_offsetpositions        r   r   r   W  s     &%    " "Qx\!!QxHE "  !!QK!v-<==r2   )r   r   r   r   r   r   )r   r    r   r!   )r)   r*   r   r+   )r  rJ   r   r    r   r  )r   rJ   r   r
  r   r`   )4r   
__future__r   argparse
contextlibr  loggingmultiprocessing.poolrP   rf   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*   rJ   rT   	getLoggerr   rM   ENOSPCr  __annotations__contextmanagerr   r(   r1   r4   r.   rm   r   r   r2   r   <module>r     s   * * * " " " " " "                                                                           ( ( ( ( ( ( . . . . . . 0 0 0 0 0 0 * * * * * * . . . . . . 0 0 0 0 0 0
uS#sC#67
8g!! 
L          	% 	% 	% 	%= = = =   p# p# p# p# p# p# p# p#f{/ {/ {/ {/ {/ {/ {/ {/|	    > > > > > >r2   