forked from KenT2/pipresents-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp_medialist.py
309 lines (253 loc) · 8.6 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
from glob import glob
import os
import csv
import json
import copy
import string
import random
from pp_utils import Monitor
# *************************************
# MEDIALIST CLASS
# ************************************
class MediaList:
"""
manages a media list of tracks and the track selected from the medialist
"""
def __init__(self):
self.clear()
self.mon=Monitor()
self.mon.on()
# 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.select(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 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):
index=self._num_tracks-1
while index >= 0:
if self._tracks[index] ['track-ref'] =="":
return index
index -=1
return -1
def at_start(self):
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):
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
return index
index +=1
return False
def display_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):
# select first anymous track in the list
index=0
while index<self._num_tracks:
if self._tracks[index] ['track-ref'] =="":
self.select(index)
return True
index +=1
return False
def finish(self):
# 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
def next(self,sequence):
if sequence=='ordered':
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._num_tracks-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 sequence=='ordered':
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._num_tracks-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,showlist_issue):
"""
opens a saved medialist
medialists are stored as json arrays.
"""
ifile = open(filename, 'rb')
mdict = json.load(ifile)
ifile.close()
self._tracks = mdict['tracks']
if 'issue' in mdict:
self.issue= mdict['issue']
else:
self.issue="1.0"
if self.issue==showlist_issue:
self._num_tracks=len(self._tracks)
self._selected_track_index=-1
return True
else:
return False
def issue(self):
return self.issue
def save_list(self,filename):
""" save a medialist """
if filename=="":
return False
dic={'issue':self.issue,'tracks':self._tracks}
filename=str(filename)
filename = string.replace(filename,'\\','/')
tries = 1
while tries<=10:
# print "save medialist ",filename
try:
ofile = open(filename, "wb")
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
# for the future
def open_csv(self,filename):
"""
opens a saved csv medialist
"""
if filename !="" and os.path.exists(filename):
ifile = open(filename, 'rb')
pl=csv.reader(ifile)
for pl_row in pl:
if len(pl_row) != 0:
entry=dict([('type',pl_row[2]),('location',pl_row[0]),('title',pl_row[1])])
self.append(copy.deepcopy(entry))
ifile.close()
return True
else:
return False