This commit is contained in:
Khushiyant 2025-03-09 17:57:31 +05:30 committed by GitHub
commit 78360a5ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 3 deletions

View File

@ -11,6 +11,40 @@ from ..types import (
)
class ContainerInfo:
def __init__(self, info: dict):
self._info = info
def __repr__(self):
return (
f"<ContainerInfo id={self._info.get('Id')}, name={self._info.get('Name')}>"
)
def __getattr__(self, item):
"""
Dynamically fetch any attribute from the underlying dictionary.
This allows direct access to all fields without manually defining them.
"""
try:
value = self._info[item]
if isinstance(value, dict):
return ContainerInfo(value)
return value
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 +817,18 @@ class ContainerApiMixin:
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):

View File

@ -1572,3 +1572,20 @@ class LinkTest(BaseAPIIntegrationTest):
x['Id'].startswith(container2_id)
]
assert len(retrieved) == 2
class ContainerInfoObjectTest(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.Config.Image == TEST_IMG
assert inspect_data.HostConfig.NetworkMode == 'bridge'