Conversion tool
This commit is contained in:
parent
6385e11a76
commit
953094d751
32
convert.py
Normal file
32
convert.py
Normal 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())))
|
Loading…
x
Reference in New Issue
Block a user