Files
flucto-heisskleber/heisskleber/console/sink.py
Felix Weiler 1d1e762912 Add verbose and pretty flags to hkcli. (#71)
* Add verbose and pretty flags to hkcli.

Adds testing for console sink.

* Add argparse help strings to hkcli.
2024-02-17 12:20:45 +08:00

38 lines
1.1 KiB
Python

import json
import time
from heisskleber.core.types import AsyncSink, Serializable, Sink
class ConsoleSink(Sink):
def __init__(self, pretty: bool = False, verbose: bool = False) -> None:
self.verbose = verbose
self.pretty = pretty
def send(self, data: dict[str, Serializable], topic: str) -> None:
verbose_topic = topic + ":\t" if self.verbose else ""
if self.pretty:
print(verbose_topic + json.dumps(data, indent=4))
else:
print(verbose_topic + str(data))
class AsyncConsoleSink(AsyncSink):
def __init__(self, pretty: bool = False, verbose: bool = False) -> None:
self.verbose = verbose
self.pretty = pretty
async def send(self, data: dict[str, Serializable], topic: str) -> None:
verbose_topic = topic + ":\t" if self.verbose else ""
if self.pretty:
print(verbose_topic + json.dumps(data, indent=4))
else:
print(verbose_topic + str(data))
if __name__ == "__main__":
sink = ConsoleSink()
while True:
sink.send({"test": "test"}, "test")
time.sleep(1)