a b/web/scweb.py
1
import subprocess
2
import sys
3
import bottle
4
import re
5
6
@bottle.route('/static/:path#.+#')
7
def server_static(path):
8
    return bottle.static_file(path, root='./static')
9
10
@bottle.route('/')
11
@bottle.view('main')
12
def top():
13
    devnull = open('/dev/null', 'w')
14
    #cmd = subprocess.Popen(['scctl', '-S'], stderr = devnull, stdout = devnull)
15
    cmd = subprocess.Popen(['scctl', '-S'])
16
    return dict(title='')
17
18
@bottle.route('/list')
19
@bottle.view('list')
20
def listReceivers():
21
    devnull = open('/dev/null', 'w')
22
    try:
23
        data = subprocess.check_output(['scctl', '-l', '-n'], stderr = devnull)
24
    except:
25
        data = "scctl error"
26
    o = []
27
    for line in data.splitlines():
28
        fields = re.split('''\s+''', line);
29
        if len(fields) == 4:
30
            status, fname, uuid, uri = fields
31
        elif len(fields) == 3:
32
            status, fname, uuid = fields
33
            uri = ''
34
        else:
35
            status = None
36
        if status is not None:
37
            o.append((fname, status, uuid, uri))
38
    return {'receivers' : o}
39
40
@bottle.route('/assoc')
41
@bottle.post('/assoc')
42
@bottle.view('assoc')
43
def assocReceivers():
44
    devnull = open('/dev/null', 'w')
45
46
    assocs = bottle.request.forms.getall('Assoc')
47
    master = bottle.request.forms.get('Master')
48
    if master != '' and len(assocs) != 0:
49
        arglist = ['scctl', '-s', master]
50
        for uuid in assocs:
51
            arglist.append(uuid)
52
            try:
53
                subprocess.check_call(arglist, stderr = devnull)
54
            except:
55
                pass
56
57
    try:
58
        data = subprocess.check_output(['scctl', '-l', '-n'], stderr = devnull)
59
    except:
60
        data = "scctl error"
61
62
    a = []
63
    o = []
64
    for line in data.splitlines():
65
        fields = re.split('''\s+''', line);
66
        if len(fields) == 4:
67
            status, fname, uuid, uri = fields
68
            if status != 'Off' and uri != '':
69
                a.append((fname, status, uuid, uri))
70
            else:
71
                o.append((fname, status, uuid, uri))
72
        elif len(fields) == 3:
73
            status, fname, uuid = fields
74
            o.append(fname, status, uuid, '')
75
    return {'active' : a, 'others' : o}
76
77
78
@bottle.route('/stop')
79
@bottle.post('/stop')
80
@bottle.view('stop')
81
def stopReceivers():
82
    devnull = open('/dev/null', 'w')
83
    for uuid in bottle.request.forms.getall('Stop'):
84
        try:
85
            subprocess.check_call(['scctl', '-x', uuid], stderr=devnull)
86
        except:
87
            pass
88
89
    try:
90
        data = subprocess.check_output(['scctl', '-l', '-n'], stderr = devnull)
91
    except:
92
        data = "scctl error"
93
94
    a = []
95
    for line in data.splitlines():
96
        fields = re.split('''\s+''', line);
97
        if len(fields) == 4:
98
            status, fname, uuid, uri = fields
99
            if status != 'Off':
100
                a.append((fname, status, uuid, uri))
101
        elif len(fields) == 3:
102
            status, fname, uuid = fields
103
            if status != 'Off':
104
                a.append(fname, status, uuid, '')
105
    return {'active' : a}