Skip to content

Commit

Permalink
Tidy up ReloadableObjectFromJson._fetch_inspect_result_json() to retu…
Browse files Browse the repository at this point in the history
…rn a loaded object (#536)
  • Loading branch information
LewisGaul authored Jan 16, 2024
1 parent 26302ea commit f2eb701
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 31 deletions.
5 changes: 2 additions & 3 deletions python_on_whales/client_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/config/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion python_on_whales/components/container/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/context/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/image/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
import warnings
from datetime import datetime
from multiprocessing.pool import ThreadPool
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/manifest/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any, Dict, List, Optional, Union

from python_on_whales.client_config import (
Expand All @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/network/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
from datetime import datetime
from typing import Any, Dict, List, Optional, Union, overload

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/node/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
from datetime import datetime
from typing import Any, Dict, List, Optional, Union, overload

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/plugin/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/secret/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any, Dict, List, Optional, Union

from python_on_whales.client_config import (
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/service/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/task/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
from datetime import datetime
from typing import Any, Dict, List, Optional, Union

Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion python_on_whales/components/volume/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import json
import os
import tempfile
from datetime import datetime
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 11 additions & 16 deletions tests/python_on_whales/test_client_config.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import json
from pathlib import Path

import pytest

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):
Expand All @@ -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)

0 comments on commit f2eb701

Please sign in to comment.