o
    7fo>                     @  sR  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 ddlmZ ddlmZ e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 dZ"d%ddZ#d%ddZ$d%ddZ%d&ddZ&d'd d!Z'd(d#d$Z(dS ))zFModule containing our file processor that tokenizes a file for checks.    )annotationsN)Any)	Generator)List)Tuple)defaults)utils)FSTRING_END)FSTRING_MIDDLE)LoadedPluginc                   @  s  e Zd ZdZdZ	dUdVddZejdWddZdXddZ	dYddZ
dZddZdZddZdZddZd[d"d#Zd\d&d'ZdZd(d)Zd]d+d,Zd^d.d/Zd_d1d2Zd`d7d8Zdad:d;Zdbd?d@ZejdcdAdBZdddEdFZdedGdHZdfdJdKZdfdLdMZdfdNdOZdgdQdRZdZdSdTZdS )h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                 C  s   || _ || _|dur|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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_fstring_start)selfr   r   r    r1   I/home/ertert/spirit/venv/lib/python3.10/site-packages/flake8/processor.py__init__>   s2   


zFileProcessor.__init__list[tokenize.TokenInfo]c                   s    t | j tt fddS )z-Return the complete set of tokens for a file.c                     s   t  S N)nextr1   	line_iterr1   r2   <lambda>|   s    z+FileProcessor.file_tokens.<locals>.<lambda>)iterr   listtokenizegenerate_tokensr0   r1   r7   r2   file_tokensx   s   
zFileProcessor.file_tokenslinenointc                 C  s
   || _ dS )z#Signal the beginning of an fstring.N)r/   )r0   r@   r1   r1   r2   fstring_start~      
zFileProcessor.fstring_starttokentokenize.TokenInfoGenerator[str, None, None]c                 c  sj    |j tkr
| j}n|jd }d| _|| _t||jd D ]}| j| jd  V  |  jd7  _qd| _dS )z0Iterate through the lines of a multiline string.r   T   FN)	typer	   r/   startr&   r"   rangeendr   )r0   rD   rI   _r1   r1   r2   multiline_string   s   


zFileProcessor.multiline_stringc                 C  s
   d| _ dS )z)Reset the blank_before attribute to zero.r   N)r   r>   r1   r1   r2   reset_blank_before   rC   z FileProcessor.reset_blank_beforec                 C  s   | j d= dS )z-Delete the first token in the list of tokens.r   N)r*   r>   r1   r1   r2   delete_first_token      z FileProcessor.delete_first_tokenc                 C  s   |  j d7  _ dS )z&Note that we visited a new blank line.rG   N)r   r>   r1   r1   r2   visited_new_blank_line      z$FileProcessor.visited_new_blank_linemapping_LogicalMappingc                 C  sL   |d d \}}| j |d  }t|d| | _| j| jk r$| j| _dS dS )z:Update the indent level based on the logical line mapping.r   rG   N)r   expand_indentr    r   r   )r0   rS   	start_row	start_col
start_liner1   r1   r2   update_state   s   zFileProcessor.update_statepluginr   c                 C  s$   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   )r0   rZ   r1   r1   r2   update_checker_state_for   s
   

z&FileProcessor.update_checker_state_forc                 C  s4   | 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*   r>   r1   r1   r2   next_logical_line   s   
zFileProcessor.next_logical_line_Logicalc                 C  s(  g }g }g }d}d }}| j D ]\}}}	}
}|tv rq|s"d|	fg}|tjkr-|| q|tjkr7t|}n
|tkrAdt| }|rx|	\}}||krl|d }|d }| j	| | }|dksf|dvrk|dvrkd| }n||krx||| | }|| |t|7 }|||
f |
\}}q|||fS )	z4Build the mapping, comments, and logical line lists.r   NxrG   ,z{[(}]) )
r*   SKIP_TOKENSr<   COMMENTappendSTRINGmutate_stringr
   r+   r   )r0   logicalcommentsrS   lengthprevious_rowprevious_column
token_typetextrI   rK   linerV   start_column	row_indexcolumn_indexprevious_textr1   r1   r2   build_logical_line_tokens   sD   








z'FileProcessor.build_logical_line_tokensast.ASTc                 C  s   t d| jS )z5Build an abstract syntax tree from the list of lines.r   )astparsejoinr   r>   r1   r1   r2   	build_ast   rR   zFileProcessor.build_ast tuple[str, str, _LogicalMapping]c                 C  sB   |   \}}}d|}d|| _| jd  d7  < || j|fS )z2Build a logical line from the current tokens list.r   r   rG   )rv   rz   r#   r.   )r0   rk   rj   mapping_listjoined_commentsr1   r1   r2   build_logical_line   s
   
z FileProcessor.build_logical_liner[   dict[str, bool]	argumentsdict[str, Any]c              	   C  sZ   i }|  D ]$\}}||v rqz	t| |||< W q ty*   |r" td| Y q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)r0   r[   r   retparamrequiredr1   r1   r2   keyword_arguments_for   s   	z#FileProcessor.keyword_arguments_for)Generator[tokenize.TokenInfo, None, None]c                 c  sB    t | jD ]}|d d | jkr dS | j| |V  qdS )z'Tokenize the file and yield the tokens.   r   N)r<   r=   	next_liner,   r*   rg   )r0   rD   r1   r1   r2   r=     s   zFileProcessor.generate_tokensmin_linemax_linedict[int, str]c                 C  s2   t ||d }d| j|d | }t||S )NrG   r   )rJ   rz   r   dictfromkeys)r0   r   r   
line_rangejoinedr1   r1   r2   _noqa_line_range  s   zFileProcessor._noqa_line_rangec           	   	   C  s   z| j }W n tjtfy   i  Y S w i }t| jd }d}|D ]:\}}\}}\}}}|tjks6|tjkr7q!t||}t	||}|tj
tjfv r[|| || t| jd }d}q!|S )z<Map from line number to the line we'll search for `noqa` in.r   r   )r?   r<   
TokenErrorSyntaxErrorr+   r   	ENDMARKERDEDENTminmaxNLNEWLINEupdater   )	r0   r?   r   r   r   tprL   s_linee_liner1   r1   r2   _noqa_line_mapping  s&   


