Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow List[str] in DockerContainerListFilters['filters'] and format_dict_for_cli() #590

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python_on_whales/components/container/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
{
"id": str,
"name": str,
"label": str,
"label": Union[str, List[str]],
Copy link
Collaborator

@LewisGaul LewisGaul May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I have with this is it special-cases label, when actually it's presumably valid to specify any of the filter types more than once? It seems like conceptually what we want is to allow specifying the same key twice, but that's not possible with normal dicts in python of course.

Any thoughts @gabrieldemarmiesse on how to resolve this? One option would be to accept a list of 2-tuples instead of a dict, i.e. something like docker.container.list(filters: Sequence[DockerContainerListFilters]) where DockerContainerListFilters = tuple[Literal["id"], str] | tuple[Literal["name"], str] | ....

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to be backward compatible (and also because it's easy to write), we can apply the Union[str, List[str]] to all values in the TypedDict. So what's done in this PR, but for all elements. What do you think?

Copy link
Collaborator

@LewisGaul LewisGaul May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't like about that is it's not truly mirroring the CLI (you don't specify --filter label=foo,bar), so it feels like sacrificing the quality of the API for minor backwards compatibility (could easily enough continue to support the old form until v1.0 if we wanted). Perhaps not all fields would make sense to be specified more than once, is that something we'd need to think about in your suggested approach?

A couple of other notes on this API:

[EDIT: realised I was looking at the image list APIs rather than container list, oops]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be worth a redesign from scratch possibly... As we're pre-1.0 I believe we can afford it

Copy link
Author

@LooLzzz LooLzzz May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe chained calls would suite this API better?
Many ORMs are using this style of API

docker.container.list()
                .filter({'label': foo})
                .filter({'label': bar})

The actual evaluation is only performed when some magic method is called,
like __repr__ / __str__ / __list__

Copy link
Collaborator

@LewisGaul LewisGaul Sep 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lazy evaluation approach has downsides that make me hesitant:

  • implementation complexity
  • cognitive overhead caused by the fact that under certain APIs the actual command gets run at expected times

I still think we should go for:

docker.container.list(filters=[
    ("label", "foo"),
    ("label", "bar=1"),
    ("label!", "foo=0"),
    ("status", "created"),
    ("status", "running"),
    ("name", "prefix-*"),
])

(Note that the CLI is a bit confusing here in that label filters are ANDed together whereas status filters are ORed together).

"exited": int,
"status": Literal[
"created", "restarting", "running", "removing", "paused", "exited", "dead"
Expand Down
17 changes: 15 additions & 2 deletions python_on_whales/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,21 @@ def stream_stdout_and_stderr(
raise DockerException(full_cmd, exit_code, stderr=full_stderr)


def format_dict_for_cli(dictionary: Dict[str, str], separator="="):
return [f"{key}{separator}{value}" for key, value in dictionary.items()]
def format_dict_for_cli(dictionary: Dict[str, Union[str, List[str]]], separator="=") -> List[str]:
result_list = []

for key, value in dictionary.items():
if isinstance(value, str):
result_list.append(
f"{key}{separator}{value}"
)
elif isinstance(value, (list, tuple)):
result_list.extend(
f"{key}{separator}{v}"
for v in value
)

return result_list


def read_env_file(env_file: Path) -> Dict[str, str]:
Expand Down
Loading