VL iXUdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddl mZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlm Z ddl!m"Z"e#e$e%e&e&e%ee%fZ'ejPe)Z*ejVhZ,de-d<de-d<ej\ d dZ/d!dZ0d"dZ1GddZ2GddZ3 d#dZ4 d$dZ5y)%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_pluginsargparse.Namespace _mp_optionsc#2K||caa dbby#bbwxYwwN)rr)pluginsoptionss T/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/flake8/checker.py _mp_preforkr4s& 'K% Ksctjtjtj ttfy#t $r t |\}}|j|caaYywxYwr)signalSIGINTSIG_IGNrr NameErrorrcheckers)argvrrs r_mp_initr#AsW MM&--0=[ =%d+#*#3#3W [=s A&A*)A*cJt|ttjS)Nfilenamerr) FileCheckerrr run_checksr&s r_mp_runr*Os ;  jlcpeZdZdZ d dZddZddZddZddZddZ ddZ dd Z dd Z dd Z y )ManageraRManage 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|_ddddd|_g|jj |jj|_||_g|_ y)z Initialize our Manager instance.r)files logical linesphysical linestokensN) style_guiderr _job_countjobs statisticsexcludeextend_excluder"results)selfr3rr"s r__init__zManager.__init__hsy'"**  OO%    M--L 0K0KL  BD r+c|jD]5\}}}tjD]}|j|xx||z cc<7|jdxxt |j z cc<y)Nr/)r9rSTATISTIC_NAMESr6len filenames)r:_r6 statistics r_process_statisticszManager._process_statistics}sm $  D Aq*%55 D  *j.CC* D D  C$77 r+c$tj|jjrtj dy|jj }|jr tjS|jS#t$rYywxYw)Nz]The --jobs option is not compatible with supplying input using - . Ignoring --jobs arguments.r) r is_using_stdinrr?LOGwarningr5is_automultiprocessing cpu_countNotImplementedErrorn_jobs)r:r5s rr4zManager._job_counts~    6 6 7 KK= ||   << &0022 {{ '  s#B BBc p|j}d}|D]"\}}}}} ||j|||||| z }$|S)Nr)coder& line_number column_numbertext physical_line)r3 handle_error) r:r&r9r3reported_results_count error_coderNcolumnrPrQs r_handle_resultszManager._handle_resultssd&& !"DK  @J VT= "k&>&>!'$+ '?'  " &%r+cdx}}|jjtjd|jD]s\}}}|jtjdd|jj |5||j ||z }ddd|t|z }u||fS#1swYxYw)aReport 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)keyN)r9sortoperator itemgetterr3processing_filerVr>)r:results_reported results_foundr&r9r@s rreportzManager.reports,-,= h11!45$(LL * Hgq LLX00A6L 7!!11(; L D$8$87$KK  L S\ )M  * /00 L Ls B::C ct|j|j5t|j|j }ddd|j yd} t|jt|j|_ |j|jd}|s!|j|jyy#1swYxYw#|s!|j|jwwxYw)zRun the checkers in parallel.NFT)rrr_try_initialize_processpoolr5r" run_seriallistimap_unorderedr*r?r9closejoin terminate)r:pool pool_closeds r run_parallelzManager.run_parallels t|| 4 E.tyy$))DD E < OO     3 3GT^^ LMDL JJL IIKK   E E  s!C AC C %Dc|jDcgc]2}t||j|jj 4c}|_ycc}w)zRun the checkers in serial.r%N)r?r'rrr(r9)r:r&s rrdzManager.run_serialsM!NN   !   jl     s7Ac |jdkDr)t|jdkDr|jy|j y#t $r+t jdtjdwxYw)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. rYz"Flake8 was interrupted by the userzEarly quit while running checksN) r5r>r?rlrdKeyboardInterruptrErFr EarlyQuitr:s rrunz Manager.runsi Jyy1}T^^!4q!8!!#!  J KK< =&&'HI I Js7A A 4A?cPtjdtt|jj |jj |jj|j|_tt|j |j|_ y)zStart checking files. :param paths: Path names to check. This is passed directly to :meth:`~Manager.make_checkers`. zMaking checkers)pathsstdin_display_namefilename_patternsr7N) rEinfotuplerrr?rur&r7minr>r5rqs rstartz Manager.startsp "# ll,,#'<<#B#B"&,,"7"7   DNN+TYY7 r+c$|jy)zStop checking files.N)rBrqs rstopz Manager.stops   "r+N)r3rrrr" Sequence[str]returnNoner~r)r~int)r&strr9Resultsr~r)r~tuple[int, int])__name__ __module__ __qualname____doc__r;rBr4rVrarlrdrrrzr|r+rr-r-Use$E&EE E  E*8 < &1$(  J$8$#r+r-ceZdZdZ ddZddZddZ ddZddZe ddZ ddZ dd Z dd Z dd Zdd Zdd Z ddZy)r'z;Manage running checks for a file and aggregate the results.c||_||_||_g|_dddd|_|j |_||_d|_|j h|j j|_|j j |_t|j j|jd<yy)zInitialize our file checker.r)r2r0r1FNr1) rr&rr9r6_make_processorr display_nameshould_processshould_ignore_filer>lines)r:r&rrs rr;zFileChecker.__init__ s    "   --/$# >> % $ 7 7D &*nn&G&G&I"ID 03DNN4H4H0IDOO, - &r+c d|jS)z)Provide helpful debugging representation.zFileChecker for r)rqs r__repr__zFileChecker.__repr__%s!$--11r+c  tj|j|jS#t$r6}|j dddt |jd|Yd}~yd}~wwxYw)NE902r: )r FileProcessorr&rOSErrorratyper)r:es rrzFileChecker._make_processor)s_ **4==$,,G G  KK1a)9)9(:"QC&@ A s), A+,A&&A+c||jdd\}}t|dr(|j|jj|}nd}|jj |||||f|S)z2Report an error by storing it in the results list.N rYr )splithasattrr noqa_line_forr9append)r:rTrNrUrPlines rrazFileChecker.report6sp  #zz#q1 J 4 %$..*D>>// args isinstancetokenize TokenError SyntaxErrorrstripr) rtokenrowrU column_offset row_offsetrQr logical_linelogical_line_lengths r_extract_syntax_informationz'FileChecker._extract_syntax_informationdsZ   ! #q!INN1%&*NN1%E!*KC y("5"5 6INN#q(INN1%&!+E#..+KCE KC QJ9k2E a MJ"!HM ( &,,T288> Z!^ $Qx &),&7#//0F : C m #FF{r+cT|jJ|j|jj}|jjD]G}|j ||} |j }|D]\}}}}|jd|||Iy#t$r|}Y1wxYw)z1Run all checks expecting an abstract syntax tree.N)treerTrNrUrP) r r& build_astrrrrrrra) r:astrcheckerrunnerrNoffsetrPr@s rrun_ast_checkszFileChecker.run_ast_checkss~~)84==8)nn&&(ll'' FnnV#n6G ! 17 , VT1 # +!    " !  !s%B B'&B'cR|jJ|jj\}}}|sy|jj|tj d|j |j jD]}|jj||j||xsd}|D]M\}}t||\}} || cxk(rdk(rnntjd||jd|| |O|jjy)z(Run all checks expecting a logical line.NzLogical line: "%s")rrrz#position of error out of bounds: %sr)r build_logical_line update_staterEdebugrrrupdate_checker_state_forr find_offsetrFranext_logical_line) r:commentsrmappingrr9rrPrNrs rrun_logical_checkszFileChecker.run_logical_checkss ~~)))*...*K*K*M',  ##G, & (;(;(=>ll// F NN 3 3F ;nnV,nGM2G '  -8-I* ]-414KK EvN # +(    ((*r+c|jJ|jjD]}|jj||j ||}|4d} |d}t|tr|f}|D]0}|\}}|jd|jj||2y#t t f$rY\wxYw)zoRun all checks for a given physical line. A single physical check may return multiple errors. N)rQrr) r rrQrr IndexError TypeErrorrrrarN)r:rQrresultr result_singlerPs rrun_physical_checkszFileChecker.run_physical_checkss ~~)))ll00 F NN 3 3F ;^^F-^HF! $ $*1IMmS1$YF%+M*7'M4KK#'$(NN$>$>,!  ! #I.sB,,B>=B>c|jJd}|j}|j}d}|jD]}|dxxdz cc<|j|||dd\}}|tj k(rtj ||}n+|dk(r&tj|r|j||d}|jr/|j|jd|jyy) zProcess tokens and trigger checks. Instead of using this directly, you should use :meth:`flake8.checker.FileChecker.run_checks`. Nrr2rYrZr) r r6generate_tokenscheck_physical_eolrOPcount_parenthesestoken_is_newlinehandle_newliner2rrr)r:parensr6file_processor prev_physicalr token_typerPs rprocess_tokenszFileChecker.process_tokenss ~~)))__  #335 %E x A %  # #E= 9$Qqz JX[[("44VTB1--e4'' 3!!HM %   $ $^%9%9"%= >  # # % !r+c |j |js#|j|j|jfS |j |j |jjd}||jd<|j|j|jfS#ttjf$r}t|tjrdnd}|j|\}}|j|||t|jd|jd|j|j|jfcYd}~Sd}~wwxYw)zRun checks against the file.NrE999rrr0)r rrr9r6rrrrrrrrarrr)r:rrMrrU logical_liness rr(zFileChecker.run_checkss  >> !)<)<$$dllDOOC C D    !    !11/B +8(  $,,??X001 D'8+>+>?6VD::1=KC KKc6d1g.>.>-?r!&&)+M N$$dllDOOC C  Ds B((EB EEEcj|jJ|tjk(r+|j|jj yt |jj dk(r5|jj|jjy|jy)z3Handle the logic when encountering a newline token.NrY) r rNEWLINErreset_blank_beforer>r2visited_new_blank_linedelete_first_token)r:rs rrzFileChecker.handle_newlines~~))) )) )  # # % NN - - / && '1 , NN 1 1 3 NN - - /  # # %r+cB|jJ|jtk(r)|jj|jdy|jt k(r)|jj |jdytj|r=|jdk(r|j|y|j|jytj|r2|jj|D]}|j|yy)z@Run physical checks if and only if it is at the end of the line.Nrr) r rr fstring_startrzr tstring_start is_eol_tokenrris_multiline_stringmultiline_string)r:rrrs rrzFileChecker.check_physical_eol's~~))) :: & NN ( (Q 8 ZZ= ( NN ( (Q 8  # #E *zzR((7((4  * *5 177> /((. /2r+N)r&rrrrrr~r)r~r)r~zprocessor.FileProcessor | None) rTz str | NonerNrrUrrPrr~r)rrrrr~r)rrr~rr)rQrr~r)r~#tuple[str, Results, dict[str, int]])rrr~r)rztokenize.TokenInforrr~r)rrrrr;rrrar staticmethodrrrrrr(rrrr+rr'r' sEJJ J $ J  J22     *288t*+4>&6@$ &/'/8;/ /r+r'c tj|t|fS#t$r}|jt vrYd}~yd}~wt $rYywxYw)z@Return a new process pool instance if we are able to create one.)initargsN)rHPoolr#rerrnoSERIAL_RETRY_ERRNOS ImportError) job_countr"errs rrcrcIsX  ##Ix4'JJ  99/ /  0     s AA AAct|tr|S|D]}|d}||ks|d}nd}dx}}|d|d|z|z fS)z*Find the offset tuple for a single offset.rrY)rr)rrx)rrr token_offsetpositions rrrYsm&%  "Qx \ !QxH  "  !! QK!v- < ==r+)rrrrr~zGenerator[None])r"r}r~r)r&rr~r)rrr"r}r~z multiprocessing.pool.Pool | None)rrrzprocessor._LogicalMappingr~r)6r __future__rargparse contextlibrloggingmultiprocessing.poolrHr\rrcollections.abcrrtypingrrflake8rr r r flake8._compatr r flake8.discover_filesrflake8.options.parse_argsrflake8.plugins.finderrrflake8.style_guiderrerxrrr getLoggerrrEENOSPCr__annotations__contextmanagerrr#r*r-r'rcrrr+rrs1*"  %$((.0*.0 uS#sC#67 8g! LL    %  % 2 % % % = q#q#h}/}/@    &  > >3>>r+