From 0776b18e64b71513dd97be34359aa8ac23042f60 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Mon, 25 Mar 2024 10:44:12 +0100 Subject: [PATCH] Allow pyluos to receive multiple Json at once. It will be merged into one. --- pyluos/io/__init__.py | 13 ++++++++++++- setup.py | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pyluos/io/__init__.py b/pyluos/io/__init__.py index 27e3525..2cfd86a 100644 --- a/pyluos/io/__init__.py +++ b/pyluos/io/__init__.py @@ -1,5 +1,6 @@ import json import logging +from mergedeep import merge, Strategy class IOHandler(object): @@ -16,7 +17,17 @@ def is_ready(self): def read(self, trials=5): try: data = self.recv() - return self.loads(data) + if data is None: + return None + table = data.splitlines() + if len(table) > 1: + # load the Json of each substring + jsn = [self.loads(sub_data) for sub_data in table] + # merge all the Json data + result = merge({}, *jsn, strategy=Strategy.ADDITIVE) + return result + else: + return self.loads(data) except Exception as e: logging.getLogger(__name__).debug('Msg read failed: {}'.format(str(e))) if trials == 0: diff --git a/setup.py b/setup.py index ed80d06..e7bc68b 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,8 @@ 'crc8', 'ipython', 'requests', - 'simple_websocket_server==0.4.2' + 'simple_websocket_server==0.4.2', + 'mergedeep' ], extras_require={ 'tests': ['pytest', 'flake8'],