|
11 | 11 | )
|
12 | 12 |
|
13 | 13 |
|
| 14 | +class ContainerInfo: |
| 15 | + def __init__(self, info: dict): |
| 16 | + self._info = info |
| 17 | + |
| 18 | + def __repr__(self): |
| 19 | + return ( |
| 20 | + f"<ContainerInfo id={self._info.get('Id')}, name={self._info.get('Name')}>" |
| 21 | + ) |
| 22 | + |
| 23 | + def __getattr__(self, item): |
| 24 | + """ |
| 25 | + Dynamically fetch any attribute from the underlying dictionary. |
| 26 | + This allows direct access to all fields without manually defining them. |
| 27 | + """ |
| 28 | + try: |
| 29 | + return self._info[item] |
| 30 | + except KeyError as err: |
| 31 | + raise AttributeError( |
| 32 | + f"'ContainerInfo' object has no attribute '{item}'" |
| 33 | + ) from err |
| 34 | + |
| 35 | + def __getitem__(self, item): |
| 36 | + """ |
| 37 | + Optional: If you'd like to access attributes using bracket notation. |
| 38 | + """ |
| 39 | + return self._info.get(item) |
| 40 | + |
| 41 | + def get_info(self): |
| 42 | + """Returns the entire dictionary for reference if needed""" |
| 43 | + return self._info |
| 44 | + |
14 | 45 | class ContainerApiMixin:
|
15 | 46 | @utils.check_resource('container')
|
16 | 47 | def attach(self, container, stdout=True, stderr=True,
|
@@ -783,16 +814,18 @@ def inspect_container(self, container):
|
783 | 814 | container (str): The container to inspect
|
784 | 815 |
|
785 | 816 | Returns:
|
786 |
| - (dict): Similar to the output of `docker inspect`, but as a |
787 |
| - single dict |
| 817 | + (ContainerInfo): Similar to the output of `docker inspect`, but as a |
| 818 | + docker.api.container.ContainerInfo object |
788 | 819 |
|
789 | 820 | Raises:
|
790 | 821 | :py:class:`docker.errors.APIError`
|
791 | 822 | If the server returns an error.
|
792 | 823 | """
|
793 |
| - return self._result( |
| 824 | + container_info = self._result( |
794 | 825 | self._get(self._url("/containers/{0}/json", container)), True
|
795 | 826 | )
|
| 827 | + return ContainerInfo(container_info) |
| 828 | + |
796 | 829 |
|
797 | 830 | @utils.check_resource('container')
|
798 | 831 | def kill(self, container, signal=None):
|
|
0 commit comments