toolchain_app.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import logging
  2. from logging import config
  3. from flask import Flask, jsonify, request
  4. from toolchain.tm_to_tmx_processor import TmToTmxProcessor
  5. from toolchain.doc_to_tmx_processor import DocToTmxProcessor
  6. TOOLCHAIN_CONFIG_PATH = "toolchains.cfg"
  7. config.dictConfig({
  8. "version": 1,
  9. "formatters": {"default": {
  10. "format": "[%(asctime)s] %(levelname)s %(module)s %(message)s"
  11. }},
  12. "handlers": {"file": {
  13. "class": "logging.handlers.RotatingFileHandler",
  14. "formatter": "default",
  15. "filename": "toolchain.log",
  16. "maxBytes": 1000000,
  17. "backupCount": 3
  18. }},
  19. "root": {
  20. "level": "INFO",
  21. "handlers": ["file"]
  22. },
  23. "disable_existing_loggers": False
  24. })
  25. logger = logging.getLogger(__name__)
  26. app = Flask(__name__)
  27. app.config.from_prefixed_env(prefix="FLASK_TC")
  28. def process_input(processor_type, request):
  29. try:
  30. additional_tmx_args = {"licence": request.json["licence"]}
  31. rejected, file_infos = processor_type(TOOLCHAIN_CONFIG_PATH, additional_tmx_args).process(
  32. request.json["id"],
  33. request.json["input"],
  34. request.json["artefact"],
  35. request.json["output"],
  36. )
  37. response_dict = {
  38. "file_infos" : file_infos,
  39. "rejected" : rejected,
  40. "success" : True,
  41. }
  42. return jsonify(response_dict)
  43. except:
  44. response_dict = {"success" : False}
  45. return jsonify(response_dict)
  46. @app.route("/tm", methods=["GET", "POST"])
  47. def process_tm():
  48. logger.info("TM processor requested")
  49. if request.method == "POST":
  50. logger.debug(str(request.json))
  51. return process_input(TmToTmxProcessor, request)
  52. return "TM to TMX toolchain"
  53. @app.route("/doc", methods=["GET", "POST"])
  54. def process_doc():
  55. logger.info("Doc processor requested")
  56. if request.method == "POST":
  57. logger.debug(str(request.json))
  58. return process_input(DocToTmxProcessor, request)
  59. return "Doc to TMX toolchain"