Your IP : 216.73.216.213


Current Path : /opt/alt/python36/lib64/python3.6/Doc/tools/extensions/__pycache__/
Upload File :
Current File : //opt/alt/python36/lib64/python3.6/Doc/tools/extensions/__pycache__/suspicious.cpython-36.pyc

3

� f�%�@s�dZddlZddlZddlZddlZddlmZddlmZddl	Z
yddlmZWn e
k
rtddlmZYnXejdejejB�jZejdkZGdd	�d	�ZGd
d�dej�ZGdd
�d
e�Zdd�Zdd�ZGdd�dej�ZdS)a�
Try to detect suspicious constructs, resembling markup
that has leaked into the final output.

Suspicious lines are reported in a comma-separated-file,
``suspicious.csv``, located in the output directory.

The file is utf-8 encoded, and each line contains four fields:

 * document name (normalized)
 * line number in the source document
 * problematic text
 * complete line showing the problematic text in context

It is common to find many false positives. To avoid reporting them
again and again, they may be added to the ``ignored.csv`` file
(located in the configuration directory). The file has the same
format as ``suspicious.csv`` with a few differences:

  - each line defines a rule; if the rule matches, the issue
    is ignored.
  - line number may be empty (that is, nothing between the
    commas: ",,"). In this case, line numbers are ignored (the
    rule matches anywhere in the file).
  - the last field does not have to be a complete line; some
    surrounding text (never more than a line) is enough for
    context.

Rules are processed sequentially. A rule matches when:

 * document names are the same
 * problematic texts are the same
 * line numbers are close to each other (5 lines up or down)
 * the rule text is completely contained into the source line

The simplest way to create the ignored.csv file is by copying
undesired entries from suspicious.csv (possibly trimming the last
field.)

Copyright 2009 Gabriel A. Genellina

�N)�nodes)�Builder)�	getLoggerz�
    ::(?=[^=])|            # two :: (but NOT ::=)
    :[a-zA-Z][a-zA-Z0-9]+| # :foo
    `|                     # ` (seldom used by itself)
    (?<!\.)\.\.[ \t]*\w+:  # .. foo: (but NOT ... else:)
    �c@seZdZdd�Zdd�ZdS)�RulecCs"||_||_||_||_d|_dS)zA rule for ignoring issuesFN)�docname�lineno�issue�line�used)�selfrrr	r
�r
�D/opt/alt/python36/lib64/python3.6/Doc/tools/extensions/suspicious.py�__init__Fs
z
Rule.__init__cCs
dj|�S)Nz{0.docname},,{0.issue},{0.line})�format)rr
r
r�__repr__Psz
Rule.__repr__N)�__name__�
__module__�__qualname__rrr
r
r
rrEs
rc@seZdZdZdZdS)�dialectz+Our dialect: uses only linefeed as newline.�
N)rrr�__doc__Zlineterminatorr
r
r
rrUsrc@sveZdZdZdZed�Zdd�Zdd�Zddd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�CheckSuspiciousMarkupBuilderzK
    Checks for possibly invalid markup that may leak into the output.
    Z
suspiciouscCsDtjj|jd�|_t|jd�j�|jtjjtjjt	�dd��dS)Nzsuspicious.csv�wz..zsusp-ignored.csv)
�os�path�joinZoutdir�
log_file_name�open�close�
load_rules�dirname�__file__)rr
r
r�initasz!CheckSuspiciousMarkupBuilder.initcCs|jjS)N)�envZ
found_docs)rr
r
r�get_outdated_docsisz.CheckSuspiciousMarkupBuilder.get_outdated_docsNcCsdS)N�r
)rr�typr
r
r�get_target_urilsz+CheckSuspiciousMarkupBuilder.get_target_uricCsdS)Nr
)rZdocnamesr
r
r�prepare_writingosz,CheckSuspiciousMarkupBuilder.prepare_writingcCs$d|_||_t||�}|j|�dS)NF)�	any_issuer�SuspiciousVisitor�walk)rrZdoctreeZvisitorr
r
r�	write_docrs
z&CheckSuspiciousMarkupBuilder.write_doccCsRdd�|jD�}|rN|jdt|�t|j�f�x|D]}|jjt|��q6WdS)NcSsg|]}|js|�qSr
)r)�.0�ruler
r
r�
<listcomp>zsz7CheckSuspiciousMarkupBuilder.finish.<locals>.<listcomp>zFound %s/%s unused rules:)�rules�warn�len�logger�info�repr)rZunused_rulesr/r
r
r�finishys
z#CheckSuspiciousMarkupBuilder.finishcCs |j|||�s|j|||�dS)N)�
is_ignored�report_issue)rr
rr	r
r
r�check_issue�sz(CheckSuspiciousMarkupBuilder.check_issuecCsd|j}xX|jD]N}|j|krq|j|kr*q|j|kr6q|jdk	rTt|j|�dkrTqd|_dSWdS)z/Determine whether this issue should be ignored.N�TF)rr1r	r
r�absr)rr
rr	rr/r
r
rr8�s



