VL i 8dZddlmZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddl mZddl mZdd lmZdd lmZd Zej,eZGd d ej2ZGddej2ZGddej2Z ddZ ddZGddZGddZ GddZ!y)z0Implementation of the StyleGuide used by Flake8.) annotationsN) Generator)Sequence)defaults) statistics)utils)base) Violation) StyleGuideceZdZdZdZdZy)Selectedz>(($  #5>>((#  '>>$<<((   &>>OO$<<((   rc|j|jrtjS|j|jrtj St j S)aDetermine if the code has been selected by the user. :param code: The code for the check that has been run. :returns: Selected.Implicitly if the selected list is empty, Selected.Explicitly if the selected list is not empty and a match was found, Ignored.Implicitly if the selected list is not empty but no match was found. ) startswithr0r rr5rrr9codes r was_selectedzDecisionEngine.was_selectedfsJ ??433 4&& & __T]] +&& &%% %rc|j|jrtjS|j|jrtj St j S)aDetermine if the code has been ignored by the user. :param code: The code for the check that has been run. :returns: Selected.Implicitly if the ignored list is empty, Ignored.Explicitly if the ignored list is not empty and a match was found, Selected.Implicitly if the ignored list is not empty but no match was found. )r=r3rrr8rr r>s r was_ignoredzDecisionEngine.was_ignoredxsJ ??422 3%% % __T\\ *%% %&& &rc|j}|j}tjd||t |t r t |t rt j St |tr t |trt jS|t jur"|tjurt j S|t jur"|tjurt jS|tjur"|t jurt jS|t jur|tjus$|t jur|tjurstfd|jD}tfd|jD}t|t|kDrt j St jStdd|d|)z-Decide if code should be ignored or selected.z#The user configured %r to be %r, %rc3FK|]}j|s|ywNr=.0sr?s r z/DecisionEngine.make_decision..sIdooa6H!I!!c3FK|]}j|s|ywrErFrGs rrJz/DecisionEngine.make_decision..sHT__Q5G!HrKz unreachable  )r@rBLOGdebug isinstancer rrrrnextr5r8lenAssertionError)r9r?r5r8r.r1s ` r make_decisionzDecisionEngine.make_decisions$$T*""4( 1      h )j(.K$$ $ ' *z'7/K## # ++ +w111$$ $ H// /7---## # ++ +8;N;N0N## # ++ +7;M;M0M ++ +7;M;M0MIT]]IIFHT\\HHF6{S[((((''' <vQxj'!KL Lrc|jj|}|7|j|}||j|<tj d|||S)aReturn the decision for a specific code. This method caches the decisions for codes to avoid retracing the same logic over and over again. We only care about the select and ignore rules as specified by the user in their configuration files and command-line flags. This method does not look at whether the specific line is being ignored in the file itself. :param code: The code for the check that has been run. z"%s" will be "%s")r-getrTrNrO)r9r?decisions r decision_forzDecisionEngine.decision_forsP::>>$'  ))$/H'DJJt  II)4 :rN)r:argparse.NamespacereturnNone)r?strrZzSelected | Ignoredr?r\rZr) rrrrr;r@rBrTrXrrrr+r+Es"  4&$'&(MTrr+ceZdZdZ d d dZ d dZd dZejd dZ d d dZ y)StyleGuideManagerz.Manage multiple style guides for a single run.NcR||_||_tj|_|xs t ||_g|_t|||j||_ |jg|j||_tj|j|_y)SInitialize our StyleGuide. .. todo:: Add parameter documentation. )deciderN)r: formatterr Statisticsstatsr+rb style_guidesr default_style_guidepopulate_style_guides_with functoolsr-_style_guide_forstyle_guide_for)r9r:rcrbs rr;zStyleGuideManager.__init__s "**, 9."9 .0#- Y G$    $ $  , ,W 5  )t/D/DErc#Ktj|j}|D]$\}}|jj ||&yw)zGenerate style guides from the per-file-ignores option. :param options: The original options parsed from the CLI and config file. :returns: A copy of the default style guide with overridden values. )filenameextend_ignore_withN)rparse_files_to_codes_mappingper_file_ignoresrgcopy)r9r:per_filerm violationss rrhz,StyleGuideManager.populate_style_guides_withsU55g6N6NO$,  Hj**//!j0  sA A cFtfd|jDdS)z3Find the StyleGuide for the filename in particular.c3FK|]}|js|ywrE) applies_to)rHgrms rrJz5StyleGuideManager._style_guide_for..s D1Q\\(-CQ DrKc4t|jxsdS)N)rRrm)rws rz4StyleGuideManager._style_guide_for..s#ajj.B/r)key)maxrfr9rms `rrjz"StyleGuideManager._style_guide_fors! D)) D/  rc#K|j|}|j|5|dddy#1swYyxYwwz9Record the fact that we're processing the file's results.N)rkprocessing_file)r9rmguides rrz!StyleGuideManager.processing_files?$$X.  " "8 , K   s#?3 ?<?cP|j|}|j||||||S)Handle an error reported by a check. :param code: The error code found, e.g., E123. :param filename: The file in which the error was found. :param line_number: The line number (where counting starts at 1) at which the error occurs. :param column_number: The column number (where counting starts at 1) at which the error occurs. :param text: The text of the error message. :param physical_line: The actual physical line causing the error. :returns: 1 if the error was reported. 0 if it was ignored. This is to allow for counting of the number of errors found that were not ignored. )rk handle_error)r9r?rm line_number column_numbertext physical_liners rrzStyleGuideManager.handle_errors4:$$X.!! (Km  rrE)r:rYrcbase_formatter.BaseFormatterrbDecisionEngine | NonerZr[)r:rYrZGenerator[StyleGuide])rmr\rZr rmr\rZrr?r\rmr\rintrrrr\r str | NonerZr) rrrrr;rhrj contextlibcontextmanagerrrrrrr_r_s8 *. F#F0F' F  F2)   %)            "     rr_ceZdZdZ d d dZd dZ d d dZejddZ ddZ ddZ d dd Z y)r z#Manage a Flake8 user's style guide.Nc||_||_||_|xs t||_||_|j r%t j|j |_yy)raN)r:rcrer+rbrmrnormalize_path)r9r:rcrermrbs rr;zStyleGuide.__init__*sS " 9."9   ==!00?DM rc"d|jdS)z5Make it easier to debug which StyleGuide we're using.z rm)r9s r__repr__zStyleGuide.__repr__>st}}oR00rc|xs |j}tj|j}|jxsg|_|jj |xsgt ||j|j|S)z8Create a copy of this style guide with different values.r) rmrqdeepcopyr:r2r#r rcre)r9rmrnr:s rrqzStyleGuide.copyBso ,t}}-- - ' 5 5 ;$$%7%=2> T^^TZZ(  rc#K|jj|||jj|ywr)rc beginningfinishedr}s rrzStyleGuide.processing_filePs1   *  )s<>cr|jytj||jg|dtS)aCheck if this StyleGuide applies to the file. :param filename: The name of the file with violations that we're potentially applying this StyleGuide to. :returns: True if this applies, False otherwise Tz! does %(whether)smatch "%(path)s")patterns log_messagelogger)rmrmatches_filenamerNr}s rrvzStyleGuide.applies_toWs> == %% mm_("CD   rc8|jj|S)aDetermine if the error code should be reported or ignored. This method only cares about the select and ignore rules as specified by the user in their configuration files and command-line flags. This method does not look at whether the specific line is being ignored in the file itself. :param code: The code for the check that has been run. )rbrXr>s rshould_report_errorzStyleGuide.should_report_erroris||((..rcP|jj}|sd}t||||dz||}|j|jt j u} |j|du} | r9| r7|jj||jj|yy)rrF) r: disable_noqar rr?rr is_inline_ignoredrchandlererecord) r9r?rmrrrrrerrorerror_is_selectedis_not_inline_ignoreds rrzStyleGuide.handle_errorws:||00 M    A       $ $UZZ 0H4E4E E !& 7 7 E N !6 NN ! !% ( JJ  e $r)NN) r:rYrcrrezstatistics.Statisticsrmrrbr)rZr\)rmrrnzSequence[str] | NonerZr r)rmr\rZboolr]rEr) rrrrr;rrqrrrrvrrrrrr r 's- $)- @#@0@% @  @ ' @(1 $37    1     **  $ /*%)222 2  2  2"2 2rr )r"list[str] | Noner#rrZtuple[str, ...]) r"rr'rr(z list[str]r#rrZr)"r __future__rargparserrqenumriloggingcollections.abcrrflake8rrrflake8.formattingr base_formatterflake8.violationr __all__ getLoggerrrNEnumr rrr%r)r+r_r rrrrs6" %$4& g!'tyy'&dii& tyy , , , , ,  , , ,  ,  ,BBJZ Z zBBr