mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-06-20 07:16:11 +01:00
Factor out the parsing of a valid cell run in the generic ipython implementation
run_cell() becomes more complicated when we add ipython version 3 support which upsets pylint because there are "too many branches (15/12)". Factor out part of the function to make pylint happy.
This commit is contained in:
@ -48,6 +48,32 @@ elif IPython:
|
|||||||
import_error_str = 'Unsupported IPython version {}'.format(IPython_ver_str)
|
import_error_str = 'Unsupported IPython version {}'.format(IPython_ver_str)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_valid_output(msg):
|
||||||
|
"""Parse a valid result from an execution of a cell in an ipython kernel"""
|
||||||
|
msg_type = msg["msg_type"]
|
||||||
|
content = msg["content"]
|
||||||
|
out = NotebookNode(output_type=msg_type)
|
||||||
|
|
||||||
|
if msg_type == "stream":
|
||||||
|
out.stream = content["name"]
|
||||||
|
out.text = content["data"]
|
||||||
|
elif msg_type in ("display_data", "pyout"):
|
||||||
|
for mime, data in content["data"].iteritems():
|
||||||
|
if mime == "text/plain":
|
||||||
|
attr = "text"
|
||||||
|
else:
|
||||||
|
attr = mime.split("/")[-1]
|
||||||
|
setattr(out, attr, data)
|
||||||
|
elif msg_type == "pyerr":
|
||||||
|
out.ename = content["ename"]
|
||||||
|
out.evalue = content["evalue"]
|
||||||
|
out.traceback = content["traceback"]
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown msg_type {}".format(msg_type))
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def run_cell(kernel_client, cell):
|
def run_cell(kernel_client, cell):
|
||||||
"""Run a cell of a notebook in an ipython kernel and return its output"""
|
"""Run a cell of a notebook in an ipython kernel and return its output"""
|
||||||
kernel_client.execute(cell.input)
|
kernel_client.execute(cell.input)
|
||||||
@ -57,36 +83,14 @@ def run_cell(kernel_client, cell):
|
|||||||
while True:
|
while True:
|
||||||
msg = kernel_client.get_iopub_msg()
|
msg = kernel_client.get_iopub_msg()
|
||||||
|
|
||||||
msg_type = msg["msg_type"]
|
if msg["msg_type"] == "status":
|
||||||
content = msg["content"]
|
if msg["content"]["execution_state"] == "idle" and input_acknowledged:
|
||||||
out = NotebookNode(output_type=msg_type)
|
|
||||||
|
|
||||||
if msg_type == "status":
|
|
||||||
if content["execution_state"] == "idle" and input_acknowledged:
|
|
||||||
break
|
break
|
||||||
else:
|
elif msg["msg_type"] == "pyin":
|
||||||
continue
|
|
||||||
elif msg_type == "pyin":
|
|
||||||
input_acknowledged = True
|
input_acknowledged = True
|
||||||
continue
|
|
||||||
elif msg_type == "stream":
|
|
||||||
out.stream = content["name"]
|
|
||||||
out.text = content["data"]
|
|
||||||
elif msg_type in ("display_data", "pyout"):
|
|
||||||
for mime, data in content["data"].iteritems():
|
|
||||||
if mime == "text/plain":
|
|
||||||
attr = "text"
|
|
||||||
else:
|
|
||||||
attr = mime.split("/")[-1]
|
|
||||||
setattr(out, attr, data)
|
|
||||||
elif msg_type == "pyerr":
|
|
||||||
out.ename = content["ename"]
|
|
||||||
out.evalue = content["evalue"]
|
|
||||||
out.traceback = content["traceback"]
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unknown msg_type {}".format(msg_type))
|
out = parse_valid_output(msg)
|
||||||
|
outs.append(out)
|
||||||
outs.append(out)
|
|
||||||
|
|
||||||
return outs
|
return outs
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user