-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpp_medialist.py
380 lines (309 loc) · 11.1 KB
/
pp_medialist.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
import json
import copy
import string
import random
from pp_utils import Monitor
"""
31/12/2016 - fixed crash if mediashow id shffled and there is one track - taks Drew Keller
"""
# *************************************
# MEDIALIST CLASS
# ************************************
class MediaList(object):
"""
manages a media list of tracks and the track selected from the medialist
"""
def __init__(self,sequence):
self.clear()
self.mon=Monitor()
self.sequence=sequence
# Functions for the editor dealing with complete list
def clear(self):
self._tracks = [] #MediaList, stored as a list of dicts
self._num_tracks=0
self._selected_track_index=-1 # index of currently selected track
def print_list(self):
print('\n')
print(self._tracks)
def first(self):
self._selected_track_index=-1
self.next(self.sequence) #let this do the work of randomaising or advancing to 0
def length(self):
return self._num_tracks
def append(self, track_dict):
# print '\ntrack dict',track_dict
"""appends a track dictionary to the end of the medialist store"""
self._tracks.append(copy.deepcopy(track_dict))
self._num_tracks+=1
def update(self,index,values):
self._tracks[index].update(values)
def remove(self,index):
self._tracks.pop(index)
self._num_tracks-=1
# deselect any track, saves worrying about whether index needs changing
self._selected_track_index=-1
def move_up(self):
if self._selected_track_index != 0:
self._tracks.insert(self._selected_track_index-1, self._tracks.pop(self._selected_track_index))
self.select(self._selected_track_index-1)
def move_down(self):
if self._selected_track_index != self._num_tracks-1:
self._tracks.insert(self._selected_track_index+1, self._tracks.pop(self._selected_track_index))
self.select(self._selected_track_index+1)
def copy(self):
self._tracks.insert(self._selected_track_index+1, copy.deepcopy(self._tracks[self._selected_track_index]))
self._num_tracks+=1
self.select(self._selected_track_index+1)
def replace(self,index,replacement):
self._tracks[index]= replacement
# Common functions work for anything
def track_is_selected(self):
if self._selected_track_index>=0:
return True
else:
return False
def selected_track_index(self):
return self._selected_track_index
def track(self,index):
return self._tracks[index]
def selected_track(self):
"""returns a dictionary containing all fields in the selected track """
return self._selected_track
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
# Dealing with anonymous tracks for use and display
def at_end(self):
# true is selected track is last anon
index=self._num_tracks-1
while index>=0:
if self._tracks[index] ['track-ref'] =="":
end=index
if self._selected_track_index==end:
return True
else:
return False
index -=1
return False
def index_of_end(self):
if self.anon_length()==0:
return False
index=self._num_tracks-1
while index >= 0:
if self._tracks[index] ['track-ref'] =="":
return index
index -=1
return -1
def at_start(self):
if self.anon_length()==0:
return False
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
start = index
if self._selected_track_index==start:
return True
else:
return False
index +=1
return False
def index_of_start(self):
if self.anon_length()==0:
return False
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
return index
index +=1
return False
def anon_length(self):
# number of anonymous tracks
count=0
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
count+=1
index +=1
return count
def start(self):
if self.anon_length()==0:
return False
# select first anonymous track in the list
if self.sequence in ('ordered','reverse'):
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
self.select(index)
return True
index +=1
return False
else:
match=random.randint(0,self.anon_length()-1)
# print 'match',match
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="" and index==match:
self.select(index)
# print index
return index
index +=1
def finish(self):
if self.anon_length()==0:
return False
if self.sequence in ('ordered','reverse'):
# select last anymous track in the list
index=self._num_tracks-1
while index>=0:
if self._tracks[index] ['track-ref'] =="":
self.select(index)
return True
index -=1
return False
else:
match=random.randint(0,self.anon_length()-1)
# print 'match',match
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="" and index==match:
self.select(index)
# print index
return index
index +=1
def select_anon_by_index(self,wanted):
if self.anon_length()==0:
return False
index=0
anon_index=0
while index != self._num_tracks:
# print index,self._tracks[index] ['track-ref'],wanted
if self._tracks[index] ['track-ref'] =="":
if anon_index==wanted:
# print 'match\n'
self.select(index)
return True
anon_index+=1
index= index+1
return False
def next(self,sequence):
if self.anon_length()==0:
return False
if sequence in ('ordered','reverse'):
if self._selected_track_index== self._num_tracks-1:
index=0
else:
index= self._selected_track_index+1
end=self._selected_track_index
else:
index=random.randint(0,self.anon_length()-1)
if index==0:
end=self._num_tracks-1
else:
end=index-1
# search for next anonymous track
# print 'index', index, 'end',end
while index != end:
if self._tracks[index] ['track-ref'] =="":
self.select(index)
return True
if index== self._num_tracks-1:
index=0
else:
index= index+1
return False
def previous(self,sequence):
if self.anon_length()==0:
return False
if sequence in ('ordered','reverse'):
if self._selected_track_index == 0:
index=self._num_tracks-1
else:
index= self._selected_track_index-1
end = self._selected_track_index
else:
index=random.randint(0,self.anon_length()-1)
if index==self._num_tracks-1:
end=0
else:
end=index+1
# print 'index', index, 'end',end
# search for previous anonymous track
while index != end :
if self._tracks[index] ['track-ref'] =="":
self.select(index)
return True
if index == 0:
index=self._num_tracks-1
else:
index= index-1
return False
# Lookup for labelled tracks
def index_of_track(self,wanted_track):
index = 0
for track in self._tracks:
if track['track-ref']==wanted_track:
return index
index +=1
return -1
# open and save
def open_list(self,filename,profile_version):
"""
opens a saved medialist
medialists are stored as json arrays.
"""
ifile = open(filename, 'r')
mdict = json.load(ifile)
ifile.close()
self._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:
self._num_tracks=len(self._tracks)
self.last_num_tracks=self._num_tracks
self._selected_track_index=-1
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])
# dummy for mediliast, in livelist the list is created from the live_track directories
def use_new_livelist(self):
pass
def create_new_livelist(self,sequence=None):
pass
def new_length(self):
return self.length()
# for medialist the content of the list never changes so return False
def livelist_changed(self):
return False
def save_list(self,filename):
""" save a medialist """
if filename=="":
return False
dic={'issue':self.medialist_version_string,'tracks':self._tracks}
filename=str(filename)
filename = str.replace(filename,'\\','/')
tries = 1
while tries<=10:
# print "save medialist ",filename
try:
ofile = open(filename, "w")
json.dump(dic,ofile,sort_keys=True,indent=1)
ofile.close()
self.mon.log(self,"Saved medialist "+ filename)
break
except IOError:
self.mon.err(self,"failed to save medialist, trying again " + str(tries))
tries+=1
return