a
    4ñ»jŠ5  ã                   @   s¼   d Z ddlmZ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mZ zddlmZ d	ZW n ey†   d
ZY n0 G dd„ deƒZG dd„ deƒZG dd„ deƒZdS )ao  
Provides a formatter that generates Sphinx-based documentation
of available step definitions (step implementations).

TODO:
  * Post-processor for step docstrings.
  * Solution for requires: table, text
  * i18n keywords

.. seealso::
    http://sphinx-doc.org/

.. note:: REQUIRES docutils
    :mod:`docutils` are needed to generate step-label for step references.
é    )Úabsolute_importÚprint_function)Ú
attrgetterN)ÚAbstractStepsFormatter)Úsphinx_util)ÚTable)Úfully_normalize_nameTFc                   @   sR   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	dd„ Z
dS )ÚStepsModulezT
    Value object to keep track of step definitions that belong to same module.
    Nc                 C   s    || _ |pg | _d | _d | _d S ©N)Úmodule_nameÚstep_definitionsÚ_nameÚ	_filename)Úselfr   r   © r   úCE:\soccer\ml_env\Lib\site-packages\behave/formatter/sphinx_steps.pyÚ__init__*   s    
zStepsModule.__init__c                 C   s0   | j d u r*| j}|r| jj}n| j}|| _ | j S r
   )r   ÚmoduleÚ__name__r   )r   r   r   r   r   r   Úname1   s    

zStepsModule.namec                 C   s2   | j s,| jr,t | jd j¡}tj |¡| _ | j S ©Nr   )r   r   ÚinspectÚgetfileÚfuncÚosÚpathÚrelpath)r   Úfilenamer   r   r   r   @   s
    zStepsModule.filenamec                 C   s&   | j rt | j d j¡S tj | j¡S r   )r   r   Ú	getmoduler   ÚsysÚmodulesÚgetr   ©r   r   r   r   r   H   s    zStepsModule.modulec                 C   s   | j }|rt |¡S d S r
   )r   r   Úgetdoc)r   r   r   r   r   Ú
module_docN   s    
zStepsModule.module_docc                 C   s   | j  |¡ d S r
   )r   Úappend)r   Ústep_definitionr   r   r   r%   U   s    zStepsModule.append)N)r   Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r$   r%   r   r   r   r   r	   %   s   




r	   c                   @   s²   e Zd ZdZdZdZdZdZeZ	dZ
dZdZd%dd	„Zed
d„ ƒZed&dd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd'dd„Zdd„ Zdd„ Zedd „ ƒZd!d"„ Zd#d$„ ZdS )(ÚSphinxStepsDocumentGeneratora×  
    Provides document generator class that generates Sphinx-based
    documentation for step definitions. The primary purpose is to:

      * help the step-library provider/writer
      * simplify self-documentation of step-libraries

    EXAMPLE:
        step_definitions = ...  # Collect from step_registry
        doc_generator = SphinxStepsDocumentGenerator(step_definitions, "output")
        doc_generator.write_docs()

    .. seealso:: http://sphinx-doc.org/
    z6.. todo::
    Step definition description is missing.
TzN# -- DOCUMENT-END ------------------------------------------------------------zstep_module.z
**Step:** Nc                 C   s   || _ || _|| _d | _d S r
   )r   ÚdestdirÚstreamÚdocument)r   r   r,   r-   r   r   r   r   x   s    z%SphinxStepsDocumentGenerator.__init__c                 C   s
   | j duS )zF
        Indicates that output towards stdout should be used.
        N©r-   r"   r   r   r   Ústdout_mode~   s    z(SphinxStepsDocumentGenerator.stdout_modec                 C   sJ   |s| j pd}|dkrd}n| ¡ }| j}d|v r>| dd¡}d||f S )NÚstepúGiven/When/Thenú`z\`z%s %s)Ú	step_typeÚ
capitalizeÚpatternÚreplace)r&   r4   Zstep_type_textÚ	step_textr   r   r   Údescribe_step_definition…   s    
z5SphinxStepsDocumentGenerator.describe_step_definitionc                 C   sP   | j s
J ‚tj | j ¡r2td| j  ƒ t | j ¡ tj | j ¡sLt | j ¡ d S )NzOOPS: remove %s)r,   r   r   ÚisfileÚprintÚremoveÚexistsÚmakedirsr"   r   r   r   Úensure_destdir_exists”   s    
z2SphinxStepsDocumentGenerator.ensure_destdir_existsc                 C   s    | j r| js| j  ¡  d | _ d S r
   )r.   r0   Úcloser"   r   r   r   Úensure_document_is_closedœ   s    
