
    c?                       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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j        e          Z eej        ej        g          Z eej        ej        ej        ej        g          Zeeeeeef         f                  Zeee         ee         ef         Z G d d          ZddZ ddZ!ddZ"ddZ#ddZ$d dZ%dS )!zFModule containing our file processor that tokenizes a file for checks.    )annotationsN)Any)	Generator)List)Tuple)defaults)utils)LoadedPluginc                      e Zd ZdZdZ	 d<d=dZed>d            Zej	        d?d            Z
d@dZd@dZd@dZdAdZdBdZd@dZdCdZdDd ZdEd"ZdFd&ZdGd+ZdHd-ZdId1ZdJd3ZdKd4ZdLd6ZdLd7ZdLd8ZdMd:Zd@d;ZdS )NFileProcessora=  Processes a file and holds state.

    This processes a file by generating tokens, logical and physical lines,
    and AST trees. This also provides a way of passing state about the file
    to checks expecting that state. Any public attribute on this object can
    be requested by a plugin. The known public attributes are:

    - :attr:`blank_before`
    - :attr:`blank_lines`
    - :attr:`checker_state`
    - :attr:`indent_char`
    - :attr:`indent_level`
    - :attr:`line_number`
    - :attr:`logical_line`
    - :attr:`max_line_length`
    - :attr:`max_doc_length`
    - :attr:`multiline`
    - :attr:`noqa`
    - :attr:`previous_indent_level`
    - :attr:`previous_logical`
    - :attr:`previous_unindented_logical_line`
    - :attr:`tokens`
    - :attr:`file_tokens`
    - :attr:`total_lines`
    - :attr:`verbose`
    FNfilenamestroptionsargparse.Namespacelineslist[str] | NonereturnNonec                   || _         || _        ||n|                                 | _        |                                  d| _        d| _        i | _        i | _        |j	        | _	        d| _
        d| _        |j        | _        d| _        d| _        |j        | _        |j        | _        d| _        d| _        d| _        d| _        g | _        t-          | j                  | _        |j        | _        ddi| _        d| _        d| _        dS )z]Initialize our file processor.

        :param filename: Name of the file to process
        Nr    Flogical lines)r   r   
read_linesr   strip_utf_bomblank_beforeblank_lines_checker_stateschecker_statehang_closingindent_charindent_levelindent_sizeline_numberlogical_linemax_line_lengthmax_doc_length	multilineprevious_indent_levelprevious_logical previous_unindented_logical_linetokenslentotal_linesverbose
statistics_file_tokens_noqa_line_mapping)selfr   r   r   s       0lib/python3.11/site-packages/flake8/processor.py__init__zFileProcessor.__init__<   s     #FUUT__5F5F
 :<-/#0'+".&6%4%&" "02-02tz??*A.=A9=    list[tokenize.TokenInfo]c                    | j         =t          | j                  t          t	          j        fd                    | _         | j         S )z-Return the complete set of tokens for a file.Nc                 "    t                     S N)next)	line_iters   r2   <lambda>z+FileProcessor.file_tokens.<locals>.<lambda>~   s    i r4   )r/   iterr   listtokenizegenerate_tokens)r1   r:   s    @r2   file_tokenszFileProcessor.file_tokensx   sT      	TZ((I $()@)@)@)@AA! !D   r4   r"   intGenerator[None, None, None]c              #  <   K   || _         d| _        dV  d| _        dS )z2Context-manager to toggle the multiline attribute.TNF)r"   r&   )r1   r"   s     r2   inside_multilinezFileProcessor.inside_multiline   s+      
 'r4   c                    d| _         dS )z)Reset the blank_before attribute to zero.r   N)r   r1   s    r2   reset_blank_beforez FileProcessor.reset_blank_before   s    r4   c                    | j         d= dS )z-Delete the first token in the list of tokens.r   N)r*   rF   s    r2   delete_first_tokenz FileProcessor.delete_first_token   s    KNNNr4   c                &    | xj         dz  c_         dS )z&Note that we visited a new blank line.   N)r   rF   s    r2   visited_new_blank_linez$FileProcessor.visited_new_blank_line   s    Ar4   mapping_LogicalMappingc                    |d         d         \  }}| j         |dz
           }t          |d|                   | _        | j        | j        k     r| j        | _        dS dS )z:Update the indent level based on the logical line mapping.r   rK   N)r   expand_indentr    r   r   )r1   rM   	start_row	start_col
