a
    4ñ»jˆ$  ã                   @   sz   d Z ddlmZ ddlZddlmZ ddlmZ zddlZW n e	yV   ddl
ZY n0 dZddd	„ZG d
d„ deƒZdS )zð
Read behave's JSON output files and store retrieved information in
:mod:`behave.model` elements.

Utility to retrieve runtime information from behave's JSON output.

REQUIRES: Python >= 2.6 (json module is part of Python standard library)
é    )Úabsolute_importN)Úmodel)ÚStatusz
Jens EngelúUTF-8c                 C   sV   t j| d|d��2}tj||d�}tƒ }| |¡}|W  d  ƒ S 1 sH0    Y  dS )zÀ
    Reads behave JSON output file back in and stores information in
    behave model elements.

    :param json_filename:  JSON filename to process.
    :return: List of feature objects.
    ZrU)ÚencodingN)ÚcodecsÚopenÚjsonÚloadÚ
JsonParserÚparse_features)Zjson_filenamer   Z
input_fileÚ	json_dataZjson_processorÚfeatures© r   ú8E:\soccer\ml_env\Lib\site-packages\behave/json_parser.pyÚparse   s
    
r   c                   @   st   e Z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edd„ ƒZedd„ ƒZdd„ ZdS )r   c                 C   s
   d | _ d S ©N)Úcurrent_scenario_outline)Úselfr   r   r   Ú__init__0   s    zJsonParser.__init__c                 C   s8   t |tƒsJ ‚g }|}|D ]}|  |¡}| |¡ q|S r   )Ú
isinstanceÚlistÚparse_featureÚappend)r   r   r   Zjson_featuresÚjson_featureÚfeaturer   r   r   r   3   s    
zJsonParser.parse_featuresc                 C   s„   |  dd¡}|  dd ¡}|  dg ¡}|  dg ¡}|  dd¡}| d¡\}}t ||||||¡}	|  dg ¡}
|
D ]}|  |	|¡ qn|	S )	NÚnameÚ ÚkeywordÚtagsÚdescriptionÚlocationú:Úelements)ÚgetÚsplitr   ZFeatureÚadd_feature_element)r   r   r   r   r   r    r!   ÚfilenameÚliner   Zjson_elementsÚjson_elementr   r   r   r   <   s    zJsonParser.parse_featurec                 C   s€   |  dd¡}| ¡ }|dkr.|  |¡}||_nN|dkrL|  |¡}| |¡ n0|dkrp|  |¡}| |¡ || _ntd| ƒ‚d S )NÚtyper   Ú
backgroundÚscenarioÚscenario_outlinez#Invalid feature-element keyword: %s)	r$   ÚlowerÚparse_backgroundr+   Úparse_scenarioZadd_scenarioÚparse_scenario_outliner   ÚKeyError)r   r   r)   ÚdatatypeÚcategoryr+   r,   r-   r   r   r   r&   K   s    



zJsonParser.add_feature_elementc           
      C   s^   |  dd¡}|  dd¡}|  dd¡}|  dg ¡}|  |¡}| d¡\}}t |||||¡}	|	S )z¨
        self.add_feature_element({
            'keyword': background.keyword,
            'location': background.location,
            'steps': [],
        })
        r   r   r   r!   Ústepsr"   )r$   Úparse_stepsr%   r   Z
Background)
r   r)   r   r   r!   Ú
json_stepsr5   r'   r(   r+   r   r   r   r/   _   s    
zJsonParser.parse_backgroundc                 C   s~   |  dd¡}|  dd¡}|  dg ¡}|  dg ¡}|  dd¡}|  dg ¡}|  |¡}| d¡\}	}
t |	|
||||¡}||_|S )	zê
        self.add_feature_element({
            'keyword': scenario.keyword,
            'name': scenario.name,
            'tags': scenario.tags,
            'location': scenario.location,
            'steps': [],
        })
        r   r   r   r    r   r!   r5   r"   )r$   r6   r%   r   ZScenarior    )r   r)   r   r   r    r   r!   r7   r5   r'   r(   r,   r   r   r   r0   p   s    

