-
Notifications
You must be signed in to change notification settings - Fork 11
/
watcher.py
66 lines (59 loc) · 2 KB
/
watcher.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
#!/usr/bin/python
import logging
import pyinotify as pi
import config
import manager
from os import path
def start():
global notifier
wm = pi.WatchManager()
notifier = pi.ThreadedNotifier(wm, handler())
notifier.name = "Queue Watcher"
notifier.daemon = 1
notifier.start()
wdd = wm.add_watch(config.watcher_path, pi.IN_MODIFY)
def shutdown():
notifier.stop()
return None
def parse_queue_file():
queue = []
remaining = 0
with open(path.join(config.watcher_path, config.watcher_file)) as file:
djid = file.readline().strip()
firstline = file.readline().strip()
if (djid != ''):
remaining = int(firstline)
try:
djid = int(djid)
except ValueError:
djid = None
if (manager.DJ().id == djid):
for line in file:
line = line.strip()
if line == "":
continue
try:
spacepos = line.index(' ')
except:
continue
stime = int(line[:spacepos])
song = line[spacepos+1:]
queue.append(manager.Song(meta=song, length=stime))
else:
logging.info("Queue discarded because djid {id} does not match".format(id=djid))
if (len(queue) > 0):
_queue = manager.Queue()
_queue.clear()
manager.NP().remaining(remaining)
_queue.append_many(queue)
try:
logging.info("Finished queue from {name}".format(name=manager.DJ().name))
except:
logging.info("Finished queue update by Unknown")
class handler(pi.ProcessEvent):
def process_IN_MODIFY(self, event):
if (event.name == 'queue.txt'):
try:
parse_queue_file()
except:
logging.exception("Problem parsing the queue file")