datastore: apply feedback from Christian Decker

And fix missing blank line (makes flake8 a bit happier).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-08-19 13:29:29 +09:30
committed by Christian Decker
parent 0b9e6c9517
commit eac6b087da

View File

@@ -27,7 +27,8 @@ def normalize_key(key: Union[Sequence[str], str]) -> List[str]:
key = [key] key = [key]
return key return key
# We turn list into nul-separated hexbytes for storage
# We turn list into nul-separated hexbytes for storage (shelve needs all keys to be strings)
def key_to_hex(key: Sequence[str]) -> str: def key_to_hex(key: Sequence[str]) -> str:
return b'\0'.join([bytes(k, encoding='utf8') for k in key]).hex() return b'\0'.join([bytes(k, encoding='utf8') for k in key]).hex()
@@ -39,7 +40,7 @@ def hex_to_key(hexstr: str) -> List[str]:
def datastore_entry(key: Sequence[str], entry: Optional[Entry]): def datastore_entry(key: Sequence[str], entry: Optional[Entry]):
"""Return a dict representing the entry""" """Return a dict representing the entry"""
if not isinstance(key, list) and not isinstance(key, tuple): if isinstance(key, str):
key = [key] key = [key]
ret = {'key': key} ret = {'key': key}
@@ -68,7 +69,7 @@ optionally insisting it be {generation}"""
khex = key_to_hex(key) khex = key_to_hex(key)
if string is not None: if string is not None:
if hex is not None: if hex is not None:
raise RpcException("Cannot specify both string or hex") raise RpcException("Cannot specify both string and hex")
data = bytes(string, encoding="utf8") data = bytes(string, encoding="utf8")
elif hex is None: elif hex is None:
raise RpcException("Must specify string or hex") raise RpcException("Must specify string or hex")
@@ -159,14 +160,11 @@ def listdatastore(plugin, key=[]):
prev = None prev = None
for khex, e in sorted(plugin.datastore.items()): for khex, e in sorted(plugin.datastore.items()):
k = hex_to_key(khex) k = hex_to_key(khex)
print("... {}".format(k))
if k[:len(key)] != key: if k[:len(key)] != key:
print("{} not equal".format(k[:len(key)]))
continue continue
# Don't print sub-children # Don't print sub-children
if len(k) > len(key) + 1: if len(k) > len(key) + 1:
print("too long")
if prev is None or k[:len(key)+1] != prev: if prev is None or k[:len(key)+1] != prev:
prev = k[:len(key)+1] prev = k[:len(key)+1]
ret += [datastore_entry(prev, None)] ret += [datastore_entry(prev, None)]