dotfiles/scripts/leveldb-dump

51 lines
1.5 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
2021-04-26 07:20:57 +00:00
2021-04-22 15:33:23 +00:00
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]]:
2021-04-26 07:20:57 +00:00
if encoding == "utf8":
try:
return b.decode("utf8")
except UnicodeDecodeError:
return list(b)
elif encoding == "base16":
return base64.b16encode(b).decode("ascii")
elif encoding == "base32":
return base64.b32encode(b).decode("ascii")
elif encoding == "base64":
return base64.b64encode(b).decode("ascii")
elif encoding == "base85":
return base64.b85encode(b).decode("ascii")
else:
assert False
2021-04-22 15:33:23 +00:00
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:
2021-04-26 07:20:57 +00:00
for key, value in iterator:
json.dump(
{
"key": bytes_to_json(key, key_encoding),
"value": bytes_to_json(value, value_encoding),
},
stdout,
)
stdout.write("\n")