start_lines        r2   update_statezFileProcessor.update_state   sk    !(AIZ	A.
)*ZiZ*@AAt// 	1 $ 0D	1 	1r4   pluginr
   c                f    d|j         v r'| j                            |j        i           | _        dS dS )z2Update the checker_state attribute for the plugin.r   N)
parametersr   
setdefault
entry_namer   )r1   rU   s     r2   update_checker_state_forz&FileProcessor.update_checker_state_for   sE    f// 	!%!5!@!@!2" "D	 	r4   c                    | j         r+| j        | _        | j         | _        | j        s| j         | _        d| _        g | _        dS )zoRecord the previous logical line.

        This also resets the tokens list and the blank_lines count.
        r   N)r#   r    r'   r(   r)   r   r*   rF   s    r2   next_logical_linezFileProcessor.next_logical_line   sP    
  	J)-):D&$($5D!$ J8<8I5r4   _Logicalc                   g }g }g }d}dx}}| j         D ]\  }}}	}
}|t          v r|sd|	fg}|t          j        k    r|                    |           ?|t          j        k    rt          |          }|rO|	\  }}||k    r1|dz
  }|dz
  }| j        |         |         }|dk    s|dvr	|dvrd| }n||k    r|||         |z   }|                    |           |t          |          z  }|                    ||
f           |
\  }}|||fS )z4Build the mapping, comments, and logical line lists.r   NrK   ,z{[(}]) )	r*   SKIP_TOKENSr>   COMMENTappendSTRINGmutate_stringr   r+   )r1   logicalcommentsrM   lengthprevious_rowprevious_column
token_typetextstartendlinerQ   start_column	row_indexcolumn_indexprevious_texts                    r2   build_logical_line_tokensz'FileProcessor.build_logical_line_tokens   s   #%)--26+ 	2 	2.JeS$[(  'u:,X-- %%%X_, +$T** E,1)L9, 	E ,q 0I#2Q#6L$(Jy$9,$GM$+ *%U2*7;57H*  *4zz$4 E <=DDNN4   c$iiFNNFC=))).1+\??'))r4   ast.ASTc                Z    t          j        d                    | j                            S )z5Build an abstract syntax tree from the list of lines.r   )astparsejoinr   rF   s    r2   	build_astzFileProcessor.build_ast   s     y,,---r4    tuple[str, str, _LogicalMapping]c                    |                                  \  }}}d                    |          }d                    |          | _        | j        dxx         dz  cc<   || j        |fS )z2Build a logical line from the current tokens list.r   r   rK   )ru   rz   r#   r.   )r1   rh   rg   mapping_listjoined_commentss        r2   build_logical_linez FileProcessor.build_logical_line   sm    *.*H*H*J*J''<''(++GGG,,(((A-((( 1<??r4   tokentokenize.TokenInfoGenerator[str, None, None]c              #     K   t          |j        d         |j        d                   D ]$}| j        |dz
           V  | xj        dz  c_        %dS )z|Split a physical line's line based on new-lines.

        This also auto-increments the line number for the caller.
        r   rK   N)rangern   ro   r   r"   )r1   r   line_nos      r2   
split_linezFileProcessor.split_line   se       U[^UYq\:: 	" 	"G*Wq[))))!	" 	"r4   rW   dict[str, bool]	argumentsdict[str, Any]c                    i }|                                 D ]L\  }}||v r
	 t          | |          ||<   # t          $ r! |r t                              d|           Y Iw xY w|S )z8Generate the keyword arguments for a list of parameters.zPPlugin requested optional parameter "%s" but this is not an available parameter.)itemsgetattrAttributeErrorLOGwarning)r1   rW   r   retparamrequireds         r2   keyword_arguments_forz#FileProcessor.keyword_arguments_for   s     )//11 	 	OE8	! 
$T511E

