diff --git a/pysmartdatamodels/README.md b/pysmartdatamodels/README.md index 81d4f93efb..f161348a94 100644 --- a/pysmartdatamodels/README.md +++ b/pysmartdatamodels/README.md @@ -200,6 +200,10 @@ print(sdm.validate_dcat_ap_distribution_sdm(content_DCAT)) print("25:") print(sdm.subject_for_datamodel(dataModel)) +# Return a fake normalized ngsi-ld format example based on the given json schema (passed as a string) +print("26 : ") +print(sdm.ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str)) + ``` ## Functions available include: @@ -439,7 +443,6 @@ print(sdm.subject_for_datamodel(dataModel)) 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) It returns a fake key value ngsi-ld format example based on the given json schema @@ -578,6 +581,20 @@ print(sdm.subject_for_datamodel(dataModel)) Usually only one element in the array isa returned because there are few clashes in data model names False if no subject is found +26- Return a fake normalized ngsi-ld format example. 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 + ## Pending features (glad to receive contributions to them) A.- Function to allow submission of improvements (i.e. missing recommended units or model) and comments to the different data models. Currently, you can do it searching for your data model here @@ -592,6 +609,6 @@ if you want to suggest other functions/ needs please let us know at info@smartda Special thanks to the following contributors: -- [fdrobnic](https://github.com/fdrobnic): Changes for porting to Windows +- [fdrobnic](https://github.com/fdrobnic): Changes for porting to Windows and a function for generating a fake example from a non-published schema - [Antonio Jara](https://twitter.com/Antonio_Jara): New function for inserting data into broker - [María José Bernal](mj.bernal@libelium.com): Necessary extension for function update_broker() to allow updating nonexistent attribute into broker diff --git a/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels.py index 2ce51e97e2..dd02f16297 100644 --- a/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels.py @@ -843,46 +843,7 @@ def ngsi_ld_example_generator(schema_url: str): payload = open_jsonref(schema_url) if payload == "": return False - output = {} - fullDict = {} - - # Parse the "allOf", "anyOf", "oneOf" structure - if "allOf" in payload: - for index in range(len(payload["allOf"])): - if "properties" in payload["allOf"][index]: - fullDict = {**fullDict, **payload["allOf"][index]["properties"]} - else: - fullDict = {**fullDict, **payload["allOf"][index]} - elif "anyOf" in payload: - for index in range(len(payload["anyOf"])): - if "properties" in payload["anyOf"][index]: - fullDict = {**fullDict, **payload["anyOf"][index]["properties"]} - else: - fullDict = {**fullDict, **payload["anyOf"][index]} - elif "oneOf" in payload: - for index in range(len(payload["oneOf"])): - if "properties" in payload["oneOf"][index]: - fullDict = {**fullDict, **payload["oneOf"][index]["properties"]} - else: - fullDict = {**fullDict, **payload["oneOf"][index]} - else: - fullDict = payload["properties"].copy() - - for prop in fullDict: - - parsedProperty = parse_property2ngsild_example({prop: fullDict[prop]}, dataModel, 0) - - # id and type should be key-value format in ngsild format - if prop in ["id"]: - output = {**output, **parsedProperty} - elif prop in ["type"]: - output = {**output, **{prop: parsedProperty}} - else: - output = {**output, **{prop: parsedProperty}} - - output["@context"] = [create_context(subject)] - - return output + return ngsi_ld_example_generator_str(payload, dataModel, subject) # 18 @@ -1613,3 +1574,78 @@ def subject_for_datamodel(datamodel): else: return subjects + +# 26 +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 + """ + + if dataModel == "" or subject == "": + return False + + output = {} + 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 + + fullDict = {} + fullDict['id'] = {} + + # Parse the "allOf", "anyOf", "oneOf" structure + if "allOf" in payload: + for index in range(len(payload["allOf"])): + if "properties" in payload["allOf"][index]: + fullDict = {**fullDict, **payload["allOf"][index]["properties"]} + else: + fullDict = {**fullDict, **payload["allOf"][index]} + elif "anyOf" in payload: + for index in range(len(payload["anyOf"])): + if "properties" in payload["anyOf"][index]: + fullDict = {**fullDict, **payload["anyOf"][index]["properties"]} + else: + fullDict = {**fullDict, **payload["anyOf"][index]} + elif "oneOf" in payload: + for index in range(len(payload["oneOf"])): + if "properties" in payload["oneOf"][index]: + fullDict = {**fullDict, **payload["oneOf"][index]["properties"]} + else: + fullDict = {**fullDict, **payload["oneOf"][index]} + else: + fullDict = payload["properties"].copy() + + for prop in fullDict: + + parsedProperty = parse_property2ngsild_example({prop: fullDict[prop]}, dataModel, 0) + + # id and type should be key-value format in ngsild format + if prop in ["id"]: + output = {**output, **parsedProperty} + elif prop in ["type"]: + output = {**output, **{prop: parsedProperty}} + else: + output = {**output, **{prop: parsedProperty}} + + output["@context"] = [create_context('dataModel.' + dataModel)] + # output["@context"] = [create_context(subject)] + + return output diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index 2ba67859cb..72fe53ea46 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -842,8 +842,44 @@ 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 + """ + + if dataModel == "" or subject == "": + return False + output = {} + 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 + + # print(payload["allOf"]) fullDict = {} + fullDict['id'] = {} # Parse the "allOf", "anyOf", "oneOf" structure if "allOf" in payload: @@ -879,7 +915,8 @@ def ngsi_ld_example_generator(schema_url: str): else: output = {**output, **{prop: parsedProperty}} - output["@context"] = [create_context(subject)] + output["@context"] = [create_context('dataModel.' + dataModel)] + # output["@context"] = [create_context(subject)] return output