-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebActor.py
81 lines (63 loc) · 1.98 KB
/
WebActor.py
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
from subprocess import Popen, PIPE
import pykka, logging, os, re
from bottle import Bottle, view, redirect
tagActor = None
FILE_DIR = '/var/lib/mpd/music/'
app = Bottle()
@app.route('/')
@view('index')
def index():
total, free = getDiskInfo()
return dict(items=items(), totalMem=total, freeMem=free)
@app.route('/add/<name>')
def add(name):
tagActor.addTag(name).get()
redirect('/')
@app.route('/remove/<tag>')
def remove(tag):
tagActor.removeTag(tag).get()
redirect('/')
@app.route('/play/<tag>')
def play(tag):
tagActor.playByTag(tag)
return 'Called tagActor with tag: %s\n' % tag
@app.route('/play/<tag>/fromStart')
def play_from_start(tag):
tagActor.playByTag(tag, fromStart=True)
return 'Called tagActor with tag: %s and fromStart=True\n' % tag
def getDiskInfo():
p = Popen("df -h .", shell=True, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
m = re.match("([0-9KMGTP\.]+)[\s]+[0-9KMGTP\.]+[\s]+([0-9KMGTP\.]+)", out.decode("utf-8"))
total = "N/A"
free = "N/A"
if m != None:
total = m.group(1)
free = m.group(2)
return total, free
def items(**k):
currentFiles = sorted(os.listdir(FILE_DIR))
tags = {name:tag for tag, name in tagActor.loadTags().get().items()}
filesTagArray = []
for fileName in currentFiles:
if fileName in tags:
filesTagArray.append({"name": fileName, "tag": tags[fileName]})
else:
filesTagArray.append({"name": fileName, "tag": None})
return filesTagArray
def runInThread(function):
from threading import Thread
t = Thread(target=function)
t.setDaemon(True)
t.start()
def runWebApp():
try:
app.run(host='0.0.0.0', port=8080)
except Exception:
logging.getLogger('zbap').info('Shutting down web server.')
class WebActor(pykka.ThreadingActor):
def __init__(self, tagAct):
super(WebActor, self).__init__()
global tagActor
tagActor = tagAct
runInThread(runWebApp)