z6SphinxStepsDocumentGenerator.ensure_document_is_closedc           
      C   s²   i }| j D ]l}|jd usJ ‚|jj}| |d ¡}|slt |j¡}t |¡}|s\J d|j ƒ‚t	|ƒ}|||< | 
|¡ q
t| ¡ tdƒd�}|D ]}t|j tdƒd�}	|	|_ q�|S )Nzstep_definition: %sr   )ÚkeyÚlocation)r   r4   rC   r   r!   r   r   r   Úgetmodulenamer	   r%   ÚsortedÚvaluesr   )
r   Zstep_modules_mapr&   Zstep_filenameÚstep_moduler   r   Ústep_modulesr   r   r   r   r   Údiscover_step_modules¡   s*    

ÿÿz2SphinxStepsDocumentGenerator.discover_step_modulesc                 C   s`   |  d¡s|  d¡s|d7 }| jr8| j}tj|dd�}n$|  ¡  tj | j	|¡}tj 
|¡}|S )Nz.rstz.txtF)Zshould_close)Úendswithr0   r-   r   ZDocumentWriterr?   r   r   Újoinr,   Úopen)r   r   r-   r.   r   r   r   Úcreate_document·   s    z,SphinxStepsDocumentGenerator.create_documentc                 C   s.   |   ¡ }|  |¡ |D ]}|  |¡ qt|ƒS r
   )rI   Úwrite_step_module_indexÚwrite_step_moduleÚlen)r   rH   rG   r   r   r   Ú
write_docsÃ   s
    
z'SphinxStepsDocumentGenerator.write_docsú	index.rstc                    sp   ˆ   |¡}| d¡ | d¡ | d¡ t‡ fdd„|D ƒƒ}|j|dd� | ¡  ˆ jrltj dˆ j	 ¡ d S )	Nz.. _docid.steps:

úStep Definitionsz9The following step definitions are provided here.

----

c                    s   g | ]}ˆ j |j ‘qS r   )Ústep_document_prefixr   )Ú.0r   r"   r   r   Ú
<listcomp>Ô   s   ÿzHSphinxStepsDocumentGenerator.write_step_module_index.<locals>.<listcomp>é   )Zmaxdepthú
%s
)
rM   ÚwriteÚwrite_headingrE   Zwrite_toctreer@   r0   r   ÚstdoutÚdocument_separator)r   rH   r   r.   Úentriesr   r"   r   rN   Ê   s    



ÿz4SphinxStepsDocumentGenerator.write_step_module_indexc                 C   s  |   ¡  | j|j }|  |¡| _| j d|j ¡ | jj|j|jd� | jr|| j d|j ¡ | j d|j ¡ | j d¡ |j	rœ|j	 
¡ }| j d| ¡ | jr¾| jjddd	� |  |j¡ | jjd
dd	� |jD ]}|  |¡ qÔ| j ¡  d | _| j�rtj d| j ¡ d S )Nz.. _docid.steps.%s:
)Úindex_idz:Module:   %s
z:Filename: %s
Ú
z%s

zStep OverviewrW   )ÚlevelrS   rX   )rA   rT   r   rM   r.   rY   rZ   Úshows_step_module_infor   r$   ÚstripÚshows_step_module_overviewÚwrite_step_module_overviewr   Úwrite_step_definitionr@   r0   r   r[   r\   )r   rG   Zdocument_namer$   r&   r   r   r   rO   Û   s,    


z.SphinxStepsDocumentGenerator.write_step_modulec                 C   sx   | j s
J ‚g d¢}t|ƒ}g d¢g d¢g d¢g d¢dœ}|D ]*}|  |¡g}| ||j ¡ | |¡ q<| j  |¡ d S )N)zStep DefinitionÚGivenÚWhenÚThenÚStep)ú  xú  rk   rk   )rk   rj   rk   rk   )rk   rk   rj   rk   )rj   rj   rj   rj   )ÚgivenÚwhenZthenr1   )r.   r   r9   Úextendr4   Úadd_rowZwrite_table)r   r   ZheadingsÚtableZstep_type_colsr&   Úrowr   r   r   rd   ö   s    
ûz7SphinxStepsDocumentGenerator.write_step_module_overviewc           	      C   sx   | j dkrd}n| j  ¡ }|f}d}g }|D ]8}|}d}|dkrHd}d}||||| jf }| |¡ q,d}|| |¡ S )	Nr1   )rf   rg   rh   ri   zsingle: %s%s; %s %sz stepri   r2   Ú z
    )r4   r5   r6   r%   rK   )	r1   Zindex_kindsÚkeywordZschemaZindex_partsZ
index_kindÚwordÚpartÚjoinerr   r   r   Úmake_step_definition_index_id  s     

z:SphinxStepsDocumentGenerator.make_step_definition_index_idc                 C   s"   t  |j¡}|s| j}| ¡ }|S r
   )r   r#   r   Údefault_step_definition_docrb   )r   r1   Údocr   r   r   Úmake_step_definition_doc  s
    z5SphinxStepsDocumentGenerator.make_step_definition_docc                 C   s¦   | j s
J ‚|  |¡}| d¡r*|dd … }d }| jr>|  |¡}|}d }| jrV| j| }trh| jrht|ƒ}| j j	|d||d� |  
|¡}| j  d| ¡ | j  d¡ d S )Nz* é   )r`   r^   Úlabelz%s
r_   )r.   r9   Ú
startswithÚmake_step_index_entriesrw   Ústep_heading_prefixÚhas_docutilsÚmake_step_labelsr   rZ   rz   rY   )r   r1   r8   r^   ÚheadingZ
step_labelZstep_definition_docr   r   r   re   #  s&    





