a
    4ñ»jr  ã                   @   s<   d dl Zd dlmZmZ G dd„ deƒZG dd„ deƒZdS )é    N)Úselect_best_encodingÚensure_stream_with_encoderc                   @   sJ   e Zd ZdZeƒ Zddd„Zedd„ ƒZe	ddd„ƒZ
d	d
„ Zdd„ ZdS )ÚStreamOpenerah  Provides a transport vehicle to open the formatter output stream
    when the formatter needs it.
    In addition, it provides the formatter with more control:

      * when a stream is opened
      * if a stream is opened at all
      * the name (filename/dirname) of the output stream
      * let it decide if directory mode is used instead of file mode
    Nc                 C   s8   |s
| j }|r|  ||¡}|| _|| _|| _| | _d S ©N)Údefault_encodingr   ÚnameÚstreamÚencodingÚshould_close_stream)ÚselfÚfilenamer   r	   © r   ú;E:\soccer\ml_env\Lib\site-packages\behave/formatter/base.pyÚ__init__   s    zStreamOpener.__init__c                 C   s   | rt j | ¡st  | ¡ d S r   )ÚosÚpathÚisdirÚmakedirs)Ú	directoryr   r   r   Úensure_dir_exists!   s    zStreamOpener.ensure_dir_existsc                 C   s
   t ||ƒS r   )Ú_ensure_stream_with_encoder)Úclsr   r	   r   r   r   r   &   s    z'StreamOpener.ensure_stream_with_encoderc                 C   sX   | j r| j jrR|  tj | j¡¡ t| jdƒ}|  || j	¡}|| _ d| _
| j
sRJ ‚| j S )NÚwT)r   Úclosedr   r   r   Údirnamer   Úopenr   r	   r
   )r   r   r   r   r   r   *   s    
zStreamOpener.openc                 C   s:   d}| j r6| jr6t| j ddƒ}|s0| j  ¡  d}d| _ |S )z´
        Close the stream, if it was opened by this stream_opener.
        Skip closing for sys.stdout and pre-opened streams.
        :return: True, if stream was closed.
        Fr   TN)r   r
   ÚgetattrÚclose)r   r   r   r   r   r   5   s    
zStreamOpener.close)NNN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ústaticmethodr   Úclassmethodr   r   r   r   r   r   r   r   
   s   



r   c                   @   s„   e Zd ZdZdZdZdd„ Ze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S )Ú	Formatteraé  
    Base class for all formatter classes.
    A formatter is an extension point (variation point) for the runner logic.
    A formatter is called while processing model elements.

    Processing Logic (simplified, without ScenarioOutline and skip logic)::

        for feature in runner.features:
            formatter = make_formatters(...)
            formatter.uri(feature.filename)
            formatter.feature(feature)
            for scenario in feature.scenarios:
                formatter.scenario(scenario)
                for step in scenario.all_steps:
                    formatter.step(step)
                    step_match = step_registry.find_match(step)
                    formatter.match(step_match)
                    if step_match:
                        step_match.run()
                    else:
                        step.status = Status.undefined
                    formatter.result(step.status)
            formatter.eof() # -- FEATURE-END
        formatter.close()
    Nc                 C   s   || _ |j| _|| _d S r   )Ústream_openerr   Úconfig)r   r%   r&   r   r   r   r   b   s    zFormatter.__init__c                 C   s
   | j j S r   )r%   r   ©r   r   r   r   Ústdout_modeg   s    zFormatter.stdout_modec                 C   s   | j s| j ¡ | _ | j S )zº
        Ensure that the output stream is open.
        Triggers the stream opener protocol (if necessary).

        :return: Output stream to use (just opened or already open).
        )r   r%   r   r'   r   r   r   r   k   s    zFormatter.openc                 C   s   dS )zvCalled before processing a file (normally a feature file).

        :param uri:  URI or filename (as string).
        Nr   )r   Úurir   r   r   r)   v   s    zFormatter.uric                 C   s   dS )zyCalled before a feature is executed.

        :param feature:  Feature object (as :class:`behave.model.Feature`)
        Nr   )r   Úfeaturer   r   r   r*   }   s    zFormatter.featurec                 C   s   dS )a  Called when a (Feature) Background is provided.
        Called after :method:`feature()` is called.
        Called before processing any scenarios or scenario outlines.

        :param background:  Background object (as :class:`behave.model.Background`)
        Nr   )r   Ú
backgroundr   r   r   r+   „   s    zFormatter.backgroundc                 C   s   dS )zœCalled before a scenario is executed (or ScenarioOutline scenarios).

        :param scenario:  Scenario object (as :class:`behave.model.Scenario`)
        Nr   )r   Úscenarior   r   r   r,   �   s    zFormatter.scenarioc                 C   s   dS )zÇCalled before a step is executed (and matched).
        NOTE: Normally called before scenario is executed for all its steps.

        :param step: Step object (as :class:`behave.model.Step`)
        Nr   ©r   Ústepr   r   r   r.   ”   s    zFormatter.stepc                 C   s   dS )z™Called when a step was matched against its step implementation.

        :param match:  Registered step (as Match), undefined step (as NoMatch).
        Nr   )r   Úmatchr   r   r   r/   œ   s    zFormatter.matchc                 C   s   dS )z—Called after processing a step (when the step result is known).

        :param step:  Step object with result (after being executed/skipped).
        Nr   r-   r   r   r   Úresult£   s    zFormatter.resultc                 C   s   dS )z6Called after processing a feature (or a feature file).Nr   r'   r   r   r   Úeofª   s    zFormatter.eofc                 C   s   |   ¡  dS )zYCalled before the formatter is no longer used
        (stream/io compatibility).
        N)Úclose_streamr'   r   r   r   r   ®   s    zFormatter.closec                 C   s,   | j r"| j | jj u sJ ‚| j ¡  d| _ dS )zpClose the stream, but only if this is needed.
        This step is skipped if the stream is sys.stdout.
        N)r   r%   r   r'   r   r   r   r2   ´   s    
zFormatter.close_stream)r   r   r    r!   r   Údescriptionr   Úpropertyr(   r   r)   r*   r+   r,   r.   r/   r0   r1   r   r2   r   r   r   r   r$   E   s"   
	r$   )	Úos.pathr   Zbehave.textutilr   r   r   Úobjectr   r$   r   r   r   r   Ú<module>   s   ;