dotfiles/scripts/leveldb-dump

50 lines
1.6 KiB
Text
Raw Normal View History

2021-04-22 15:33:23 +00:00
#!/usr/bin/env python3
import argparse
from pathlib import Path
from typing import Union
import plyvel
import json
from sys import stdout
import base64
parser = argparse.ArgumentParser()
encoding_names = ["utf8", "base16", "base32", "base64", "base85"]
parser.add_argument("--encoding", "-e", choices=encoding_names, default="utf8")
parser.add_argument("--key-encoding", choices=encoding_names, default=None)
parser.add_argument("--value-encoding", choices=encoding_names, default=None)
2021-04-22 15:33:23 +00:00
parser.add_argument("db_path", type=Path)
cli_args = parser.parse_args()
def bytes_to_json(b: bytes, encoding: str) -> Union[str, list[int]]:
if encoding == "utf8":
2021-04-22 15:33:23 +00:00
try:
return b.decode("utf8")
except UnicodeDecodeError:
return list(b)
elif encoding == "base16":
2021-04-22 15:33:23 +00:00
return base64.b16encode(b).decode("ascii")
elif encoding == "base32":
2021-04-22 15:33:23 +00:00
return base64.b32encode(b).decode("ascii")
elif encoding == "base64":
2021-04-22 15:33:23 +00:00
return base64.b64encode(b).decode("ascii")
elif encoding == "base85":
2021-04-22 15:33:23 +00:00
return base64.b85encode(b).decode("ascii")
else:
assert False
key_encoding: str = cli_args.key_encoding or cli_args.encoding
value_encoding: str = cli_args.value_encoding or cli_args.encoding
2021-04-22 15:33:23 +00:00
db = plyvel.DB(str(cli_args.db_path), create_if_missing=False)
with db.iterator() as iterator:
for key, value in iterator:
json.dump(
{
"key": bytes_to_json(key, key_encoding),
"value": bytes_to_json(value, value_encoding),
},
stdout,
)
2021-04-22 15:33:23 +00:00
stdout.write("\n")