mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-14 18:10:40 +00:00
Add prototype
This commit is contained in:
23
vre_manager/Dockerfile
Normal file
23
vre_manager/Dockerfile
Normal file
@ -0,0 +1,23 @@
|
||||
FROM python:3-slim
|
||||
|
||||
MAINTAINER Patrick Jentsch <p.jentsch@uni-bielefeld.de>
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
|
||||
EXPOSE 5000
|
||||
|
||||
RUN pip install \
|
||||
connexion[swagger-ui] \
|
||||
flask-cors
|
||||
|
||||
RUN mkdir -p \
|
||||
/root/vre_manager/request_handlers \
|
||||
/root/vre_manager/swagger
|
||||
|
||||
WORKDIR /root/vre_manager
|
||||
|
||||
COPY vre_manager.py /root/vre_manager
|
||||
COPY swagger/vre_manager.yml /root/vre_manager/swagger
|
||||
COPY request_handlers/jobs.py /root/vre_manager/request_handlers
|
||||
|
||||
ENTRYPOINT ["python", "/root/vre_manager/vre_manager.py"]
|
114
vre_manager/request_handlers/jobs.py
Normal file
114
vre_manager/request_handlers/jobs.py
Normal file
@ -0,0 +1,114 @@
|
||||
import flask
|
||||
import os
|
||||
|
||||
|
||||
UPLOAD_DIRECTORY = os.path.abspath("/root/vre_files/jobs")
|
||||
|
||||
|
||||
job_counter = 0
|
||||
jobs = []
|
||||
|
||||
|
||||
def create_job(file, language, name, service, user):
|
||||
global job_counter
|
||||
|
||||
if (service == "nlp" and file.content_type != "text/plain"):
|
||||
flask.abort(415)
|
||||
if (service == "ocr" and file.content_type != "application/pdf"):
|
||||
flask.abort(415)
|
||||
|
||||
job = {}
|
||||
job["file"] = file.filename
|
||||
job["id"] = "job-" + str(job_counter + 1)
|
||||
job["language"] = language
|
||||
job["name"] = name
|
||||
job["report"] = ""
|
||||
job["service"] = service
|
||||
job["status"] = "queued"
|
||||
job["user"] = user
|
||||
|
||||
job_directory = os.path.join(UPLOAD_DIRECTORY, job["id"])
|
||||
if not os.path.exists(job_directory):
|
||||
os.makedirs(job_directory)
|
||||
file.save(os.path.join(job_directory, file.filename))
|
||||
|
||||
jobs.append(job)
|
||||
job_counter += 1
|
||||
|
||||
return flask.make_response(flask.jsonify(job), 201)
|
||||
|
||||
|
||||
def delete_job(id):
|
||||
job_directory = os.path.join(UPLOAD_DIRECTORY, id)
|
||||
|
||||
for job in jobs:
|
||||
if job["id"] == id:
|
||||
if job["status"] != "queued":
|
||||
flask.abort(423)
|
||||
os.remove(os.path.join(job_directory, job["file"]))
|
||||
os.rmdir(job_directory)
|
||||
job["file"] = None
|
||||
job["language"] = None
|
||||
job["name"] = None
|
||||
job["report"] = None
|
||||
job["service"] = None
|
||||
job["status"] = "deleted"
|
||||
return flask.make_response("", 204)
|
||||
|
||||
flask.abort(404)
|
||||
|
||||
|
||||
def get_job(id):
|
||||
for job in jobs:
|
||||
if job["id"] == id:
|
||||
return job
|
||||
|
||||
flask.abort(404)
|
||||
|
||||
|
||||
def get_jobs(name = None, service = None, status = None, user = None):
|
||||
filtered_jobs = []
|
||||
|
||||
for job in jobs:
|
||||
if name and job["name"] != name:
|
||||
continue
|
||||
if service and job["service"] != service:
|
||||
continue
|
||||
if status and job["status"] != status:
|
||||
continue
|
||||
if user and job["user"] != user:
|
||||
continue
|
||||
filtered_jobs.append(job)
|
||||
|
||||
return filtered_jobs
|
||||
|
||||
|
||||
def update_job(id, file = None, language = None, name = None, report = None, status = None):
|
||||
job_directory = os.path.join(UPLOAD_DIRECTORY, id)
|
||||
|
||||
for job in jobs:
|
||||
if job["id"] == id:
|
||||
if status:
|
||||
job["status"] = status
|
||||
if report:
|
||||
job["report"] = report
|
||||
return job
|
||||
if job["status"] != "queued":
|
||||
flask.abort(423)
|
||||
if file:
|
||||
if (job["service"] == "nlp" and file.content_type != "text/plain"):
|
||||
flask.abort(415)
|
||||
if (job["service"] == "ocr" and file.content_type != "application/pdf"):
|
||||
flask.abort(415)
|
||||
os.remove(os.path.join(job_directory, job["file"]))
|
||||
file.save(os.path.join(job_directory, file.filename))
|
||||
job["file"] = file.filename
|
||||
if language:
|
||||
job["language"] = language
|
||||
if report:
|
||||
job["report"] = report
|
||||
if name:
|
||||
job["name"] = name
|
||||
return job
|
||||
|
||||
flask.abort(404)
|
225
vre_manager/swagger/vre_manager.yml
Normal file
225
vre_manager/swagger/vre_manager.yml
Normal file
@ -0,0 +1,225 @@
|
||||
swagger: "2.0"
|
||||
info:
|
||||
contact:
|
||||
email: "p.jentsch@uni-bielefeld.de"
|
||||
name: "Patrick Jentsch"
|
||||
title: "SFB 1288 | INF: Plattformname - ReST API"
|
||||
version: "1.0.0"
|
||||
consumes:
|
||||
- application/json
|
||||
produces:
|
||||
- application/json
|
||||
|
||||
|
||||
basePath: /vre
|
||||
|
||||
|
||||
definitions:
|
||||
# Enums
|
||||
Service:
|
||||
type: string
|
||||
enum: &SERVICES
|
||||
- ocr
|
||||
- nlp
|
||||
Status:
|
||||
type: string
|
||||
enum: &STATUSES
|
||||
- deleted
|
||||
- failed
|
||||
- finished
|
||||
- queued
|
||||
- running
|
||||
Job:
|
||||
type: object
|
||||
properties:
|
||||
file:
|
||||
type: string
|
||||
id:
|
||||
type: string
|
||||
language:
|
||||
type: string
|
||||
name:
|
||||
type: string
|
||||
report:
|
||||
type: string
|
||||
service:
|
||||
type: string
|
||||
enum: *SERVICES
|
||||
status:
|
||||
type: string
|
||||
enum: *STATUSES
|
||||
user:
|
||||
type: string
|
||||
Jobs:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/definitions/Job'
|
||||
|
||||
|
||||
paths:
|
||||
/jobs:
|
||||
get:
|
||||
operationId: request_handlers.jobs.get_jobs
|
||||
tags:
|
||||
- jobs
|
||||
summary: Get a list of all jobs matching the filter(s)
|
||||
description: Get a list of all jobs matching the filter(s)
|
||||
parameters:
|
||||
- name: name
|
||||
in: query
|
||||
type: string
|
||||
description: The name to filter for
|
||||
required: False
|
||||
- name: service
|
||||
in: query
|
||||
type: string
|
||||
enum: *SERVICES
|
||||
description: The service to filter for
|
||||
required: False
|
||||
- name: status
|
||||
in: query
|
||||
type: string
|
||||
description: The status to filter for
|
||||
required: False
|
||||
- name: user
|
||||
in: query
|
||||
type: string
|
||||
description: The user to filter for
|
||||
required: False
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/Jobs'
|
||||
post:
|
||||
operationId: request_handlers.jobs.create_job
|
||||
tags:
|
||||
- jobs
|
||||
summary: Create a new job
|
||||
description: Create a new job
|
||||
consumes:
|
||||
- multipart/form-data
|
||||
parameters:
|
||||
- name: file
|
||||
in: formData
|
||||
type: file
|
||||
description: File
|
||||
required: True
|
||||
- name: language
|
||||
in: query
|
||||
description: Language
|
||||
type: string
|
||||
required: True
|
||||
- name: name
|
||||
in: query
|
||||
description: Name
|
||||
type: string
|
||||
required: True
|
||||
- name: service
|
||||
in: query
|
||||
description: Service
|
||||
type: string
|
||||
enum: *SERVICES
|
||||
required: True
|
||||
- name: user
|
||||
in: query
|
||||
description: User
|
||||
type: string
|
||||
required: True
|
||||
responses:
|
||||
201:
|
||||
description: Created
|
||||
schema:
|
||||
$ref: '#/definitions/Job'
|
||||
415:
|
||||
description: Unsupported Media Type
|
||||
/jobs/{id}:
|
||||
delete:
|
||||
operationId: request_handlers.jobs.delete_job
|
||||
tags:
|
||||
- jobs
|
||||
summary: Delete job (specified by {job_id})
|
||||
description: Delete job (specified by {job_id})
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
description: Job identifier
|
||||
type: string
|
||||
required: True
|
||||
responses:
|
||||
204:
|
||||
description: No Content
|
||||
404:
|
||||
description: Not Found
|
||||
423:
|
||||
description: Locked
|
||||
get:
|
||||
operationId: request_handlers.jobs.get_job
|
||||
tags:
|
||||
- jobs
|
||||
summary: Get job (specified by {job_id})
|
||||
description: Get job (specified by {job_id})
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
description: Job identifier
|
||||
type: string
|
||||
required: True
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/Job'
|
||||
404:
|
||||
description: Not Found
|
||||
put:
|
||||
operationId: request_handlers.jobs.update_job
|
||||
tags:
|
||||
- jobs
|
||||
summary: Update job (specified by {job_id})
|
||||
description: Update job (specified by {job_id})
|
||||
consumes:
|
||||
- multipart/form-data
|
||||
parameters:
|
||||
- name: file
|
||||
in: formData
|
||||
type: file
|
||||
description: File
|
||||
required: False
|
||||
- name: id
|
||||
in: path
|
||||
description: Job identifier
|
||||
type: string
|
||||
required: True
|
||||
- name: language
|
||||
in: query
|
||||
description: Language
|
||||
type: string
|
||||
required: False
|
||||
- name: name
|
||||
in: query
|
||||
description: Name
|
||||
type: string
|
||||
required: False
|
||||
- name: report
|
||||
in: query
|
||||
description: Report
|
||||
type: string
|
||||
required: False
|
||||
- name: status
|
||||
in: query
|
||||
description: Status
|
||||
type: string
|
||||
enum: *STATUSES
|
||||
required: False
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/Job'
|
||||
404:
|
||||
description: Not Found
|
||||
415:
|
||||
description: Unsupported Media Type
|
||||
423:
|
||||
description: Locked
|
7
vre_manager/vre_manager.py
Normal file
7
vre_manager/vre_manager.py
Normal file
@ -0,0 +1,7 @@
|
||||
import connexion
|
||||
import flask_cors
|
||||
|
||||
app = connexion.FlaskApp(__name__, specification_dir='swagger/')
|
||||
app.add_api('vre_manager.yml')
|
||||
flask_cors.CORS(app.app)
|
||||
app.run(port=5000)
|
Reference in New Issue
Block a user