zJsonParser.parse_scenarioc              	   C   s    |  dd¡}|  dd¡}|  dg ¡}|  dg ¡}|  dd¡}|  dg ¡}|  dg ¡}|  |¡}	g }
|rp|  |¡}
| d	¡\}}tj||||||	|
d
�}||_|S )a&  
        self.add_feature_element({
            'keyword': scenario_outline.keyword,
            'name': scenario_outline.name,
            'tags': scenario_outline.tags,
            'location': scenario_outline.location,
            'steps': [],
            'examples': [],
        })
        r   r   r   r    r   r!   r5   Úexamplesr"   )r   r5   r8   )r$   r6   Úparse_examplesr%   r   ZScenarioOutliner    )r   r)   r   r   r    r   r!   r7   Zjson_examplesr5   r8   r'   r(   r-   r   r   r   r1   †   s$    

þz!JsonParser.parse_scenario_outlinec                 C   s&   g }|D ]}|   |¡}| |¡ q|S r   )Ú
parse_stepr   )r   r7   r5   Z	json_stepÚstepr   r   r   r6   ¤   s
    
zJsonParser.parse_stepsc                 C   sº   |  dd¡}|  dd¡}|  dd¡}|  dd¡}|  dd¡}t|tƒrPd |¡}d}|  d	d¡}|rn|  |¡}| d
¡\}	}
t |	|
|||¡}||_||_	|  dd¡}|r¶|  
||¡ |S )aŒ  
        s = {
            'keyword': step.keyword,
            'step_type': step.step_type,
            'name': step.name,
            'location': step.location,
        }

        if step.text:
            s['text'] = step.text
        if step.table:
            s['table'] = self.make_table(step.table)
        element = self.current_feature_element
        element['steps'].append(s)
        r   r   r   Ú	step_typer!   ÚtextNÚ
Útabler"   Úresult)r$   r   r   ÚjoinÚparse_tabler%   r   ZStepr=   r?   Úadd_step_result)r   r)   r   r   r<   r!   r=   r?   Ú
json_tabler'   r(   r;   Újson_resultr   r   r   r:   «   s&    


zJsonParser.parse_stepc                 C   sT   |  dd¡}|  dd¡}|  dd¡}t|tƒr8d |¡}t |¡| _|| _|| _dS )zÊ
        steps = self.current_feature_element['steps']
        steps[self._step_index]['result'] = {
            'status': result.status.name,
            'duration': result.duration,
        }
        Ústatusr   Údurationr   Úerror_messageNr>   )	r$   r   r   rA   r   Ú	from_namerF   rG   rH   )r;   rE   Zstatus_namerG   rH   r   r   r   rC   Ï   s    	

zJsonParser.add_step_resultc                 C   s*   |   dg ¡}|   dg ¡}tj||d�}|S )z¤
        table_data = {
            'headings': table.headings,
            'rows': [ list(row) for row in table.rows ]
        }
        return table_data
        ÚheadingsÚrows)rK   )r$   r   ÚTable)rD   rJ   rK   r?   r   r   r   rB   á   s    	zJsonParser.parse_tablec           
      C   sf   |  dd¡}|  dd¡}|  dd¡}d}|  dd¡}|rB|  |¡}| d¡\}}t |||||¡}	|	S )aC  
        e = {
            'keyword': examples.keyword,
            'name': examples.name,
            'location': examples.location,
        }

        if examples.table:
            e['table'] = self.make_table(examples.table)

        element = self.current_feature_element
        element['examples'].append(e)
        r   r   r   r!   Nr?   r"   )r$   rB   r%   r   ZExamples)
r   r)   r   r   r!   r?   rD   r'   r(   r8   r   r   r   r9   ð   s    
zJsonParser.parse_examplesN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r&   r/   r0   r1   r6   r:   ÚstaticmethodrC   rB   r9   r   r   r   r   r   .   s   	$

r   )r   )Ú__doc__Ú
__future__r   r   Zbehaver   Zbehave.model_corer   r	   ÚImportErrorZ
simplejsonÚ
__author__r   Úobjectr   r   r   r   r   Ú<module>   s   	