!    KKB    	 
s   6(A! A!)Generator[tokenize.TokenInfo, None, None]c              #     K   t          j        | j                  D ]:}|d         d         | j        k    r dS | j                            |           |V  ;dS )z'Tokenize the file and yield the tokens.   r   N)r>   r?   	next_liner,   r*   rd   )r1   r   s     r2   r?   zFileProcessor.generate_tokens  sl      -dn== 	 	EQx{T-- Ku%%%KKKK		 	r4   min_linemax_linedict[int, str]c                    t          ||dz             }d                    | j        |dz
  |                   }t                              ||          S )NrK   r   )r   rz   r   dictfromkeys)r1   r   r   
line_rangejoineds        r2   _noqa_line_rangezFileProcessor._noqa_line_range  sJ    8X\22
HqL8$;<==}}Z000r4   
str | Nonec                ~   | j         	 | j        }i }t          | j                  dz   }d}|D ]\  }}\  }}\  }	}}|t          j        k    r n}t          ||          }t          ||	          }|t          j        t          j	        fv rB|
                    |                     ||                     t          | j                  dz   }d}|dk    r)|
                    |                     ||                     || _         n## t          j        t          f$ r
 i | _         Y nw xY w| j                             |          S )z7Retrieve the line which will be used to determine noqa.Nr   )r0   r@   r+   r   r>   	ENDMARKERminmaxNLNEWLINEupdater   
TokenErrorSyntaxErrorget)
r1   r"   r@   r   r   r   tp_s_linee_lines
             r2   noqa_line_forzFileProcessor.noqa_line_for  si   " 	..". tz??Q.:E & &6B;FAQX// "8V44H"8V44Hhk8+;<< &

4#8#88#L#LMMM#&tz??Q#6#%
 r> JJJt44XxHHIII*-''9 '5 - - - +-'''-B &**;777s   D D"!D"c                    | j         | j        k    rdS | j        | j                  }| xj         dz  c_         | j        #|dd         t          j        v r|d         | _        |S )z Get the next line from the list.r   rK   Nr   )r"   r,   r   r   r   
WHITESPACE)r1   rp   s     r2   r   zFileProcessor.next_line=  sp    t// 	2z$*+A 	'RaRH4G(G 	'#AwDr4   	list[str]c                    | j         dk    r(| j        j        pd| _         |                                 }n|                                 }|S )z%Read the lines for this file checker.-stdin)r   r   stdin_display_nameread_lines_from_stdinread_lines_from_filename)r1   r   s     r2   r   zFileProcessor.read_linesG  sK    =C 	4 L;FwDM..00EE1133Er4   c                J   	 t          j        | j                  5 }|                                cddd           S # 1 swxY w Y   dS # t          t
          f$ rH t          | j        d          5 }|                                cddd           cY S # 1 swxY w Y   Y dS w xY w)zRead the lines for a file.Nzlatin-1)encoding)r>   openr   	readlinesr   UnicodeError)r1   fds     r2   r   z&FileProcessor.read_lines_from_filenameP  s7   	&t}-- &||~~& & & & & & & & & & & & & & & & & &\* 	& 	& 	& dmi888 &B||~~& & & & & & & & & & & & & & & & & & & & &	&sU   A	 <A	 A  A	 A A	 	'B"0BB"B	B"B	B"!B"c                (    t          j                    S )z Read the lines from standard in.)r	   stdin_get_linesrF   s    r2   r   z#FileProcessor.read_lines_from_stdin[  s    $&&&r4   boolc                    | j         j        s t          d | j        D                       rdS t          d | j        D                       rt                              d           dS dS )zCheck if ``flake8: noqa`` is in the file to be ignored.

        :returns:
            True if a line matches :attr:`defaults.NOQA_FILE`,
            otherwise False
        c              3  T   K   | ]#}t           j                            |          V  $d S r8   )r   	NOQA_FILEmatch.0rp   s     r2   	<genexpr>z3FileProcessor.should_ignore_file.<locals>.<genexpr>f  sD       1
 1
/3H$$T**1
 1
 1
 1
 1
 1
r4   Tc              3  T   K   | ]#}t           j                            |          V  $d S r8   )r   r   searchr   s     r2   r   z3FileProcessor.should_ignore_file.<locals>.<genexpr>j  s3      HHT#**400HHHHHHr4   z[Detected `flake8: noqa` on line with code. To ignore an error on a line use `noqa` instead.F)r   disable_noqaanyr   r   r   rF   s    r2   should_ignore_filez FileProcessor.should_ignore_file_  s     |( 	S 1
 1
