-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_livelist.py
342 lines (271 loc) · 11.7 KB
/
pp_livelist.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
import os
import json
import copy
import random
import configparser
import time
from pp_definitions import PPdefinitions
from pp_utils import Monitor
from pp_livelistfetcher import LiveListFetcher
class LiveList(object):
video_player='mpv'
def __init__(self,sequence):
self.mon=Monitor()
self.sequence=sequence
self._tracks=[]
self._num_tracks=0
self.last_num_tracks=-1
self.llf = LiveListFetcher() # start livelist fetcher
# ***************************
# Medialist Tracks
# ***************************
# medialist is kept for residual tracks with track references
def open_list(self,filename,profile_version):
"""
opens a saved medialist
medialists are stored as json arrays.
for liveshow medialist should contain only tracks with a track reference e.g. child track and empty track
"""
ifile = open(filename, 'r')
mdict = json.load(ifile)
ifile.close()
self.medialist_tracks = mdict['tracks']
if 'issue' in mdict:
self.medialist_version_string= mdict['issue']
else:
self.medialist_version_string="1.0"
if self.medialist_version()==profile_version:
return True
else:
return False
def medialist_version(self):
vitems=self.medialist_version_string.split('.')
if len(vitems)==2:
# cope with 2 digit version numbers before 1.3.2
return 1000*int(vitems[0])+100*int(vitems[1])
else:
return 1000*int(vitems[0])+100*int(vitems[1])+int(vitems[2])
# lookup index from track_ref
def index_of_track(self,wanted_track):
index = 0
for track in self.medialist_tracks:
if track['track-ref']==wanted_track:
return index
index +=1
return -1
# return dictionary of
def track(self,index):
return self.medialist_tracks[index]
# ***************************
# Livelist Tracks
# ***************************
# the methods mirror medialist methods for anonymous tracks
# get live_track directories from liveshow
def live_tracks(self,dir1,dir2):
self.pp_live_dir1=dir1
self.pp_live_dir2=dir2
# and pass them to the fetcher
self.llf.live_tracks(dir1,dir2)
def length(self):
return self._num_tracks
## def display_length(self):
## self.create_new_livelist()
## self._tracks=copy.deepcopy(self.new_livelist)
## self._num_tracks=len(self._tracks)
## self._selected_track_index=-1
## return self._num_tracks
def anon_length(self):
return self._num_tracks
def next(self,sequence):
if sequence in('ordered','reverse'):
if self._selected_track_index==self._num_tracks-1:
self._selected_track_index=0
else:
self._selected_track_index +=1
self.select(self._selected_track_index)
return True
else:
cand=random.randint(0,self._num_tracks-1)
# print '\nnext - initial cand',cand
if len(self.played_tracks)==self._num_tracks:
# all played so start again
# stop same track being played twice
if self.played_tracks[-1]==cand:
cand+=1
if cand == self._num_tracks:
cand=0
self.played_tracks=[cand]
self._selected_track_index = cand
# print 'all played',self._selected_track_index
# print self.played_tracks
self.select(self._selected_track_index)
return True
else:
while True:
# print 'trying',cand
if cand not in self.played_tracks:
self.played_tracks.append(cand)
self._selected_track_index = cand
# print 'add to played',self._selected_track_index
# print self.played_tracks
self.select(self._selected_track_index)
return True
else:
cand+=1
if cand == self._num_tracks:
cand=0
# print 'increment candidate to ',cand
def previous(self,sequence):
if sequence in ('ordered','reverse'):
if self._selected_track_index == 0:
self._selected_track_index=self._num_tracks-1
else:
self._selected_track_index -=1
self.select(self._selected_track_index)
return True
else:
cand=random.randint(0,self._num_tracks-1)
# print '\nprevious - initial cand',cand
if len(self.played_tracks)==self._num_tracks:
# all played so start again
# stop same track being played twice
if self.played_tracks[-1]==cand:
cand+=1
if cand == self._num_tracks:
cand=0
self.played_tracks=[cand]
self._selected_track_index = cand
# print 'all played',self._selected_track_index
# print self.played_tracks
self.select(self._selected_track_index)
return True
else:
while True:
# print 'trying',cand
if cand not in self.played_tracks:
self.played_tracks.append(cand)
self._selected_track_index = cand
# print 'add to played',self._selected_track_index
# print self.played_tracks
self.select(self._selected_track_index)
return True
else:
cand+=1
if cand == self._num_tracks:
cand=0
# print 'increment candidate to ',cand
def start(self):
if self._num_tracks==0:
return False
else:
self._selected_track_index=-1
self.played_tracks=[]
self.next(self.sequence)
return True
def finish(self):
if self._num_tracks==0:
return False
else:
self._selected_track_index=self._num_tracks-1
self.played_tracks=[]
self.next(self.sequence)
return True
def at_start(self):
if self._selected_track_index==0:
return True
else:
return False
def at_end(self):
if self._selected_track_index==self._num_tracks-1:
return True
else:
return False
def select(self,index):
"""does housekeeping necessary when a track is selected"""
if self._num_tracks>0 and index>=0 and index< self._num_tracks:
self._selected_track_index=index
self._selected_track = self._tracks[index]
return True
else:
return False
def selected_track(self):
"""returns a dictionary containing all fields in the selected track """
return self._selected_track
# ***************************
# Constructing NEW Livelist
# ***************************
def livelist_changed(self):
if self.new_livelist != self._tracks:
return True
else:
return False
def new_length(self):
return len(self.new_livelist)
def use_new_livelist(self):
self.last_num_tracks=self._num_tracks
# will have only anonymous tracks
self._tracks=copy.deepcopy(self.new_livelist)
self._num_tracks=len(self._tracks)
self._selected_track_index=-1
return True
def create_new_livelist(self,sequence):
# fetch new livelist if available
self.llf.fetch_livelist()
self.new_livelist=[]
if os.path.exists(self.pp_live_dir1):
for track_file in os.listdir(self.pp_live_dir1):
track_file = self.pp_live_dir1 + os.sep + track_file
(root_name,leaf)=os.path.split(track_file)
if leaf[0] == '.':
continue
else:
(root_file,ext_file)= os.path.splitext(track_file)
if (ext_file.lower() in PPdefinitions.IMAGE_FILES+PPdefinitions.VIDEO_FILES+PPdefinitions.AUDIO_FILES+PPdefinitions.WEB_FILES) or (ext_file.lower()=='.cfg'):
self.livelist_add_track(track_file)
if os.path.exists(self.pp_live_dir2):
for track_file in os.listdir(self.pp_live_dir2):
track_file = self.pp_live_dir2 + os.sep + track_file
(root_name,leaf)=os.path.split(track_file)
if leaf[0] == '.':
continue
else:
(root_file,ext_file)= os.path.splitext(track_file)
if (ext_file.lower() in PPdefinitions.IMAGE_FILES+PPdefinitions.VIDEO_FILES+PPdefinitions.AUDIO_FILES+PPdefinitions.WEB_FILES) or (ext_file.lower()=='.cfg'):
self.livelist_add_track(track_file)
if sequence in('ordered','shuffle'):
self.new_livelist= sorted(self.new_livelist, key= lambda track: os.path.basename(track['location']).lower(),reverse=False)
else:
self.new_livelist= sorted(self.new_livelist, key= lambda track: os.path.basename(track['location']).lower(),reverse=True)
# self.print_livelist()
def print_livelist(self):
print('LIVELIST')
for it in self.new_livelist:
print('type: ', it['type'], 'loc: ',it['location'],'\nplugin cfg: ', it['plugin'])
print('')
def livelist_add_track(self,afile):
(root,title)=os.path.split(afile)
(root_plus,ext)= os.path.splitext(afile)
if ext.lower() in PPdefinitions.IMAGE_FILES:
self.livelist_new_track(PPdefinitions.new_tracks['image'],{'title':title,'track-ref':'','location':afile})
if ext.lower() in PPdefinitions.VIDEO_FILES:
self.livelist_new_track(PPdefinitions.new_tracks[LiveList.video_player],{'title':title,'track-ref':'','location':afile})
if ext.lower() in PPdefinitions.AUDIO_FILES:
self.livelist_new_track(PPdefinitions.new_tracks['audio'],{'title':title,'track-ref':'','location':afile})
if ext.lower() in PPdefinitions.WEB_FILES:
self.livelist_new_track(PPdefinitions.new_tracks['chrome'],{'title':title,'track-ref':'','location':afile})
if ext.lower()=='.cfg':
self.livelist_new_plugin(afile,title)
def livelist_new_plugin(self,plugin_cfg,title):
# read the file which is a plugin cfg file into a dictionary
self.plugin_config = configparser.ConfigParser(inline_comment_prefixes = (';',))
self.plugin_config.read(plugin_cfg)
self.plugin_params = dict(self.plugin_config.items('plugin'))
# create a new livelist entry of a type specified in the config file with plugin
# miss entry if type is not config file
if 'type' in self.plugin_params:
self.livelist_new_track(PPdefinitions.new_tracks[self.plugin_params['type']],{'title':title,'track-ref':'','plugin':plugin_cfg,'location':plugin_cfg})
def livelist_new_track(self,fields,values):
new_track=fields
self.new_livelist.append(copy.deepcopy(new_track))
last = len(self.new_livelist)-1
self.new_livelist[last].update(values)