<s> from __future__ import absolute_import <EOL> from behave . formatter . base import Formatter <EOL> import base64 <EOL> import six <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> class JSONFormatter ( Formatter ) : <EOL> name = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> dumps_kwargs = { } <EOL> split_text_into_lines = True <EOL> json_number_types = six . integer_types + ( float , ) <EOL> json_scalar_types = json_number_types + ( six . text_type , bool , type ( None ) ) <EOL> def __init__ ( self , stream_opener , config ) : <EOL> super ( JSONFormatter , self ) . __init__ ( stream_opener , config ) <EOL> self . stream = self . open ( ) <EOL> self . feature_count = <NUM_LIT:0> <EOL> self . current_feature = None <EOL> self . current_feature_data = None <EOL> self . _step_index = <NUM_LIT:0> <EOL> def reset ( self ) : <EOL> self . current_feature = None <EOL> self . current_feature_data = None <EOL> self . _step_index = <NUM_LIT:0> <EOL> def uri ( self , uri ) : <EOL> pass <EOL> def feature ( self , feature ) : <EOL> self . reset ( ) <EOL> self . current_feature = feature <EOL> self . current_feature_data = { <EOL> '<STR_LIT>' : feature . keyword , <EOL> '<STR_LIT:name>' : feature . name , <EOL> '<STR_LIT>' : list ( feature . tags ) , <EOL> '<STR_LIT:location>' : six . text_type ( feature . location ) , <EOL> '<STR_LIT:status>' : None , <EOL> } <EOL> element = self . current_feature_data <EOL> if feature . description : <EOL> element [ '<STR_LIT:description>' ] = feature . description <EOL> def background ( self , background ) : <EOL> element = self . add_feature_element ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : background . keyword , <EOL> '<STR_LIT:name>' : background . name , <EOL> '<STR_LIT:location>' : six . text_type ( background . location ) , <EOL> '<STR_LIT>' : [ ] , <EOL> } ) <EOL> if background . name : <EOL> element [ '<STR_LIT:name>' ] = background . name <EOL> self . _step_index = <NUM_LIT:0> <EOL> for step_ in background . steps : <EOL> self . step ( step_ ) <EOL> def scenario ( self , scenario ) : <EOL> element = self . add_feature_element ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : scenario . keyword , <EOL> '<STR_LIT:name>' : scenario . name , <EOL> '<STR_LIT>' : scenario . tags , <EOL> '<STR_LIT:location>' : six . text_type ( scenario . location ) , <EOL> '<STR_LIT>' : [ ] , <EOL> } ) <EOL> if scenario . description : <EOL> element [ '<STR_LIT:description>' ] = scenario . description <EOL> self . _step_index = <NUM_LIT:0> <EOL> def scenario_outline ( self , scenario_outline ) : <EOL> element = self . add_feature_element ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : scenario_outline . keyword , <EOL> '<STR_LIT:name>' : scenario_outline . name , <EOL> '<STR_LIT>' : scenario_outline . tags , <EOL> '<STR_LIT:location>' : six . text_type ( scenario_outline . location ) , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } ) <EOL> if scenario_outline . description : <EOL> element [ '<STR_LIT:description>' ] = scenario_outline . description <EOL> self . _step_index = <NUM_LIT:0> <EOL> @ classmethod <EOL> def make_table ( cls , table ) : <EOL> table_data = { <EOL> '<STR_LIT>' : table . headings , <EOL> '<STR_LIT>' : [ list ( row ) for row in table . rows ] <EOL> } <EOL> return table_data <EOL> def examples ( self , examples ) : <EOL> e = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : examples . keyword , <EOL> '<STR_LIT:name>' : examples . name , <EOL> '<STR_LIT:location>' : six . text_type ( examples . location ) , <EOL> } <EOL> if examples . table : <EOL> e [ '<STR_LIT>' ] = self . make_table ( examples . table ) <EOL> element = self . current_feature_element <EOL> element [ '<STR_LIT>' ] . append ( e ) <EOL> def step ( self , step ) : <EOL> s = { <EOL> '<STR_LIT>' : step . keyword , <EOL> '<STR_LIT>' : step . step_type , <EOL> '<STR_LIT:name>' : step . name , <EOL> '<STR_LIT:location>' : six . text_type ( step . location ) , <EOL> } <EOL> if step . text : <EOL> text = step . text <EOL> if self . split_text_into_lines and "<STR_LIT:\n>" in text : <EOL> text = text . splitlines ( ) <EOL> s [ '<STR_LIT:text>' ] = text <EOL> if step . table : <EOL> s [ '<STR_LIT>' ] = self . make_table ( step . table ) <EOL> element = self . current_feature_element <EOL> element [ '<STR_LIT>' ] . append ( s ) <EOL> def match ( self , match ) : <EOL> args = [ ] <EOL> for argument in match . arguments : <EOL> argument_value = argument . value <EOL> if not isinstance ( argument_value , self . json_scalar_types ) : <EOL> argument_value = argument . original <EOL> assert isinstance ( argument_value , self . json_scalar_types ) <EOL> arg = { <EOL> '<STR_LIT:value>' : argument_value , <EOL> } <EOL> if argument . name : <EOL> arg [ '<STR_LIT:name>' ] = argument . name <EOL> if argument . original != argument_value : <EOL> arg [ '<STR_LIT>' ] = argument . original <EOL> args . append ( arg ) <EOL> match_data = { <EOL> '<STR_LIT:location>' : six . text_type ( match . location ) or "<STR_LIT>" , <EOL> '<STR_LIT>' : args , <EOL> } <EOL> if match . location : <EOL> steps = self . current_feature_element [ '<STR_LIT>' ] <EOL> steps [ self . _step_index ] [ '<STR_LIT>' ] = match_data <EOL> def result ( self , result ) : <EOL> steps = self . current_feature_element [ '<STR_LIT>' ] <EOL> steps [ self . _step_index ] [ '<STR_LIT:result>' ] = { <EOL> '<STR_LIT:status>' : result . status , <EOL> '<STR_LIT>' : result . duration , <EOL> } <EOL> if result . error_message and result . status == '<STR_LIT>' : <EOL> error_message = result . error_message <EOL> if self . split_text_into_lines and "<STR_LIT:\n>" in error_message : <EOL> error_message = error_message . splitlines ( ) <EOL> result_element = steps [ self . _step_index ] [ '<STR_LIT:result>' ] <EOL> result_element [ '<STR_LIT>' ] = error_message <EOL> self . _step_index += <NUM_LIT:1> <EOL> def embedding ( self , mime_type , data ) : <EOL> step = self . current_feature_element [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> step [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT>' : mime_type , <EOL> '<STR_LIT:data>' : base64 . b64encode ( data ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) , <EOL> } ) <EOL> def eof ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . current_feature_data : <EOL> return <EOL> self . update_status_data ( ) <EOL> if self . feature_count == <NUM_LIT:0> : <EOL> self . write_json_header ( ) <EOL> else : <EOL> self . write_json_feature_separator ( ) <EOL> self . write_json_feature ( self . current_feature_data ) <EOL> self . current_feature_data = None <EOL> self . feature_count += <NUM_LIT:1> <EOL> def close ( self ) : <EOL> self . write_json_footer ( ) <EOL> self . close_stream ( ) <EOL> def add_feature_element ( self , element ) : <EOL> assert self . current_feature_data is not None <EOL> if '<STR_LIT>' not in self . current_feature_data : <EOL> self . current_feature_data [ '<STR_LIT>' ] = [ ] <EOL> self . current_feature_data [ '<STR_LIT>' ] . append ( element ) <EOL> return element <EOL> @ property <EOL> def current_feature_element ( self ) : <EOL> assert self . current_feature_data is not None <EOL> return self . current_feature_data [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> def update_status_data ( self ) : <EOL> assert self . current_feature <EOL> assert self . current_feature_data <EOL> self . current_feature_data [ '<STR_LIT:status>' ] = self . current_feature . status <EOL> def write_json_header ( self ) : <EOL> self . stream . write ( '<STR_LIT>' ) <EOL> def write_json_footer ( self ) : <EOL> self . stream . write ( '<STR_LIT>' ) <EOL> def write_json_feature ( self , feature_data ) : <EOL> self . stream . write ( json . dumps ( feature_data , ** self . dumps_kwargs ) ) <EOL> self . stream . flush ( ) <EOL> def write_json_feature_separator ( self ) : <EOL> self . stream . write ( "<STR_LIT>" ) <EOL> class PrettyJSONFormatter ( JSONFormatter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> dumps_kwargs = { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : True } </s>
