Создайте StreamLineBuilder генератор:
import json
class StreamLineBuildGenerator(object):
def __init__(self, json_data):
self.__dict__ = json.loads(json_data)
, а затем использовать этот генератор для того, чтобы разобрать ваш поток:
import docker
docker_client = docker.Client(version="1.18", base_url="unix:///var/run/docker.sock")
generator = docker_client.build(nocache=False, rm=True, stream=True, tag="my_image_tag", path="my_path")
for line in generator:
try:
stream_line = StreamLineBuildGenerator(line)
if hasattr(stream_line, "error"):
print(stream_line.error)
if hasattr(stream_line, "errorDetail"):
if not stream_line.error == stream_line.errorDetail["message"]:
if hasattr(stream_line.errorDetail, "code"):
print("[" + stream_line.errorDetail["code"] + "] ", False)
print(stream_line.errorDetail["message"])
except ValueError:
# If we are not able to deserialize the received line as JSON object, just print it out
print(line)
continue