PythonParse.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.coffee.bus.script;
  2. import cn.hutool.http.webservice.SoapUtil;
  3. import cn.hutool.json.JSON;
  4. import cn.hutool.json.JSONUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.python.core.*;
  7. import org.python.util.PythonInterpreter;
  8. import org.springframework.stereotype.Component;
  9. import javax.xml.soap.SOAPMessage;
  10. import java.util.Properties;
  11. /**
  12. * @author lifang
  13. * @version 1.0.0
  14. * @ClassName PythonParse.java
  15. * @Description TODO
  16. * @createTime 2022年03月28日 10:58:00
  17. */
  18. @Slf4j
  19. @Component
  20. public class PythonParse implements ScriptParse {
  21. private final PythonInterpreter interpreter;
  22. private PyFunction pyFunction;
  23. public PythonParse() {
  24. Properties p = new Properties();
  25. p.setProperty("python.console.encoding", "UTF-8");
  26. PySystemState systemState = Py.getSystemState();
  27. PyString xml = new PyString(Thread.currentThread().getContextClassLoader().getResource("python").getPath());
  28. systemState.path.append(xml);
  29. PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
  30. interpreter = new PythonInterpreter();
  31. }
  32. @Override
  33. public String getId() {
  34. return "python";
  35. }
  36. @Override
  37. public String functionName() {
  38. return "parse";
  39. }
  40. @Override
  41. public void reset(String script){
  42. interpreter.exec(Py.newStringUTF8(script));
  43. pyFunction = interpreter.get(functionName(), PyFunction.class);
  44. }
  45. @Override
  46. public ExecuteResult exec(String param) {
  47. ExecuteResult executeResult = new ExecuteResult();
  48. try {
  49. long start = System.currentTimeMillis();
  50. PyObject result = pyFunction.__call__(Py.newStringUTF8(param));
  51. long end = System.currentTimeMillis();
  52. executeResult.setSuccess(true);
  53. executeResult.setUseTime(end-start);
  54. executeResult.setResult( JSONUtil.parse(result));
  55. }catch (Exception e){
  56. executeResult.setSuccess(false);
  57. executeResult.setException(e);
  58. }
  59. return executeResult;
  60. }
  61. public static void main(String[] args){
  62. Properties p = new Properties();
  63. p.setProperty("python.console.encoding", "UTF-8");
  64. PySystemState systemState = Py.getSystemState();
  65. PyString xml = new PyString("E:\\software\\coffee-boot\\coffee-admin\\src\\main\\resources\\python");
  66. // PyString xml = new PyString("_classpath_:python");
  67. systemState.path.append(xml);
  68. PythonInterpreter.initialize(System.getProperties(), p, new String[] {});
  69. PythonInterpreter pythonInterpreter = new PythonInterpreter();
  70. String str = "# -*- coding: utf-8 -*-\n" +
  71. "import sys\n" +
  72. "reload(sys)\n" +
  73. "sys.setdefaultencoding('utf-8')\n" +
  74. "import json\n" +
  75. "import xmltodict as xmltodict\n" +
  76. "\n" +
  77. "\n" +
  78. "def parse(str):\n" +
  79. "\n" +
  80. " json_result={}\n" +
  81. " json_obj= json.loads(str)\n" +
  82. "\n" +
  83. " json_result['1']=json_obj['name']\n" +
  84. " return json_result";
  85. pythonInterpreter.exec(Py.newStringUTF8(str));
  86. PyFunction parse = pythonInterpreter.get("parse", PyFunction.class);
  87. PyObject pyObject = parse.__call__(Py.newStringUTF8( "{\"name\": \"小明\", \"age\": \"12\"}"));
  88. JSON parse1 = JSONUtil.parse(pyObject);
  89. pythonInterpreter.close();
  90. System.out.println(pyObject);
  91. System.out.println(parse1);
  92. }
  93. }