summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/VcrIndex.py
blob: 80212ee56193e0deb39444a924df883e7d583abc (plain)
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
#
# A VCR index.
#
import os
import string

error='VcrIndex.error'
VERSION_STRING='#!VcrIndex 1.1\n'
PREV_VERSION_STRING='#!VcrIndex 1.0\n'

class VcrIndex:
	def __init__(self, name):
		self.curmovie = None
		self.curscene = None
		self.modified = 0
		self.filename = name
		self.basename = os.path.basename(name)
		self.editable = []
		if not name:
			self.movies = {}
			return
		try:
			fp = open(name, 'r')
		except IOError:
			self.movies = {}
			return
		header = fp.readline()
		if header == PREV_VERSION_STRING:
			print 'Converting old-format database...'
			data = fp.read(100000)
			self.movies = eval(data)
			for m in self.movies.keys():
				d = self.movies[m]
				newd = {}
				for s in d.keys():
					newsd = {}
					newsd['START'] = d[s]
					if s == 'START':
						s = '-ALL-'
					newd[s] = newsd
				
				self.movies[m] = newd
			print 'Done.'
			return
		if header <> VERSION_STRING:
			print 'VcrIndex: incorrect version string:', header
			self.movies = {}
			return
		data = fp.read(100000)
		self.movies = eval(data)
	#
	# Save database to given file (or same file as read from if no
	# filename given).
	# 
	def save(self, name):
		if not name:
			name = self.filename
		if not name:
			raise error, 'No filename given'
		self.filename = name
		bupname = name + '~'
		try:
			os.unlink(bupname)
		except os.error:
			pass
		try:
			os.rename(name, bupname)
		except os.error:
			pass
		fp = open(name, 'w')
		data = str(self.movies)
		fp.write(VERSION_STRING)
		fp.write(data)
		fp.write('\n')
		fp.close()
		self.modified = 0
	#
	# Get a list of movie names in tape order
	#
	def get_movienames(self):
		names = self.movies.keys()
		sorted = []
		for name in names:
			sorted.append(self.movies[name]['-ALL-']['START'], name)
		sorted.sort()
		rv = []
		for pos, name in sorted:
			rv.append(name)
		return rv
	#
	# Get a list of scene names in tape order
	#
	def get_scenenames(self):
		if not self.curmovie:
			return []
		scenedict = self.movies[self.curmovie]
		names = scenedict.keys()
		sorted = []
		for name in names:
			sorted.append(scenedict[name], name)
		sorted.sort()
		rv = []
		for pos, name in sorted:
			rv.append(name)
		return rv
	#
	# Get a list of scene ids (format '00:02:32:12 name') in tape order
	#
	def get_sceneids(self):
		if not self.curmovie:
			return []
		scenedict = self.movies[self.curmovie]
		names = scenedict.keys()
		sorted = []
		for name in names:
			sorted.append(scenedict[name]['START'], name)
		sorted.sort()
		rv = []
		for pos, name in sorted:
			str = '%02d:%02d:%02d:%02d ' % pos
			rv.append(str + name)
		return rv
	#
	# Does a movie exist?
	#
	def movie_exists(self, name):
		return self.movies.has_key(name)
	#
	# Select a movie.
	#
	def movie_select(self, name):
		if not self.movies.has_key(name):
			raise error, 'No such movie: '+name
		self.curmovie = name
		self.curscene = None
	#
	# Get movie dictionary, or raise an error if no current movie.
	#
	def _getmoviedict(self):
		if not self.curmovie:
			raise error, 'No current movie'
		return self.movies[self.curmovie]
	#
	# Rename a movie.
	#
	def movie_rename(self, newname):
		scenedict = self._getmoviedict()
		if self.movie_exists(newname):
			raise error, 'Movie already exists: '+newname
		del self.movies[self.curmovie]
		self.movies[newname] = scenedict
		self.curmovie = newname
		self.modified = 1
	#
	# Copy a movie.
	#
	def movie_copy(self, newname):
		scenedict = self._getmoviedict()
		if self.movie_exists(newname):
			raise error, 'Movie already exists: '+newname
		newdict = {}
		for k in scenedict.keys():
			olddata = scenedict[k]
			newdata = {}
			for i in olddata.keys():
				newdata[i] = olddata[i]
			newdict[k] = newdata
		self.movies[newname] = newdict
		self.curmovie = newname
		self.modified = 1
	#
	# Delete a movie.
	#
	def movie_delete(self):
		if not self.curmovie:
			raise error, 'No current movie'
		del self.movies[self.curmovie]
		self.curmovie = None
		self.curscene = None
		self.modified = 1
	#
	# Create a new movie.
	#
	def movie_new(self, name, pos):
		if self.movie_exists(name):
			raise error, 'Movie already exists: '+name
		newdict = {}
		newsdict = {}
		newsdict['START'] = pos
		newdict['-ALL-'] = newsdict
		self.movies[name] = newdict
		self.curmovie = name
		self.curscene = None
		self.modified = 1
	#
	# Does a scene exist?
	#
	def scene_exists(self, name):
		scenedict = self._getmoviedict()
		return scenedict.has_key(name)
	#
	# Select a current scene.
	#
	def scene_select(self, name):
		scenedict = self._getmoviedict()
		if not scenedict.has_key(name):
			raise error, 'No such scene: '+name
		self.curscene = name
	#
	# Rename a scene.
	#
	def scene_rename(self, newname):
		scenedict = self._getmoviedict()
		if not self.curscene:
			raise error, 'No current scene'
		if scenedict.has_key(newname):
			raise error, 'Scene already exists: '+newname
		if self.curscene == '-ALL-':
			raise error, 'Cannot rename -ALL-'
		scenedict[newname] = scenedict[self.curscene]
		del scenedict[self.curscene]
		self.curscene = newname
		self.modified = 1
	#
	# Copy a scene.
	#
	def scene_copy(self, newname):
		scenedict = self._getmoviedict()
		if not self.curscene:
			raise error, 'No current scene'
		if scenedict.has_key(newname):
			raise error, 'Scene already exists: '+newname
		scenedict[newname] = scenedict[self.curscene]
		self.curscene = newname
		self.modified = 1
	#
	# Delete a scene.
	#
	def scene_delete(self):
		scenedict = self._getmoviedict()
		if not self.curscene:
			raise error, 'No current scene'
		if self.curscene == '-ALL-':
			raise error, 'Cannot delete -ALL-'
		del scenedict[self.curscene]
		self.curscene = None
		self.modified = 1
	#
	# Add a new scene.
	#
	def scene_new(self, newname, pos):
		scenedict = self._getmoviedict()
		if scenedict.has_key(newname):
			raise error, 'Scene already exists: '+newname
		newdict = {}
		newdict['START'] = pos
		scenedict[newname] = newdict
		self.curscene = newname
		self.modified = 1
	#
	# Get scene data.
	#
	def _getscenedata(self):
		scenedict = self._getmoviedict()
		if not self.curscene:
			raise error, 'No current scene'
		return scenedict[self.curscene]
	#
	# Data manipulation routines.
	#
	def pos_get(self):
		return self._getscenedata()['START']
	#
	def pos_set(self, pos):
		data = self._getscenedata()
		data['START'] = pos
		self.modified = 1
	#
	def comment_get(self):
		data = self._getscenedata()
		if data.has_key('COMMENT'):
			return data['COMMENT']
		else:
			return ''
	#
	def comment_set(self, comment):
		data = self._getscenedata()
		data['COMMENT'] = comment
		self.modified = 1
	#
	# Get the scene id of the current scene.
	#
	def get_cursceneid(self):
		pos = self._getscenedata()['START']
		str = '%02d:%02d:%02d:%02d ' % pos
		return str + self.curscene
	#
	# Convert a scene id to a scene name.
	#
	def scene_id2name(self, id):
		pos = string.find(id, ' ')
		if pos <= 0:
			raise error, 'Not a scene id: '+id
		return id[pos+1:]
	#
	# Select a scene given a position.
	#
	def pos_select(self, pos):
		prevmovie = None
		movies = self.get_movienames()
		for movie in movies:
			mpos = self.movies[movie]['-ALL-']['START']
			if mpos > pos:
				break
			prevmovie = movie
		if not prevmovie:
			raise error, 'Scene before BOT'
			
		self.movie_select(prevmovie)
		scenes = self.get_scenenames()
		scenedict = self._getmoviedict()
		prevscene = 'START'
		for scene in scenes:
			if scenedict[scene]['START'] > pos:
				break
			prevscene = scene
		self.scene_select(prevscene)