From 826de52bf8e376d0e0803fb2039ffabecd49ac84 Mon Sep 17 00:00:00 2001 From: fdrobnic <35068993+fdrobnic@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:50:38 +0100 Subject: [PATCH 1/5] Update pysmartdatamodels.py Added the ngsi_ld_example_generator_str() function to be able to generate fake examples before the model is published. --- .../pysmartdatamodels/pysmartdatamodels.py | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index 387c03c3a6..496365fae4 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -1120,6 +1120,26 @@ def ngsi_ld_example_generator(schema_url: str): payload = open_jsonref(schema_url) if payload == "": return False + return ngsi_ld_example_generator_str(payload, dataModel, subject) + +def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): + """It returns a fake normalized ngsi-ld format example based on the given json schema + Parameters: + schema: schema.json contents + dataModel: repo name + subject: model name + + Returns: + if the input parameter exists and the json schema is a valide json: + a fake normalized ngsi-ld format example stored in dictionary format + if there's any problem related to input parameter and json schema: + False + """ + + payload = json.loads(schema) + if payload == "": + return False + # print(payload["allOf"]) output = {} fullDict = {} @@ -1159,7 +1179,7 @@ def ngsi_ld_example_generator(schema_url: str): else: output = {**output, **{prop: parsedProperty}} # echo("output", output) - output["@context"] = [create_context(subject)] + output["@context"] = [create_context('dataModel.' + dataModel)] # print("======================") # print(json.dumps(output)) @@ -1504,4 +1524,4 @@ def generate_sql_schema(model_yaml: str) -> str: result = sql_data_types + "\n" + table_create_statement print(result) - return result \ No newline at end of file + return result From 8589c17578b781f6df8f7eb1cc3d9f568b78ca9c Mon Sep 17 00:00:00 2001 From: fdrobnic <35068993+fdrobnic@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:50:45 +0100 Subject: [PATCH 2/5] Update README.md Added the ngsi_ld_example_generator_str() function to be able to generate fake examples before the model is published. --- pysmartdatamodels/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pysmartdatamodels/README.md b/pysmartdatamodels/README.md index 487573e204..17e0b35a7a 100644 --- a/pysmartdatamodels/README.md +++ b/pysmartdatamodels/README.md @@ -356,6 +356,19 @@ print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUr if there's any problem related to input parameter and json schema: False +Function ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str) + + It returns a fake normalized ngsi-ld format example based on the given json schema + Parameters: + schema: schema.json contents + dataModel: repo name + subject: model name + + Returns: + if the input parameter exists and the json schema is a valide json: + a fake normalized ngsi-ld format example stored in dictionary format + if there's any problem related to input parameter and json schema: + False 18- Return a fake key value ngsi-ld format example. Function ngsi_ld_keyvalue_example_generator(schemaUrl) From b229bb3f14cb00d4fb981b4b3f734f952187346f Mon Sep 17 00:00:00 2001 From: fdrobnic <35068993+fdrobnic@users.noreply.github.com> Date: Wed, 24 Jan 2024 19:22:53 +0100 Subject: [PATCH 3/5] Update pysmartdatamodels.py Added the ngsi_ld_example_generator_str() function to be able to generate fake examples before the model is published. --- pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index 496365fae4..a9d3e74b5c 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -1144,6 +1144,7 @@ def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): output = {} fullDict = {} # echo("payload", payload) + fullDict['id'] = {} if "allOf" in payload: for index in range(len(payload["allOf"])): if "properties" in payload["allOf"][index]: From 8a116d8e20cbb8ec4686b995ca4025e3e064d639 Mon Sep 17 00:00:00 2001 From: fdrobnic <35068993+fdrobnic@users.noreply.github.com> Date: Wed, 8 May 2024 23:44:51 +0200 Subject: [PATCH 4/5] Update pysmartdatamodels.py --- .../pysmartdatamodels/pysmartdatamodels.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index a9d3e74b5c..4e6147a08e 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -1136,7 +1136,21 @@ def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): False """ - payload = json.loads(schema) + if dataModel == "" or subject == "": + return False + + tz = pytz.timezone("Europe/Madrid") + + try: + payload = json.loads(schema) + except ValueError: + output["result"] = False + output["cause"] = "Schema parameter value is not a valid json" + output["time"] = str(datetime.datetime.now(tz=tz)) + # output["parameters"] = {"schema_url: ": schema_url} + print(json.dumps(output)) + sys.exit() + if payload == "": return False From 09b3c2691aa48781aa941dc9e22266ec7bf04118 Mon Sep 17 00:00:00 2001 From: fdrobnic <35068993+fdrobnic@users.noreply.github.com> Date: Thu, 9 May 2024 00:11:17 +0200 Subject: [PATCH 5/5] Update pysmartdatamodels.py --- pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index 4e6147a08e..ff86eead56 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -1139,6 +1139,7 @@ def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): if dataModel == "" or subject == "": return False + output = {} tz = pytz.timezone("Europe/Madrid") try: @@ -1155,7 +1156,6 @@ def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): return False # print(payload["allOf"]) - output = {} fullDict = {} # echo("payload", payload) fullDict['id'] = {}