package api.common.util;


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Slf4j
public class PythonUtil {

    /*
        C：惯例，违反了编码风格标准
        R：重构，代码非常糟糕
        W：警告，某些 Python 特定的问题
        E：错误，很可能是代码中的错误
        F：致命错误，阻止 Pylint 进一步运行的错误
     */

    public static String LINE_TOO_LONG = "C0301";
    public static String C = "C";
    public static String R = "R";
    public static String W = "W";
    public static String E = "E";
    public static String E0401 = "E0401";
    public static String E0601 = "E0601";
    public static String F = "F";
    public static String PASS = "Your code has been rated at 10.00/10";

    /**
     * @param pythonCode python 代码
     * @param disables   忽略指定等级的日志
     */
    @SneakyThrows
    public static String pylint(String pythonCode, String... disables) {
        //1 把代码保存成本地文件
        String filePath = "/SimulationCloud/Evaluate/test/" + UUID.randomUUID().toString().replace("-", "") + ".py";
        FileUtil.writeStringToLocalFile(pythonCode, filePath);
        //2 执行命令检查格式
        StringBuilder command = new StringBuilder("pylint3 --disable=");
        for (String disable : disables) {
            command.append(disable).append(",");
        }
        command.append(" ").append(filePath);
        return LinuxUtil.execute(command.toString());
    }
}

