o
    EDiA                     @  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)	Generator)Any)defaults)utils)FSTRING_END)FSTRING_MIDDLE)TSTRING_END)TSTRING_MIDDLE)LoadedPluginc                   @  s  e Zd ZdZdZ	dWdXddZejdYddZdZddZ	dZd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\d*d+Zd_d-d.Zd`d0d1Zdad3d4Zdbd9d:Zdcd<d=ZdddAdBZejdedCdDZdfdGdHZdgdIdJZdhdLdMZdhdNdOZdhdPdQZdidSdTZd\dUdVZdS )j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_tstring_start)selfr   r   r    r2   I/var/www/Datamplify/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)nextr2   	line_iterr2   r3   <lambda>|   s    z+FileProcessor.file_tokens.<locals>.<lambda>)iterr   listtokenizegenerate_tokensr1   r2   r8   r3   file_tokensx   s   
zFileProcessor.file_tokenslinenointc                 C  
   || _ dS )z#Signal the beginning of an fstring.N)r/   r1   rA   r2   r2   r3   fstring_start~      
zFileProcessor.fstring_startc                 C  rC   )z#Signal the beginning of an tstring.N)r0   rD   r2   r2   r3   tstring_start   rF   zFileProcessor.tstring_starttokentokenize.TokenInfoGenerator[str]c                 c  s|    |j tkr
| j}n|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/   r	   r0   startr&   r"   rangeendr   )r1   rH   rM   _r2   r2   r3   multiline_string   s   



zFileProcessor.multiline_stringc                 C  s
   d| _ dS )z)Reset the blank_before attribute to zero.r   N)r   r?   r2   r2   r3   reset_blank_before   rF   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?   r2   r2   r3   delete_first_token      z FileProcessor.delete_first_tokenc                 C  s   |  j d7  _ dS )z&Note that we visited a new blank line.rK   N)r   r?   r2   r2   r3   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   rK   N)r   expand_indentr    r   r   )r1   rW   	start_row	start_col
start_liner2   r2   r3   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   )r1   r^   r2   r2   r3   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?   r2   r2   r3   next_logical_line   s   
zFileProcessor.next_logical_line_Logicalc                 C  sd  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thv rY|	d|	d }dt
||  }|
d |
d | f}
|dur|dur|	\}}||kr|d }|d }| j| | }|dks|dvr|d	vrd
| }n||kr||| | }|| |t
|7 }|||
f |
\}}q|||fS )z4Build the mapping, comments, and logical line lists.r   N{}xrK   ,z{[(}]) )r*   SKIP_TOKENSr=   COMMENTappendSTRINGmutate_stringr   r
   countr+   r   )r1   logicalcommentsrW   lengthprevious_rowprevious_column
token_typetextrM   rO   linebrace_offsetrZ   start_column	row_indexcolumn_indexprevious_textr2   r2   r3   build_logical_line_tokens   sN   








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?   r2   r2   r3   	build_ast   rV   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   rK   )r~   r   r#   r.   )r1   rr   rq   mapping_listjoined_commentsr2   r2   r3   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)r1   r_   r   retparamrequiredr2   r2   r3   keyword_arguments_for   s   	z#FileProcessor.keyword_arguments_forGenerator[tokenize.TokenInfo]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*   rm   )r1   rH   r2   r2   r3   r>     s   zFileProcessor.generate_tokensmin_linemax_linedict[int, str]c                 C  s2   t ||d }d| j|d | }t||S )NrK   r   )rN   r   r   dictfromkeys)r1   r   r   
line_rangejoinedr2   r2   r3   _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   )	r1   r@   r   r   r   tprP   s_linee_liner2   r2   r3   _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)r1   r"   r2   r2   r3   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   rK   Nr   )r"   r,   r   r   r   
WHITESPACE)r1   rx   r2   r2   r3   r   B  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)r1   r   r2   r2   r3   r   L  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)r1   fdr2   r2   r3   r   U  s   (*z&FileProcessor.read_lines_from_filenamec                 C  s   t  S )z Read the lines from standard in.)r   stdin_get_linesr?   r2   r2   r3   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 r6   )r   	NOQA_FILEmatch.0rx   r2   r2   r3   	<genexpr>k  s    
z3FileProcessor.should_ignore_file.<locals>.<genexpr>Tc                 s  r   r6   )r   r   searchr   r2   r2   r3   r   o  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?   r2   r2   r3   should_ignore_filed  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   rK   u   ﻿   u   ï»¿)r   r?   r2   r2   r3   r   x  s   zFileProcessor.strip_utf_bomr6   )r   r   r   r   r   r   r   r   )r   r5   )rA   rB   r   r   )rH   rI   r   rJ   )r   r   )rW   rX   r   r   )r^   r   r   r   )r   rd   )r   r   )r   r   )r_   r   r   r   r   r   )r   r   )r   rB   r   rB   r   r   )r   r   )r"   rB   r   r   )r   r   )r   r   )r   r   ) __name__
__module____qualname____doc__noqar4   	functoolscached_propertyr@   rE   rG   rQ   rR   rS   rU   r]   rb   rc   r~   r   r   r   r>   r   r   r   r   r   r   r   r   r   r2   r2   r2   r3   r      s>    :










.








	

r   rH   rI   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   rK   Nz\
)r   lstriprH   r2   r2   r3   is_eol_token  s   ,r   c                 C  s$   | j tthv p| j tjkod| jv S )z$Check if this is a multiline string.
)rL   r   r	   r=   rn   stringr   r2   r2   r3   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   r2   r2   r3   token_is_newline  rT   r   current_parentheses_countrB   
token_textr   c                 C  s$   |dv r| d S |dv r| d S | S )z Count the number of parentheses.z([{rK   ri   r2   )r   r   r2   r2   r3   count_parentheses  s
   r   rx   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)rx   r2   r2   r3   rY     s   rY   rw   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   rK   N)z"""z'''r   rg   )indexr+   )rw   rM   rO   r2   r2   r3   ro     s   $ro   )rH   rI   r   r   )r   rB   r   r   r   rB   )rx   r   r   rB   )rw   r   r   r   ),r   
__future__r   argparser   r   loggingr=   collections.abcr   typingr   flake8r   r   flake8._compatr   r   r	   r
   flake8.plugins.finderr   	getLoggerr   r   	frozensetr   r   INDENTr   rk   r<   tuplerB   rX   r   rd   r   r   r   r   r   rY   ro   r2   r2   r2   r3   <module>   s@    
  
h



	