33 lines
802 B
Python
33 lines
802 B
Python
#!/usr/bin/env python3
|
|
|
|
import cbor, json, os, sys
|
|
|
|
FORMATS = ["json", "cbor"]
|
|
|
|
DECODE = {
|
|
"json": lambda x: {"blocks": [[*i[:3], bytes.fromhex(i[3])] for i in json.loads(x.decode())["blocks"]]},
|
|
"cbor": cbor.loads
|
|
}
|
|
|
|
ENCODE = {
|
|
"json": lambda x: json.dumps({"blocks": [[*i[:3], i[3].hex()] for i in x["blocks"]]}).encode(),
|
|
"cbor": cbor.dumps
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
if "--help" in sys.argv or "-h" in sys.argv:
|
|
print("""Usage:
|
|
python3 convert.py <input_format> <output_format>
|
|
Takes data in stdin and writes data in stdout.
|
|
|
|
Formats: cbor, json
|
|
""")
|
|
exit()
|
|
|
|
input_format = sys.argv[1]
|
|
output_format = sys.argv[2]
|
|
|
|
stdin = os.fdopen(sys.stdin.fileno(), 'rb')
|
|
stdout = os.fdopen(sys.stdout.fileno(), 'wb')
|
|
stdout.write(ENCODE[output_format](DECODE[input_format](stdin.read())))
|