Switch to unified view

a/web/scweb.py b/web/scweb.py
1
# Copyright (C) 2017-2018 J.F.Dockes
2
#   This program is free software; you can redistribute it and/or modify
3
#   it under the terms of the GNU General Public License as published by
4
#   the Free Software Foundation; either version 2 of the License, or
5
#   (at your option) any later version.
6
#
7
#   This program is distributed in the hope that it will be useful,
8
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
#   GNU General Public License for more details.
11
#
12
#   You should have received a copy of the GNU General Public License
13
#   along with this program; if not, write to the
14
#   Free Software Foundation, Inc.,
15
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
17
from __future__ import print_function
1
import subprocess
18
import subprocess
2
import sys
19
import sys
3
import bottle
20
import bottle
4
import re
21
import re
5
import time
22
import time
6
23
24
def _msg(s):
25
    print("%s"%s, file=sys.stderr)
26
27
cmd = None
28
def _maybestartserver():
29
    global cmd
30
    if not cmd:
31
        devnull = open('/dev/null', 'w')
32
        cmd = subprocess.Popen(['scctl', '-S'], stderr=devnull, stdout=devnull)
33
        devnull.close()
34
        
7
SPLITRE = '''\|\|'''
35
SPLITRE = b'''\|\|'''
8
36
9
def _listReceivers():
37
def _listReceivers():
38
    _maybestartserver()
10
    devnull = open('/dev/null', 'w')
39
    devnull = open('/dev/null', 'w')
11
    try:
40
    try:
12
        data = subprocess.check_output(['scctl', '-lm'], stderr = devnull)
41
        data = subprocess.check_output(['scctl', '-lm'], stderr = devnull)
13
    except:
42
    except:
14
        data = "scctl error"
43
        data = "scctl error"
15
    o = []
44
    o = []
16
    for line in data.splitlines():
45
    for line in data.splitlines():
17
        #print >> sys.stderr, line
46
        #_msg(line)
18
        fields = re.split(SPLITRE, line);
47
        fields = re.split(SPLITRE, line);
19
        if len(fields) == 4:
48
        if len(fields) == 4:
20
            status, fname, uuid, uri = fields
49
            status, fname, uuid, uri = fields
21
        elif len(fields) == 3:
50
        elif len(fields) == 3:
22
            status, fname, uuid = fields
51
            status, fname, uuid = fields
...
...
25
            status = None
54
            status = None
26
        if status:
55
        if status:
27
            status = status.strip()
56
            status = status.strip()
28
        if status is not None:
57
        if status is not None:
29
            o.append((fname, status, uuid, uri))
58
            o.append((fname, status, uuid, uri))
59
    devnull.close()
30
    return o
60
    return o
31
61
32
@bottle.route('/static/:path#.+#')
62
@bottle.route('/static/:path#.+#')
33
def server_static(path):
63
def server_static(path):
34
    return bottle.static_file(path, root='./static')
64
    return bottle.static_file(path, root='./static')
35
65
36
@bottle.route('/')
66
@bottle.route('/')
37
@bottle.view('main')
67
@bottle.view('main')
38
def top():
68
def top():
39
    devnull = open('/dev/null', 'w')
69
    _maybestartserver()
40
    cmd = subprocess.Popen(['scctl', '-S'], stderr = devnull, stdout = devnull)
41
    # Sleep a wee bit to give a chance to the server to initialize
70
    # Sleep a wee bit to give a chance to the server to initialize
42
    time.sleep(1)
71
    time.sleep(1)
43
    return dict(title='')
72
    return dict(title='')
44
73
45
@bottle.route('/list')
74
@bottle.route('/list')
...
...
49
78
50
@bottle.route('/assoc')
79
@bottle.route('/assoc')
51
@bottle.post('/assoc')
80
@bottle.post('/assoc')
52
@bottle.view('assoc')
81
@bottle.view('assoc')
53
def assocReceivers():
82
def assocReceivers():
83
    _maybestartserver()
54
    devnull = open('/dev/null', 'w')
84
    devnull = open('/dev/null', 'w')
55
85
56
    assocs = bottle.request.forms.getall('Assoc')
86
    assocs = bottle.request.forms.getall('Assoc')
57
    sender = bottle.request.forms.get('Sender')
87
    sender = bottle.request.forms.get('Sender')
58
    if sender != '' and len(assocs) != 0:
88
    if sender != '' and len(assocs) != 0:
59
        arglist = ['scctl', '-r', sender]
89
        arglist = ['scctl', '-r', sender]
60
        for uuid in assocs:
90
        for uuid in assocs:
61
            arglist.append(uuid)
91
            arglist.append(uuid)
62
        print >> sys.stderr, arglist
92
        _msg(arglist)
63
93
64
        try:
94
        try:
65
            subprocess.check_call(arglist, stderr = devnull)
95
            subprocess.check_call(arglist, stderr = devnull)
66
        except:
96
        except:
67
            pass
97
            pass
...
...
74
    s = []
104
    s = []
75
    for line in data.splitlines():
105
    for line in data.splitlines():
76
        fields = re.split(SPLITRE, line);
106
        fields = re.split(SPLITRE, line);
77
        fname, uuid, reason, uri = fields
107
        fname, uuid, reason, uri = fields
78
        s.append((fname, uuid, uri))
108
        s.append((fname, uuid, uri))
79
109
    devnull.close()
80
    return {'receivers' : _listReceivers(), 'senders' : s}
110
    return {'receivers' : _listReceivers(), 'senders' : s}
81
111
82
112
83
@bottle.route('/stop')
113
@bottle.route('/stop')
84
@bottle.post('/stop')
114
@bottle.post('/stop')
85
@bottle.view('stop')
115
@bottle.view('stop')
86
def stopReceivers():
116
def stopReceivers():
117
    _maybestartserver()
87
    devnull = open('/dev/null', 'w')
118
    devnull = open('/dev/null', 'w')
88
    for uuid in bottle.request.forms.getall('Stop'):
119
    for uuid in bottle.request.forms.getall('Stop'):
89
        try:
120
        try:
90
            subprocess.check_call(['scctl', '-x', uuid], stderr=devnull)
121
            subprocess.check_call(['scctl', '-x', uuid], stderr=devnull)
91
        except:
122
        except:
...
...
105
                a.append((fname, status, uuid, uri))
136
                a.append((fname, status, uuid, uri))
106
        elif len(fields) == 3:
137
        elif len(fields) == 3:
107
            status, fname, uuid = fields
138
            status, fname, uuid = fields
108
            if status != 'Off':
139
            if status != 'Off':
109
                a.append(fname, status, uuid, '')
140
                a.append(fname, status, uuid, '')
141
    devnull.close()
110
    return {'active' : a}
142
    return {'active' : a}