From 6302131f7c882f38f29e2aed565fbacd465af170 Mon Sep 17 00:00:00 2001 From: Michaela Date: Tue, 2 Feb 2021 13:49:23 +1000 Subject: [PATCH] run black and make output unbuffered --- example.py | 6 ++++-- pyproject.toml | 2 +- sondehub/__init__.py | 17 +++++++++++------ sondehub/__main__.py | 29 ++++++++++++++++++++++++----- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/example.py b/example.py index 169c572..6a3ae1b 100644 --- a/example.py +++ b/example.py @@ -1,9 +1,11 @@ import sondehub + def on_message(message): print(message) -test = sondehub.Stream(sondes=["R3320848"], on_message=on_message) -#test = sondehub.Stream(on_message=on_message) + +#test = sondehub.Stream(sondes=["R3320848"], on_message=on_message) +test = sondehub.Stream(on_message=on_message) while 1: pass diff --git a/pyproject.toml b/pyproject.toml index 98797b1..1d4c0cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sondehub" -version = "0.1.4" +version = "0.1.5" description = "SDK to access SondeHub open data" authors = ["Michaela "] readme = "README.md" diff --git a/sondehub/__init__.py b/sondehub/__init__.py index 1ca9efc..8c04439 100644 --- a/sondehub/__init__.py +++ b/sondehub/__init__.py @@ -3,7 +3,8 @@ from urllib.parse import urlparse import http.client import json -class Stream(): + +class Stream: def __init__(self, sondes: list = ["#"], on_connect=None, on_message=None): self.mqttc = mqtt.Client(transport="websockets") self._sondes = sondes @@ -17,6 +18,7 @@ class Stream(): (result, mid) = self.mqttc.subscribe(f"sondes/{sonde}", 0) if result != mqtt.MQTT_ERR_SUCCESS: self.ws_connect() + def remove_sonde(self, sonde): self._sondes.remove(sonde) (result, mid) = self.mqttc.unsubscribe(f"sondes/{sonde}", 0) @@ -27,14 +29,16 @@ class Stream(): url = self.get_url() urlparts = urlparse(url) headers = { - "Host": "{0:s}".format(urlparts.netloc), + "Host": "{0:s}".format(urlparts.netloc), } - - self.mqttc.on_message = self._on_message # self.on_message + + self.mqttc.on_message = self._on_message # self.on_message self.mqttc.on_connect = self._on_connect self.mqttc.on_disconnect = self._on_disconnect - self.mqttc.ws_set_options(path="{}?{}".format(urlparts.path, urlparts.query), headers=headers) + self.mqttc.ws_set_options( + path="{}?{}".format(urlparts.path, urlparts.query), headers=headers + ) try: self.mqttc.tls_set() except ValueError: @@ -54,12 +58,13 @@ class Stream(): def _on_message(self, mqttc, obj, msg): if self.on_message: self.on_message(json.loads(msg.payload)) + def _on_connect(self, mqttc, obj, flags, rc): if mqtt.MQTT_ERR_SUCCESS != rc: self.ws_connect() if self.on_connect: self.on_connect() - + def _on_disconnect(self, client, userdata, rc): self.ws_connect() diff --git a/sondehub/__main__.py b/sondehub/__main__.py index 1560760..422f985 100644 --- a/sondehub/__main__.py +++ b/sondehub/__main__.py @@ -1,20 +1,39 @@ import json import sondehub import argparse +import os +import sys + +unbuffered = os.fdopen(sys.stdout.fileno(), "wb", 0) + def on_message(message): - print(json.dumps(message)) + unbuffered.write(json.dumps(message).encode()) + unbuffered.write("\n".encode()) + def main(): - parser = argparse.ArgumentParser(description='Sondehub CLI') - parser.add_argument('--serial', dest="sondes", default=["#"],nargs="*", help="Filter to sonde serial", type=str, action="append") + + parser = argparse.ArgumentParser(description="Sondehub CLI") + parser.add_argument( + "--serial", + dest="sondes", + default=["#"], + nargs="*", + help="Filter to sonde serial", + type=str, + action="append", + ) args = parser.parse_args() - if len(args.sondes) > 1: # we need to drop the default value if the user specifies sepcific sondes + if ( + len(args.sondes) > 1 + ): # we need to drop the default value if the user specifies sepcific sondes args.sondes = args.sondes[1:] sondes = [item for sublist in args.sondes for item in sublist] test = sondehub.Stream(on_message=on_message, sondes=sondes) while 1: pass + if __name__ == "__main__": - main() \ No newline at end of file + main()