Skip to content

Conversation

@Venefilyn
Copy link
Member

By default the printer now outputs compact JSON which is more aligned
with what is expected of Cockpit. When using control it will now print
it compact by default.

Signed-off-by: Freya Gustavsson [email protected]

@Venefilyn Venefilyn added the no-test For doc/workflow changes, or experiments which don't need a full CI run, label Dec 18, 2025
@Venefilyn Venefilyn force-pushed the src/misc-print-compact branch from 13f534c to caed9bb Compare December 18, 2025 14:26
Copy link
Member

@jelly jelly left a comment

Choose a reason for hiding this comment

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

If you go to the effort add pretty printing why not add a flag for it for the cli? I assume you want to pretty print:

PYTHONPATH=src python3 -m cockpit.misc.print open echo channel=x

And you can't use jq because it emits Cockpit protocol frames.

"""Send a json message (built from **kwargs) on a channel"""
self.data(channel, json.dumps(kwargs, indent=2).encode() + b'\n')
# Cockpit expect JSON values to be compact, otherwise it won't parse it correctly
params: Any = {"separators": None, "indent": None}
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick/hint, we have JsonObject in Python

Copy link
Member Author

@Venefilyn Venefilyn Dec 18, 2025

Choose a reason for hiding this comment

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

Doesn't pass local checks due to params["key"] assignment below

Argument of type "tuple[Literal[','], Literal[':']]" cannot be assigned to parameter "value" of type "None" in function "__setitem__"
  "tuple[Literal[','], Literal[':']]" is not assignable to "None"PylancereportArgumentType

) -> None:
"""Send a json message (built from **kwargs) on a channel"""
self.data(channel, json.dumps(kwargs, indent=2).encode() + b'\n')
# Cockpit expect JSON values to be compact, otherwise it won't parse it correctly
Copy link
Member

Choose a reason for hiding this comment

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

Is that true?

PYTHONPATH=src python3 -m cockpit.misc.print open echo channel=x : data x "b'foo'" : done x | PYTHONPATH=src python3 -m cockpit.bridge

Works fine with indent is 40, we also don't set separators by default so they are (',', ': '). So you can simplify this by just making it pretty print more indented but that doesn't seem to change too much

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Improved the comment a bit

By default the printer now outputs compact JSON which is more aligned
with what is expected of Cockpit. When using `control` it will now print
it compact by default.

This can be changed by using `--pretty-json` argument.

Signed-off-by: Freya Gustavsson <[email protected]>
@Venefilyn Venefilyn force-pushed the src/misc-print-compact branch from caed9bb to 270ad58 Compare December 18, 2025 16:08
@Venefilyn
Copy link
Member Author

Added --pretty-json argument

📦[spytec@cockpit]~/projects/cockpit/cockpit% PYTHONPATH=src python3 -m cockpit.misc.print control authorize cookie="session26151766064575" response="Basic YWRtaW46Zm9vYmFy"              
51

{"command":"init","host":"localhost","version":1}
94

{"command":"authorize","cookie":"session26151766064575","response":"Basic YWRtaW46Zm9vYmFy"}
📦[spytec@cockpit]~/projects/cockpit/cockpit% PYTHONPATH=src python3 -m cockpit.misc.print --pretty-json control authorize cookie="session26151766064575" response="Basic YWRtaW46Zm9vYmFy"
70

{
    "command": "init",
    "host": "localhost",
    "version": 1
}
113

{
    "command": "authorize",
    "cookie": "session26151766064575",
    "response": "Basic YWRtaW46Zm9vYmFy"
}

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

Labels

documentation no-test For doc/workflow changes, or experiments which don't need a full CI run,

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants