123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import logging
- from logging import config
- from flask import Flask, jsonify, request
- from toolchain.tm_to_tmx_processor import TmToTmxProcessor
- from toolchain.doc_to_tmx_processor import DocToTmxProcessor
- TOOLCHAIN_CONFIG_PATH = "toolchains.cfg"
- config.dictConfig({
- "version": 1,
- "formatters": {"default": {
- "format": "[%(asctime)s] %(levelname)s %(module)s %(message)s"
- }},
- "handlers": {"file": {
- "class": "logging.handlers.RotatingFileHandler",
- "formatter": "default",
- "filename": "toolchain.log",
- "maxBytes": 1000000,
- "backupCount": 3
- }},
- "root": {
- "level": "INFO",
- "handlers": ["file"]
- },
- "disable_existing_loggers": False
- })
- logger = logging.getLogger(__name__)
- app = Flask(__name__)
- app.config.from_prefixed_env(prefix="FLASK_TC")
- def process_input(processor_type, request):
- try:
- additional_tmx_args = {"licence": request.json["licence"]}
- rejected, file_infos = processor_type(TOOLCHAIN_CONFIG_PATH, additional_tmx_args).process(
- request.json["id"],
- request.json["input"],
- request.json["artefact"],
- request.json["output"],
- )
- response_dict = {
- "file_infos" : file_infos,
- "rejected" : rejected,
- "success" : True,
- }
- return jsonify(response_dict)
- except:
- response_dict = {"success" : False}
- return jsonify(response_dict)
- @app.route("/tm", methods=["GET", "POST"])
- def process_tm():
- logger.info("TM processor requested")
- if request.method == "POST":
- logger.debug(str(request.json))
- return process_input(TmToTmxProcessor, request)
- return "TM to TMX toolchain"
- @app.route("/doc", methods=["GET", "POST"])
- def process_doc():
- logger.info("Doc processor requested")
- if request.method == "POST":
- logger.debug(str(request.json))
- return process_input(DocToTmxProcessor, request)
- return "Doc to TMX toolchain"
|