Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pysmartdatamodels.py #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions pysmartdatamodels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
41 changes: 38 additions & 3 deletions pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,10 +1120,45 @@ def ngsi_ld_example_generator(schema_url: str):
payload = open_jsonref(schema_url)
if payload == "": return False

# print(payload["allOf"])
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 = {}
# echo("payload", payload)
fullDict['id'] = {}
if "allOf" in payload:
for index in range(len(payload["allOf"])):
if "properties" in payload["allOf"][index]:
Expand Down Expand Up @@ -1159,7 +1194,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))

Expand Down Expand Up @@ -1504,4 +1539,4 @@ def generate_sql_schema(model_yaml: str) -> str:
result = sql_data_types + "\n" + table_create_statement
print(result)

return result
return result