Skip to content

Instantly share code, notes, and snippets.

@kanna5
Last active December 22, 2022 15:05
Show Gist options
  • Save kanna5/9cdfdaa514c2f7dcee77d69a25948bef to your computer and use it in GitHub Desktop.
Save kanna5/9cdfdaa514c2f7dcee77d69a25948bef to your computer and use it in GitHub Desktop.
Convert the output of `pacman -Qi` into JSON
#!/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