Skip to content

Commit

Permalink
Use separate status attributes for ComposeProject model (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall7m authored Nov 3, 2022
1 parent f849a1a commit cb2fff4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
10 changes: 8 additions & 2 deletions python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from python_on_whales.components.compose.models import ComposeConfig, ComposeProject
from python_on_whales.utils import (
format_dict_for_cli,
parse_ls_status_count,
run,
stream_stdout_and_stderr,
to_list,
Expand Down Expand Up @@ -353,11 +354,16 @@ def ls(
full_cmd = self.docker_compose_cmd + ["ls", "--format", "json"]
full_cmd.add_flag("--all", all)
full_cmd.add_args_list("--filter", format_dict_for_cli(filters))

return [
ComposeProject(
name=proj["Name"],
status=proj["Status"].split("(")[0],
count=int(proj["Status"].split("(")[1].strip(")")),
created=parse_ls_status_count(proj["Status"], "created"),
running=parse_ls_status_count(proj["Status"], "running"),
restarting=parse_ls_status_count(proj["Status"], "restarting"),
exited=parse_ls_status_count(proj["Status"], "exited"),
paused=parse_ls_status_count(proj["Status"], "paused"),
dead=parse_ls_status_count(proj["Status"], "dead"),
config_files=[
Path(path)
for path in proj.get("ConfigFiles", "").split(",")
Expand Down
8 changes: 6 additions & 2 deletions python_on_whales/components/compose/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class ComposeConfig(BaseModel):

class ComposeProject(BaseModel):
name: str
status: str
count: int
created: int = 0
running: int = 0
restarting: int = 0
exited: int = 0
paused: int = 0
dead: int = 0
config_files: Optional[List[Path]]
7 changes: 7 additions & 0 deletions python_on_whales/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,10 @@ def format_time_for_docker(time_object: Union[datetime, timedelta]) -> str:
return time_object.strftime("%Y-%m-%dT%H:%M:%S")
elif isinstance(time_object, timedelta):
return f"{time_object.total_seconds()}s"


def parse_ls_status_count(status_output, status) -> int:
try:
return int(status_output.split(status + "(")[1].split(")")[0])
except IndexError:
return 0
13 changes: 8 additions & 5 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,26 +724,29 @@ def test_compose_port():
d.compose.down(timeout=1)


def test_compose_ls_project_running():
def test_compose_ls_project_multiple_statuses():
d = DockerClient(
compose_files=[
PROJECT_ROOT
/ "tests/python_on_whales/components/dummy_compose_ends_quickly.yml",
PROJECT_ROOT / "tests/python_on_whales/components/dummy_compose.yml",
],
compose_compatibility=True,
compose_project_name="test_compose_ls",
)
d.compose.up(["busybox"], detach=True)
d.compose.up(["alpine", "dodo"], detach=True)
time.sleep(2)

projects = d.compose.ls()
projects = d.compose.ls(all=True)
project = [
proj
for proj in projects
if proj.name == d.compose.client_config.compose_project_name
][0]

assert project.status == "running"
assert project.count == 1
assert project.running == 1
assert project.exited == 1
assert project.paused == 0
if project.config_files:
assert sorted(project.config_files) == sorted(d.client_config.compose_files)

Expand Down

0 comments on commit cb2fff4

Please sign in to comment.