o
    €Ï²@  ã                   @   sv   d Z ddlmZ ddlmZ ddlm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ZG dd	„ d	ejƒZdS )
z,Cloud SDK markdown document linter renderer.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úconsole_attr)Útext_rendererc                       sê   e Zd ZdZdZdZdZdZdZdgZ	‡ fdd	„Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Z ‡  Z!S )6ÚLinterRendererzBRenders markdown to a list of lines where there is a linter error.©ÚNAMEÚEXAMPLESÚDESCRIPTIONzPOSITIONAL ARGUMENTSzREQUIRED FLAGSzOPTIONAL FLAGSÚFLAGSzLIST COMMAND FLAGSé   )ÚmeÚweÚIÚusÚheÚsheÚhimÚher)ÚtheÚaÚan)z--quietz--helpzgcloud topicc              	      sÒ   t t| ƒj|i |¤Ž tjdd| _| j ¡ | _d | _| j	| _
t ¡ | _t ¡ | _| j| _	| j| j| j| j| j| j| j| jdœ| _d| _d| _d| _d| _d| _d| _d| _d| _d| _g | _g | _t  ¡ | _!d S )NÚascii)Úencodingr   Ú Fr   )"Úsuperr   Ú__init__r   ÚConsoleAttrÚ_attrÚ
GetBulletsÚ_bulletÚ	_csi_charÚ_outÚ	_file_outÚioÚStringIOÚ	_null_outÚ_bufferÚ_analyze_nameÚ_analyze_examplesÚ_analyze_descriptionÚ_analyze_argument_sectionsÚ_analyzeÚ_headingÚ_prev_headingÚ_example_errorsÚ_has_example_sectionÚexampleÚcommand_nameÚname_sectionÚcommand_name_lengthÚcommand_textÚequals_violation_flagsÚnonexistent_violation_flagsÚcollectionsÚOrderedDictÚfindings)ÚselfÚargsÚkwargs©Ú	__class__© úS/tmp/google-cloud-sdk/lib/googlecloudsdk/core/document_renderers/linter_renderer.pyr   ,   s:   

ùzLinterRenderer.__init__c                 C   s\   |   ¡  | j ¡ r| jr|  | j| j ¡ ¡ t ¡ | _| jdkr$|  ¡  | j| _| j	| _d S )Nr
   )
Úcheck_indentation_for_examplesr(   Úgetvaluer/   Ú_Analyzer%   r&   Úcheck_example_section_errorsr#   r.   ©r<   ÚheadingrA   rA   rB   Ú_CaptureOutputL   s   

zLinterRenderer._CaptureOutputc                 C   s   | j | _d S ©N)r'   r#   rG   rA   rA   rB   Ú_DiscardOutputY   ó   zLinterRenderer._DiscardOutputc                 C   s   | j | ||ƒ d S rJ   )r-   )r<   rH   ÚsectionrA   rA   rB   rE   \   ó   zLinterRenderer._Analyzec                 C   s   d  ||¡S )Nz{}_{}_CHECK)Úformat)r<   rH   ÚcheckrA   rA   rB   Ú_check_name_   rL   zLinterRenderer._check_namec                 C   s   || j d |¡< d S )Nz# {} FAILED©r;   rO   )r<   Ú
check_nameÚmessagerA   rA   rB   Ú_add_failureb   rN   zLinterRenderer._add_failurec                 C   ó   d| j d |¡< d S )Nr   z# {} SUCCESSrR   )r<   rS   rA   rA   rB   Ú_add_successe   rN   zLinterRenderer._add_successc                 C   rV   )Nr   z'There are no errors for the {} section.rR   rG   rA   rA   rB   Ú_add_no_errors_summaryh   s   ÿÿz%LinterRenderer._add_no_errors_summaryc                 C   sD   |   ¡ r| js|  |  dd¡d¡ n| jr| js|  d¡ d| _dS )zØRaise violation if the examples section does not contain a valid example.

    Also, wrap up the examples section by specifying there are no errors in the
    section.

    See go/cloud-sdk-help-text#formatting.
    r
   ÚPRESENTz9You have not included an example in the Examples section.TN)Úneeds_exampler2   rU   rQ   r1   r0   rX   ©r<   rA   rA   rB   rF   l   s   
þ

z+LinterRenderer.check_example_section_errorsc                 C   sR   |   |d¡}| ¡ d }| ¡ | jv r |  |d |¡¡ d}|S |  |¡ d}|S )a  Raise violation if the section begins with an article.

    See go/cloud-sdk-help-text#formatting.

    Arguments:
      heading: str, the name of the section.
      section: str, the contents of the section.

    Returns:
      True if there was a violation. False otherwise.
    ÚARTICLESr   z3Please do not start the {} section with an article.TF)rQ   ÚsplitÚlowerÚ	_ARTICLESrU   rO   rW   )r<   rH   rM   rS   Ú
