-
Notifications
You must be signed in to change notification settings - Fork 60
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
Enhancement: print the source event, output data or event in JSON instead of plain text format #92
Comments
Local patchOn my test machine, I replaced line 2053
with
This prints one line for each event. My script reads each line and parses with Does Note on newlines: json.dumps() defaults to |
To parse the lines from matrix-commander I am using:
|
I've made some very basic implementation which uses the flag Maybe with a bit of work this can be turned into a PR https://github.com/HoboDev/matrix-commander/tree/feat-output-json |
@HoboDev That's unrelated. This issue is for the data structure returned verbatim from the server. A Matrix room is a list of json objects that follow the Client-Server API specification and matrix-commander has the json inside A client should use the Client-Server specification to parse matrix-commander output into anything. Instead your code parses the standard json into a non-standard json with limited information. This is a maintainance nightmare in the long term, because it ties the downloader with the parser, the Client-Server spec is changing, and anyone will request their fields of interest. @HoboDev Could you write a library to parse the Client-Server json (= wanted matrix-commander output) into a high-level data structure ( |
I've replaced the very same line you have changed to get json output and added some more code. What I did is more like a rough draft or proof of concept since I didn't know which specification to follow and didn't have time for going into details. I don't recall that the code I've added parses anything that must be the existing code. I'm just using some variables to get the info and put it into a structure. Unfortunately I've seen your issue after I was done with my draft. Would have been easier to start like this. I guess I could write a library but neither am I well versed in python nor do I have the enough free time for digging into this. So what is your recommendation? Just outputting all json data as it arrives? |
Another thing just came through my mind (because I don't really know the spec and didn't have time to look at the resulting data): Wouldn't it be more sensible to output the whole event? Let's talk flags...
Then we could have both. The spec would remain unchanged and the next program could sort it out. |
Thank you for your valuable input. You make valid points. I also see that there are several issues, all somewhat related. Let me think a bit about it, maybe the best is an incremental approach. Get the JSON event data published for listening, thereafter step-by-step add other event/JSON related output issues. |
@HoboDev wrote this in Issue #94
I moved it over here because I want all the discussion to be in the same place, here in Issue #92. |
A question I have: In JSON, should that be
If you run Is that the best? Same question will come up once received messages are done. Should it output 1 line with 1 JSON object for each message read, or just a total of 1 line with a JSON array of all N messages? What do you think? I was leaning towards just returning 1 single array object. The user can then use |
I'd follow the standard Unix practice of printing one object on one line.
The purpose of the Unix way is that a client can read an object as soon as it's downloaded. Otherwise
The Unix way also has the nice property of SIGPIPE: |
@opk12 thanks for the input and feedback. Anyone else, any more feedback? |
Option 1 , currently implemented, output is 1 line with 1 JSON array (with N objects in it)
Option 2 , currently not implemented, output is N lines each one with 1 JSON object; preferred by @opk12 as explained above
|
- new action workflow for Docker - changed `--output raw` to print 1 line PER data-item, i.e. N lines with 1 JSON object each (instead of 1 line in TOTAL with a JSON array on N JSON objects) - added code to cover --output for actions --listen and --tail - see Issue #92
I changed the code to follow @opk12 advice. Now the Option 2 is implemented. Now a separate line and a separate JSON object is printed for each data-set (e.g. message received, room-member found, etc.) Test it with E.g. the new code delivers this:
|
Have a look at the new release. Try something like @opk12 |
Wow, I was literally looking for how to do this and saw your release 4 hours ago. Thank you! |
@8go My use case is specifically about having spec-defined events:
In other words, my use case is specifically about printing the source event unaltered, as returned verbatim from the Matrix homeserver without any kind of manipulation. It's not about collecting the fields and making a new dict, it's about treating the matrix-nio event object as opaque and passing it on as-is to the client. |
Another point is guaranteed interoperability with other libraries in the Matrix ecosystem, i.e.
To be clear, experience shows that, if one needs to touch the Matrix events because an easy interface is not sufficient, learning the spec is the best favor they can do to themselves in the long term. |
OK, @opk12 , your point is valid. But ... Let's take an example: Observation: In summary,
How would you improve that? |
- added new option `--get-client-info` - `--output` has been added to all functions, all functions that have output can do the output in JSON now - see Issue #92
The description now reads:
|
Props for the big work and effort. I am following the releases and I see the pervasive code changes. I personally need a room's events ( This is just a 2-line change, |
How about a ? |
I kind of foresaw that some day something like this would happen (I didnt think it would happen so soon, hahaha), that is why it is not a binary on-off |
- implemented `--output json-spec` to sprint messages etc as spec, as provided by matrix-nio, without modifications - see Issue #92
@opk12 try out |
Thank you! Here are some points.
About
Maybe there is some use case that I am missing, but why printing matrix-nio objects at all in the
|
@opk12 Regarding:
Only reason is that
Yes, It would make sense to define/document
Are you suggesting that 4> Some find it convenient to shorten if a == A1 or a == A2 to the idiomatic if a in (A1, A2, A3) Thanks for pointing that out, easy to fix. Question: |
I suggest that it raises an error: another option was meant (it is a typo), or, to silence output, the usual shell redirection
As far as I know (I studied room events), there are no other kinds of events (the spec says "event" and "room event" interchangeably) or other cases for
Sure, it works and I migrated my room dump script! Another code simplification is possible. Rather than making the output inside an if, all outputs can be made in advance. Adding other For example, this
can become (
|
--listen
and--tail
print events in a human-readable format. I want the json, to parse it easily in my script. Could matrix-commander print the json by default or with a command-line flag?The text was updated successfully, but these errors were encountered: