Conversion tool

This commit is contained in:
Pascal Engélibert 2020-05-10 15:45:42 +02:00
parent 6385e11a76
commit 953094d751
1 changed files with 32 additions and 0 deletions

32
convert.py Normal file
View File

@ -0,0 +1,32 @@
#!/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())))