summaryrefslogtreecommitdiff
path: root/website/helpers.py
blob: 651a886b03df75d941a4e8ed596f1e95c81f2465 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import re
import time
import subprocess

from soc.settings import PROJECT_DIR
 
def scilab_run(code, token):
    plot_exists = False
    #Finding the plot and appending xs2jpg function
    p = re.compile(r'.*plot.*\(.*,.*,*\).*\n')
    if p.search(code):
        plot_exists = True
        code = code + '\n'
        temp = code
        code = ''
        start, end, count = 0, 0, 0
        current_time = time.time()
        plot_path = []
        for (count, match) in enumerate(p.finditer(temp)):
            print "count==================",count
            plot_path.append(
                PROJECT_DIR + '/static/tmp/{0}.jpg'.format(int(current_time) + count)
            )
            end = match.end()
            code += temp[start:end]
            code += 'xs2jpg(gcf(), "{0}");\n'.format(plot_path[-1])
            start = end
        code += temp[end:]

    # Create a file for the current request
    canvas_off = """
    function turnCanvasOff()
       m = getscilabmode();
       if (m=="STD"|m=="NW") then
           previousDisp = disp;
           prot = funcprot();
           funcprot(0);
           deff("disp(str)", "");
           usecanvas(%f);
           disp = previousDisp;
           funcprot(prot);
       end
    endfunction
    turnCanvasOff();
    clear turnCanvasOff;
    """

    file_path = PROJECT_DIR + '/static/tmp/' + token + '.sci'

    f = open(file_path, "w")
    f.write("mode(-1);\nlines(0);\nmode(-1);\ntry\nmode(2);\n")
    f.write('lines(0)\n')
    f.write(canvas_off)
    f.write(code)
    f.write('\nexit')
    f.write("\nmode(-1);\nexit();\ncatch\nmode(-1);\ndisp(lasterror());\nexit();")
    f.close()
    
    if plot_exists:
        cmd = 'export DISPLAY=:99 && '
        cmd +=  'xvfb-run -a cat {0}'.format(file_path) + ' | scilab-adv-cli -nw -nb'
        print "############################"
        print cmd
    else:
        cmd = 'cat {0}'.format(file_path) + ' | scilab-adv-cli -nw'

    output = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
    ).communicate()[0]
    #os.remove(file_path)
    output = trim(output)
    return output

def trim(output):
    atoms_banner = """
    Start Image Processing Tool 2
        Load macros
        Load help

    SIVP - Scilab Image and Video Processing Toolbox
        Load macros
        Load gateways
        Load help
        Load demos


     
     Start identification   
     
     Load macros   
     
     Load help   
    """
    output = output.replace(atoms_banner, '')
    return output