oGW dZddlZddlZddlZddlZddlZddlZddlZddlm Z ddlm Z ddlm Z ddlm Z ddlm Z ddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZdZejeZGddejZGddejZ GddejZ!de e e"de e e"dee"dffdZ#de e e"dee"dfde e"de e e"dee"dff dZ$Gdd Z%Gd!d"Z&Gd#d$Z'dS)%z0Implementation of the StyleGuide used by Flake8.N)Dict) Generator)List)Optional)Sequence)Set)Tuple)Union)defaults) statistics)utils)base) Violation) StyleGuideceZdZdZdZdZdS)Selectedz($ $ $  #5>(# # #  '>O$<(     &>O$<(     rcodec||jr tjS||jr tjSt jS)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. ) startswithr7rrr=rrrArCs r was_selectedzDecisionEngine.was_selectedksL ??43 4 4 && & __T] + + && &% %rc||jr tjS||jr tjSt jS)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. )rEr:rrr@rrrFs r was_ignoredzDecisionEngine.was_ignored}sL ??42 3 3 '% % __T\ * * '% %& &rc|}|}td||t |t r!t |t r t jSt |tr!t |tr t jS|t jur|tjur t jS|t jur|tjur t jS|tj ur|t j ur t jS|t jur|tjus|t j ur|tj urxtfd|j D}tfd|j D}t|t|kr t jSt jStdd|d|)z-Decide if code should be ignored or selected.z#The user configured %r to be %r, %rc3FK|]}||VdSNrE.0srCs r z/DecisionEngine.make_decision..s4IIdooa6H6HI!IIIIIIrc3FK|]}||VdSrLrMrNs rrQz/DecisionEngine.make_decision..s4HHT__Q5G5GH!HHHHHHrz unreachable  )rGrILOGdebug isinstancerr rrrnextr=r@lenAssertionError)rArCr=r@r5r8s ` r make_decisionzDecisionEngine.make_decisions$$T**""4(( 1        h ) ) Mj(.K.K M$ $ ' * * Mz'7/K/K M# # + + +w111$ $ H/ / /7---# # + + +8;N0N0N# # + + +7;M0M0M + + +7;M0M0MIIIIT]IIIIIFHHHHT\HHHHHF6{{S[[(((('' !K!K!Kx!K!K'!K!KLL Lrc|j|}|;||}||j|<td|||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. Nz"%s" will be "%s")r4getrZrTrU)rArCdecisions r decision_forzDecisionEngine.decision_forsX:>>$''  ))$//H'DJt  II)4 : : :r)rrrrargparse NamespacerBstrr rrrGrIr rZr^rrrr1r1Js   2 t    4&&x/@)A&&&&$''h.?(@''''&(M#(M((M(M(M(MTrr1ceZdZdZ ddejdejdee ddfdZ dejde dfd Z d e dd fd Zejd e de dfd Z dde d e dedede dee defdZdee eefddfdZdS)StyleGuideManagerz.Manage multiple style guides for a single run.Nr2 formatterdeciderr#c||_||_tj|_|pt ||_g|_t|||j||_ ttj |j g| ||_tjd|j|_dS)SInitialize our StyleGuide. .. todo:: Add parameter documentation. )reN)maxsize)r2rdr Statisticsstatsr1re style_guidesrdefault_style_guidelist itertoolschainpopulate_style_guides_with functools lru_cache_style_guide_forstyle_guide_for)rAr2rdres rrBzStyleGuideManager.__init__s "*,, 9."9"9 .0#- Y G$ $ $  ! O)*//88     Ay24@@@  !  rrNNc#Ktj|j}|D]#\}}|j||V$dS)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)r parse_files_to_codes_mappingper_file_ignoresrlcopy)rAr2per_filerw violationss rrpz,StyleGuideManager.populate_style_guides_withsm5g6NOO$,   Hj*//!j0      rrwrcJtfd|jDdS)z3Find the StyleGuide for the filename in particular.c3FK|]}||VdSrL) applies_to)rOgrws rrQz5StyleGuideManager._style_guide_for..s4 D D1Q\\(-C-C DQ D D D D D Drc.t|jpdS)N)rXrw)rs rz4StyleGuideManager._style_guide_for..s#aj.B//r)key)maxrkrArws `rrsz"StyleGuideManager._style_guide_fors: D D D D) D D D//    rc#K||}||5|VddddS#1swxYwYdSz9Record the fact that we're processing the file's results.N)rtprocessing_file)rArwguides rrz!StyleGuideManager.processing_files $$X..  " "8 , ,  KKK                  s?AArC line_number column_numbertext physical_linec`||}|||||||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. )rt handle_error)rArCrwrrrrrs rrzStyleGuideManager.handle_errors<:$$X..!! (Km   rdiffinfocD|jD]}||dS)a5Update the StyleGuides to filter out information not in the diff. This provides information to the underlying StyleGuides so that only the errors in the line number ranges are reported. :param diffinfo: Dictionary mapping filenames to sets of line number ranges. N)rkadd_diff_ranges)rArrs rrz!StyleGuideManager.add_diff_ranges1s7& , ,E  ! !( + + + + , ,rrL)rrrrr_r`base_formatter BaseFormatterrr1rBrrprars contextlibcontextmanagerrintrrrrrrrrcrcs88 -1   # "/ .)       :) + ,        + ,(,              }        D ,S#c(](; , , , , , , ,rrccjeZdZdZ ddejdejdej de e de e f dZ d e fd Z dde e d e ee d dfd Zejde d ed fdZde d efdZde d efdZ dde de dedede de e d efdZdee eefd dfdZdS)rz#Manage a Flake8 user's style guide.Nr2rdrjrwrec||_||_||_|pt||_||_|jrt j|j|_i|_dS)rgN) r2rdrjr1rerwr normalize_path _parsed_diff)rAr2rdrjrwres rrBzStyleGuide.__init__Asa " 9."9"9   = @!0??DM13rr#cd|jdS)z5Make it easier to debug which StyleGuide we're using.z rw)rAs r__repr__zStyleGuide.__repr__Vs0t}0000rrxc|p|j}tj|j}|jpg|_|j|pgt ||j|j|S)z8Create a copy of this style guide with different values.r) rwr{deepcopyr2r9r"rrdrj)rArwrxr2s rr{zStyleGuide.copyZsr ,t}- -- ' 5 ;$$%7%=2>>> T^TZ(    rruc#zK|j||V|j|dSr)rd beginningfinishedrs rrzStyleGuide.processing_filehsC   ***  )))))rc`|jdStj||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 NTz! does %(whether)smatch "%(path)s")patterns log_messagelogger)rwr matches_filenamerTrs rrzStyleGuide.applies_toqsD = 4% m_DDD     rrCc6|j|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. )rer^rFs rshould_report_errorzStyleGuide.should_report_errors|((...rrrrrcz|jj}|sd}t||||dz||}||jt ju} ||du} ||j } | r:| r8| r6|j ||j |dSdS)rrF)r2 disable_noqarrrCr ris_inline_ignoredis_inrrdhandlerjrecord) rArCrwrrrrrerrorerror_is_selectedis_not_inline_ignoredis_included_in_diffs rrzStyleGuide.handle_errors:|0  M    A        $ $UZ 0 0H4E E !& 7 7 E E N#kk$*;<<  !6 ;N  N ! !% ( ( ( J  e $ $ $1qrrc||_dS)a(Update the StyleGuide to filter out information not in the diff. This provides information to the StyleGuide so that only the errors in the line number ranges are reported. :param diffinfo: Dictionary mapping filenames to sets of line number ranges. N)r)rArs rrzStyleGuide.add_diff_rangess%r)NNrL)rrrrr_r`rrr rirrar1rBrrr{rrrrboolrr rrrrrrrrrrr>s--#',0 44#4"/4$ 4 3- 4 .) 4444*1#1111 #'6:   3-  %Xc]3        ** + ,**** 3 4    $ / / / / / /*(,3333 3  3  3 }3 3333j %S#c(](; % % % % % % %rr)(rr_rr{enumrqrnloggingtypingrrrrrrr r flake8r r r flake8.formattingrrflake8.violationr__all__ getLoggerrrTEnumrrr rar+r/r1rcrrrrrs*66 444444&&&&&& g!!'''''ty'''&&&&&di&&&     ty   , T#Y , T#Y , 38_ ,,,, , T#Y  ,38_ ,3i , T#Y  ,  38_ , , , ,BBBBBBBBJl,l,l,l,l,l,l,l,^Q%Q%Q%Q%Q%Q%Q%Q%Q%Q%r