from sextante.core.SextanteConfig import SextanteConfig
import os
from sextante.core.SextanteUtils import mkdir, SextanteUtils
import subprocess
from sextante.core.SextanteLog import SextanteLog
import stat

class RUtils:

    RSCRIPTS_FOLDER = "R_SCRIPTS_FOLDER"
    R_FOLDER = "R_FOLDER"

    @staticmethod
    def RFolder():
        folder = SextanteConfig.getSetting(RUtils.R_FOLDER)
        if folder == None:
            folder =""

        return folder

    @staticmethod
    def RScriptsFolder():
        folder = SextanteConfig.getSetting(RUtils.RSCRIPTS_FOLDER)
        if folder == None:
            folder = os.path.join(os.path.dirname(__file__), "scripts")
        mkdir(folder)

        return folder

    @staticmethod
    def createRScriptFromRCommands(commands):
        scriptfile = open(RUtils.getRScriptFilename(), "w")
        for command in commands:
            scriptfile.write(command + "\n")
        scriptfile.close()


    @staticmethod
    def getRScriptFilename():
        return SextanteUtils.userFolder() + os.sep + "sextante_script.r"


    @staticmethod
    def getConsoleOutputFilename():
        return RUtils.getRScriptFilename()+".Rout"


    @staticmethod
    def executeRAlgorithm(alg):
        RUtils.consoleResults = []
        RUtils.verboseCommands = alg.getVerboseCommands();
        RUtils.createRScriptFromRCommands(alg.getFullSetOfRCommands())
        if SextanteUtils.isWindows():
            command = [RUtils.RFolder() + os.sep + "bin" + os.sep + "R.exe", "CMD", "BATCH", "--vanilla", RUtils.getRScriptFilename()]
        else:
            os.chmod(RUtils.getRScriptFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
            command = ["R", "CMD", "BATCH", "--vanilla", RUtils.getRScriptFilename()]

        proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True)
        proc.wait()
        RUtils.createConsoleOutput()
        loglines = []
        loglines.append("R execution console output")
        loglines += RUtils.consoleResults
        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)

    @staticmethod
    def createConsoleOutput():
        add = False
        lines = open(RUtils.getConsoleOutputFilename())
        line = lines.readline().strip("\n").strip(" ")
        while line != "":
            if line.startswith(">"):
                line = line[1:].strip(" ")
                if line in RUtils.verboseCommands:
                    add = True
                else:
                    add = False
            elif add:
                RUtils.consoleResults.append("<p>" + line + "</p>\n");
            line = lines.readline().strip("\n").strip(" ")


    @staticmethod
    def getConsoleOutput():
        s = "<font face=\"courier\">\n"
        s+="<h2> R Output</h2>\n"
        for line in RUtils.consoleResults:
            s+=line
        s+="</font>\n"

        return s


