Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 8819ae1

Browse files
committed
Adds a plugin to dump raw RPC calls to a JSON file (#57)
1 parent 7204e2d commit 8819ae1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

etheno/jsonrpc.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import json
2+
from typing import TextIO, Union
3+
4+
from .etheno import EthenoPlugin
5+
6+
class JSONRPCExportPlugin(EthenoPlugin):
7+
def __init__(self, out_stream: Union[str, TextIO]):
8+
self._was_path = isinstance(out_stream, str)
9+
if self._was_path:
10+
self._output = open(out_stream, 'w', encoding='utf8')
11+
else:
12+
self._output = out_stream
13+
self._output.write('[')
14+
self._count = 0
15+
16+
def before_post(self, post_data):
17+
if self._count > 0:
18+
self._output.write(',')
19+
self._count += 1
20+
self._output.write('\n')
21+
json.dump(post_data, self._output)
22+
self._output.flush()
23+
24+
def finalize(self):
25+
if self._count:
26+
self._output.write('\n')
27+
self._output.write(']')
28+
self._output.flush()
29+
if self._was_path:
30+
self._output.close()
31+
if hasattr(self._output, 'name'):
32+
self.logger.info(f'Raw JSON RPC messages dumped to {self._output.name}')

0 commit comments

Comments
 (0)