z FileProcessor._noqa_line_mappingr"   
str | Nonec                 C  s   | j |S )z7Retrieve the line which will be used to determine noqa.)r   get)r0   r"   r1   r1   r2   noqa_line_for.  s   zFileProcessor.noqa_line_forc                 C  sT   | j | jkrdS | j| j  }|  j d7  _ | jdu r(|dd tjv r(|d | _|S )z Get the next line from the list.r   rG   Nr   )r"   r,   r   r   r   
WHITESPACE)r0   rq   r1   r1   r2   r   5  s   
zFileProcessor.next_line	list[str]c                 C  s0   | j dkr| jjp
d| _ |  }|S |  }|S )z%Read the lines for this file checker.-stdin)r   r   stdin_display_nameread_lines_from_stdinread_lines_from_filename)r0   r   r1   r1   r2   r   ?  s   
zFileProcessor.read_linesc                 C  s   zt | j}| W  d   W S 1 sw   Y  W dS  ttfyG   t| jdd}| W  d    Y S 1 s?w   Y  Y dS w )zRead the lines for a file.Nzlatin-1)encoding)r<   openr   	readlinesr   UnicodeError)r0   fdr1   r1   r2   r   H  s   (*z&FileProcessor.read_lines_from_filenamec                 C  s   t  S )z Read the lines from standard in.)r   stdin_get_linesr>   r1   r1   r2   r   S  s   z#FileProcessor.read_lines_from_stdinboolc                 C  sF   | j jstdd | jD rdS td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                 s      | ]	}t j|V  qd S r5   )r   	NOQA_FILEmatch.0rq   r1   r1   r2   	<genexpr>^  s    
z3FileProcessor.should_ignore_file.<locals>.<genexpr>Tc                 s  r   r5   )r   r   searchr   r1   r1   r2   r   b  s    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   r>   r1   r1   r2   should_ignore_fileW  s   z FileProcessor.should_ignore_filec                 C  sr   | j sdS | j d dd dkr| j d dd | j d< dS | j d dd dkr7| j d dd | j d< dS dS )z-Strip the UTF bom from the lines of the file.Nr   rG   u   ﻿   u   ï»¿)r   r>   r1   r1   r2   r   k  s   zFileProcessor.strip_utf_bomr5   )r   r   r   r   r   r   r   r   )r   r4   )r@   rA   r   r   )rD   rE   r   rF   )r   r   )rS   rT   r   r   )rZ   r   r   r   )r   r`   )r   rw   )r   r|   )r[   r   r   r   r   r   )r   r   )r   rA   r   rA   r   r   )r   r   )r"   rA   r   r   )r   r   )r   r   )r   r   )__name__
__module____qualname____doc__noqar3   	functoolscached_propertyr?   rB   rM   rN   rO   rQ   rY   r^   r_   rv   r{   r   r   r=   r   r   r   r   r   r   r   r   r   r1   r1   r1   r2   r      s<    :









%








	

r   rD   rE   r   r   c                 C  s,   | d t v p| d | d d d  dkS )z+Check if the token is an end-of-line token.r      r   rG   Nz\
)r   lstriprD   r1   r1   r2   is_eol_tokenx  s   ,r   c                 C  s    | j tkp| j tjkod| jv S )z$Check if this is a multiline string.
)rH   r	   r<   rh   stringr   r1   r1   r2   is_multiline_string}  s   
r   c                 C  s   | d t v S )z0Check if the token type is a newline token type.r   )r   r   r1   r1   r2   token_is_newline  rP   r   current_parentheses_countrA   
token_textr   c                 C  s$   |dv r| d S |dv r| d S | S )z Count the number of parentheses.z([{rG   rc   r1   )r   r   r1   r1   r2   count_parentheses  s
   r   rq   c                 C  s   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)rq   r1   r1   r2   rU     s   rU   rp   c                 C  sb   |  | d d }t| d }| dd dv r|d7 }|d8 }| d| d||   | |d  S )zReplace contents with 'xxx' to prevent syntax matching.

    >>> mutate_string('"abc"')
    '"xxx"'
    >>> mutate_string("'''abc'''")
    "'''xxx'''"
    >>> mutate_string("r'abc'")
    "r'xxx'"
    r   rG   N)z"""z'''r   ra   )indexr+   )rp   rI   rK   r1   r1   r2   ri     s   $ri   )rD   rE   r   r   )r   rA   r   r   r   rA   )rq   r   r   rA   )rp   r   r   r   ))r   
__future__r   argparserx   r   loggingr<   typingr   r   r   r   flake8r   r   flake8._compatr	   r
   flake8.plugins.finderr   	getLoggerr   r   	frozensetr   r   INDENTr   re   rA   rT   r   r`   r   r   r   r   r   rU   ri   r1   r1   r1   r2   <module>   s@    
  
[



	