7;z1
 1
 1
 .
 .
 	 4HHTZHHHHH 	KK6   55r4   c                   | j         sdS t          | j         d         d                   }|dvrdS |dk    r| j         d         dd         | j         d<   dS | j         d         dd         dk    r| j         d         dd         | j         d<   dS dS )z-Strip the UTF bom from the lines of the file.Nr   )     r   rK      u   ï»¿)r   ord)r1   
first_bytes     r2   r   zFileProcessor.strip_utf_boms  s    z 	FAq)**
^+ 	F  	. JqM!""-DJqMMMZ]2A2.0 	. JqM!""-DJqMMM	. 	.r4   r8   )r   r   r   r   r   r   r   r   )r   r5   )r"   rA   r   rB   )r   r   )rM   rN   r   r   )rU   r
   r   r   )r   r]   )r   rv   )r   r|   )r   r   r   r   )rW   r   r   r   r   r   )r   r   )r   rA   r   rA   r   r   )r"   rA   r   r   )r   r   )r   r   )r   r   )__name__
__module____qualname____doc__noqar3   propertyr@   
contextlibcontextmanagerrD   rG   rI   rL   rT   rZ   r\   ru   r{   r   r   r   r?   r   r   r   r   r   r   r   r    r4   r2   r   r      s        8 D #'	:> :> :> :> :>x ! ! ! X!             1 1 1 1      !* !* !* !*F. . . .@ @ @ @" " " "   .   1 1 1 1
&8 &8 &8 &8P      	& 	& 	& 	&' ' ' '   (. . . . . .r4   r   r   r   r   r   c                    | d         t           v p1| d         | d         d         d                                         dk    S )z+Check if the token is an end-of-line token.r      r   rK   Nz\
)r   lstripr   s    r2   is_eol_tokenr     s<    8wL%(58A;=="9"@"@"B"Bf"LLr4   c                B    | d         t           j        k    o	d| d         v S )z$Check if this is a multiline string.r   
rK   )r>   re   r   s    r2   is_multiline_stringr     s!    8x&;458+;;r4   c                     | d         t           v S )z0Check if the token type is a newline token type.r   )r   r   s    r2   token_is_newliner     s    8wr4   current_parentheses_countrA   
token_textr   c                *    |dv r| dz   S |dv r| dz
  S | S )z Count the number of parentheses.z([{rK   r`   r   )r   r   s     r2   count_parenthesesr     s8    U -(1,,	u	 -(1,,$$r4   rp   c                F    t          |                     d                    S )zReturn the amount of indentation.

    Tabs are expanded to the next multiple of 8.

    >>> expand_indent('    ')
    4
    >>> expand_indent('\t')
    8
    >>> expand_indent('       \t')
    8
    >>> expand_indent('        \t')
    16
       )r+   
expandtabs)rp   s    r2   rP   rP     s     tq!!"""r4   rm   c                    |                      | d                   dz   }t          |           dz
  }| dd         dv r
|dz  }|dz  }| d|         d||z
  z  z   | |d         z   S )zReplace contents with 'xxx' to prevent syntax matching.

    >>> mutate_string('"abc"')
    '"xxx"'
    >>> mutate_string("'''abc'''")
    "'''xxx'''"
    >>> mutate_string("r'abc'")
    "r'xxx'"
    r   rK   N)z"""z'''r   x)indexr+   )rm   rn   ro   s      r2   rf   rf     s}     JJtBx  1$E
d))a-CBCCyN" 
q<#u--STT
::r4   )r   r   r   r   )r   rA   r   r   r   rA   )rp   r   r   rA   )rm   r   r   r   )&r   
__future__r   argparserx   r   loggingr>   typingr   r   r   r   flake8r   r	   flake8.plugins.finderr
   	getLoggerr   r   	frozensetr   r   INDENTDEDENTrb   rA   rN   r   r]   r   r   r   r   r   rP   rf   r   r4   r2   <module>r      s
   L L " " " " " "  



                                           . . . . . .g!!
)X[("23
4
4i[("HOX_E  uS%S/123cDI67d. d. d. d. d. d. d. d.NM M M M
< < < <
   
% % % %# # # #(; ; ; ; ; ;r4   