Skip to content

Commit

Permalink
Merge pull request #94 from Thomas55555/return-containers-as-dictionary
Browse files Browse the repository at this point in the history
return containers as dictionary
  • Loading branch information
Thomas55555 authored Oct 29, 2024
2 parents decaa0c + 780a8f2 commit c493cef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
17 changes: 9 additions & 8 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ async def main() -> None:
aiotainer_api = PortainerClient(AsyncTokenAuth(websession), poll=True)
await aiotainer_api.connect()
settings = await aiotainer_api.get_settings()
print("SETTINGS")
print("::::::::")
pprint.pprint(settings)
for test in aiotainer_api.data.values():
for snapshot in test.snapshots:
for container in snapshot.docker_snapshot_raw.containers:
print(container.names[0].strip("/"), container.state, container.id)
a = await aiotainer_api.get_status_specific(2)
pprint.pprint(a)
for node in aiotainer_api.data:
for container_id in (
aiotainer_api.data[node].snapshots[-1].docker_snapshot_raw.containers
):
pprint.pprint(
aiotainer_api.data[node]
.snapshots[-1]
.docker_snapshot_raw.containers[container_id]
)
await asyncio.sleep(2000)
await aiotainer_api.close()
await websession.close()
Expand Down
10 changes: 9 additions & 1 deletion src/aiotainer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ class Container(DataClassDictMixin):
class DockerSnapshotRaw(DataClassDictMixin):
"""Dataclass for raw Docker snapshot data."""

containers: list[Container] = field(metadata=field_options(alias="Containers"))
containers: dict[str, Container] = field(
metadata=field_options(
alias="Containers",
deserialize=lambda containers_list: {
container_data["Id"]: Container.from_dict(container_data)
for container_data in containers_list
},
)
)


@dataclass
Expand Down
10 changes: 4 additions & 6 deletions src/aiotainer/portainer_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ class FDOConfiguration(DataClassDictMixin):
seconds_per_unit = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}


def convert_to_seconds(s: str) -> int:
"""Convert string to seconds."""
return int(s[:-1]) * seconds_per_unit[s[-1]]
# def convert_to_seconds(s: str) -> int:
# """Convert string to seconds."""
# return int(s[:-1]) * seconds_per_unit[s[-1]]


@dataclass
Expand All @@ -154,9 +154,7 @@ class EdgeConfiguration(DataClassDictMixin):
ping_interval: int = field(default=0, metadata=field_options(alias="PingInterval"))
snapshot_interval: int = field(
default=0,
metadata=field_options(
alias="SnapshotInterval", deserialize=convert_to_seconds
),
metadata=field_options(alias="SnapshotInterval"),
)
async_mode: bool = field(default=False, metadata=field_options(alias="AsyncMode"))

Expand Down

0 comments on commit c493cef

Please sign in to comment.