z'CheckSuspiciousMarkupBuilder.is_ignoredcCs�|js|jj�d|_|j|||�trB|jd|j|||f�n@|jd|jjtj	�d�||jtj	�d�|j
�jtj	�d�f�d|j_dS)NTz[%s:%d] "%s" found in "%-.120s"�replace�)
r*r4r5�write_log_entry�py3r2r�encode�sys�getdefaultencoding�stripZappZ
statuscode)r�textrr	r
r
rr9�s
z)CheckSuspiciousMarkupBuilder.report_issuecCs�tr>t|jd�}tj|t�}|j|j|||j�g�|j	�nJt|jd�}tj|t�}|j|jj
d�||j
d�|j�j
d�g�|j	�dS)N�aZabzutf-8)r@rr�csv�writerrZwriterowrrDrrA)rrr	rE�frHr
r
rr?�s
z,CheckSuspiciousMarkupBuilder.write_log_entrycCs|jjddd�g|_}ytr,t|d�}n
t|d�}Wntk
rLdSXx�ttj|��D]�\}}t	|�dkr�t
d||d|f��|\}}}}	|r�t|�}nd}ts�|jd	�}|jd	�}|	jd	�}	t
||||	�}
|j|
�q^W|j�|jjd
t	|j��dS)z�Load database of previously ignored issues.

        A csv file, with exactly the same format as suspicious.csv
        Fields: document name (normalized), line number, issue, surrounding text
        zloading ignore rules... r>)Znonl�r�rbN�zwrong format in %s, line %d: %szutf-8zdone, %d rules loaded)r4r5r1r@r�IOError�	enumeraterG�readerr3�
ValueError�int�decoder�appendr)r�filenamer1rI�i�rowrrr	rEr/r
r
rr �s0




z'CheckSuspiciousMarkupBuilder.load_rules)N)rrrr�namerr4r#r%r(r)r-r7r:r8r9r?r r
r
r
rrZs
	rcCs&d}x|dkr |r |j}|j}qW|S)z*Obtain line number information for a node.N)�parentr
)�noderr
r
r�
get_lineno�s

rZcCs:|jdd|�d}|jd|�}|dkr.t|�}|||�S)a text may be a multiline string; extract
    only the line containing the given character index.

    >>> extract_line("abc
defgh
i", 6)
    >>> 'defgh'
    >>> for i in (0, 2, 3, 4, 10):
    ...   print extract_line("abc
defgh
i", i)
    abc
    abc
    abc
    defgh
    defgh
    i
    rrr>)�rfind�findr3)rE�index�p�qr
r
r�extract_line�s
r`c@s4eZdZdZdd�Zdd�ZeZdd�Zdd	�Zd
S)r+rcCstjj||�||_dS)N)r�GenericNodeVisitorr�builder)rZdocumentrbr
r
rr�szSuspiciousVisitor.__init__cCs�t|tjtjf�r�|j�}tt|�p&d|j�|_}t�}xPt	|�D]D}|j
�}t||j��}||f|krD|j
j|||�|j||f�qDWdS)Nr)�
isinstancerZTextZimageZastext�maxrZ�
lastlineno�set�
detect_all�groupr`�startrbr:�add)rrYrEr�seen�matchr	r
r
r
r�
default_visitszSuspiciousVisitor.default_visitcCs
d|_dS)Nr)re)rrYr
r
r�visit_documentsz SuspiciousVisitor.visit_documentcCs
tj�dS)N)rZSkipNode)rrYr
r
r�
visit_commentszSuspiciousVisitor.visit_commentN)	rrrrerrmZ
unknown_visitrnror
r
r
rr+�s
r+)rr)rr�rerGrBZdocutilsrZsphinx.buildersrZsphinx.utilZsphinxZsphinx.util.loggingr�ImportErrorZlogging�compile�UNICODE�VERBOSE�finditerrg�version_infor@rZexcelrrrZr`rar+r
r
r
r�<module>*s*