mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
pyln: add context manager to simpify filter use.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Changelog-Added: pyln: LightningRpc has new `reply_filter` context manager for reducing output of RPC commands.
This commit is contained in:
@@ -285,6 +285,7 @@ class UnixDomainSocketRpc(object):
|
|||||||
self.executor = executor
|
self.executor = executor
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self._notify = None
|
self._notify = None
|
||||||
|
self._filter = None
|
||||||
if caller_name is None:
|
if caller_name is None:
|
||||||
self.caller_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
self.caller_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
||||||
else:
|
else:
|
||||||
@@ -379,6 +380,8 @@ class UnixDomainSocketRpc(object):
|
|||||||
"id": this_id,
|
"id": this_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if filter is None:
|
||||||
|
filter = self._filter
|
||||||
if filter is not None:
|
if filter is not None:
|
||||||
request["filter"] = filter
|
request["filter"] = filter
|
||||||
|
|
||||||
@@ -438,6 +441,22 @@ class UnixDomainSocketRpc(object):
|
|||||||
yield
|
yield
|
||||||
self._notify = old
|
self._notify = old
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def reply_filter(self, filter):
|
||||||
|
"""Filter the fields returned from am RPC call (or more than one)..
|
||||||
|
|
||||||
|
This is a context manager and should be used like this:
|
||||||
|
|
||||||
|
```python
|
||||||
|
with rpc.reply_filter({"transactions": [{"outputs": [{"amount_msat": true, "type": true}]}]}):
|
||||||
|
rpc.listtransactions()
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
old = self._filter
|
||||||
|
self._filter = filter
|
||||||
|
yield
|
||||||
|
self._filter = old
|
||||||
|
|
||||||
|
|
||||||
class LightningRpc(UnixDomainSocketRpc):
|
class LightningRpc(UnixDomainSocketRpc):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -705,7 +705,8 @@ class PrettyPrintingLightningRpc(LightningRpc):
|
|||||||
"result": res
|
"result": res
|
||||||
}, indent=2))
|
}, indent=2))
|
||||||
|
|
||||||
if schemas and schemas[1] and not filter:
|
# FIXME: if filter set, just remove "required" from schemas?
|
||||||
|
if schemas and schemas[1] and filter is None and self._filter is None:
|
||||||
schemas[1].validate(res)
|
schemas[1].validate(res)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|||||||
@@ -2823,6 +2823,11 @@ def test_field_filter(node_factory, chainparams):
|
|||||||
dec = l1.rpc.call('decodepay', {'bolt11': inv['bolt11']}, filter={"currency": True})
|
dec = l1.rpc.call('decodepay', {'bolt11': inv['bolt11']}, filter={"currency": True})
|
||||||
assert dec == {"currency": chainparams['bip173_prefix']}
|
assert dec == {"currency": chainparams['bip173_prefix']}
|
||||||
|
|
||||||
|
# Use context manager:
|
||||||
|
with l1.rpc.reply_filter({"currency": True}):
|
||||||
|
dec = l1.rpc.decodepay(bolt11=inv['bolt11'])
|
||||||
|
assert dec == {"currency": chainparams['bip173_prefix']}
|
||||||
|
|
||||||
# Two fields
|
# Two fields
|
||||||
dec = l1.rpc.call('decodepay', {'bolt11': inv['bolt11']}, filter={"currency": True, "payment_hash": True})
|
dec = l1.rpc.call('decodepay', {'bolt11': inv['bolt11']}, filter={"currency": True, "payment_hash": True})
|
||||||
assert dec == {"currency": chainparams['bip173_prefix'],
|
assert dec == {"currency": chainparams['bip173_prefix'],
|
||||||
|
|||||||
Reference in New Issue
Block a user