-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy path50_swagger_generator_v3.0.py
216 lines (188 loc) · 7.4 KB
/
50_swagger_generator_v3.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# This program generates automatically the swagger.yaml for each data model
from github import Github
import yaml
def open_jsonref(fileUrl):
import jsonref
import requests
if fileUrl[0:4] == "http":
# es URL
pointer = requests.get(fileUrl)
return jsonref.loads(pointer.content.decode('utf-8'))
else:
# es file
file = open(fileUrl, "r")
return jsonref.loads(file.read())
def open_json(fileUrl):
import json
import requests
if fileUrl[0:4] == "http":
# es URL
pointer = requests.get(fileUrl)
return json.loads(pointer.content.decode('utf-8'))
else:
# es file
file = open(fileUrl, "r")
return json.loads(file.read())
def github_push_from_variable(contentVariable, repoName, fileTargetPath, message, globalUser, token):
from github import Github
g = Github(token)
repo = g.get_organization(globalUser).get_repo(repoName)
try:
file = repo.get_contents("/" + fileTargetPath)
update = True
except:
update = False
if update:
repo.update_file(fileTargetPath, message, contentVariable, file.sha)
else:
repo.create_file(fileTargetPath, message, contentVariable, "master")
def echo(concept, variable):
print("*** " + concept + " ***")
print(variable)
print("--- " + concept + " ---")
def read_github_file(fileName, repoName):
from github import Github
credentialsFile = "/home/fiware/credentials.json"
credentials = open_json(credentialsFile)
token = credentials["token"]
g = Github(token)
globalUser = credentials["globalUser"]
repository = g.get_organization(globalUser).get_repo(repoName)
file_content = repository.get_contents(fileName)
return file_content.decoded_content.decode()
def read_yaml(fileUrl):
import yaml
import requests
if fileUrl[0:4] == "http":
# es URL
pointer = requests.get(fileUrl)
return yaml.safe_load(pointer.content.decode('utf-8'))
else:
# es file
file = open(fileUrl, "r")
return yaml.safe_load.loads(file.read())
def github_delete_file_push(originalPath, repoName, message, globalUser, token):
from github import Github
g = Github(token)
repo = g.get_organization(globalUser).get_repo(repoName)
try:
destinyObject = repo.get_contents(originalPath)
destinyFileExist = True
except:
destinyFileExist = False
if destinyFileExist:
commit = repo.delete_file(originalPath, message, destinyObject.sha)
return [True, commit]
else:
return [False, "not found"]
#credentialsFile = "/home/aabella/transparentia/CLIENTES/EU/FIWARE/credentials.json"
credentialsFile = "/home/fiware/credentials.json"
credentials = open_json(credentialsFile)
token = credentials["token"]
globalUser = credentials["globalUser"]
g = Github(token)
configFile ="datamodels_to_publish.json"
dataModelsToPublish = open_jsonref(configFile)
echo("subject", dataModelsToPublish["subject"])
echo("dataModels", dataModelsToPublish["dataModels"])
echo("filter or no ", dataModelsToPublish["filterDataModels"])
echo("languages ", dataModelsToPublish["languages"])
repoName = dataModelsToPublish["subject"]
dataModels = dataModelsToPublish["dataModels"]
# CONSTANTS
modelYaml = "model.yaml"
swaggerHeader = """---
# Copyleft (c) 2022 Contributors to Smart Data Models initiative
# """
swaggerBody = """paths:
/ngsi-ld/v1/entities:
get:
description: "Retrieve a set of entities which matches a specific query from an NGSI-LD system"
parameters:
-
in: query
name: type
required: true
schema:
enum:
- Building
type: string
responses:
? "200"
:
content:
application/ld+json:
examples:
keyvalues:
summary: "Key-Values Pairs"
value:
-
$ref: "https://smart-data-models.github.io/dataModel.Building/Building/examples/example.json"
normalized:
summary: "Normalized NGSI-LD"
value:
-
$ref: "https://smart-data-models.github.io/dataModel.Building/Building/examples/example-normalized.jsonld"
description: OK
tags:
- ngsi-ld
tags:
-
description: "NGSI-LD Linked-data Format"
name: ngsi-ld"""
tab = " "
nl = chr(10)
rootModelUrl = "https://smart-data-models.github.io/"
outputFile = "test.yaml"
# CONSTANTS #
for dataModel in dataModels:
pathModelYaml = "/" + dataModel + "/" + modelYaml
try:
modelContent = read_github_file(pathModelYaml, repoName)
print(type(modelContent))
modelPresent = True
print(modelContent)
modelDict = yaml.safe_load(modelContent)
except:
modelPresent = False
if modelPresent:
swaggerContent = swaggerHeader + nl
swaggerContent += nl
swaggerContent += nl
swaggerContent += "components:" + nl
swaggerContent += 1 * tab + "schemas: " + nl
swaggerContent += 2 * tab + dataModel + ": " + nl
swaggerContent += 3 * tab + "$ref: " + chr(34) + rootModelUrl + repoName + "/" + dataModel + "/" + modelYaml + "#/" + dataModel + chr(34) + nl
swaggerContent += "info:" + nl
swaggerContent += 1 * tab + "description: |" + nl
if "description" in modelDict[dataModel]:
swaggerContent += 2 * tab + modelDict[dataModel]["description"] + nl
swaggerContent += 1 * tab + "title: " + dataModel + nl
swaggerContent += 1 * tab + "version: " + chr(34) + modelDict[dataModel]["x-version"] + chr(34) + nl
swaggerContent += "openapi: " + chr(34) + "3.0.0" + chr(34) + nl
swaggerContent += nl
customBody = swaggerBody.replace("- Building", "- " + dataModel).replace("/dataModel.Building/Building/", "/" + repoName + "/" + dataModel + "/")
for line in customBody.splitlines():
print(line)
swaggerContent += line + nl
print(swaggerContent)
#with open(outputFile, "w") as output:
# output.write(swaggerContent)
##############################################################
# remove the old swagger.yaml #
##############################################################
originalPath = dataModel + "/" + "swagger.yaml"
message = "updated swagger.yaml"
[ok, result] = github_delete_file_push(originalPath, repoName, message, globalUser, token)
##############################################################
# remove the old new_model.yaml #
##############################################################
originalPath = dataModel + "/" + "new_model.yaml"
message = "removed new_model.yaml"
[ok, result] = github_delete_file_push(originalPath, repoName, message, globalUser, token)
##############################################################
# push the new swagger.yaml #
##############################################################
contentVariable = swaggerContent
fileTargetPath = dataModel + "/" + "swagger.yaml"
github_push_from_variable(contentVariable, repoName, fileTargetPath, message, globalUser, token)