Skip to content

Conversation

gctucker
Copy link
Collaborator

@gctucker gctucker commented Dec 8, 2023

Add kernelci.cli.echo_json() function as a convenience for the common
use-case where some JSON data needs to be dumped on stdout.  Update
all subcommands accordingly to use it.  This takes into account that
indent=0 should mean no formatting, rather than indentation with only
line returns and no spaces.

@JenySadadia
Copy link
Collaborator

LGTM. Please fix linter errors.

@gctucker
Copy link
Collaborator Author

Thanks! Please don't review PRs that are not ready for review though ;)

Add kernelci.cli.echo_json() function as a convenience for the common
use-case where some JSON data needs to be dumped on stdout.  Update
all subcommands accordingly to use it.  This takes into account that
indent=0 should mean no formatting, rather than indentation with only
line returns and no spaces.

Signed-off-by: Guillaume Tucker <[email protected]>
@gctucker gctucker marked this pull request as ready for review December 12, 2023 08:44
attributes = split_attributes(attributes)
nodes = api.get_nodes(attributes, offset, limit)
data = json.dumps(nodes, indent=indent)
data = json.dumps(nodes, indent=indent or None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

indent will be set to None by default if not provided.
Also, indent=indent or None would be evaluated to None if user has provided 0 as indent.

Suggested change
data = json.dumps(nodes, indent=indent or None)
data = json.dumps(nodes, indent=indent)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, I made this change on purpose as otherwise it prints the output formatted with newlines and no identation space when indent=0. So None is needed in this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's also explained in the git commit and the echo_json() docstring.

attributes = split_attributes(attributes)
users = api.get_users(attributes)
data = json.dumps(users, indent=indent)
data = json.dumps(users, indent=indent or None)
Copy link
Collaborator

@JenySadadia JenySadadia Dec 13, 2023

Choose a reason for hiding this comment

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

Same comment as above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course, I fixed this everywhere :) And then wrapped it up in a helper function.

@gctucker
Copy link
Collaborator Author

With the indent or None approach as per this PR:

$ ./kci node find name=checkout --api=staging -l1 --indent=0
[{"id": "64b52b7fb5b4a9b8c05e0874", "kind": "node", "name": "checkout", "path": ["checkout"], "group": null, "revision": {"tree": "kernelci", "url": "https://github.com/kernelci/linux.git", "branch": "staging-mainline", "commit": "273cecedf983df3b9cc3fba85b773be154852975", "describe": "staging-mainline-20230717.0", "version": {"version": 6, "patchlevel": 5, "sublevel": null, "extra": "-rc2-1-g273cecedf983", "name": null}, "patchset": null}, "parent": null, "state": "done", "result": null, "artifacts": {"tarball": "http://storage.staging.kernelci.org/api/linux-kernelci-staging-mainline-staging-mainline-20230717.0.tar.gz"}, "data": null, "created": "2023-07-17T11:52:31.913000", "updated": "2023-07-17T12:05:29.780000", "timeout": "2023-07-17T17:52:31.913000", "holdoff": "2023-07-17T12:03:49.144000", "owner": "staging.kernelci.org", "user_groups": []}]

With just indent left to 0 and not converted to None:

$ ./kci node find name=checkout --api=staging -l1 --indent=0
[
{
"id": "64b52b7fb5b4a9b8c05e0874",
"kind": "node",
"name": "checkout",
"path": [
"checkout"
],
"group": null,
"revision": {
"tree": "kernelci",
"url": "https://github.com/kernelci/linux.git",
"branch": "staging-mainline",
"commit": "273cecedf983df3b9cc3fba85b773be154852975",
"describe": "staging-mainline-20230717.0",
"version": {
"version": 6,
"patchlevel": 5,
"sublevel": null,
"extra": "-rc2-1-g273cecedf983",
"name": null
},
"patchset": null
},
"parent": null,
"state": "done",
"result": null,
"artifacts": {
"tarball": "http://storage.staging.kernelci.org/api/linux-kernelci-staging-mainline-staging-mainline-20230717.0.tar.gz"
},
"data": null,
"created": "2023-07-17T11:52:31.913000",
"updated": "2023-07-17T12:05:29.780000",
"timeout": "2023-07-17T17:52:31.913000",
"holdoff": "2023-07-17T12:03:49.144000",
"owner": "staging.kernelci.org",
"user_groups": []
}
]

@JenySadadia
Copy link
Collaborator

With just indent left to 0 and not converted to None:

$ ./kci node find name=checkout --api=staging -l1 --indent=0
[
{
"id": "64b52b7fb5b4a9b8c05e0874",
"kind": "node",
"name": "checkout",
"path": [
"checkout"
],
"group": null,
"revision": {
"tree": "kernelci",
"url": "https://github.com/kernelci/linux.git",
"branch": "staging-mainline",
"commit": "273cecedf983df3b9cc3fba85b773be154852975",
"describe": "staging-mainline-20230717.0",
"version": {
"version": 6,
"patchlevel": 5,
"sublevel": null,
"extra": "-rc2-1-g273cecedf983",
"name": null
},
"patchset": null
},
"parent": null,
"state": "done",
"result": null,
"artifacts": {
"tarball": "http://storage.staging.kernelci.org/api/linux-kernelci-staging-mainline-staging-mainline-20230717.0.tar.gz"
},
"data": null,
"created": "2023-07-17T11:52:31.913000",
"updated": "2023-07-17T12:05:29.780000",
"timeout": "2023-07-17T17:52:31.913000",
"holdoff": "2023-07-17T12:03:49.144000",
"owner": "staging.kernelci.org",
"user_groups": []
}
]

But isn't it what a user expects with indent=0? I mean this is a normal behavior of json.dumps with indent=0.

@gctucker
Copy link
Collaborator Author

It doesn't matter what json.dumps() does, what matters is that when a user wants no indentation there should be no formatting at all. If we want to have the option to actually output the JSON formatted with newlines and no indentation, then I would argue for a special indent value e.g. --indent=newline or something. But I really don't think that's what people would expect. No formatting is standard for storing JSON in a file or machine-to-machine data exchanges, and that's what indent=0 does here. Indentation is useful for human-readable data, and in this case nobody wants an output with less than one space as then it's not really readable.

@JenySadadia
Copy link
Collaborator

JenySadadia commented Dec 14, 2023

It doesn't matter what json.dumps() does, what matters is that when a user wants no indentation there should be no formatting at all. If we want to have the option to actually output the JSON formatted with newlines and no indentation, then I would argue for a special indent value e.g. --indent=newline or something. But I really don't think that's what people would expect. No formatting is standard for storing JSON in a file or machine-to-machine data exchanges, and that's what indent=0 does here. Indentation is useful for human-readable data, and in this case nobody wants an output with less than one space as then it's not really readable.

Makes sense. Thanks for clarifying.

@JenySadadia JenySadadia added this pull request to the merge queue Dec 14, 2023
Merged via the queue into kernelci:main with commit c4a3c1f Dec 14, 2023
@gctucker gctucker deleted the cli-echo-json branch December 14, 2023 08:48
@gctucker
Copy link
Collaborator Author

Thanks for the review :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants