From 541ee0d5651a546d677b2cd97db4b58fce0f3760 Mon Sep 17 00:00:00 2001 From: Khushiyant Date: Mon, 16 Sep 2024 14:00:50 +0530 Subject: [PATCH] feat: add ContainerInfo--object as return object to inspect_container --- docker/api/container.py | 39 +++++++++++++++++++++++-- tests/integration/api_container_test.py | 16 ++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/docker/api/container.py b/docker/api/container.py index d1b870f9c..b361d4b22 100644 --- a/docker/api/container.py +++ b/docker/api/container.py @@ -11,6 +11,37 @@ ) +class ContainerInfo: + def __init__(self, info: dict): + self._info = info + + def __repr__(self): + return ( + f"" + ) + + def __getattr__(self, item): + """ + Dynamically fetch any attribute from the underlying dictionary. + This allows direct access to all fields without manually defining them. + """ + try: + return self._info[item] + except KeyError as err: + raise AttributeError( + f"'ContainerInfo' object has no attribute '{item}'" + ) from err + + def __getitem__(self, item): + """ + Optional: If you'd like to access attributes using bracket notation. + """ + return self._info.get(item) + + def get_info(self): + """Returns the entire dictionary for reference if needed""" + return self._info + class ContainerApiMixin: @utils.check_resource('container') def attach(self, container, stdout=True, stderr=True, @@ -783,16 +814,18 @@ def inspect_container(self, container): container (str): The container to inspect Returns: - (dict): Similar to the output of `docker inspect`, but as a - single dict + (ContainerInfo): Similar to the output of `docker inspect`, but as a + docker.api.container.ContainerInfo object Raises: :py:class:`docker.errors.APIError` If the server returns an error. """ - return self._result( + container_info = self._result( self._get(self._url("/containers/{0}/json", container)), True ) + return ContainerInfo(container_info) + @utils.check_resource('container') def kill(self, container, signal=None): diff --git a/tests/integration/api_container_test.py b/tests/integration/api_container_test.py index 0215e14c2..0e1aca584 100644 --- a/tests/integration/api_container_test.py +++ b/tests/integration/api_container_test.py @@ -1572,3 +1572,19 @@ def test_remove_link(self): x['Id'].startswith(container2_id) ] assert len(retrieved) == 2 + +class CoontainerInfoObejectTest(BaseAPIIntegrationTest): + def test_container_info_object(self): + container = self.client.create_container( + TEST_IMG, 'true', host_config=self.client.create_host_config()) + self.tmp_containers.append(container) + self.client.start(container) + + inspect_data = self.client.inspect_container(container) + assert isinstance(inspect_data, docker.api.container.ContainerInfo) + assert inspect_data['Config']['Image'] == TEST_IMG + assert inspect_data['HostConfig']['NetworkMode'] == 'bridge' + + # attribute style access + assert inspect_data.Id == container['Id'] + assert inspect_data["Id"] == container["Id"]