o
    h@                     @  sJ  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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	dSdTddZedUddZdVddZdWddZ	dXddZ
dXddZdXddZdYd"d#ZdZd&d'ZdXd(d)Zd[d+d,Zd\d.d/Zd]d1d2Zd^d7d8Zd_d:d;Zd`d?d@ZdadCdDZdbdEdFZdcdHdIZdcdJdKZdcdLdMZdddOdPZdXdQdRZdS )e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| _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_fstring_start)selfr   r   r    r3   F/var/www/html/ai/venv/lib/python3.10/site-packages/flake8/processor.py__init__=   s6   


zFileProcessor.__init__list[tokenize.TokenInfo]c                   s2   | j du rt| j tt fdd| _ | j S )z-Return the complete set of tokens for a file.Nc                     s   t  S N)nextr3   	line_iterr3   r4   <lambda>   s    z+FileProcessor.file_tokens.<locals>.<lambda>)r/   iterr   listtokenizegenerate_tokensr2   r3   r9   r4   file_tokensz   s   

zFileProcessor.file_tokenslinenointc                 C  s
   || _ dS )z#Signal the beginning of an fstring.N)r1   )r2   rB   r3   r3   r4   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	   r1   startr&   r"   rangeendr   )r2   rF   rK   _r3   r3   r4   multiline_string   s   


zFileProcessor.multiline_stringc                 C  s
   d| _ dS )z)Reset the blank_before attribute to zero.r   N)r   r@   r3   r3   r4   reset_blank_before   rE   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@   r3   r3   r4   delete_first_token      z FileProcessor.delete_first_tokenc                 C  s   |  j d7  _ dS )z&Note that we visited a new blank line.rI   N)r   r@   r3   r3   r4   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   rI   N)r   expand_indentr    r   r   )r2   rU   	start_row	start_col
start_liner3   r3   r4   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   )r2   r\   r3   r3   r4   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@   r3   r3   r4   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xrI   ,z{[(}]) )
r*   SKIP_TOKENSr>   COMMENTappendSTRINGmutate_stringr
   r+   r   )r2   logicalcommentsrU   lengthprevious_rowprevious_column
token_typetextrK   rM   linerX   start_column	row_indexcolumn_indexprevious_textr3   r3   r4   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@   r3   r3   r4   	build_ast   rT   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   rI   )rx   r|   r#   r.   )r2   rm   rl   mapping_listjoined_commentsr3   r3   r4   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)r2   r]   r   retparamrequiredr3   r3   r4   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*   ri   )r2   rF   r3   r3   r4   r?     s   zFileProcessor.generate_tokensmin_linemax_linedict[int, str]c                 C  s2   t ||d }d| j|d | }t||S )NrI   r   )rL   r|   r   dictfromkeys)r2   r   r   
line_rangejoinedr3   r3   r4   _noqa_line_range  s   zFileProcessor._noqa_line_ranger"   
str | Nonec           
   	   C  s   | j du rnz| j}W n tjtfy   i | _ Y nUw i }t| jd }d}|D ]6\}}\}}\}	}}|tjkr9 n%t||}t	||	}|tj
tjfv r]|| || t| jd }d}q'|dkrk|| || || _ | j |S )z7Retrieve the line which will be used to determine noqa.Nr   r   )r0   rA   r>   
TokenErrorSyntaxErrorr+   r   	ENDMARKERminmaxNLNEWLINEupdater   get)
r2   r"   rA   r   r   r   tprN   s_linee_liner3   r3   r4   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   rI   Nr   )r"   r,   r   r   r   
WHITESPACE)r2   rs   r3   r3   r4   r   @  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)r2   r   r3   r3   r4   r   J  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)r2   fdr3   r3   r4   r   S  s   (*z&FileProcessor.read_lines_from_filenamec                 C  s   t  S )z Read the lines from standard in.)r   stdin_get_linesr@   r3   r3   r4   r   ^  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 r7   )r   	NOQA_FILEmatch.0rs   r3   r3   r4   	<genexpr>i  s    
z3FileProcessor.should_ignore_file.<locals>.<genexpr>Tc                 s  r   r7   )r   r   searchr   r3   r3   r4   r   m  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@   r3   r3   r4   should_ignore_fileb  s   z FileProcessor.should_ignore_filec                 C  s   | 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   rI      u   ï»¿)r   ord)r2   
first_byter3   r3   r4   r   v  s   zFileProcessor.strip_utf_bomr7   )r   r   r   r   r   r   r   r   )r   r6   )rB   rC   r   r   )rF   rG   r   rH   )r   r   )rU   rV   r   r   )r\   r   r   r   )r   rb   )r   ry   )r   r~   )r]   r   r   r   r   r   )r   r   )r   rC   r   rC   r   r   )r"   rC   r   r   )r   r   )r   r   )r   r   )__name__
__module____qualname____doc__noqar5   propertyrA   rD   rO   rP   rQ   rS   r[   r`   ra   rx   r}   r   r   r?   r   r   r   r   r   r   r   r   r3   r3   r3   r4   r      s8    =










%





(


	

r   rF   rG   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   rI   Nz\
)r   lstriprF   r3   r3   r4   is_eol_token  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.
)rJ   r	   r>   rj   stringr   r3   r3   r4   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   r3   r3   r4   token_is_newline  rR   r   current_parentheses_countrC   
token_textr   c                 C  s$   |dv r| d S |dv r| d S | S )z Count the number of parentheses.z([{rI   re   r3   )r   r   r3   r3   r4   count_parentheses  s
   r   rs   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)rs   r3   r3   r4   rW     s   rW   rr   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   rI   N)z"""z'''r   rc   )indexr+   )rr   rK   rM   r3   r3   r4   rk     s   $rk   )rF   rG   r   r   )r   rC   r   r   r   rC   )rs   r   r   rC   )rr   r   r   r   )(r   
__future__r   argparserz   loggingr>   typingr   r   r   r   flake8r   r   flake8._compatr	   r
   flake8.plugins.finderr   	getLoggerr   r   	frozensetr   r   INDENTDEDENTrg   rC   rV   r   rb   r   r   r   r   r   rW   rk   r3   r3   r3   r4   <module>   s>    
  
k



	