Created
June 22, 2021 04:18
-
-
Save mgraczyk/2eaa19e6d2f09fa4cf3baa8a93fc6233 to your computer and use it in GitHub Desktop.
leveldb_cat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import sys | |
try: | |
import plyvel | |
except ImportError: | |
print("Install plyvel: python3 -m pip install plyvel") | |
sys.exit(1) | |
def usage(): | |
print(f"usage: {sys.argv[0]} [-h/--help] [--hex-keys] [--hex-values] path") | |
sys.exit(1) | |
if "--help" in sys.argv or "-h" in sys.argv or len(sys.argv) <= 1: | |
usage() | |
path = next((v for v in sys.argv[1:] if not v.startswith('-')), None) | |
if path is None: | |
usage() | |
identity = lambda x: str(x.decode()) | |
as_hex = lambda x: x.hex() | |
fk = as_hex if "--hex-keys" in sys.argv else identity | |
fv = as_hex if "--hex-values" in sys.argv else identity | |
db = plyvel.DB(path, create_if_missing=False) | |
try: | |
for key, value in db: | |
print(f"{fk(key)} -> {fv(value)}") | |
except UnicodeDecodeError: | |
print("Cannot show key or value, try --hex-keys or --hex-values", file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment