Skip to content
Snippets Groups Projects
Commit 4d5a0695 authored by Cornelia Michlits's avatar Cornelia Michlits
Browse files

add onto endpoints post, get, ..., fix get_uri json runtime error

parent b1b25d00
Branches
Tags
3 merge requests!129New module for citation as they occur multiple,!121Modified logging, modified logging level, modified flasgger endpoint,!102240 onto incl
Showing
with 130 additions and 1410 deletions
......@@ -23,7 +23,7 @@ HEALTHCHECK --interval=10s --timeout=5s --retries=12 CMD service_ready
EXPOSE $PORT_APP
COPY ./us-yml/*.yml ./
COPY ./onto/*.ttl ./onto/
COPY ./ontologies/* ./ontologies/
COPY ./*.py ./
ENTRYPOINT [ "python", "./pywsgi.py" ]
......
import os
import rdflib
from flask import Flask, request, jsonify
import logging
import py_eureka_client.eureka_client as eureka_client
from flasgger import Swagger
from flasgger.utils import swag_from
from flasgger import LazyString, LazyJSONEncoder
from list import list_units, get_uri
from list_concept import get_concept
from list import list_units, get_uri as list_get_uri
from validate import validator, stringmapper
from gevent.pywsgi import WSGIServer
from save import insert_mdb_concepts, insert_mdb_columns_concepts
from werkzeug.utils import secure_filename
from pathlib import Path
from onto_feat import search_ontologies, setup_ontology_dir, list_ontologies, ontology_exists, get_ontology, allowed_file
from logging.config import dictConfig
......@@ -92,7 +95,7 @@ def validate(unit):
def get_uri(uname):
logging.debug('endpoint get uri, uname=%s, body=%s', uname, request)
try:
res = get_uri(uname)
res = list_get_uri(uname)
logging.debug('get uri resulted in uri: %s', res)
return jsonify(res), 200
except Exception as e:
......@@ -143,7 +146,7 @@ def save_column_concept():
def get_concept(cname):
logging.debug('endpoint get concept, cname=%s, body=%s', cname, request)
try:
res = get_concept(cname)
res = search_ontologies(cname)
logging.debug('get concept resulted in concept: %s', res)
return jsonify(res), 200
except Exception as e:
......@@ -151,6 +154,38 @@ def get_concept(cname):
res = {"success": False, "message": str(e)}
return jsonify(res), 500
ONTOLOGIES_DIRECTORY = 'ontologies'
@app.route('/api/ontologies', methods=["POST"], endpoint='upload_onto')
@swag_from('ontologie.yml')
def post_ontologies():
if 'file' not in request.files:
return "no file", 500
file = request.files['file']
if file.filename == '':
return "no file selected", 500
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
if ontology_exists(Path(filename).stem):
return "ontology name already exists", 500
setup_ontology_dir()
file.save(os.path.join(ONTOLOGIES_DIRECTORY, filename))
logging.debug('created ontology: %s', filename)
return "created", 200
@app.route('/api/ontologies', methods=["GET"], endpoint='get_ontos')
@swag_from('ontologies.yml')
def get_ontologies():
print(list_ontologies())
return jsonify(list_ontologies())
@app.route('/api/ontologies/<name>', methods=["GET"], endpoint='get_onto')
@swag_from('ontologie.yml')
def get_ontologies(name):
ontology = get_ontology(name)
if ontology is None:
return "ontology does not exist", 404
return ontology
rest_server_port = int(os.getenv("PORT_APP"))
eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'),
......
......@@ -10,14 +10,14 @@ import rdflib
import re
g = rdflib.Graph()
g.namespace_manager.bind('om', 'http://www.ontology-of-units-of-measure.org/resource/om-2/')
g.parse('onto/om-2.ttl', format='turtle')
g.parse('ontologies/om-2.ttl', format='turtle')
om = rdflib.Namespace('http://www.ontology-of-units-of-measure.org/resource/om-2/')
rdf_schema = rdflib.Namespace('http://www.w3.org/2000/01/rdf-schema#')
f = rdflib.Graph()
f.namespace_manager.bind('qudt', 'http://qudt.org/2.1/vocab/unit')
f.parse('onto/VOCAB_QUDT-UNITS-ALL-v2.1.ttl', format='turtle')
f.parse('ontologies/VOCAB_QUDT-UNITS-ALL-v2.1.ttl', format='turtle')
#qudt = rdflib.Namespace('http://qudt.org/2.1/vocab/unit')
......@@ -50,6 +50,6 @@ def get_uri(name):
"""
qres = g.query(uri_query)
for row in qres:
return {"URI": row.uri}
return {"URI": str(row.uri)}
else:
return None
\ No newline at end of file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 9 11:25:52 2022
@author: Cornelia Michlits
"""
import rdflib
import re
import sys
g = rdflib.Graph()
g.namespace_manager.bind('iaq', 'https://w3id.org/digitalconstruction/IndoorAirQuality#')
g.parse('onto/iaq.nt', format='nt')
f = rdflib.Graph()
f.namespace_manager.bind('geom', 'http://data.ign.fr/def/geometrie')
f.parse('onto/def--geometrie.ttl', format='turtle')
h = rdflib.Graph()
h.namespace_manager.bind('op', 'http://environment.data.gov.au/def/op')
h.parse('onto/def--op.nt', format='nt')
def get_concept(string,limit=sys.maxsize, offset=0):
if bool(re.match('^[a-zA-Z0-9\-\\\s]+$',string)):
l_query = """
SELECT ?s ?p ?o
WHERE{
?s ?p ?o .
FILTER regex(str(?s),\""""+string+"""\","i")
}LIMIT """+str(limit)+""" OFFSET """+str(offset)
qres1 = g.query(l_query)
res = list()
for row in qres1:
res.append({"S-URI": str(row.s), "P": str(row.p), "O": str(row.o)})
qres2 = f.query(l_query)
for row in qres2:
res.append({"S-URI": str(row.s), "P": str(row.p), "O": str(row.o)})
return res
qres3 = h.query(l_query)
for row in qres3:
res.append({"S-URI": str(row.s), "P": str(row.p), "O": str(row.o)})
return res
else:
return None
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
import re
import glob
import sys
import os
from pathlib import Path
ALLOWED_EXTENSIONS = {'ttl', 'nt'}
ONTOLOGIES_DIRECTORY = 'ontologies'
ONTOLOGY_EXTENSIONS = {'.ttl': 'turtle', '.nt': 'nt'}
def search_ontologies(query, limit=sys.maxsize, offset=0):
if not bool(re.match('^[a-zA-Z0-9\-\\\s]+$', query)):
return None
ontology_files = glob.glob(ONTOLOGIES_DIRECTORY + "/*")
matches = []
for file in ontology_files:
format = ONTOLOGY_EXTENSIONS[Path(file).suffix]
g = rdflib.Graph()
g.parse(file, format=format)
l_query = """
SELECT ?s ?p ?o
WHERE{
?s ?p ?o .
FILTER regex(str(?s),\"""" + query + """\","i")
}LIMIT """ + str(limit) + """ OFFSET """ + str(offset)
qres1 = g.query(l_query)
for row in qres1:
matches.append({"S-URI": str(row.s), "P": str(row.p), "O": str(row.o)})
return matches
def setup_ontology_dir():
if not os.path.exists(ONTOLOGIES_DIRECTORY):
os.mkdir(ONTOLOGIES_DIRECTORY)
def list_ontologies():
setup_ontology_dir()
return list(map(lambda filename: Path(filename).stem, glob.glob(ONTOLOGIES_DIRECTORY + "/*")))
def ontology_exists(name):
setup_ontology_dir()
return name in list_ontologies()
def get_ontology(name):
setup_ontology_dir()
files = glob.glob(ONTOLOGIES_DIRECTORY + "/" + name + ".*")
if len(files) == 0:
return None
with open(files[0]) as f:
return f.read()
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
\ No newline at end of file
......@@ -8,7 +8,7 @@ parameters:
- in: "path"
type: "string"
name: "uname"
description: "to-do description"
description: "Name of a unit in OM"
required: true
responses:
200:
......
summary: "Get ontology"
description: "This is a simple API for getting ontologies (.nt, .ttl files) used in DB-Repo."
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: file
required: false
in: formData
type: file
responses:
200:
description: "OK"
201:
description: "Created"
405:
description: "Invalid input"
\ No newline at end of file
summary: "Get list of ontologies"
description: "This is a simple API for getting the list of all ontologies contained in DB-Repo."
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "OK"
405:
description: "Invalid input"
\ No newline at end of file
......@@ -7,7 +7,7 @@ produces:
parameters:
- in: "body"
name: "body"
description: "to-do description"
description: "JSON containing cdbid (database ID), cid (column ID), tid (table ID), uri (URI of the concept). Make sure the concept already exists in the MDB. "
required: true
schema:
type: "object"
......
......@@ -7,7 +7,7 @@ produces:
parameters:
- in: "body"
name: "body"
description: "to-do description"
description: "Json body containing name and uri, cf. example below."
required: true
schema:
type: "object"
......
......@@ -8,7 +8,7 @@ Created on Thu Dec 2 23:31:39 2021
import rdflib
g = rdflib.Graph()
g.namespace_manager.bind('om', 'http://www.ontology-of-units-of-measure.org/resource/om-2/')
g.parse('onto/om-2.ttl', format='turtle')
g.parse('ontologies/om-2.ttl', format='turtle')
om = rdflib.Namespace('http://www.ontology-of-units-of-measure.org/resource/om-2/')
_exhausted = object()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment