diff --git a/python_on_whales/client_config.py b/python_on_whales/client_config.py index db34160d..207961c1 100644 --- a/python_on_whales/client_config.py +++ b/python_on_whales/client_config.py @@ -239,14 +239,13 @@ def _parse_json_object(self, json_object: Dict[str, Any]): raise NotImplementedError def _fetch_and_parse_inspect_result(self, reference: str): - json_str = self._fetch_inspect_result_json(reference) - json_object = json.loads(json_str)[0] + json_object = self._fetch_inspect_result_json(reference) try: return self._parse_json_object(json_object) except pydantic.ValidationError as err: fd, json_response_file = tempfile.mkstemp(suffix=".json", text=True) with open(json_response_file, "w") as f: - f.write(json_str) + json.dump(json_object, f, indent=2) raise ParsingError( f"There was an error parsing the json response from the Docker daemon. \n" diff --git a/python_on_whales/components/config/cli_wrapper.py b/python_on_whales/components/config/cli_wrapper.py index 36fd534d..19bad0e9 100644 --- a/python_on_whales/components/config/cli_wrapper.py +++ b/python_on_whales/components/config/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from datetime import datetime from pathlib import Path from typing import Any, Dict, List, Optional, Union, overload @@ -30,7 +31,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["config", "inspect", reference]) + json_str = run(self.docker_cmd + ["config", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]): return ConfigInspectResult(**json_object) diff --git a/python_on_whales/components/container/cli_wrapper.py b/python_on_whales/components/container/cli_wrapper.py index ea81faf9..439cf0ab 100644 --- a/python_on_whales/components/container/cli_wrapper.py +++ b/python_on_whales/components/container/cli_wrapper.py @@ -91,7 +91,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove(volumes=True) def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["container", "inspect", reference]) + json_str = run(self.docker_cmd + ["container", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]): return ContainerInspectResult(**json_object) diff --git a/python_on_whales/components/context/cli_wrapper.py b/python_on_whales/components/context/cli_wrapper.py index 9f2af125..2506c6f8 100644 --- a/python_on_whales/components/context/cli_wrapper.py +++ b/python_on_whales/components/context/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from dataclasses import dataclass from pathlib import Path from typing import Any, Dict, List, Optional, Union, overload @@ -36,7 +37,8 @@ def _fetch_inspect_result_json(self, reference: Optional[str]): full_cmd = self.docker_cmd + ["context", "inspect"] if reference is not None: full_cmd.append(reference) - return run(full_cmd) + json_str = run(full_cmd) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]): return ContextInspectResult(**json_object) diff --git a/python_on_whales/components/image/cli_wrapper.py b/python_on_whales/components/image/cli_wrapper.py index 788b39a3..f1106752 100644 --- a/python_on_whales/components/image/cli_wrapper.py +++ b/python_on_whales/components/image/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import warnings from datetime import datetime from multiprocessing.pool import ThreadPool @@ -45,7 +46,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove(force=True) def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["image", "inspect", reference]) + json_str = run(self.docker_cmd + ["image", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> ImageInspectResult: return ImageInspectResult(**json_object) diff --git a/python_on_whales/components/manifest/cli_wrapper.py b/python_on_whales/components/manifest/cli_wrapper.py index 45f964f5..78cbe6c8 100644 --- a/python_on_whales/components/manifest/cli_wrapper.py +++ b/python_on_whales/components/manifest/cli_wrapper.py @@ -1,3 +1,4 @@ +import json from typing import Any, Dict, List, Optional, Union from python_on_whales.client_config import ( @@ -24,7 +25,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return f'[{run(self.docker_cmd + ["manifest", "inspect", reference])}]' + json_str = run(self.docker_cmd + ["manifest", "inspect", reference]) + return json.loads(json_str) def _parse_json_object( self, json_object: Dict[str, Any] diff --git a/python_on_whales/components/network/cli_wrapper.py b/python_on_whales/components/network/cli_wrapper.py index 0a526a6d..750d3287 100644 --- a/python_on_whales/components/network/cli_wrapper.py +++ b/python_on_whales/components/network/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from datetime import datetime from typing import Any, Dict, List, Optional, Union, overload @@ -30,7 +31,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["network", "inspect", reference]) + json_str = run(self.docker_cmd + ["network", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> NetworkInspectResult: return NetworkInspectResult(**json_object) diff --git a/python_on_whales/components/node/cli_wrapper.py b/python_on_whales/components/node/cli_wrapper.py index 9b407583..ba7e1c73 100644 --- a/python_on_whales/components/node/cli_wrapper.py +++ b/python_on_whales/components/node/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from datetime import datetime from typing import Any, Dict, List, Optional, Union, overload @@ -27,7 +28,8 @@ def __init__( super().__init__(client_config, "id", reference, is_immutable_id) def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["node", "inspect", reference]) + json_str = run(self.docker_cmd + ["node", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> NodeInspectResult: return NodeInspectResult(**json_object) diff --git a/python_on_whales/components/plugin/cli_wrapper.py b/python_on_whales/components/plugin/cli_wrapper.py index 554572a7..b49816bd 100644 --- a/python_on_whales/components/plugin/cli_wrapper.py +++ b/python_on_whales/components/plugin/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from typing import Any, Dict, List, Optional, Union, overload from python_on_whales.client_config import ( @@ -28,7 +29,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove(force=True) def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["plugin", "inspect", reference]) + json_str = run(self.docker_cmd + ["plugin", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> PluginInspectResult: return PluginInspectResult(**json_object) diff --git a/python_on_whales/components/secret/cli_wrapper.py b/python_on_whales/components/secret/cli_wrapper.py index d9793d6c..39e2a6a6 100644 --- a/python_on_whales/components/secret/cli_wrapper.py +++ b/python_on_whales/components/secret/cli_wrapper.py @@ -1,3 +1,4 @@ +import json from typing import Any, Dict, List, Optional, Union from python_on_whales.client_config import ( @@ -22,7 +23,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["secret", "inspect", reference]) + json_str = run(self.docker_cmd + ["secret", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> SecretInspectResult: return SecretInspectResult(**json_object) diff --git a/python_on_whales/components/service/cli_wrapper.py b/python_on_whales/components/service/cli_wrapper.py index 9ec30cff..cb9d0946 100644 --- a/python_on_whales/components/service/cli_wrapper.py +++ b/python_on_whales/components/service/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from datetime import datetime, timedelta from typing import Any, Dict, List, Literal, Optional, Union, overload @@ -41,7 +42,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["service", "inspect", reference]) + json_str = run(self.docker_cmd + ["service", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> ServiceInspectResult: return ServiceInspectResult(**json_object) diff --git a/python_on_whales/components/task/cli_wrapper.py b/python_on_whales/components/task/cli_wrapper.py index 3fd9cf5c..e5ffcd9d 100644 --- a/python_on_whales/components/task/cli_wrapper.py +++ b/python_on_whales/components/task/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from datetime import datetime from typing import Any, Dict, List, Optional, Union @@ -26,7 +27,8 @@ def __init__( super().__init__(client_config, "id", reference, is_immutable_id) def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["inspect", reference]) + json_str = run(self.docker_cmd + ["inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]) -> TaskInspectResult: return TaskInspectResult(**json_object) diff --git a/python_on_whales/components/volume/cli_wrapper.py b/python_on_whales/components/volume/cli_wrapper.py index d04c77db..7cb647ed 100644 --- a/python_on_whales/components/volume/cli_wrapper.py +++ b/python_on_whales/components/volume/cli_wrapper.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import os import tempfile from datetime import datetime @@ -33,7 +34,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.remove() def _fetch_inspect_result_json(self, reference): - return run(self.docker_cmd + ["volume", "inspect", reference]) + json_str = run(self.docker_cmd + ["volume", "inspect", reference]) + return json.loads(json_str)[0] def _parse_json_object(self, json_object: Dict[str, Any]): return VolumeInspectResult(**json_object) diff --git a/tests/python_on_whales/test_client_config.py b/tests/python_on_whales/test_client_config.py index 60685d71..63128a1d 100644 --- a/tests/python_on_whales/test_client_config.py +++ b/tests/python_on_whales/test_client_config.py @@ -1,3 +1,4 @@ +import json from pathlib import Path import pytest @@ -5,21 +6,15 @@ from python_on_whales import docker from python_on_whales.client_config import ParsingError -fake_json_message = """ -[ - { - "CreatedAt": "2020-10-08T18:32:55Z", - "Driver": ["dummy", "fake", ["driver"]], - "Labels": { - "com.docker.stack.namespace": "dodo" - }, - "Mountpoint": "/var/lib/docker/volumes/dodo_traefik-data/_data", - "Name": "dodo_traefik-data", - "Options": null, - "Scope": "local" - } -] -""" +fake_json_message = { + "CreatedAt": "2020-10-08T18:32:55Z", + "Driver": ["dummy", "fake", ["driver"]], + "Labels": {"com.docker.stack.namespace": "dodo"}, + "Mountpoint": "/var/lib/docker/volumes/dodo_traefik-data/_data", + "Name": "dodo_traefik-data", + "Options": None, + "Scope": "local", +} def test_pretty_exception_message_and_report(mocker): @@ -40,4 +35,4 @@ def test_pretty_exception_message_and_report(mocker): else: raise IndexError - assert Path(word).read_text() == fake_json_message + assert Path(word).read_text() == json.dumps(fake_json_message, indent=2)