Open examon opened 6 years ago
That could be nice indeed. How do you imagine the mapping would go?
I'd be very interested in converting the Augeas tree to JSON. The tricky part is what exactly the JSON representation would look like - it would be easy enough to do a direct translation of the tree into JSON, i.e. each node in the tree would become an object
{ "label": <label>
"value": <value>
"children: [ <node>, <node>, ... ] }
I have been hoping to get to a more compact representation, something where each level in the tree becomes a JSON object; e.g., something like the tree at /files/etc/hosts/1
might become
{ "ipaddr": "127.0.0.1",
"canonical": "localhost",
"alias": [ "localhost.localdomain", "local.example.com" ] }
but such a representation would lose ordering (since the keys in a JSON object are not ordered) and it's not clear how you deal, e.g., with #comment
nodes interspersed with other nodes. It's also hard to produce output that is stable, i.e., where the form of the output is the same for a given file, regardless of what a file contains. As an example, you might turn the options from /etc/fstab
into an array of strings and that will work until one of those options has a value. At that point, each option would need to become an object.
Looking at your code, I see some other ways in which it could be sped up:
get_lenses
and process_augeas_data
run augtool, and have it load everything which can be very slowaug_to_xml
directly, which returns an xmlNode
, avoiding parsing the XML textfile
by doing a get /augeas/files/<file>/lens
Mod.lns
, you'd do match /augeas/files//path[../lens = "Mod.lns" or ../lens = "@Mod"]
augmatch
does it - it should be possible to emulate that technique in Python, though that might be overkill, and would require the not-yet-released Augeas 1.10 ;)@lutter
it would be easy enough to do a direct translation of the tree into JSON, i.e. each node in the tree would become an object
I think this would be good for my use case.
Basically, the code that I've sent you should be doing direct translation from xml to json. Then, we take that json and filter things that we need.
If augeas would support dump-json, then we could drop our xml2json code, which would be very nice.
Sounds good .. now I (or somebody else) just needs to find the time to implement this ;)
Btw, Is there API for dumping xml? I could not find anything. Right now, I'm doing the things I need with API, but then I need call augtool dump-xml
. I'm not sure if there is better way.
@lutter
Hi, I want to ask if it would be possible to add support for json output. It would help me and my team a lot if there was a dump-json command for augtool in addition to the dump-xml.
Right now, we are transforming XML output that augtool produces to the matching json. The problem is that this transformation takes relatively long time (because of fnmatch, see https://github.com/leapp-to/leapp-actors/blob/master/src/actors/common/augeas/augeas.py#L53).
Thank you