mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +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.logger = logger
|
||||
self._notify = None
|
||||
self._filter = None
|
||||
if caller_name is None:
|
||||
self.caller_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
||||
else:
|
||||
@@ -379,6 +380,8 @@ class UnixDomainSocketRpc(object):
|
||||
"id": this_id,
|
||||
}
|
||||
|
||||
if filter is None:
|
||||
filter = self._filter
|
||||
if filter is not None:
|
||||
request["filter"] = filter
|
||||
|
||||
@@ -438,6 +441,22 @@ class UnixDomainSocketRpc(object):
|
||||
yield
|
||||
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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user