first_wordÚfound_articlerA   rA   rB   Úcheck_for_articles}   s   ÿ
þz!LinterRenderer.check_for_articlesc              	   C   sl   |   |d¡}tt d¡ | ¡ ¡ƒ}| | j¡}|r/tt	|ƒƒ}|  
|d |d |¡¡¡ |S |  |¡ |S )z:Raise violation if the section contains personal pronouns.ÚPRONOUNz	[\w/\-_]+zCPlease remove the following personal pronouns in the {} section:
{}Ú
)rQ   ÚsetÚreÚcompileÚfindallr^   ÚintersectionÚ_PERSONAL_PRONOUNSÚsortedÚlistrU   rO   ÚjoinrW   )r<   rH   rM   rS   Úwords_in_sectionÚfound_pronounsÚfound_pronouns_listrA   rA   rB   Úcheck_for_personal_pronouns”   s   þÿ
ÿz*LinterRenderer.check_for_personal_pronounsc                 C   sd   |   |d¡}tt d¡ |¡ƒ}tt d¡ |¡ƒ}||k}|r+|  |d |¡¡ |S |  |¡ |S )a#  Raise violation if the section contains unmatched double backticks.

    This check counts the number of double backticks in the section and ensures
    that there are an equal number of closing double single-quotes. The common
    mistake is to use a single double-quote to close these values, which breaks
    the rendering. See go/cloud-sdk-help-text#formatting.

    Arguments:
      heading: str, the name of the section.
      section: str, the contents of the section.

    Returns:
      True if there was a violation. None otherwise.
    ÚDOUBLE_BACKTICKSz``z''zxThere are unbalanced double backticks and double single-quotes in the {} section. See go/cloud-sdk-help-text#formatting.)rQ   Úlenrf   rg   rh   rU   rO   rW   )r<   rH   rM   rS   Údouble_backticks_countÚdouble_single_quotes_countÚ
unbalancedrA   rA   rB   Ú$check_for_unmatched_double_backticks£   s   ý
ÿz3LinterRenderer.check_for_unmatched_double_backticksc                 C   sB   | j r	| j jr	dS d| jv rdS | jD ]}| j |¡r dS qdS )z*Check whether command requires an example.FÚalphaT)Úcommand_metadataÚis_groupr3   Ú_NON_COMMAND_SURFACE_GROUPSÚ
startswith)r<   ÚnamerA   rA   rB   rZ   ¿   s   

ÿzLinterRenderer.needs_examplec                 C   s:   | j dkr| j ¡ s|  |  dd¡d¡ d| _d S d S d S )Nr
   ÚSECTION_FORMATzThe examples section is not formatted properly. This is likely due to indentation. Please make sure the section is aligned with the heading and not indented.T)r/   r(   rD   rU   rQ   r0   r[   rA   rA   rB   rC   Ë   s   
þ
úz-LinterRenderer.check_indentation_for_examplesc                 C   s¤   | j  ¡ r| jr|  | j| j  ¡ ¡ |  ¡  | j  ¡  | j ¡  |  ¡  | jD ](}| j| rD| j	 
t |¡d t | j| ¡ d ¡ q'| j	 
t |¡d ¡ q'd S )Nz: rd   )r(   rD   r/   rE   rC   Úcloser'   rF   r;   r$   ÚwriteÚsixÚ	text_type)r<   ÚelementrA   rA   rB   ÚFinishÔ   s"   



ÿÿÿúzLinterRenderer.Finishc                 C   s,   || _ || jv r|  |¡ d S |  |¡ d S rJ   )r.   Ú_HEADINGS_TO_LINTrI   rK   )r<   ÚlevelrH   rA   rA   rB   ÚHeadingã   s   
zLinterRenderer.Headingc                 C   s  | j r| j jsƒ| jdkr…| jr!| j d¡r!| j d¡| ¡  | _n| dd¡| _| d¡s‡| j | j	¡r‰d| _
|  |  dd¡¡ | j| jd …  ¡ }g }|D ]}| dd¡}|dkr\ n| d¡rf| |¡ qN|  |¡ d	d
„ |D ƒ}| j r‹| j jr|  |¡ d S d S d S d S d S d S d S d S )Nr
   ú\z$ r   TrY   z\--ú--c                 S   s   g | ]	}|  d ¡d ‘qS )ú=r   )Ú	partition)Ú.0ÚflagrA   rA   rB   Ú
<listcomp>  s    z*LinterRenderer.Example.<locals>.<listcomp>)ry   rz   r.   r6   ÚendswithÚrstripÚstripÚreplacer|   r3   r2   rW   rQ   r5   r]   ÚappendÚ_analyze_example_flags_equalsÚflagsÚ_check_valid_flags)r<   ÚlineÚrest_of_commandÚ
flag_namesÚwordr•   rA   rA   rB   ÚExampleê   s6   



