o
    f#,                     @   s   d Z dZddlZddlZddlZg dZdadadd Zdd Z	G d	d
 d
e
Zdd ZG dd dZdd Zdd ZedkrDe	  dS dS )a  The Tab Nanny despises ambiguous indentation.  She knows no mercy.

tabnanny -- Detection of ambiguous indentation

For the time being this module is intended to be called as a script.
However it is possible to import it into an IDE and use the function
check() described below.

Warning: The API provided by this module is likely to change in future
releases; such changes may not be backward compatible.
6    N)checkNannyNagprocess_tokensc                  G   s6   d}| D ]}t j|t|  d}qt jd d S )N  
)sysstderrwritestr)argsseparg r   /usr/lib/python3.10/tabnanny.pyerrprint    s
   r   c               
   C   s   dd l } z|  tjdd  d\}}W n | jy+ } zt| W Y d }~d S d }~ww |D ]\}}|dkr:td a|dkrBtd aq.|sPtdtjd d d S |D ]}t| qRd S )Nr      qvz-qz-vzUsage:z[-v] file_or_directory ...)getoptr	   argverrorr   filename_onlyverboser   )r   optsr   msgoar   r   r   r   main'   s(   
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   zk
    Raised by process_tokens() if detecting an ambiguous indent.
    Captured and handled in check().
    c                 C   s   |||| _ | _| _d S N)linenor   line)selfr    r   r!   r   r   r   __init__@   s   zNannyNag.__init__c                 C      | j S r   )r    r"   r   r   r   
get_linenoB      zNannyNag.get_linenoc                 C   r$   r   )r   r%   r   r   r   get_msgD   r'   zNannyNag.get_msgc                 C   r$   r   )r!   r%   r   r   r   get_lineF   r'   zNannyNag.get_lineN)__name__
__module____qualname____doc__r#   r&   r(   r)   r   r   r   r   r   ;   s    r   c           	   
   C   sN  t j| rDt j| sDtrtd| f  t | }|D ]%}t j| |}t j|r1t j|r=t j|dd dkrAt	| qdS zt
| }W n tyf } ztd| |f  W Y d}~dS d}~ww tdkrqtd|   zz
tt
|j W n t
jy } ztd| |f  W Y d}~W |  dS d}~w ty } ztd	| |f  W Y d}~W |  dS d}~w ty } zE| }| }trtd
| |f  td|f  t|  nd| v rd|  d } trt|  nt| |t| W Y d}~W |  dS d}~ww W |  n|  w tr%td| f  dS dS )a~  check(file_or_dir)

    If file_or_dir is a directory and not a symbolic link, then recursively
    descend the directory tree named by file_or_dir, checking all .py files
    along the way. If file_or_dir is an ordinary Python source file, it is
    checked for whitespace related problems. The diagnostic messages are
    written to standard output using the print statement.
    z%r: listing directoryNz.pyz%r: I/O Error: %sr   zchecking %r ...z%r: Token Error: %sz%r: Indentation Error: %sz)%r: *** Line %d: trouble in tab city! ***zoffending line: %rr   "z%r: Clean bill of health.)ospathisdirislinkr   printlistdirjoinnormcaser   tokenizeopenOSErrorr   r   generate_tokensreadline
TokenErrorcloseIndentationErrorr   r&   r)   r(   r   repr)	filenamesnamefullnamefr   nagbadliner!   r   r   r   r   I   sj   


r   c                   @   sL   e Zd Zd\ZZdd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )
Whitespacez 	c           	      C   s   || _ tjtj}}g }d } }}| j D ]9}||kr$|d }|d }q||krN|d }|d }|t|krC|dg|t| d   }|| d ||< d}q || _|| _t||f| _t|dk| _	d S )Nr   r   )
rawrH   STlennnttuplenorm	is_simple)	r"   wsrJ   rK   countbrM   rN   chr   r   r   r#      s(   

zWhitespace.__init__c                 C   s   | j \}}tt|d |S Nr   )rP   maxrL   )r"   rS   trailingr   r   r   longest_run_of_spaces   s   
z Whitespace.longest_run_of_spacesc                 C   sH   | j \}}d}t|t|D ]}||| ||   }q|||| j   S )Nr   )rP   rangerL   rN   )r"   tabsizerS   rX   ilir   r   r   indent_level   s
   
zWhitespace.indent_levelc                 C   s   | j |j kS r   )rP   )r"   otherr   r   r   equal   s   zWhitespace.equalc                 C   sb   t |  | d }g }td|d D ]}| |||kr.||| |||f q|S rV   rW   rY   rZ   r^   appendr"   r_   rM   r   tsr   r   r   not_equal_witness      zWhitespace.not_equal_witnessc                 C   sp   | j |j krdS | jr|jr| j|jkS t|  | d }td|d D ]}| |||kr5 dS q&dS )NFr      T)rM   rQ   rN   rW   rY   rZ   r^   )r"   r_   rM   rd   r   r   r   less   s   zWhitespace.lessc                 C   sb   t |  | d }g }td|d D ]}| |||kr.||| |||f q|S rV   ra   rc   r   r   r   not_less_witness  rf   zWhitespace.not_less_witnessN)r*   r+   r,   rJ   rK   r#   rY   r^   r`   re   rh   ri   r   r   r   r   rH      s    rH   c                 C   s8   dd | D }d}t | dkr|d }|d d| S )Nc                 s   s    | ]	}t |d  V  qdS )r   N)r   ).0tupr   r   r   	<genexpr>  s    z#format_witnesses.<locals>.<genexpr>zat tab sizer   sr   z, )rL   r6   )wfirstsprefixr   r   r   format_witnesses  s
   rq   c                 C   s  t j}t j}t j}t jt jf}tdg}d}| D ]l\}}}	}
}||kr&d}q||krRd}t|}|d |sL|d |}dt	| }t
|	d |||| q||kr\d}|d= q|r||vrd}t|}|d |s|d |}dt	| }t
|	d ||qd S )Nr   r   r   zindent not greater e.g. zindent not equal e.g. )r8   INDENTDEDENTNEWLINECOMMENTNLrH   rh   ri   rq   r   rb   r`   re   )tokensrs   rt   ru   JUNKindentscheck_equaltypetokenstartendr!   thisguywitnessr   r   r   r   r     s:   

r   __main__)r-   __version__r0   r	   r8   __all__r   r   r   r   	Exceptionr   r   rH   rq   r   r*   r   r   r   r   <module>   s&   = 7
