-
Notifications
You must be signed in to change notification settings - Fork 17
/
Clip.py
1115 lines (1021 loc) · 52.6 KB
/
Clip.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2003 - 2015 The Board of Regents of the University of Wisconsin System
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
"""This module implements the Clip class as part of the Data Objects."""
__author__ = 'David Woods <[email protected]>, Nathaniel Case'
DEBUG = False
if DEBUG:
print "Clip DEBUG is ON!"
# import wxPython
import wx
# import Python's os module
import os
# import Python's types module
import types
# import Transana's Clip Keyword object
import ClipKeywordObject
# import Transana's Collection Object
import Collection
# Import the Transana base Data Object
import DataObject
# Import the Transana Database Interface
import DBInterface
# import Transana's Dialogs
import Dialogs
# Import Transana's Episode Object
import Episode
# import Transana's Miscellaneous Functions
import Misc
# import Transana's Note Object
import Note
# Import Transana's exceptions
from TransanaExceptions import *
# Import the Transana Constants
import TransanaConstants
# Import Transana's Global Variables
import TransanaGlobal
# import Transana's Transcript object
import Transcript
TIMECODE_CHAR = "\\'a4" # Note that this differs from the TIMECODE_CHAR in TranscriptEditor.py
# because this is for RTF text and that is for parsed text.
class Clip(DataObject.DataObject):
"""This class defines the structure for a clip object. A clip object
describes a portion of a video (or other media) file."""
def __init__(self, id_or_num=None, collection_name=None, collection_parent=0, skipText=False):
"""Initialize an Clip object."""
# skipText indicates that the transcripts can be left off. This leads to significantly faster transcript loading
# particularly when we start having large transcripts with embedded images.
# Create a Data Object
DataObject.DataObject.__init__(self)
# By default, use the Video Root folder if one has been defined
self.useVideoRoot = (TransanaGlobal.configData.videoPath != '')
# Remember if we're supposed to skip the RTF Text
self.skipText = skipText
if type(id_or_num) in (int, long):
self.db_load_by_num(id_or_num)
elif isinstance(id_or_num, types.StringTypes):
self.db_load_by_name(id_or_num, collection_name, collection_parent)
else:
self.number = 0
self.id = ''
self.comment = ''
self.collection_num = 0
self.collection_id = ''
self.episode_num = 0
# Keep a list of transcript objects associated with this clip
self.transcripts = []
self.media_filename = ""
# Initialize an empty list for additional media files
self.additional_media_files = []
self.clip_start = 0
self.clip_stop = 0
# With multiple videos, it's possible the clip time values need an offset
# (if the first Episode video isn't included in the Clip.)
self.offset = 0
# With multiple videos, it's possible to not want the AUDIO for the first video track,
# but default to True
self.audio = 1
self.sort_order = 0
# Create empty placeholders for Series and Episode IDs. These only get populated if the
# values are needed, and cannot be implemented in the regular LOADs because the Series/
# Episode may no longer exist.
self._series_id = ""
self._episode_id = ""
def __repr__(self):
str = 'Clip Object Definition:\n'
str = str + "number = %s\n" % self.number
str = str + "id = %s\n" % self.id
str = str + "comment = %s\n" % self.comment
str = str + "collection_num = %s\n" % self.collection_num
str = str + "collection_id = %s\n" % self.collection_id
str = str + "episode_num = %s\n" % self.episode_num
str = str + "media_filename = %s\n" % self.media_filename
str = str + "Additional media file:\n"
for addFile in self.additional_media_files:
str += ' %s %s %s %s\n' % (addFile['filename'], addFile['offset'], addFile['length'], addFile['audio'])
str = str + "clip_start = %s (%s)\n" % (self.clip_start, Misc.time_in_ms_to_str(self.clip_start))
str = str + "clip_stop = %s (%s)\n" % (self.clip_stop, Misc.time_in_ms_to_str(self.clip_stop))
str += "offset = %s\n" % Misc.time_in_ms_to_str(self.offset)
str += "audio = %s\n" % self.audio
str = str + "sort_order = %s\n" % self.sort_order
# str += "isLocked = %s\n" % self._isLocked
# str += "recordlock = %s\n" % self.recordlock
# str += "locktime = %s\n" % self.locktime
# Iterate through transcript objects
for tr in self.transcripts:
str = str + '\nClip Transcript: %s from %s\n' % (tr.number, tr.source_transcript)
str = str + '%s\n' % tr
for kws in self.keyword_list:
str = str + "\nKeyword: %s" % kws.keywordPair
str = str + '\n'
return str.encode('utf8')
def __eq__(self, other):
""" Compares two Clip objects to each other """
if other == None:
return False
else:
## print "Clip.__eq__():", len(self.__dict__.keys()), len(other.__dict__.keys())
## for key in self.__dict__.keys():
## print key, self.__dict__[key] == other.__dict__[key],
## if self.__dict__[key] != other.__dict__[key]:
## print self.__dict__[key], other.__dict__[key]
## else:
## print
## print
return self.__dict__ == other.__dict__
# Public methods
def GetTranscriptWithoutTimeCodes(self):
""" Returns a copy of the Transcript Text with the Time Code information removed. """
newText = self.text
while True:
timeCodeStart = newText.find(TIMECODE_CHAR)
if timeCodeStart == -1:
break
timeCodeEnd = newText.find('>', timeCodeStart)
newText = newText[:timeCodeStart] + newText[timeCodeEnd + 1:]
# We should also replace TAB characters with spaces
while True:
tabStart = newText.find(chr(wx.WXK_TAB), 0)
if tabStart == -1:
break
newText = newText[:tabStart] + ' ' + newText[tabStart + 1:]
return newText
def db_load_by_name(self, clip_name, collection_name, collection_parent=0):
"""Load a record by ID / Name."""
self.clear()
# If we're in Unicode mode, we need to encode the parameter so that the query will work right.
if 'unicode' in wx.PlatformInfo:
clip_name = clip_name.encode(TransanaGlobal.encoding)
collection_name = collection_name.encode(TransanaGlobal.encoding)
# Get a database connection
db = DBInterface.get_db()
# craft a query to get Clip data
query = """ SELECT a.*, b.*
FROM Clips2 a, Collections2 b
WHERE ClipID = %s AND
a.CollectNum = b.CollectNum AND
b.CollectID = %s AND
b.ParentCollectNum = %s """
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Get a database cursor
c = db.cursor()
# Execute the query
c.execute(query, (clip_name, collection_name, collection_parent))
# rowcount doesn't work for sqlite!
if TransanaConstants.DBInstalled == 'sqlite3':
n = 1
else:
n = c.rowcount
# if we don't get exactly one result ...
if (n != 1):
# close the cursor
c.close()
# clear the current Clip
self.clear()
# Raise an exception saying the record is not found
raise RecordNotFoundError, (collection_name + ", " + clip_name, n)
# If we get exactly one result ...
else:
# ... get the data categories from the cursor
r = DBInterface.fetch_named(c)
# if sqlite and no results ...
if (TransanaConstants.DBInstalled == 'sqlite3') and (r == {}):
# ... close the database cursor ...
c.close()
# ... clear the current object ...
self.clear()
# ... and raise an exception
raise RecordNotFoundError, (collection_name + ", " + clip_name, n)
# Load the data into the Clip object
self._load_row(r)
# Load additional Media Files, which aren't handled in the "old" code
self.load_additional_vids()
# Refresh the Keywords
self.refresh_keywords()
# Close the Database Cursor
c.close()
def db_load_by_num(self, num):
"""Load a record by record number."""
self.clear()
# Get a database Connection
db = DBInterface.get_db()
# Craft a query to get the Clip data
query = """
SELECT a.*, b.*
FROM Clips2 a, Collections2 b
WHERE a.ClipNum = %s AND
a.CollectNum = b.CollectNum
"""
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Get a database cursor
c = db.cursor()
# Execute the query
c.execute(query, (num, ))
# rowcount doesn't work for sqlite!
if TransanaConstants.DBInstalled == 'sqlite3':
n = 1
else:
n = c.rowcount
# If we don't get exactly one result ...
if (n != 1):
# close the database cursor
c.close()
# clear the current Clip object
self.clear()
# Raise an exception indicating the data was not found
raise RecordNotFoundError, (num, n)
# If we get exactly one result (or use sqlite) ...
else:
# ... get the data from the cursor
r = DBInterface.fetch_named(c)
# If sqlite and no results ...
if (TransanaConstants.DBInstalled == 'sqlite3') and (r == {}):
# ... close the cursor ...
c.close()
# ... clear the current object ...
self.clear()
# ... and raise an exception
raise RecordNotFoundError, (num, 0)
# ... load the data into the Clip Object
self._load_row(r)
# Load Additional Media Files, which aren't handled in the "old" code
self.load_additional_vids()
# Refresh the Keywords
self.refresh_keywords()
# Close the database cursor
c.close()
def db_save(self, use_transactions=True):
"""Save the record to the database using Insert or Update as appropriate."""
# Define and implement Demo Version limits
if TransanaConstants.demoVersion and (self.number == 0):
# Get a DB Cursor
c = DBInterface.get_db().cursor()
# Find out how many records exist
c.execute('SELECT COUNT(ClipNum) from Clips2')
res = c.fetchone()
c.close()
# Define the maximum number of records allowed
maxClips = TransanaConstants.maxClips
# Compare
if res[0] >= maxClips:
# If the limit is exceeded, create and display the error using a SaveError exception
prompt = _('The Transana Demonstration limits you to %d Clip records.\nPlease cancel the "Add Clip" dialog to continue.')
if 'unicode' in wx.PlatformInfo:
prompt = unicode(prompt, 'utf8')
raise SaveError, prompt % maxClips
# Sanity checks
if self.id == "":
raise SaveError, _("Clip ID is required.")
elif (self.collection_num == 0):
raise SaveError, _("Parent Collection number is required.")
elif self.media_filename == "":
raise SaveError, _("Media Filename is required.")
# If a user Adjusts Indexes, it's possible to have a clip that starts BEFORE the media file.
elif self.clip_start < 0.0:
raise SaveError, _("Clip cannot start before media file begins.")
else:
# videoPath probably has the OS.sep character, but we need the generic "/" character here.
videoPath = TransanaGlobal.configData.videoPath
# Determine if we are supposed to extract the Video Root Path from the Media Filename and extract it if appropriate
if self.useVideoRoot and (videoPath == self.media_filename[:len(videoPath)]):
tempMediaFilename = self.media_filename[len(videoPath):]
else:
tempMediaFilename = self.media_filename
# Substitute the generic OS seperator "/" for the Windows "\".
tempMediaFilename = tempMediaFilename.replace('\\', '/')
# If we're not in Unicode mode ...
if 'ansi' in wx.PlatformInfo:
# ... we don't need to encode the string values, but we still need to copy them to our local variables.
id = self.id
comment = self.comment
# If we're in Unicode mode ...
else:
# Encode strings to UTF8 before saving them. The easiest way to handle this is to create local
# variables for the data. We don't want to change the underlying object values. Also, this way,
# we can continue to use the Unicode objects where we need the non-encoded version. (error messages.)
id = self.id.encode(TransanaGlobal.encoding)
tempMediaFilename = tempMediaFilename.encode(TransanaGlobal.encoding)
videoPath = videoPath.encode(TransanaGlobal.encoding)
comment = self.comment.encode(TransanaGlobal.encoding)
self._sync_collection()
values = (id, self.collection_num, self.episode_num, \
tempMediaFilename, \
self.clip_start, self.clip_stop, self.offset, self.audio, comment, \
self.sort_order)
if (self._db_start_save() == 0):
if DBInterface.record_match_count("Clips2", ("ClipID", "CollectNum"), (id, self.collection_num) ) > 0:
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(_('A Clip named "%s" already exists in this Collection.\nPlease enter a different Clip ID.'), 'utf8') % self.id
else:
prompt = _('A Clip named "%s" already exists in this Collection.\nPlease enter a different Clip ID.') % self.id
raise SaveError, prompt
# Duplicate Clip IDs with a Quote ID within a Collection are not allowed.
if DBInterface.record_match_count("Quotes2", ("QuoteID", "CollectNum"), (id, self.collection_num)) > 0:
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(_('A Quote named "%s" already exists in this Collection.\nPlease enter a different Clip ID.'), 'utf8')
else:
prompt = _('A Quote named "%s" already exists in this Collection.\nPlease enter a different Clip ID.')
raise SaveError, prompt % self.id
# insert the new record
query = """
INSERT INTO Clips2
(ClipID, CollectNum, EpisodeNum,
MediaFile, ClipStart, ClipStop, ClipOffset, Audio, ClipComment,
SortOrder)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
else:
if DBInterface.record_match_count("Clips2", ("ClipID", "CollectNum", "!ClipNum"),
(id, self.collection_num, self.number)) > 0:
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(_('A Clip named "%s" already exists in this Collection.\nPlease enter a different Clip ID.'), 'utf8') % self.id
else:
prompt = _('A Clip named "%s" already exists in this Collection.\nPlease enter a different Clip ID.') % self.id
raise SaveError, prompt
# Duplicate Clip ID with Quote ID within a Collection are not allowed.
if DBInterface.record_match_count("Quotes2", ("QuoteID", "CollectNum"),
(id, self.collection_num)) > 0:
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(_('A Quote named "%s" already exists in this Collection.\nPlease enter a different Clip ID.'), 'utf8')
else:
prompt = _('A Quote named "%s" already exists in this Collection.\nPlease enter a different Clip ID.')
raise SaveError, prompt % self.id
# update the record
query = """
UPDATE Clips2
SET ClipID = %s,
CollectNum = %s,
EpisodeNum = %s,
MediaFile = %s,
ClipStart = %s,
ClipStop = %s,
ClipOffset = %s,
Audio = %s,
ClipComment = %s,
SortOrder = %s
WHERE ClipNum = %s
"""
values = values + (self.number,)
# Get a database cursor
c = DBInterface.get_db().cursor()
# If we're using Transactions ...
if use_transactions:
# ... start the transaction
c.execute('BEGIN')
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Execute the Insert / Update query
c.execute(query, values)
# If this is a NEW record
if self.number == 0:
# ... signal that the number has been changed
numberChanged = True
# If we are dealing with a brand new Clip, it does not yet know its
# record number. It HAS a record number, but it is not known yet.
# The following query should produce the correct record number.
query = """
SELECT ClipNum FROM Clips2
WHERE ClipID = %s AND
CollectNum = %s
"""
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Get a database cursor
tempDBCursor = DBInterface.get_db().cursor()
# Execute the query
tempDBCursor.execute(query, (id, self.collection_num))
# Get the query results
data = tempDBCursor.fetchall()
# If there ARE results ...
if len(data) == 1:
# ... extract the object number
self.number = data[0][0]
# If there are NOT results ...
else:
# ... and we are using Transactions ...
if use_transactions:
# ... then roll back the transaction, as there is a problem
c.execute('ROLLBACK')
# Raise an exception
raise RecordNotFoundError, (self.id, len(data))
# Close the database cursor
tempDBCursor.close()
# If this record already has a record number ...
else:
# ... signal that the record number was not changed
numberChanged = False
# If we are dealing with an existing Clip, delete all the Keywords
# in anticipation of putting them all back in after we deal with the
# Clip Transcript
DBInterface.delete_all_keywords_for_a_group(0, 0, self.number, 0, 0)
# Now let's deal with the Clip's Transcripts
# If we're NOT skipping the Transcripts ...
if not self.skipText:
# For each transcript in the list of clip transcripts ...
for tr in self.transcripts:
# Assign the clip's number as the transcript's clip number
tr.clip_num = self.number
# There is a spot (in XML Import) where we signal that we DON'T want the Clip
# Transcript saved by setting its number to -1.
if tr.number != -1:
# save the transcript
tr.db_save()
# If we're not saving Transcripts, we can also skip Additional Videos!!
# To save the additional video file names, we must first delete them from the database!
# Craft a query to remove all existing Additonal Videos
query = "DELETE FROM AdditionalVids2 WHERE ClipNum = %s"
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Execute the query
c.execute(query, (self.number, ))
# Define the query to insert the additional media files into the databse
query = "INSERT INTO AdditionalVids2 (EpisodeNum, ClipNum, MediaFile, VidLength, Offset, Audio) VALUES (%s, %s, %s, %s, %s, %s)"
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# For each additional media file ...
for vid in self.additional_media_files:
# Encode the filename
tmpFilename = vid['filename'].encode(TransanaGlobal.encoding)
# Determine if we are supposed to extract the Video Root Path from the Media Filename and extract it if appropriate
if self.useVideoRoot and (videoPath == tmpFilename[:len(videoPath)]):
tmpFilename = tmpFilename[len(videoPath):]
# Substitute the generic OS seperator "/" for the Windows "\".
tmpFilename = tmpFilename.replace('\\', '/')
# In Windows single-user, audio of "True" isn't being stored correctly. It seems to be OK in MU. (Haven't checked Mac-SU.)
if vid['audio'] == True:
vid['audio'] = 1
# Get the data for each insert query
data = (0, self.number, tmpFilename, vid['length'], vid['offset'], vid['audio'])
# Execute the query
c.execute(query, data)
# Initialize a blank error prompt
prompt = ''
# Add the Clip keywords back. Iterate through the Keyword List
for kws in self._kwlist:
# Try to add the Clip Keyword record. If it is NOT added, the keyword has been changed by another user!
if not DBInterface.insert_clip_keyword(0, 0, self.number, 0, 0, kws.keywordGroup, kws.keyword, kws.example):
# if the prompt isn't blank ...
if prompt != '':
# ... add a couple of line breaks to it
prompt += u'\n\n'
# Add the current keyword to the error prompt
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt += unicode(_('Keyword "%s : %s" cannot be added to Clip "%s".\nAnother user must have edited the keyword while you were adding it.'), 'utf8') % (kws.keywordGroup, kws.keyword, self.id)
# If there is an error prompt ...
if prompt != '':
# If the Clip's Number was changed ...
if numberChanged:
# ... change it back to zero!!
self.number = 0
# If we're NOT skipping the Transcripts ...
if not self.skipText:
# For each transcript in the list of clip transcripts ...
for tr in self.transcripts:
# ... change the clip's number back to zero too.
tr.number = 0
tr.clip_num = 0
# Undo the database save transaction
if use_transactions:
c.execute('ROLLBACK')
# Close the Database Cursor
c.close()
# Complete the error prompt
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt += u'\n\n' + unicode(_('Please remove, and possibly replace, this keyword.'), 'utf8')
# ... raise a SaveError exception using the error prompt
raise SaveError, prompt
# If there's no error prompt ...
else:
# ... Commit the database transaction
if use_transactions:
c.execute('COMMIT')
# Close the Database Cursor
c.close()
def db_delete(self, use_transactions=True, examplesPrompt=True):
""" Delete this object record from the database. Parameters indicate if we should use DB Transactions
and if we should prompt about the deletion of Keyword Examples. (in Clip Merging, for example, we
don't want to prompt.) """
# Assume success to begin
result = 1
try:
# Initialize delete operation, begin transaction if necessary
(db, c) = self._db_start_delete(use_transactions)
# If this clip serves as a Keyword Example, we should prompt the user about
# whether it should really be deleted
kwExampleList = DBInterface.list_all_keyword_examples_for_a_clip(self.number)
if len(kwExampleList) > 0:
if len(kwExampleList) == 1:
prompt = _('Clip "%s" has been defined as a Keyword Example for Keyword "%s : %s".')
data = (self.id, kwExampleList[0][0], kwExampleList[0][1])
else:
prompt = _('Clip "%s" has been defined as a Keyword Example for multiple Keywords.')
data = self.id
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(prompt, 'utf8') % data
prompt = prompt + unicode(_('\nAre you sure you want to delete it?'), 'utf8')
else:
prompt = prompt % data
prompt = prompt + _('\nAre you sure you want to delete it?')
dlg = Dialogs.QuestionDialog(None, prompt, _('Delete Clip'))
# if we should prompt about examples, then show the dialog. If the user responds "No" ...
if examplesPrompt and (dlg.LocalShowModal() == wx.ID_NO):
# ... destroy the dialog box.
dlg.Destroy()
# A Transcaction was started and the record was locked in _db_start_delete(). Unlock it here if the
# user cancels the delete (after rolling back the Transaction)!
if self.isLocked:
# c (the database cursor) only exists if the record lock was obtained!
# if we are using Transactions locally ...
if use_transactions:
# We must roll back the transaction before we unlock the record.
c.execute("ROLLBACK")
# Close the database cursor
c.close()
# unlock the Clip record
self.unlock_record()
# Exit the Delete method, indicating the delete did not succeed.
return 0
# If we don't show the dialog at all, or if the users doesn't say "No" ...
else:
# ... then just destroy the dialog box.
dlg.Destroy()
# Detect, Load, and Delete all Clip Notes.
notes = self.get_note_nums()
for note_num in notes:
note = Note.Note(note_num)
result = result and note.db_delete(0)
del note
del notes
# Okay, theoretically we have a lock on this clip's Transcripts, in self.transcripts. However, that lock
# prevents us from deleting it!! Oops. Therefore, IF we have a legit lock on it, unlock it for
# the delete.
# For each transcript in the list of clip transcripts ...
for tr in self.transcripts:
# ... if the transcript is locked ...
if tr.isLocked:
# ... then we need to unlock it before we can delete it.
tr.unlock_record()
# Now try to delete the transcript
result = result and tr.db_delete(0)
# NOTE: It is important for the calling routine to delete references to the Keyword Examples
# from the screen. However, that code does not belong in the Clip Object, but in the
# user interface. That is why it is not included here as part of the result.
# Delete all related references in the ClipKeywords table
if result:
DBInterface.delete_all_keywords_for_a_group(0, 0, self.number, 0, 0)
if result:
# Craft a query to remove all existing Additonal Videos
query = "DELETE FROM AdditionalVids2 WHERE ClipNum = %s"
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Execute the query
c.execute(query, (self.number, ))
# Delete the actual Clip record.
self._db_do_delete(use_transactions, c, result)
# Cleanup
c.close()
self.clear()
except RecordLockedError, e:
# if a sub-record is locked, we may need to unlock the Clip record (after rolling back the Transaction)
if self.isLocked:
# c (the database cursor) only exists if the record lock was obtained!
# We must roll back the transaction before we unlock the record.
c.execute("ROLLBACK")
c.close()
self.unlock_record()
raise e
except:
raise
return result
def lock_record(self):
""" Override the DataObject Lock Method """
# Also lock the Clip Transcript records
# If we're skipping Transcripts but want to lock the Clip ...
if self.skipText:
# ... that's a programming error! That should not be allowed!
tmpDlg = Dialogs.ErrorDialog(None, unicode('PROGRAMMING ERROR - Locking Clip "%s"\nwithout locking Clip Transcripts!', 'utf8') % self.id)
tmpDlg.ShowModal()
tmpDlg.Destroy()
# Raise an exception before locking any transcripts!
raise RecordLockedError(user='David Woods')
# We can run into trouble if one of a multiple transcript records is locked. Specifically, if a later
# transcript is already locked, trying to lock it below will raise an exception, but the earlier
# transcripts will have gotten locked now and will remain so improperly.
# Therefore, let's check to see if the transcripts are already locked before trying to lock them.
# Iterate through the transcripts ...
for tr in self.transcripts:
# If a transcript is locked ...
if tr.isLocked or ((tr.record_lock != '') and (tr.record_lock != None)):
# Raise an exception before locking any transcripts!
raise RecordLockedError(user=tr.record_lock)
# For each transcript in the clip transcripts list ...
for tr in self.transcripts:
# ... lock the transcript
tr.lock_record()
# Lock the Clip Record. Call this second so the Clip is not identified as locked if the
# Clip Transcript record lock fails.
DataObject.DataObject.lock_record(self)
def unlock_record(self):
""" Override the DataObject Unlock Method """
# Unlock the Clip Record
DataObject.DataObject.unlock_record(self)
# Also unlock the Clip Transcript records
# For each transcript in the clip transcript list ...
for tr in self.transcripts:
# ... unlock the transcript
tr.unlock_record()
def duplicate(self):
# Inherit duplicate method
# BUG: This duplicate() call wipes out the Keyword Example numbers of BOTH copies of the Clips!!
# The keyword_list is clearly a pointer, not a copy of the list object.
# newClip = DataObject.duplicate(self)
# INSTEAD: Let's just create a new clip, loading the existing data from the database!
# If we know the Clip Number, the Clip is in the database ...
if self.number != 0:
# ... so we can just load it
newClip = Clip(self.number)
# If we have a clips that's not currently in the database ...
else:
# ... we can use the old duplicate method. If needed, we may want to copy all the data manually here.
newClip = DataObject.DataObject.duplicate(self)
# Eliminate the new clip's object number so it will get a new on when saved.
newClip.number = 0
# A new Clip should get a new Clip Transcripts!
for tr in newClip.transcripts:
tr.number = 0
# Sort Order should not be duplicated!
newClip.sort_order = 0
# Copying a Clip should not cause additional Keyword Examples to be created.
# We need to strip the "example" status for all keywords in the new clip.
for clipKeyword in newClip.keyword_list:
clipKeyword.example = 0
return newClip
def clear_keywords(self):
"""Clear the keyword list."""
self._kwlist = []
def refresh_keywords(self):
"""Clear the keyword list and refresh it from the database."""
self._kwlist = []
kwpairs = DBInterface.list_of_keywords(Clip=self.number)
for data in kwpairs:
tempClipKeyword = ClipKeywordObject.ClipKeyword(data[0], data[1], clipNum=self.number, example=data[2])
self._kwlist.append(tempClipKeyword)
def add_keyword(self, kwg, kw, example=0):
""" Add a keyword to the keyword list. By default, it is NOT a keyword example. """
# We need to check to see if the keyword is already in the keyword list
keywordFound = False
# Iterate through the list
for clipKeyword in self._kwlist:
# If we find a match, set the flag and quit looking.
if (clipKeyword.keywordGroup == kwg) and (clipKeyword.keyword == kw):
keywordFound = True
break
# If the keyword is not found, add it. (If it's already there, we don't need to do anything!)
if not keywordFound:
# Create an appropriate ClipKeyword Object
tempClipKeyword = ClipKeywordObject.ClipKeyword(kwg, kw, clipNum=self.number, example=example)
# Add it to the Keyword List
self._kwlist.append(tempClipKeyword)
def remove_keyword(self, kwg, kw):
"""Remove a keyword from the keyword list. The value returned by this function can be:
0 Keyword NOT deleted. (probably overridden by the user)
1 Keyword deleted, but it was NOT a Keyword Example
2 Keyword deleted, and it WAS a Keyword Example. """
# We need different return codes for failure, success of a Non-Example, and success of an Example.
# If it's an example, we need to remove the Node on the Database Tree Tab
# Let's assume the Delete will fail (or be refused by the user) until it actually happens.
delResult = 0
# We need to find the keyword in the keyword list
# Iterate through the keyword list
for index in range(len(self._kwlist)):
# Look for the entry to be deleted
if (self._kwlist[index].keywordGroup == kwg) and (self._kwlist[index].keyword == kw):
# If it's a Keyword Example ...
if self._kwlist[index].example == 1:
# ... build and encode the prompt ...
if 'unicode' in wx.PlatformInfo:
# Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data.
prompt = unicode(_('Clip "%s" has been designated as an example of Keyword "%s : %s".\nRemoving this Keyword from the Clip will also remove the Clip as a Keyword Example.\n\nDo you want to remove Clip "%s" as an example of Keyword "%s : %s"?'), 'utf8')
else:
prompt = _('Clip "%s" has been designated as an example of Keyword "%s : %s".\nRemoving this Keyword from the Clip will also remove the Clip as a Keyword Example.\n\nDo you want to remove Clip "%s" as an example of Keyword "%s : %s"?')
# ... ask the user if they really want to remove the example keyword ...
dlg = Dialogs.QuestionDialog(TransanaGlobal.menuWindow, prompt % (self.id, kwg, kw, self.id, kwg, kw))
result = dlg.LocalShowModal()
dlg.Destroy()
# ... if the user sayd "Yes" ...
if result == wx.ID_YES:
# If the entry is found and the user confirms, delete it
del self._kwlist[index]
# Signal that a Keyword Example was deleted, so the GUI can update.
delResult = 2
else:
# If the entry is found, delete it and stop looking
del self._kwlist[index]
# Signal that the delete was successful and was NOT an Example.
delResult = 1
# Once the entry has been found, stop looking for it
break
# Signal whether the delete was successful
return delResult
def has_keyword(self, kwg, kw):
""" Determines if the Episode has a given keyword assigned """
# Assume the result will be false
res = False
# Iterate through the keyword list
for keyword in self.keyword_list:
# See if the text passed in matches the strings in the keyword objects in the keyword list
if (kwg == keyword.keywordGroup) and (kw == keyword.keyword):
# If so, signal that it HAS been found
res = True
# If found, we don't need to look any more!
break
# Return the results
return res
def load_additional_vids(self):
"""Load additional media file names from the database."""
# Get a database connection
db = DBInterface.get_db()
# Create a database cursor
c = db.cursor()
# Define the DB Query
query = "SELECT MediaFile, VidLength, Offset, Audio FROM AdditionalVids2 WHERE ClipNum = %s ORDER BY AddVidNum"
# Adjust query for sqlite if needed
query = DBInterface.FixQuery(query)
# Execute the query
c.execute(query, (self.number, ))
# For each video in the query results ...
for (vidFilename, vidLength, vidOffset, audio) in c.fetchall():
# Detection of the use of the Video Root Path is platform-dependent and must be done for EACH filename!
if wx.Platform == "__WXMSW__":
# On Windows, check for a colon in the position, which signals the presence or absence of a drive letter
useVideoRoot = (vidFilename[1] != ':') and (vidFilename[:2] != '//')
else:
# On Mac OS-X and *nix, check for a slash in the first position for the root folder designation
useVideoRoot = (vidFilename[0] != '/')
# If we are using the Video Root Path, add it to the Filename
if useVideoRoot:
video = TransanaGlobal.configData.videoPath.replace('\\', '/') + DBInterface.ProcessDBDataForUTF8Encoding(vidFilename)
else:
video = DBInterface.ProcessDBDataForUTF8Encoding(vidFilename)
# Add the video to the additional media files list
self.additional_media_files = {'filename' : video,
'length' : vidLength,
'offset' : vidOffset,
'audio' : audio}
# Close the database cursor
c.close()
def remove_an_additional_vid(self, indx):
""" remove ONE additional media file from the list of additional media files """
del(self._additional_media[indx])
def GetNodeData(self, includeClip=True):
""" Returns the Node Data list (list of parent collections) needed for Database Tree Manipulation """
# Load the Clip's collection
tempCollection = Collection.Collection(self.collection_num)
# If we're including the Clip in the Node Data ...
if includeClip:
# ... get the Collection's node data and tack the Clip's ID on the end.
return tempCollection.GetNodeData() + (self.id,)
# If we're NOT including the Clip in the Node data ...
else:
# ... just return the Collection's Node Data.
return tempCollection.GetNodeData()
def GetNodeString(self, includeClip=True):
""" Returns a string that delineates the full nested collection structure for the present clip.
if includeClip=False, only the Collection path will be returned. """
# Load the Clip's Collection
tempCollection = Collection.Collection(self.collection_num)
# If we're including the Clip in the Node String ...
if includeClip:
# ... get the Collection's node string and tack the Clip's ID on the end.
return tempCollection.GetNodeString() + ' > ' + self.id
# If we're NOT including the Clip in the Node String ...
else:
# ... just return the Collection's Node String.
return tempCollection.GetNodeString()
# Private methods
def _load_row(self, r):
self.number = r['ClipNum']
self.id = r['ClipID']
self.comment = r['ClipComment']
self.collection_num = r['CollectNum']
self.collection_id = r['CollectID']
self.episode_num = r['EpisodeNum']
self.media_filename = r['MediaFile']
self.clip_start = r['ClipStart']
self.clip_stop = r['ClipStop']
self.offset = r['ClipOffset']
self.audio = r['Audio']
self.sort_order = r['SortOrder']
# I've seen once instance of incomplete updating of the database on 2.40 upgrade database modification. The following
# fixes problems caused there.
# If the offset is NULL in the database, it would come up as None here....
if self.offset == None:
# If so, convert it to 0 to prevent problems loading clips. (It should have been converted.)
self.offset = 0
# If self.audio is None, it's NULL in the database.
if self.audio == None:
# It should have been converted to 1.
self.audio = 1
# Initialize a list of Transcript objects
self.transcripts = []
# If we're NOT skipping the Transcripts ...
if not self.skipText:
# Load the Clip Transcripts. Get the list of clip transcripts from the database and interate ...
for tr in DBInterface.list_clip_transcripts(self.number):
# Create a Transcript Object, passing each Transcript's Transcript Number (parameter 0)
tempTranscript = Transcript.Transcript(tr[0])
# Append the transcript object to the Clip's Transcript List
self.transcripts.append(tempTranscript)
# If we're in Unicode mode, we need to encode the data from the database appropriately.
# (unicode(var, TransanaGlobal.encoding) doesn't work, as the strings are already unicode, yet aren't decoded.)
if 'unicode' in wx.PlatformInfo:
self.id = DBInterface.ProcessDBDataForUTF8Encoding(self.id)
self.comment = DBInterface.ProcessDBDataForUTF8Encoding(self.comment)
self.collection_id = DBInterface.ProcessDBDataForUTF8Encoding(self.collection_id)
self.media_filename = DBInterface.ProcessDBDataForUTF8Encoding(self.media_filename)
# Remember whether the MediaFile uses the VideoRoot Folder or not.
# Detection of the use of the Video Root Path is platform-dependent.
if wx.Platform == "__WXMSW__":
# On Windows, check for a colon in the position, which signals the presence or absence of a drive letter
self.useVideoRoot = (self.media_filename[1] != ':') and (self.media_filename[:2] != '\\\\')
else:
# On Mac OS-X and *nix, check for a slash in the first position for the root folder designation
self.useVideoRoot = (self.media_filename[0] != '/')
# If we are using the Video Root Path, add it to the Filename
if self.useVideoRoot:
self.media_filename = TransanaGlobal.configData.videoPath.replace('\\', '/') + self.media_filename
def _sync_collection(self):
"""Synchronize the Collection ID property to reflect the current state
of the Collection Number property."""
tempCollection = Collection.Collection(self.collection_num)
self.collection_id = tempCollection.id
def _get_col_num(self):
return self._col_num
def _set_col_num(self, num):
self._col_num = num
def _del_col_num(self):
self._col_num = 0
def _get_ep_num(self):
return self._ep_num
def _set_ep_num(self, num):
self._ep_num = num
def _del_ep_num(self):
self._ep_num = 0
def _get_t_num(self):
return self._t_num
def _set_t_num(self, num):
self._t_num = num
def _del_t_num(self):
self._t_num = 0
def _get_clip_transcript_nums(self):
# Initialize a list object
tempList = []
# Iterate through the transcripts ...
for tr in self.transcripts:
# ... and append their numbers to the list.
tempList.append(tr.number)
# Return the list
return tempList
def _get_fname(self):
return self._fname.replace('/', os.sep)
def _set_fname(self, fname):
self._fname = fname.replace('\\', '/')
def _del_fname(self):
self._fname = ""
def _get_additional_media(self):
temp_additional_media = []
for vid in self._additional_media:
vid['filename'] = vid['filename'].replace('/', os.sep)
temp_additional_media.append(vid)
return temp_additional_media
def _set_additional_media(self, vidDict):
# If we receive a Dictionary Object ...
if isinstance(vidDict, dict):
# ... replace the backslashes in the filename item
vidDict['filename'] = vidDict['filename'].replace('\\', '/')
# Add the dictionary object to the media files list