€
ä
zLinterRenderer.Examplec                 C   s&   |D ]}|| j jvr| j |¡ qd S rJ   )ry   r•   r8   r“   ©r<   r•   r   rA   rA   rB   r–     s
   €þz!LinterRenderer._check_valid_flagsc                 C   s8   |D ]}d|vr|| j jvr|| jvr| j |¡ qd S )NrŠ   )ry   Ú
bool_flagsÚ_NON_BOOL_FLAGS_ALLOWLISTr7   r“   rœ   rA   rA   rB   r”     s   
€ýz,LinterRenderer._analyze_example_flags_equalsc           
   	   C   sè   |   ||¡p|  ||¡}|  |d¡}g }| d¡}dd„ |D ƒ}d}|D ]-}	|rO| d¡s7t d| ¡ d ¡rOt| ¡ ƒd	k rO|	 ¡ d  ¡ | j	v rO| 
|¡ |	}q$|rdd
}|  |d |d |¡¡¡ n|  |¡ |sr|  |¡ dS dS )aš  Raise violation if the section contains unmatched double backticks.

    This check confirms that argument sections follow our help text style guide.
    The help text for individual arguments should not begin with an article.
    See go/cloud-sdk-help-text#formatting.

    Arguments:
      heading: str, the name of the section.
      section: str, the contents of the section.

    Returns:
      None.
    ÚARG_ARTICLESrd   c                 S   s    g | ]}|  ¡ s|r| ¡ ‘qS rA   )Úisspacer‘   )rŒ   r—   rA   rA   rB   rŽ   )  s    þþÿz=LinterRenderer._analyze_argument_sections.<locals>.<listcomp>r   r‰   z[A-Z_]r   é   TzfPlease fix the help text for the following arguments which begin with an article in the {} section:
{}N)rq   rb   rQ   r]   r|   rf   Úmatchrs   r^   r_   r“   rU   rO   rm   rW   rX   )
r<   rH   rM   Ú
has_errorsrS   Úflags_with_articlesÚall_lines_in_sectionÚnon_empty_lines_in_sectionÚ	prev_liner—   rA   rA   rB   r,     s<   
ÿ
ÿÿ
þþ
ÿz)LinterRenderer._analyze_argument_sectionsc                 C   s  |   ||¡p|  ||¡}t d| ¡ ¡}|  dd¡}t|ƒdks,t|ƒdkr8|d  ¡ s8d| _|  |d¡ d}n
|d | _|  	|¡ |  dd¡}d	 
|d
  ¡  ¡ ¡| _t| jƒ| _t| j ¡ ƒ| jkrt|  |d t | j¡¡¡ d}n|  	|¡ |s‚|  |¡ d S d S )Nz\s-\s?r	   r   é   r   z*Please add an explanation for the command.TÚLENGTHú r   zBPlease shorten the name section description to less than {} words.)rq   rb   rf   r]   r‘   rQ   rs   r4   rU   rW   rm   r3   r5   Ú_NAME_WORD_LIMITrO   r   r‚   rX   )r<   rH   rM   r£   Úsection_partsrS   rA   rA   rB   r)   @  s8   
ÿÿ

ý
ÿzLinterRenderer._analyze_namec                 C   sD  d| _ |  ||¡}| jjs |  ||¡rd}|  ||¡rd}|  |d¡}| jrXd}d}tt	| jƒd ƒD ]}|t
 | j| ¡d 7 }q3|t
 | jd ¡7 }|  |d |¡¡ d}n|  |¡ |  |d¡}| jr–d}d}tt	| jƒd ƒD ]}|t
 | j| ¡d 7 }qs|t
 | jd ¡7 }|  |d	 |¡¡ n|  |¡ || _d S d S )
NTÚFLAG_EQUALSr   r¨   z, éÿÿÿÿzVThere should be an `=` between the flag name and the value for the following flags: {}ÚNONEXISTENT_FLAGz%The following flags are not valid: {})r1   rb   ry   rz   rq   rw   rQ   r7   Úrangers   r   r‚   rU   rO   rW   r8   r0   )r<   rH   rM   r£   rS   Úlist_contentsr   rA   rA   rB   r*   f  sZ   ÿÿý
ÿÿÿþ

Þz LinterRenderer._analyze_examplesc                 C   s<   |   ||¡|  ||¡|  ||¡f}t|ƒs|  |¡ d S d S rJ   )rq   rw   rb   ÚanyrX   )r<   rH   rM   r£   rA   rA   rB   r+     s   
ÿ
ýÿz#LinterRenderer._analyze_description)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r…   r«   rj   r_   rž   r{   r   rI   rK   rE   rQ   rU   rW   rX   rF   rb   rq   rw   rZ   rC   r„   r‡   r›   r–   r”   r,   r)   r*   r+   Ú__classcell__rA   rA   r?   rB   r      s>     	!*&'r   )r¶   Ú
__future__r   r   r   r9   r%   rf   Úgooglecloudsdk.core.consoler   Ú&googlecloudsdk.core.document_renderersr   r   ÚTextRendererr   rA   rA   rA   rB   Ú<module>   s   