-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
439 lines (392 loc) · 15.6 KB
/
app.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# app.py
import base64
import logging
import os
from io import BytesIO
from typing import Any, List, Optional
import uvicorn
import yaml
from fastapi import Body, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import AnyUrl, BaseModel, Field
from rdflib import URIRef
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.responses import HTMLResponse
from starlette_wtf import CSRFProtectMiddleware, csrf_protect
import forms
import maptomethod
import settings
setting = settings.Setting()
config_name = os.environ.get("APP_MODE") or "development"
middleware = [
Middleware(
SessionMiddleware,
secret_key=os.environ.get("APP_SECRET", "changemeNOW"),
),
# Middleware(CSRFProtectMiddleware, csrf_secret=os.environ.get('APP_SECRET','changemeNOW')),
Middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
),
Middleware(
uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware, trusted_hosts="*"
),
]
app = FastAPI(
title=setting.name,
description=setting.desc,
version=setting.version,
contact={
"name": setting.contact_name,
"url": setting.org_site,
"email": setting.admin_email,
},
license_info={
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
},
openapi_url=setting.openapi_url,
# openapi_tags=tags_metadata,
docs_url=setting.docs_url,
redoc_url=None,
swagger_ui_parameters={"syntaxHighlight": False},
# swagger_favicon_url="/static/resources/favicon.svg",
servers=[
{"url": setting.server, "description": "Production environment"},
],
# root_path="/api/v1",
root_path_in_servers=False,
middleware=middleware,
)
app.mount("/static/", StaticFiles(directory="static", html=True), name="static")
templates = Jinja2Templates(directory="templates")
print(os.environ.get("APP_MODE", "production"))
if os.environ.get("APP_MODE", "production") == "development":
print("fetching methods form MSEO repo")
app.methods_dict = {
"DIN_EN_ISO_527": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl"
}
else:
app.methods_dict = maptomethod.get_methods()
# flash integration flike flask flash
def flash(request: Request, message: Any, category: str = "info") -> None:
if "_messages" not in request.session:
request.session["_messages"] = []
request.session["_messages"].append({"message": message, "category": category})
def get_flashed_messages(request: Request):
return request.session.pop("_messages") if "_messages" in request.session else []
templates.env.globals["get_flashed_messages"] = get_flashed_messages
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def index(request: Request):
start_form = await forms.StartForm.from_formdata(request)
start_form.method_sel.choices = [(v, k) for k, v in app.methods_dict.items()]
# request.session.clear()
return templates.TemplateResponse(
"index.html",
{
"request": request,
"start_form": start_form,
"mapping_form": "",
"result": "",
"setting": setting,
},
)
@app.post("/create_mapper", response_class=HTMLResponse, include_in_schema=False)
async def create_mapper(request: Request):
# print(request.headers)
authorization = request.headers.get("Authorization", None)
# print(f"create mapper authorization: {authorization}")
if authorization:
logging.debug("got authorization header")
logging.debug(await request.body())
start_form = await forms.StartForm.from_formdata(request)
logging.debug(start_form.data)
start_form.method_sel.choices = [(v, k) for k, v in app.methods_dict.items()]
mapping_form = ""
logging.info("create mapping")
if await start_form.validate_on_submit():
if not start_form.data_url.data:
start_form.data_url.data = start_form.data_url.render_kw["placeholder"]
flash(
request,
"URL Data File empty: using placeholder value for demonstration",
"info",
)
data_url = start_form.data_url.data
request.session["data_url"] = data_url
# if url to method graph provided use it if not use select widget
if start_form.method_url.data:
method_url = start_form.method_url.data
else:
method_url = start_form.method_sel.data
request.session["method_url"] = method_url
request.session["use_template_rowwise"] = start_form.use_template_rowwise.data
# entrys from advanced form
mapping_subject_class_uris = (
start_form.advanced.data_subject_super_class_uris.data
)
request.session["mapping_subject_class_uris"] = mapping_subject_class_uris
mapping_predicate_uri = start_form.advanced.mapping_predicate_uri.data
request.session["mapping_predicate_uri"] = mapping_predicate_uri
mapping_object_class_uris = (
start_form.advanced.method_object_super_class_uris.data
)
request.session["mapping_object_class_uris"] = mapping_object_class_uris
try:
mapper = maptomethod.Mapper(
data_url=data_url,
method_url=method_url,
use_template_rowwise=request.session["use_template_rowwise"],
mapping_predicate_uri=URIRef(mapping_predicate_uri),
data_subject_super_class_uris=[
URIRef(uri) for uri in mapping_subject_class_uris
],
method_object_super_class_uris=[
URIRef(uri) for uri in mapping_object_class_uris
],
authorization=authorization,
)
flash(request, str(mapper), "info")
# flash(request, str(mapper.subjects), "info")
except Exception as err:
flash(request, str(err), "error")
# print(mapper.objects.keys())
# only named instances in the data can be mapped
else:
info_choices = [
(id, value["text"])
for id, value in mapper.subjects.items()
if "text" in value.keys()
]
info_choices.insert(0, (None, "None"))
select_forms = forms.get_select_entries(mapper.objects.keys(), info_choices)
mapping_form = await forms.MappingFormList.from_formdata(request)
mapping_form.assignments.entries = select_forms
request.session["auth"] = authorization
logging.debug("session: {}".format(request.session))
return templates.TemplateResponse(
"index.html",
{
"request": request,
"start_form": start_form,
"mapping_form": mapping_form,
"result": "",
"setting": setting,
},
)
@app.post("/map", response_class=HTMLResponse, include_in_schema=False)
async def map(request: Request):
authorization = request.headers.get("Authorization", None) or request.session.get(
"auth", None
)
if authorization:
logging.debug("got authorization header")
logging.debug("get map")
logging.debug("session: {}".format(request.session))
# logging.debug(request.data)
formdata = await request.form()
data_url = request.session.get("data_url", None)
method_url = request.session.get("method_url", None)
method_sel = request.session.get("method_url", None)
subjects = request.session.get("subjects", None)
objects = request.session.get("objects", None)
use_template_rowwise = request.session.get("use_template_rowwise", False)
mapping_subject_class_uris = request.session.get("mapping_subject_class_uris", None)
mapping_predicate_uri = request.session.get("mapping_predicate_uri", None)
mapping_object_class_uris = request.session.get("mapping_object_class_uris", None)
start_form = forms.StartForm(
request, data_url=data_url, method_url=method_url, method_sel=method_sel
)
start_form.method_sel.choices = [(v, k) for k, v in app.methods_dict.items()]
# entrys from advanced form
result = ""
filename = ""
result_string = ""
payload = ""
select_dict = dict(formdata)
maplist = [(k, v) for k, v in select_dict.items() if v != "None"]
logging.info("Creating mapping file for mapping list: {}".format(maplist))
request.session["maplist"] = maplist
logging.debug("subjects: {}".format(subjects))
logging.debug("objects: {}".format(objects))
with maptomethod.Mapper(
data_url=data_url,
method_url=method_url,
use_template_rowwise=use_template_rowwise,
mapping_predicate_uri=URIRef(mapping_predicate_uri),
data_subject_super_class_uris=[
URIRef(uri) for uri in mapping_subject_class_uris
],
method_object_super_class_uris=[
URIRef(uri) for uri in mapping_object_class_uris
],
maplist=maplist,
subjects=subjects,
objects=objects,
authorization=authorization,
) as mapper:
result = mapper.to_pretty_yaml()
filename = result["filename"]
result_string = result["filedata"]
# print(type(result_string))
# print(result_string)
b64 = base64.b64encode(result_string.encode())
payload = b64.decode()
return templates.TemplateResponse(
"index.html",
{
"request": request,
"start_form": start_form,
"mapping_form": "",
"filename": filename,
"payload": payload,
"result": result_string,
"setting": setting,
},
)
class QueryRequest(BaseModel):
url: AnyUrl = Field(
"", title="Graph Url", description="Url to the sematic dataset to query"
)
entity_classes: List = Field(
[],
title="Class List",
description="List of super classes to query for",
)
class Config:
json_schema_extra = {
"example": {
"url": "https://github.com/Mat-O-Lab/CSVToCSVW/raw/main/examples/example-metadata.json",
"entity_classes": [
"http://www.w3.org/ns/csvw#Column",
"http://www.w3.org/ns/oa#Annotation",
],
},
}
@app.post("/api/entities")
def query_entities(request: QueryRequest, req: Request):
authorization = req.headers.get("Authorization", None)
# translate urls in entity_classes list to URIRef objects
request.entity_classes = [URIRef(str(url)) for url in request.entity_classes]
return maptomethod.query_entities(
str(request.url), request.entity_classes, authorization
)
class MappingRequest(BaseModel):
data_url: AnyUrl = Field(
"", title="Datas Graph Url", description="Url to data metadata to use."
)
method_url: AnyUrl = Field(
"", title="Method Graph Url", description="Url to knowledge graph to use."
)
use_template_rowwise: Optional[bool] = Field(
False,
title="Use Template Rowwise",
description="If to duplicate the Method Graph for each row.",
omit_default=True,
)
data_super_classes: List = Field(
[maptomethod.OA.Annotation, maptomethod.CSVW.Column],
title="Subject Super Classes",
description="List of subject super classes to query for mapping partners in data.",
)
predicate: AnyUrl = Field(
maptomethod.ContentToBearingRelation,
title="predicate property",
description="Predicate Property to connect data to method entities.",
)
method_super_classes: List = Field(
[maptomethod.InformtionContentEntity, maptomethod.TemporalRegionClass],
title="Object Super Classes",
description="List of object super classes to query for mapping partners in method graph.",
)
map: dict = Field(
title="Map Dict",
description="Dict of with key as individual name of objects in knowledge graph and ids of indivuals in data metadata as values to create mapping rules for.",
)
class Config:
json_schema_extra = {
"example": {
"data_url": "https://github.com/Mat-O-Lab/CSVToCSVW/raw/main/examples/example-metadata.json",
"method_url": "https://github.com/Mat-O-Lab/MSEO/raw/main/methods/DIN_EN_ISO_527-3.drawio.ttl",
"data_super_classes": [
maptomethod.OA.Annotation,
maptomethod.CSVW.Column,
],
"predicate": maptomethod.ContentToBearingRelation,
"method_super_classes": [
maptomethod.InformtionContentEntity,
maptomethod.TemporalRegionClass,
],
"map": {
"SpecimenName": "AktuelleProbe0",
"StrainMeasurementInformation": "table-1-Dehnung",
},
},
}
class YAMLResponse(StreamingResponse):
media_type = "application/x-yaml"
@app.post("/api/mapping", response_class=YAMLResponse)
def mapping(request: MappingRequest, req: Request) -> StreamingResponse:
authorization = req.headers.get("Authorization", None)
if authorization:
logging.debug("got authorization header")
try:
result = maptomethod.Mapper(
str(request.data_url),
str(request.method_url),
method_object_super_class_uris=[
URIRef(str(uri)) for uri in request.method_super_classes
],
mapping_predicate_uri=URIRef(str(request.predicate)),
use_template_rowwise=request.use_template_rowwise,
data_subject_super_class_uris=[
URIRef(str(uri)) for uri in request.data_super_classes
],
maplist=request.map.items(),
authorization=authorization,
).to_pretty_yaml()
except Exception as err:
raise HTTPException(status_code=500, detail=str(err))
data_bytes = BytesIO(result["filedata"].encode())
filename = result["filename"]
headers = {
"Content-Disposition": "attachment; filename={}".format(filename),
"Access-Control-Expose-Headers": "Content-Disposition",
}
media_type = "application/x-yaml"
return StreamingResponse(content=data_bytes, media_type=media_type, headers=headers)
@app.get("/info", response_model=settings.Setting)
async def info() -> dict:
return setting
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app_mode = os.environ.get("APP_MODE") or "production"
with open("log_config.yml") as f:
config = yaml.load(f)
logging.config.dictConfig(config)
if app_mode == "development":
reload = True
access_log = True
config["root"]["level"] = "DEBUG"
else:
reload = False
access_log = False
config["root"]["level"] = "ERROR"
print("Log Level Set To {}".format(config["root"]["level"]))
uvicorn.run(
"app:app",
host="0.0.0.0",
port=port,
reload=reload,
access_log=access_log,
log_config=config,
)
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(content={"message": exc.detail}, status_code=exc.status_code)