ÿ
z2SphinxStepsDocumentGenerator.write_step_definition)NN)N)rR   )r   r'   r(   r)   rx   ra   rc   r~   r€   r�   r\   rT   r   r   r*   r0   Ústaticmethodr9   r?   rA   rI   rM   rQ   rN   rO   rd   rw   rz   re   r   r   r   r   r+   \   s4   



r+   c                       sP   e Zd ZdZdZdZeZ‡ fdd„Ze	dd„ ƒZ
dd	„ Zd
d„ Zdd„ Z‡  ZS )ÚSphinxStepsFormatteral  
    Provides formatter class that generates Sphinx-based documentation
    for all registered step definitions. The primary purpose is to:

      * help the step-library provider/writer
      * simplify self-documentation of step-libraries

    .. note::
        Supports dry-run mode.
        Supports destination directory mode to write multiple documents.
    zsphinx.stepsz9Generate sphinx-based documentation for step definitions.c                    s   t t| ƒ ||¡ |j| _d S r
   )Úsuperr„   r   r   r,   )r   Zstream_openerÚconfig©Ú	__class__r   r   r   P  s    zSphinxStepsFormatter.__init__c                 C   s8   g }| j j ¡ D ]"\}}|D ]}||_| |¡ qq|S )z+Derive step definitions from step-registry.)Ústep_registryÚstepsÚitemsr4   r%   )r   rŠ   r4   r   r1   r   r   r   r   T  s    z%SphinxStepsFormatter.step_definitionsc                 C   s   | j s|  ¡  |  ¡  dS )zCalled at end of test run.N)r‰   Zdiscover_step_definitionsÚreportr"   r   r   r   r@   _  s    zSphinxStepsFormatter.closec                 C   s,   | j }| jr|| j| jd�S || j| jd�S )Nr/   )r,   )Údoc_generator_classr0   r   r-   r,   )r   Zgenerator_classr   r   r   Úcreate_document_generatorf  s    z.SphinxStepsFormatter.create_document_generatorc                 C   s8   |   ¡ }| ¡ }| js4d}tj || j|| jf ¡ d S )Nz0%s: Written %s document(s) into directory '%s'.
)rŽ   rQ   r0   r   r[   rY   r   r,   )r   Zdocument_generatorZdocument_countsÚmsgr   r   r   rŒ   m  s
    zSphinxStepsFormatter.report)r   r'   r(   r)   r   Údescriptionr+   r�   r   r*   r   r@   rŽ   rŒ   Ú__classcell__r   r   r‡   r   r„   @  s   

r„   )r)   Ú
__future__r   r   Úoperatorr   r   Úos.pathr   r   Zbehave.formatter.stepsr   Zbehave.formatterr   Zbehave.modelr   Zdocutils.nodesr   r€   ÚImportErrorÚobjectr	   r+   r„   r   r   r   r   Ú<module>   s"   
7 e