Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pysmartdatamodels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]([email protected]): Necessary extension for function update_broker() to allow updating nonexistent attribute into broker
116 changes: 76 additions & 40 deletions pysmartdatamodels/pysmartdatamodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
39 changes: 38 additions & 1 deletion pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down