File size: 1,395 Bytes
618ae26
9c91784
618ae26
 
9c91784
 
 
618ae26
196fdcc
9c91784
 
618ae26
 
 
 
36394ae
9c91784
 
 
618ae26
9c91784
 
 
 
 
 
 
618ae26
1b179fe
9c91784
1b179fe
 
 
9c91784
1b179fe
 
9c91784
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from ..utils.functions import readYaml
import contextlib
import traceback
import io

class REPLManager:
    def __init__(self):
        params = readYaml("params.yaml")
        self.__persistentGlobals = {"__name__": "__main__"}
        self.__stdout = io.StringIO()
        self.__stderr = io.StringIO()
        exec(params.get("redisFunctionCode"), self.__persistentGlobals)
        exec(params.get("panelChartDataCode"), self.__persistentGlobals)
        exec(params.get("jsonSerializer"), self.__persistentGlobals)
        self.__globals = dict(self.__persistentGlobals)

    def run(self, codeString):
        with contextlib.redirect_stdout(self.__stdout), contextlib.redirect_stderr(self.__stderr):
            try:
                exec(codeString, self.__globals)
            except Exception:
                traceback.print_exc(file=self.__stderr)
        output, error = self.__stdout.getvalue(), self.__stderr.getvalue()
        self.__stdout.truncate(0)
        self.__stdout.seek(0)
        self.__stderr.truncate(0)
        self.__stderr.seek(0)
        self.__globals = dict(self.__persistentGlobals)
        if (output != "") & (error == ""):
            return output
        elif (output != "") & (error != ""):
            return output
        elif (output == "") & (error != ""):
            return error
        else:
            return output

replManager = REPLManager()