From 578928c06c8fdf78e671d9b08bc0afe3a2de68f6 Mon Sep 17 00:00:00 2001 From: Pierre Fayolle Date: Tue, 7 Nov 2023 16:32:07 +0100 Subject: [PATCH 1/3] Allow sending metric with null values When a metric value is prompted, the user can press Ctrl-D to indicate the metric should be sent with the null flag --- shell.py | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/shell.py b/shell.py index 00bac04..fc26878 100644 --- a/shell.py +++ b/shell.py @@ -5,7 +5,7 @@ import cmd2 import sparkplug_b_pb2 -from sparkplug_b import MetricDataType, addMetric, initDatasetMetric +from sparkplug_b import MetricDataType, addMetric, addNullMetric, initDatasetMetric import sp_helpers from sp_helpers import MsgType @@ -24,6 +24,20 @@ def str_to_int(string): return int(string, 16) return int(string) + +def str_to_bool(string): + """Convert a user string to a bool value + + Accepted values for True are: True, Yes, Y, 1 + The string is case-insensitive + All other values are considered equal to False + """ + string = string.lower() + if string in ["true", "yes", "y", "1"]: + return True + return False + + def get_bytearray_str(bytes_array): """String representation of a bytearray value.""" size = len(bytes_array) @@ -277,15 +291,17 @@ def query_yes_no(self, prompt): def prompt_user_simple_datatype(self, name, datatype): """Ask the user to give a value for simple datatypes.""" prompt = f"[{sp_helpers.datatype_to_str(datatype)}] {name}: " + value = None + try: + usr_input = self.read_input(prompt) + except EOFError: + return None if datatype in sp_helpers.boolean_value_types: - usr_input = self.select([(True, "True"), - (False, "False")], prompt) - value = usr_input + value = str_to_bool(usr_input) elif datatype in sp_helpers.int_value_types + sp_helpers.long_value_types: - usr_input = self.read_input(prompt) value = str_to_int(usr_input) elif datatype in sp_helpers.string_value_types: - value = self.read_input(prompt) + value = usr_input return value def forge_dataset_metric(self, payload, metric): @@ -301,7 +317,10 @@ def forge_dataset_metric(self, payload, metric): name = f"{metric.name}/{col}" value = self.prompt_user_simple_datatype(name, datatype) element = row.elements.add() - sp_helpers.set_typed_value(datatype, element, value) + if value is not None: + sp_helpers.set_typed_value(datatype, element, value) + else: + element.is_null = True new_row = self.query_yes_no("new row ?") def forge_payload_from_metric(self, payload, metric): @@ -312,8 +331,11 @@ def forge_payload_from_metric(self, payload, metric): if metric.datatype in simple_datatypes: value = self.prompt_user_simple_datatype(metric.name, metric.datatype) - addMetric(payload, metric.name, metric.alias, metric.datatype, - value) + if value is not None: + addMetric(payload, metric.name, metric.alias, metric.datatype, + value) + else: + addNullMetric(payload, metric.name, metric.alias, metric.datatype) elif metric.datatype == MetricDataType.DataSet: self.forge_dataset_metric(payload, metric) else: From 968ffce2358c7cb46d06e9363d73b01aaa911c52 Mon Sep 17 00:00:00 2001 From: Pierre Fayolle Date: Tue, 7 Nov 2023 17:16:39 +0100 Subject: [PATCH 2/3] compliance: set the version number for pylint and pylint-protobuf Trying to fix pylint issue on github runner --- .github/workflows/compliance.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/compliance.yml b/.github/workflows/compliance.yml index f2b7b78..714432b 100644 --- a/.github/workflows/compliance.yml +++ b/.github/workflows/compliance.yml @@ -28,8 +28,8 @@ jobs: working-directory: enki run: | pip install -r requirements.txt - pip install pylint - pip install pylint-protobuf + pip install pylint==2.17.5 + pip install pylint-protobuf==0.20.2 - name: Running pylint working-directory: enki From e83a796d11ab2bb0cacf5ab83be7646240cb74ba Mon Sep 17 00:00:00 2001 From: Pierre Fayolle Date: Wed, 8 Nov 2023 10:40:13 +0100 Subject: [PATCH 3/3] Moved pylint install lines out of compliance workflow Moved to a dedicated requirements_pylint.txt file used by the worflow --- .github/workflows/compliance.yml | 5 +---- requirements_pylint.txt | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 requirements_pylint.txt diff --git a/.github/workflows/compliance.yml b/.github/workflows/compliance.yml index 714432b..7d8599d 100644 --- a/.github/workflows/compliance.yml +++ b/.github/workflows/compliance.yml @@ -27,10 +27,7 @@ jobs: - name: Install python dependencies working-directory: enki run: | - pip install -r requirements.txt - pip install pylint==2.17.5 - pip install pylint-protobuf==0.20.2 - + pip install -r requirements_pylint.txt - name: Running pylint working-directory: enki run: | diff --git a/requirements_pylint.txt b/requirements_pylint.txt new file mode 100644 index 0000000..eba2826 --- /dev/null +++ b/requirements_pylint.txt @@ -0,0 +1,4 @@ +-r requirements.txt + +pylint==2.17.5 +pylint-protobuf==0.20.2