/usr/lib/python3/dist-packages/behave/json_parser.py is in python3-behave 1.2.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | # -*- coding: utf-8 -*-
"""
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)
"""
from behave import model
import codecs
try:
import json
except ImportError:
# -- PYTHON 2.5 backward compatible: Use simplejson module.
import simplejson as json
__author__ = "Jens Engel"
# ----------------------------------------------------------------------------
# FUNCTIONS:
# ----------------------------------------------------------------------------
def parse(json_filename, encoding="UTF-8"):
"""
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.
"""
with codecs.open(json_filename, "rU", encoding=encoding) as fp:
json_data = json.load(fp, encoding=encoding)
json_processor = JsonParser()
features = json_processor.parse_features(json_data)
return features
# ----------------------------------------------------------------------------
# CLASSES:
# ----------------------------------------------------------------------------
class JsonParser(object):
def parse_features(self, json_data):
assert isinstance(json_data, list)
features = []
json_features = json_data
for json_feature in json_features:
feature = self.parse_feature(json_feature)
features.append(feature)
return features
def parse_feature(self, json_feature):
name = json_feature.get("name", "")
keyword = json_feature.get("keyword", None)
tags = json_feature.get("tags", [])
description = json_feature.get("description", [])
location = json_feature.get("location", "")
filename, line = location.split(":")
feature = model.Feature(filename, line, keyword, name, tags, description)
json_elements = json_feature.get("elements", [])
for json_element in json_elements:
self.add_feature_element(feature, json_element)
return feature
def add_feature_element(self, feature, json_element):
datatype = json_element.get("type", "")
category = datatype.lower()
if category == "background":
background = self.parse_background(json_element)
feature.background = background
elif category == "scenario":
scenario = self.parse_scenario(json_element)
feature.add_scenario(scenario)
elif category == "scenario_outline":
scenario_outline = self.parse_scenario_outline(json_element)
feature.add_scenario(scenario_outline)
self.current_scenario_outline = scenario_outline
# elif category == "examples":
# examples = self.parse_examples(json_element)
# self.current_scenario_outline.examples = examples
else:
raise KeyError("Invalid feature-element keyword: %s" % category)
def parse_background(self, json_element):
"""
self.add_feature_element({
'keyword': background.keyword,
'location': background.location,
'steps': [],
})
"""
keyword = json_element.get("keyword", "")
name = json_element.get("name", "")
location = json_element.get("location", "")
json_steps = json_element.get("steps", [])
steps = self.parse_steps(json_steps)
filename, line = location.split(":")
background = model.Background(filename, line, keyword, name, steps)
return background
def parse_scenario(self, json_element):
"""
self.add_feature_element({
'keyword': scenario.keyword,
'name': scenario.name,
'tags': scenario.tags,
'location': scenario.location,
'steps': [],
})
"""
keyword = json_element.get("keyword", "")
name = json_element.get("name", "")
description = json_element.get("description", [])
tags = json_element.get("tags", [])
location = json_element.get("location", "")
json_steps = json_element.get("steps", [])
steps = self.parse_steps(json_steps)
filename, line = location.split(":")
scenario = model.Scenario(filename, line, keyword, name, tags, steps)
scenario.description = description
return scenario
def parse_scenario_outline(self, json_element):
"""
self.add_feature_element({
'keyword': scenario_outline.keyword,
'name': scenario_outline.name,
'tags': scenario_outline.tags,
'location': scenario_outline.location,
'steps': [],
'examples': [],
})
"""
keyword = json_element.get("keyword", "")
name = json_element.get("name", "")
description = json_element.get("description", [])
tags = json_element.get("tags", [])
location = json_element.get("location", "")
json_steps = json_element.get("steps", [])
json_examples = json_element.get("examples", [])
steps = self.parse_steps(json_steps)
examples = []
if json_examples:
examples = self.parse_examples(json_examples)
filename, line = location.split(":")
scenario_outline = model.ScenarioOutline(filename, line, keyword, name,
tags=tags, steps=steps, examples=examples)
scenario_outline.description = description
return scenario_outline
def parse_steps(self, json_steps):
steps = []
for json_step in json_steps:
step = self.parse_step(json_step)
steps.append(step)
return steps
def parse_step(self, json_element):
"""
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)
"""
keyword = json_element.get("keyword", "")
name = json_element.get("name", "")
step_type = json_element.get("step_type", "")
location = json_element.get("location", "")
text = json_element.get("text", None)
if isinstance(text, list):
text = "\n".join(text)
table = None
json_table = json_element.get("table", None)
if json_table:
table = self.parse_table(json_table)
filename, line = location.split(":")
step = model.Step(filename, line, keyword, step_type, name)
step.text = text
step.table = table
json_result = json_element.get("result", None)
if json_result:
self.add_step_result(step, json_result)
return step
def add_step_result(self, step, json_result):
"""
steps = self.current_feature_element['steps']
steps[self._step_index]['result'] = {
'status': result.status,
'duration': result.duration,
}
"""
status = json_result.get("status", "")
duration = json_result.get("duration", 0)
error_message = json_result.get("error_message", None)
if isinstance(error_message, list):
error_message = "\n".join(error_message)
step.status = status
step.duration = duration
step.error_message = error_message
def parse_table(self, json_table):
"""
table_data = {
'headings': table.headings,
'rows': [ list(row) for row in table.rows ]
}
return table_data
"""
headings = json_table.get("headings", [])
rows = json_table.get("rows", [])
table = model.Table(headings, rows=rows)
return table
def parse_examples(self, json_element):
"""
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)
"""
keyword = json_element.get("keyword", "")
name = json_element.get("name", "")
location = json_element.get("location", "")
table = None
json_table = json_element.get("table", None)
if json_table:
table = self.parse_table(json_table)
filename, line = location.split(":")
examples = model.Examples(filename, line, keyword, name, table)
return examples
|