forked from LibreOffice/libresign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.py
123 lines (94 loc) · 3.39 KB
/
playlist.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import config
from request import Request
class Playlist():
def __init__ (self):
# all files that have been uploaded
self.all_files = []
# files that are being played
self.playlist = []
# current file index
self.current = 0
def handle_web_request (self, msg):
mtype = msg.get("type")
if mtype == Request.ADD_FILE:
self.load_files()
if mtype == Request.PLAY_FILE:
print("play file")
filename = msg.get("file")
self.select_file(filename)
if mtype == Request.ORDER:
from_i = msg.get("from")
to_i = msg.get("to")
self.order_playlist(from_i, to_i)
if mtype == Request.QUEUE_FILE:
to_i = msg.get("to")
filename = msg.get("file")
self.queue_file(to_i, filename)
if mtype == Request.REMOVE_FILE:
filename = msg.get("file")
self.dequeue(filename)
# load previously-uploaded presentations
def load_files (self):
path = config.SAVE_FOLDER
self.all_files = []
for f in os.listdir(path):
if os.path.isfile(os.path.join(path, f)):
item = {"file" : f}
self.all_files.append(f)
print("loaded presentation files", self.all_files)
def load_playlist (self):
fd = open("playlist", "r")
for line in fd:
self.playlist.append({"file" : line[:-1]})
fd.close()
print("loaded playlist", self.playlist)
# save playlist file (list of items, in order)
def save_playlist (self):
fd = open("playlist", "w")
for i in self.playlist:
fd.write(i.get("file"))
fd.write('\n')
fd.close()
print("saved playlist", self.playlist)
# swap files in places from_i and to_i
def order_playlist (self, from_i, to_i):
if (from_i >= 0 and from_i < len(self.playlist) and
to_i >= 0 and to_i < len(self.playlist)):
tmp = self.playlist.pop(from_i)
self.playlist.insert(to_i, tmp)
self.save_playlist()
# add file to playlist
def queue_file (self, to_index, filename):
if self.all_files.count(filename) == 0:
return
for item in self.playlist:
if item.get("file") == filename:
return
if to_index >= 0 and to_index <= len(self.playlist):
self.playlist.insert(to_index, {'file' : filename})
self.save_playlist()
# remove file from playlist
def dequeue (self, filename):
for item in self.playlist:
if item.get("file") == filename:
self.playlist.remove(item)
break
self.save_playlist()
# select file to be played right now
def select_file (self, filename):
c = 0
for item in self.playlist:
if item.get("file") == filename:
self.current = c
break
c += 1
print("play", self.current, filename)
# return filename of current presentation to be played
def get_current (self):
if len(self.playlist) > self.current:
return self.playlist[self.current]['file']
else:
return None
def next (self):
self.current = (self.current + 1) % len(self.playlist)