Last active
December 22, 2022 15:05
-
-
Save kanna5/9cdfdaa514c2f7dcee77d69a25948bef to your computer and use it in GitHub Desktop.
Convert the output of `pacman -Qi` into JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 -S | |
""" | |
Convert the output of `pacman -Qi` into JSON | |
Usage: pacman -Qi | python3 -S pacman2j.py | |
""" | |
import json | |
import sys | |
current = {} | |
last_key = "" | |
for line in sys.stdin: | |
stripped = line.strip() | |
if not stripped: | |
continue | |
if line.startswith(" ") and last_key != "" and last_key in current: | |
# continuation | |
current[last_key] += "\n" + stripped | |
continue | |
parts = line.split(":", 1) | |
if len(parts) != 2: | |
continue | |
key, value = parts[0].strip(), parts[1].strip() | |
if key == "Name": | |
if len(current): | |
print(json.dumps(current, separators=(",", ":"))) | |
current = {} | |
current[key] = value | |
last_key = key | |
if len(current): | |
print(json.dumps(current, separators=(",", ":"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment