-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPVVJState.py
434 lines (378 loc) · 14.2 KB
/
MPVVJState.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# MPV-VJ2 Copyright 2016 paulguy <paulguy119@gmail.com>
#
# This file is part of MPV-VJ2.
#
# MPV-VJ2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MPV-VJ2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MPV-VJ2. If not, see <http://www.gnu.org/licenses/>.
import random
class PlaylistEntry:
def __init__(self, item, played=False):
if type(item) != str:
raise TypeError
self.item = item
self.played = played
def setPlayed(self):
self.played = True
def setNotPlayed(self):
self.played = False
class Playlist:
def __init__(self, name, loop, rand):
if type(name) != str:
raise TypeError
self.name = name
self.entries = []
self.currentCue = -1
self.loop = loop
self.random = rand
def insertEntry(self, value, played=False, idx=-1):
if idx < 0:
self.entries.append(PlaylistEntry(value, played))
else:
self.entries.insert(idx, PlaylistEntry(value, played))
if self.currentCue == -1:
self.currentCue = 0
def delEntryByIndex(self, idx):
del self.entries[idx]
if idx == self.currentCue:
self.currentCue = -1
elif idx < self.currentCue:
self.currentCue -= 1
def getEntries(self):
entries = []
for entry in self.entries:
entries.append({'name': entry.item, 'played': entry.played})
return entries
def setCurrent(self, idx):
if idx < 0 or idx > len(self.entries) - 1:
raise IndexError
self.currentCue = idx
def getCurrent(self):
return self.currentCue, self.entries[self.currentCue].name
def advance(self):
if len(self.entries) == 0:
raise RuntimeError("Empty playlist.")
# this function is only used for advancing after something has been played
# so set this now
self.entries[self.currentCue].setPlayed()
if self.random:
notPlayed = []
for entry in enumerate(self.entries): # select an entry that hasn't been played
if not entry[1].played:
notPlayed.append(entry[0])
if (len(notPlayed) == 0):
if self.loop:
for entry in self.entries:
entry.setNotPlayed()
self.currentCue = random.randrange(len(self.entries))
else:
self.currentCue = -1
else:
self.currentCue = notPlayed[random.randrange(len(notPlayed))]
else:
if self.currentCue == len(self.entries) - 1:
if self.loop:
for entry in self.entries:
entry.setNotPlayed()
self.currentCue = 0
else:
self.currentCue = -1
else:
self.currentCue += 1
class MPVVJState:
def __init__(self):
self.playlists = []
self.mpvopts = {}
self.currentPlaylist = None
def findPlaylistByName(self, name):
for pl in enumerate(self.playlists):
if pl[1].name == name:
return pl
return None
def newPlaylist(self, name, loop, random):
self.playlists.append(Playlist(name, loop, random))
def newPlaylists(self, obj):
if 'playlists' not in obj:
return "No 'playlists'."
if type(obj['playlists']) != list:
return "'playlists' is not a list."
for item in enumerate(obj['playlists']):
if type(item[1]) != dict:
return "'playlists' item not a dict."
if 'name' not in item[1]:
return "'playlists' item with no 'name'."
if type(item[1]['name']) != str:
return "'name' is not a string."
try:
if type(item[1]['loop']) != bool:
return "'loop' is not a bool."
except KeyError:
pass
try:
if type(item[1]['random']) != bool:
return "'random' is not a bool."
except KeyError:
pass
if self.findPlaylistByName(item[1]['name']) is not None:
return "Playlist item '" + item[1]['name'] + "' already exists."
for item2 in enumerate(obj['playlists']):
if item[0] == item2[0]:
continue
if item[1]['name'] == item2[1]['name']:
return "Duplicate name '" + item[1]['name'] + "'."
for item in obj['playlists']:
loop = False
random = False
try:
loop = item['loop']
except KeyError:
pass
try:
random = item['random']
except KeyError:
pass
self.newPlaylist(item['name'], loop, random)
return None
def deletePlaylist(self, idx):
if self.currentPlaylist == idx:
self.currentPlaylist = None
elif self.currentPlaylist > idx:
self.currentPlaylist -= 1
del self.playlists[idx]
def deletePlaylists(self, obj):
if 'playlists' not in obj:
return "No 'playlists' list."
if type(obj['playlists']) != list:
return "'playlists' is not a list."
indices = []
for item in obj['playlists']:
if type(item) != str:
return "'playlists' item not a string."
pl = self.findPlaylistByName(item)
if pl is None:
return "Playlist " + item + " does not exist."
indices.append(pl[0])
for idx in indices:
self.deletePlaylist(idx)
return None
def addEntries(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
if 'entries' not in obj:
return "No 'entries'."
if type(obj['entries']) != list:
return "'entries' is not a list."
location = -1
try:
if type(obj['location']) != int:
return "'location' is not an integer."
else:
location = obj['location']
except KeyError:
pass
for item in obj['entries']:
if type(item) != dict:
return "'entries' item is not a dict."
if 'name' not in item:
return "'entries' item with no 'name'."
if type(item['name']) != str:
return "'name' is not a string."
try:
if type(item['played']) != bool:
return "'played' is not a bool."
except KeyError:
pass
for item in obj['entries']:
played = False
try:
played = item['played']
except KeyError:
pass
pl[1].insertEntry(item['name'], played, location)
if location >= 0:
location += 1
return None
def deleteEntries(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
if 'entries' not in obj:
return "No 'entries' list."
if type(obj['entries']) != list:
return "'entries' is not a list."
lastEntry = len(pl[1].entries) - 1
for entry in obj['entries']:
if type(entry) != int:
return "'entries' item is not an int."
if entry < 0 or entry > lastEntry:
return "Entry " + str(entry) + " out of range."
obj['entries'].sort(reverse=True)
for entry in obj['entries']:
pl[1].delEntryByIndex(entry)
return None
def getPlaylists(self):
playlists = []
for pl in self.playlists:
playlists.append({'name': pl.name, 'random': pl.random, 'loop': pl.loop, 'current': pl.currentCue})
return playlists
def setCurrentPlaylist(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if obj['playlist'] is None:
self.currentPlaylist = None
return None
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
if len(pl[1].entries) == 0:
return "Playlist " + obj['playlist'] + " has no entries."
self.currentPlaylist = pl[0]
return None
def setPlaylistCurrentEntry(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
if 'item' not in obj:
return "No 'item'."
if type(obj['item']) != int:
return "'item' is not an integer."
if obj['item'] < 0 or obj['item'] > len(pl[1].entries) - 1:
return "'item'=" + str(obj['item']) + " out of range."
pl[1].setCurrent(obj['item'])
return None
def setPlaylistRandom(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
value = None
try:
if type(obj['value']) != bool:
return "'value' is not a bool."
value = obj['value']
except KeyError:
pass
if value is None:
pl[1].random = not pl[1].random
obj['value'] = pl[1].random
else:
pl[1].random = value
return None
def setPlaylistLooping(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
value = None
try:
if type(obj['value']) != bool:
return "'value' is not a bool."
value = obj['value']
except KeyError:
pass
if value is None:
pl[1].loop = not pl[1].loop
obj['value'] = pl[1].loop
else:
pl[1].loop = value
return None
def setPlayed(self, obj):
if 'playlist' not in obj:
return "No 'playlist'."
if type(obj['playlist']) != str:
return "'playlist' is not a string."
pl = self.findPlaylistByName(obj['playlist'])
if pl is None:
return "Playlist " + obj['playlist'] + " does not exist."
value = None
if 'item' not in obj:
return "No 'item'."
if type(obj['item']) != int:
return "'item' is not an integer."
try:
if type(obj['value']) != bool:
return "'value' is not a bool."
value = obj['value']
except KeyError:
pass
if value is None:
pl[1].entries[obj['item']].played = not pl[1].entries[obj['item']].played
obj['value'] = pl[1].entries[obj['item']].played
else:
pl[1].entries[obj['item']].played = value
return None
def setMpvOpts(self, obj):
if 'opts' not in obj:
return "No 'opts'."
if type(obj['opts']) != dict:
return "'opts' is not a dict."
for key in obj['opts'].keys():
if type(key) != str:
return "Option key isn't a string."
if type(obj['opts'][key]) != str:
return "Option value isn't a string."
self.mpvopts = obj['opts']
return None
def addMPVOpt(self, name, value):
if type(name) != str or type(value) != str:
raise TypeError
self.mpvopts.update({name: value})
def findMPVOptsByName(self, name):
opts = []
for opt in enumerate(self.mpvopt):
if opt[1].name == name:
opts.append(opt)
return opts
def delMPVOptsByName(self, name):
opts = self.findMPVOptsByName(name)
for opt in opts:
del self.mpvopts[opt[0]]
return len(opts)
def getCurrent(self):
if self.currentPlaylist is None: # no playlist selected
return None
currentPlaylist = self.playlists[self.currentPlaylist]
if len(currentPlaylist.entries) == 0: # empty playlist selected
return None
if currentPlaylist.currentCue == -1: # reached end
return None
return (currentPlaylist.name,
currentPlaylist.currentCue,
currentPlaylist.entries[currentPlaylist.currentCue].item)
def advance(self):
if self.currentPlaylist is None: # no playlist selected
raise RuntimeError("No playlist selected.")
currentPlaylist = self.playlists[self.currentPlaylist]
if len(currentPlaylist.entries) == 0: # empty playlist selected
raise RuntimeError("Empty playlist.")
currentPlaylist.advance()