-
Notifications
You must be signed in to change notification settings - Fork 1
/
messager.py
1519 lines (1273 loc) · 55.2 KB
/
messager.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
#!/usr/bin/python
'''messager-0.6.py
Manage scheduled messages. Add new messages,
modify or delete existing messages.
change in v0.6 - enable change of message type
change in v0.7 - fix show information last played
add how often played and date created
'''
import pygtk
import gtk
import gobject
import pango
import sys
import psycopg2
import datetime
import calendar
import os
import time
import gst
import pygst
import threading
import thread
import subprocess
import shutil
import ConfigParser
config = ConfigParser.SafeConfigParser()
#get variables from config file
config = ConfigParser.SafeConfigParser()
config.read('/usr/local/etc/threedradio.conf')
dir_msg = config.get('Paths', 'dir_msg')
dir_backup = config.get('Paths', 'dir_backup')
dir_img = config.get('Paths', 'dir_img')
logo = config.get('Images', 'logo')
pg_user = config.get('Messager', 'pg_user')
pg_password = config.get('Messager', 'pg_password')
pg_server = config.get('Common', 'pg_server')
pg_msg_database = config.get('Common', 'pg_msg_database')
header_font = pango.FontDescription("Sans Bold 14")
subheader_font = pango.FontDescription("Sans Bold 12")
class ShowInfo():
def __init__(self, msg_info):
dialog = gtk.Dialog("Show Information", None, 0, (
gtk.STOCK_OK, gtk.RESPONSE_OK))
self.messager = Messager()
table_msg = gtk.Table(12, 2, False)
code = msg_info[0]
title = msg_info[1]
msg_type = msg_info[2]
nq = msg_info[3]
expiry = self.format_expiry(msg_info[4])
filename = msg_info[5]
producer = msg_info[6]
dur = msg_info[7]
if dur:
dur = self.format_dur(int(dur))
else:
dur = "N/A"
created = msg_info[8]
last_played = self.last_played(code)
frequency = self.frequency(code) + " times"
label_code0 = gtk.Label("ID Code: ")
label_code1 = gtk.Label(code)
label_title0 = gtk.Label("Message Title: ")
label_title1 = gtk.Label(title)
label_msg_type0 = gtk.Label("Message Type: ")
label_msg_type1 = gtk.Label(msg_type)
label_nq0 = gtk.Label("End Cue: ")
label_nq1 = gtk.Label(nq)
label_expiry0 = gtk.Label("Expiry Date: ")
label_expiry1 = gtk.Label(expiry)
label_filename0 = gtk.Label("Audio File: ")
label_filename1 = gtk.Label(filename)
label_producer0 = gtk.Label("Producer: ")
label_producer1 = gtk.Label(producer)
label_dur0 = gtk.Label("Duration: ")
label_dur1 = gtk.Label(dur)
label_created0 = gtk.Label("Created: ")
label_created1 = gtk.Label(created)
label_last_played0 = gtk.Label("Last Played: ")
label_last_played1 = gtk.Label(last_played)
label_frequency0 = gtk.Label("How often played: ")
label_frequency1 = gtk.Label(frequency)
table_msg.attach(label_code0, 0, 1, 0, 1, True, True, 5, 5)
table_msg.attach(label_code1, 1, 2, 0, 1, True, True, 5, 5)
table_msg.attach(label_title0, 0, 1, 1, 2, True, True, 5, 5)
table_msg.attach(label_title1, 1, 2, 1, 2, True, True, 5, 5)
table_msg.attach(label_msg_type0, 0, 1, 3, 4, True, True, 5, 5)
table_msg.attach(label_msg_type1, 1, 2, 3, 4, True, True, 5, 5)
table_msg.attach(label_nq0, 0, 1, 4, 5, True, True, 5, 5)
table_msg.attach(label_nq1, 1, 2, 4, 5, True, True, 5, 5)
table_msg.attach(label_expiry0, 0, 1, 5, 6, True, True, 5, 5)
table_msg.attach(label_expiry1, 1, 2, 5, 6, True, True, 5, 5)
table_msg.attach(label_filename0, 0, 1, 6, 7, True, True, 5, 5)
table_msg.attach(label_filename1, 1, 2, 6, 7, True, True, 5, 5)
table_msg.attach(label_producer0, 0, 1, 7, 8, True, True, 5, 5)
table_msg.attach(label_producer1, 1, 2, 7, 8, True, True, 5, 5)
table_msg.attach(label_dur0, 0, 1, 8, 9, True, True, 5, 5)
table_msg.attach(label_dur1, 1, 2, 8, 9, True, True, 5, 5)
table_msg.attach(label_created0, 0, 1, 9, 10, True, True, 5, 5)
table_msg.attach(label_created1, 1, 2, 9, 10, True, True, 5, 5)
table_msg.attach(label_last_played0, 0, 1, 10, 11, True, True, 5, 5)
table_msg.attach(label_last_played1, 1, 2, 10, 11, True, True, 5, 5)
table_msg.attach(label_frequency0, 0, 1, 11, 12, True, True, 5, 5)
table_msg.attach(label_frequency1, 1, 2, 11, 12, True, True, 5, 5)
dialog.vbox.pack_start(table_msg, True, True, 0)
dialog.show_all()
dialog.run()
dialog.destroy()
def format_expiry(self, expiry):
dt = datetime.datetime.strptime(expiry, "%Y-%m-%d")
expiry = datetime.datetime.strftime(dt, "%d/%m/%Y")
return expiry
def format_dur(self, dur):
m,s = divmod(dur, 60)
if m < 60:
str_dur = "%02i:%02i" %(m,s)
return str_dur
else:
h,m = divmod(m, 60)
str_dur = "%i:%02i:%02i" %(h,m,s)
return str_dur
return str_dur
def last_played(self, code):
query = "select when_played from playlog where id_type='msg' AND id_code='{0}' ORDER BY when_played DESC LIMIT 1".format(code)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
tup_last_played = cur.fetchall()
if tup_last_played:
last_played = tup_last_played[0][0].strftime("%c")
else:
last_played = "Not Available"
cur.close()
conn.close()
return last_played
def frequency(self,code):
query = "SELECT COUNT (*) FROM playlog WHERE id_code='{0}'".format(code)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
frequency = str(cur.fetchall()[0][0])
cur.close()
conn.close()
return frequency
def filetime(self,filename, msg_type):
print("In Progress")
class ChangeMessage():
def __init__(self, msg_info):
dialog = gtk.Dialog("Change Message", None, 0, (
gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
self.messager = Messager()
self.filepath_new = False
print(msg_info)
table_msg = gtk.Table(8, 2, False)
hbox_filename = gtk.HBox(False, 0)
self.msg_info = msg_info
self.idcode = msg_info[0]
self.title = msg_info[1]
self.msg_type = msg_info[2]
self.nq = msg_info[3]
self.expiry = msg_info[4]
self.filename = msg_info[5]
self.producer = msg_info[6]
self.dur = str(msg_info[7])
header = "ID Code: {0}".format(self.idcode)
label_header = gtk.Label(header)
label_header.modify_font(subheader_font)
label_type = gtk.Label("Message Type")
self.cb_msg = gtk.combo_box_new_text()
self.make_type_cb()
self.label_filename = gtk.Label(self.filename)
self.label_dur_blank0 = gtk.Label(" ")
self.label_dur_blank1 = gtk.Label(" ")
self.label_dur = gtk.Label(self.dur)
self.label_dur_sec= gtk.Label("sec")
btn_filename = gtk.Button("Audio File")
btn_filename.connect("clicked", self.select_file)
label_title = gtk.Label("Message Title")
self.entry_title=gtk.Entry(30)
self.entry_title.set_text(self.title)
label_nq = gtk.Label("End Cue")
self.entry_nq = gtk.Entry(30)
self.entry_nq.set_text(self.nq)
label_expiry = gtk.Label("Expiry Date")
self.entry_expiry = gtk.Entry(10)
expiry = self.format_expiry(self.expiry)
self.entry_expiry.set_text(expiry)
label_prod = gtk.Label("Producer")
self.entry_prod = gtk.Entry(50)
self.entry_prod.set_text(self.producer)
btn_change = gtk.Button("Change")
btn_change.set_size_request(180, 30)
btn_change.modify_font(subheader_font)
btn_change.connect("clicked", self.change_message)
hbox_filename.pack_start(btn_filename, False)
hbox_filename.pack_start(self.label_dur_blank0, True)
hbox_filename.pack_start(self.label_filename, True)
hbox_filename.pack_end(self.label_dur_sec, True)
hbox_filename.pack_end(self.label_dur, True)
hbox_filename.pack_end(self.label_dur_blank1, True)
table_msg.attach(label_header, 0, 2, 0, 1, True, True, 0, 5)
table_msg.attach(label_type, 0, 1, 1, 2, True, True, 0, 0)
table_msg.attach(self.cb_msg, 1, 2, 1, 2, True, True, 5, 0)
table_msg.attach(hbox_filename, 0, 2, 2, 3, True, True, 0, 0)
table_msg.attach(label_title, 0, 1, 3, 4, True, True, 0, 0)
table_msg.attach(self.entry_title, 1, 2, 3, 4, True, True, 0, 0)
table_msg.attach(label_nq, 0, 1, 4, 5, True, True, 0, 0)
table_msg.attach(self.entry_nq, 1, 2, 4, 5, True, True, 0, 0)
table_msg.attach(label_expiry, 0, 1, 5, 6, True, True, 0, 0)
table_msg.attach(self.entry_expiry, 1, 2, 5, 6, True, True, 0, 0)
table_msg.attach(label_prod, 0, 1, 6, 7, True, True, 0, 0)
table_msg.attach(self.entry_prod, 1, 2, 6, 7, True, True, 0, 0)
table_msg.attach(btn_change, 0, 2, 7, 8, True, True, 5, 5)
dialog.vbox.pack_start(table_msg, True, True, 0)
dialog.show_all()
response = dialog.run()
if response == gtk.RESPONSE_OK:
self.change_message(None)
dialog.destroy()
def make_type_cb(self):
type_rows = self.messager.get_types()
msg_types = [i[0] for i in type_rows]
for item in msg_types:
self.cb_msg.append_text(item)
cb_index = msg_types.index(self.msg_type)
self.cb_msg.set_active(cb_index)
def format_expiry(self, expiry):
dt = datetime.datetime.strptime(expiry, "%Y-%m-%d")
expiry = datetime.datetime.strftime(dt, "%d/%m/%Y")
return expiry
def select_file(self, widget):
self.filepath_new = self.messager.getfile()
split_filename = os.path.split(self.filepath_new)
filename = split_filename[-1]
#calculate duration
ex = "/opt/local/bin/soxi"
arg1 = "-D"
filepath = self.filepath_new
process = subprocess.Popen([ex, arg1, filepath], shell=False, stdout=subprocess.PIPE)
dur = process.communicate()[0]
try:
dur_seconds = int(round(float(dur)))
dur_seconds = str(dur_seconds)
self.label_dur.set_text(dur_seconds)
self.label_dur_sec.set_text(" sec ")
self.label_filename.set_text(filename)
except ValueError:
self.label_dur.set_text("")
str_error = "This does not appear to be a valid audio file"
self.messager.error_dialog(str_error)
def change_message(self, widget):
#Check which fields have changed
msg_type = self.cb_msg.get_active_text()
print(msg_type)
if msg_type == self.msg_type:
msg_type = ""
filename = self.label_filename.get_text()
if filename == self.filename:
filename = ""
dur = self.label_dur.get_text()
if dur == self.dur:
dur = ""
else:
dur = int(dur)
title = self.entry_title.get_text()
if title == self.title:
title = ""
nq = self.entry_nq.get_text()
if nq == self.nq:
nq = ""
expiry = self.entry_expiry.get_text()
if expiry == self.expiry:
expiry = ""
producer = self.entry_prod.get_text()
if producer == self.producer:
producer = ""
fields = dict([
("type", msg_type),
("filename", filename),
("duration", dur),
("title", title),
("nq", nq),
("expirydate", expiry),
("fldproducer", producer)
])
self.make_change(fields)
self.dialog_added()
def make_change(self, fields):
if self.filepath_new:
filename_old = self.filename
filepath_new = self.filepath_new
self.replace_file(filename_old, filepath_new)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
for item in fields:
val = fields[item]
if val:
cur = conn.cursor()
str_set = "UPDATE messagelist SET {0}=" .format(item)
str_code = "'{0}'".format(self.idcode)
query = "UPDATE messagelist SET %s=" % (item) + '%s WHERE code=%s'
cur.execute(query, (val, self.idcode))
conn.commit()
cur.close()
conn.close()
msg_type = fields["type"]
if msg_type:
print("the message type listed here is " + msg_type)
self.change_msg_type(msg_type)
def replace_file(self, filename_old, filepath_new):
'''
archive the old audio file and copy over the new one.
'''
dt = datetime.datetime.now()
dir_date = datetime.datetime.strftime(dt, "%Y%m")
# the folder where the old audio file will be moved
path_backup = os.path.join(dir_backup, dir_date)
type_12 = self.msg_type[0:12]
type_12 = type_12.lower()
# the folder in which the audio file is kept
audio_location = os.path.join(dir_msg, type_12)
# full path of the old audio file
audiofile_old = os.path.join(audio_location, filename_old)
# name of the new audio file
filename_new = os.path.basename(filepath_new)
# destination path for new audio file including name of the file
path_dest = os.path.join(dir_msg, type_12, filename_new)
# full destination path for the old audio file including name of the file
backed_up = os.path.join(path_backup, filename_old)
# does the backup folder exist? If not, create
if not os.path.isdir(path_backup):
os.mkdir(path_backup)
# is there already a backup of the old audio file name?
# if so, add timestamp to old filename
if os.path.exists(backed_up):
now = datetime.datetime.strftime(dt, "%y%m%d%H%M")
filenow = filename_old + now
backed_up = os.path.join(path_backup, filenow)
# move the old file if it exists
if os.path.exists(audiofile_old):
shutil.move(audiofile_old, backed_up)
# copy the new file over
shutil.copyfile(filepath_new, path_dest)
def change_msg_type(self, msg_type):
# create the new path from the msg_type
# obtain the existing path from either
# self.filepath_new (if file was changed)
# or
# adding self.msg_type and self.filename
type_12 = self.msg_type[0:12]
type_12 = type_12.lower()
new_type_12 = msg_type[0:12]
new_type_12 = new_type_12.lower()
if self.filepath_new:
filename_move = os.path.basename(self.filepath_new)
else:
filename_move = self.filename
path_src = os.path.join(dir_msg, type_12, filename_move)
path_dest = os.path.join(dir_msg, new_type_12, filename_move)
print("moving file from " + path_src + " to " + path_dest)
#check that expiry date is valid
try:
shutil.copyfile(path_src, path_dest)
except IOError:
str_error = "There was a problem relocating the file to the new folder, please see a station Tech."
self.messager.error_dialog(str_error)
return
def dialog_added(self):
str_info = "Message Changed"
self.messager.info_dialog(str_info)
class NewMessage:
def __init__(self):
dialog = gtk.Dialog("New Message", None, 0, (
gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
self.messager = Messager()
self.filepath_new = False
# table for buttons and drop-down list
table_msg = gtk.Table(8, 2, False)
hbox_filename = gtk.HBox(False, 0)
#labels
label_add = gtk.Label("Add New Message")
#label_add.modify_font(subheader_font)
label_type = gtk.Label("Message Type")
self.cb_msg = gtk.combo_box_new_text()
self.make_type_cb()
label_title = gtk.Label("Message Title")
self.entry_title=gtk.Entry(30)
label_code = gtk.Label("Message Code")
self.entry_code = gtk.Entry(6)
label_nq = gtk.Label("End Cue")
self.entry_nq = gtk.Entry(30)
label_expiry = gtk.Label("Expiry Date")
self.entry_expiry = gtk.Entry(10)
next_month = self.get_next_month()
self.entry_expiry.set_text(next_month)
self.label_filename = gtk.Label("Audio File")
self.label_dur_blank0 = gtk.Label(" ")
self.label_dur_blank1 = gtk.Label(" ")
self.label_dur = gtk.Label("")
self.label_dur_sec= gtk.Label("")
btn_filename = gtk.Button("Select")
btn_filename.connect("clicked", self.select_file)
label_prod = gtk.Label("Producer")
self.entry_prod = gtk.Entry(50)
btn_add = gtk.Button("Add")
btn_add.connect("clicked", self.add_message)
hbox_filename.pack_start(btn_filename, False)
hbox_filename.pack_start(self.label_dur_blank0, True)
hbox_filename.pack_start(self.label_filename, True)
hbox_filename.pack_end(self.label_dur_sec, True)
hbox_filename.pack_end(self.label_dur, True)
hbox_filename.pack_end(self.label_dur_blank1, True)
table_msg.attach(label_add, 0, 2, 0, 1, False, False, 0, 5)
table_msg.attach(label_type, 0, 1, 1, 2, False, False, 0, 0)
table_msg.attach(self.cb_msg, 1, 2, 1, 2, True, True, 5, 0)
table_msg.attach(label_code, 0, 1, 2, 3, False, False, 0, 0)
table_msg.attach(self.entry_code, 1, 2, 2, 3, True, True, 0, 0)
table_msg.attach(label_title, 0, 1, 3, 4, False, False, 0, 0)
table_msg.attach(self.entry_title, 1, 2, 3, 4, True, True, 0, 0)
table_msg.attach(label_nq, 0, 1, 4, 5, False, False, 0, 0)
table_msg.attach(self.entry_nq, 1, 2, 4, 5, True, True, 0, 0)
table_msg.attach(label_expiry, 0, 1, 5, 6, False, False, 0, 0)
table_msg.attach(self.entry_expiry, 1, 2, 5, 6, True, True, 0, 0)
table_msg.attach(hbox_filename, 0, 2, 6, 7, True, True, 0, 0)
table_msg.attach(label_prod, 0, 1, 7, 8, False, False, 0, 0)
table_msg.attach(self.entry_prod, 1, 2, 7, 8, True, True, 0, 0)
dialog.vbox.pack_start(table_msg, True, True, 0)
dialog.vbox.pack_start(btn_add, False, False, 0)
dialog.show_all()
dialog.run()
dialog.destroy()
def make_type_cb(self):
type_rows = self.messager.get_types()
for item in type_rows:
self.cb_msg.append_text(item[0])
def select_file(self, widget):
self.filepath_new = self.messager.getfile()
split_filename = os.path.split(self.filepath_new)
filename = split_filename[-1]
if len(filename) > 32:
str_error = "The name of your file is too long. It must be 32 characters or less"
self.messager.error_dialog(str_error)
#calculate duration
ex = "/opt/local/bin/soxi"
arg1 = "-D"
filepath = self.filepath_new
print(filepath)
process = subprocess.Popen([ex, arg1, filepath], shell=False, stdout=subprocess.PIPE)
dur = process.communicate()[0]
try:
dur_seconds = int(round(float(dur)))
dur_seconds = str(dur_seconds)
self.label_dur.set_text(dur_seconds)
self.label_dur_sec.set_text(" sec ")
self.label_filename.set_text(filename)
except ValueError:
self.label_dur.set_text("")
str_error = "This does not appear to be a valid audio file"
self.messager.error_dialog(str_error)
def get_next_month(self):
''''
thanks Dave Webb on Stack Overflow site
http://stackoverflow.com/questions/4130922/how-to-increment-datetime-month-in-python
'''
sourcedate = datetime.datetime.today()
months = 1
month = sourcedate.month - 1 + months
year = sourcedate.year + month / 12
month = month % 12 + 1
day = min(sourcedate.day,calendar.monthrange(year,month)[1])
next_month = "{0}/{1}/{2}".format(day, month, year)
return next_month
def add_message(self, widget):
msg_type = self.cb_msg.get_active_text()
title = self.entry_title.get_text()
code = self.entry_code.get_text()
nq = self.entry_nq.get_text()
expiry_date = self.entry_expiry.get_text()
filepath = self.filepath_new
producer = self.entry_prod.get_text()
dur = int(self.label_dur.get_text())
created = datetime.datetime.now()
#check the type
if not msg_type:
str_error = "Message not added. You need to select a message type"
self.messager.error_dialog(str_error)
return
#chack if the title is blank
if not title:
str_error = "Message not added. You need enter a title"
self.messager.error_dialog(str_error)
return
#check if code is in use or blank
if code:
code = code.upper()
check_code = self.check_code(code)
if check_code:
str_error = "Message not added. The ID code is in use. Enter a unique ID code"
self.messager.error_dialog(str_error)
return
else:
str_error = "Message not added. You need enter a unique ID code for the message"
self.messager.error_dialog(str_error)
return
#check that the nq is not blank
if not nq:
str_error = "Message not added. You need enter an end cue for the message"
self.messager.error_dialog(str_error)
return
#check that expiry date is valid
try:
dt_expiry = datetime.datetime.strptime(expiry_date, "%d/%m/%Y")
except ValueError:
str_error = "Message not added - invalid expiry date. Please enter a valid date dd/mm/yyyy"
self.messager.error_dialog(str_error)
return
#check if the filepath exists and the name is not duplicate
if not filepath:
str_error = "Message not added - you need to select where the file is located"
self.messager.error_dialog(str_error)
return
else:
filename = self.label_filename.get_text()
if filename == "Audio File":
str_error = "Message not added - you need to select where the file is located"
else:
type_12 = msg_type[0:12]
type_12 = type_12.lower()
destination_filepath = dir_msg + type_12 + "/" + filename
if os.path.isfile(destination_filepath) and not filepath == destination_filepath:
str_error = "Message not added - the file '{0}' exists in the '{1}' folder".format(
filename, type_12)
self.messager.error_dialog(str_error)
return
#check that the producer is not blank
if not producer:
str_error = "Message not added. Please enter your name as producer"
self.messager.error_dialog(str_error)
return
#check that the duration is correct
if not dur:
str_error = "You need to select a valid audio file"
self.messager.error_dialog(str_error)
return
if not filepath == destination_filepath:
self.copy_file(filepath, type_12)
self.add_to_db(msg_type, title, code, nq, expiry_date, filename, producer, dur, created)
self.dialog_added()
self.clear_fields()
def check_msg_type(self, msg_type):
query = "select * from typelist where type='{0}'".format(msg_type)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
conn.close()
if result:
return True
else:
return False
def check_code(self, code):
query = "select * from messagelist where code='{0}'".format(code)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
conn.close()
if result:
return True
else:
return False
def copy_file(self, filepath, type_12):
filename = os.path.basename(filepath)
path_dest = os.path.join(dir_msg, type_12, filename)
print("Destination path is")
print(path_dest)
shutil.copyfile(filepath, path_dest)
def add_to_db(self, msg_type, title, code, nq, expiry_date, filename, producer, dur, created):
expiry_date = datetime.datetime.strptime(expiry_date, "%d/%m/%Y")
col_items = '(code, title, type, nq, expirydate, filename, fldproducer, duration, created)'
#query = "INSERT INTO messagelist (code, title, type, nq, expirydate, filename, fldproducer, duration) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')".format(
# code, title, msg_type, nq, expiry_date, filename, producer, dur)
val_items = (code, title, msg_type, nq, expiry_date, filename, producer, dur, created)
SQL = "INSERT INTO messagelist" + col_items + \
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(SQL, val_items)
conn.commit()
cur.close()
conn.close()
def clear_fields(self):
self.entry_title.set_text("")
self.entry_code.set_text("")
self.entry_nq.set_text("")
next_month = self.get_next_month()
self.entry_expiry.set_text(next_month)
self.label_filename.set_text("Audio File")
self.label_dur.set_text("")
self.label_dur_sec.set_text("")
self.entry_prod.set_text("")
type_12 = ""
msg_type = ""
title = ""
code = ""
nq = ""
expiry_date = ""
producer = ""
dur = 0
filename = ""
filepath = ""
def dialog_added(self):
str_info = "Message Added"
self.messager.info_dialog(str_info)
class NewType():
def __init__(self):
dialog = gtk.Dialog("New Message Type", None, 10, (
gtk.STOCK_CLOSE, gtk.RESPONSE_CANCEL,
gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
self.messager = Messager()
label_type = gtk.Label("Name of the new message type")
self.entry_type = gtk.Entry(max=20)
self.entry_type.connect("activate", self.add_type)
label_desc = gtk.Label("Description of new message type")
self.entry_desc = gtk.Entry(max=70)
self.entry_desc.connect("activate", self.add_type)
btn_new = gtk.Button("Add")
btn_new.connect("clicked", self.add_type)
dialog.vbox.pack_start(label_type, False, False, 5)
dialog.vbox.pack_start(self.entry_type, False, False, 5)
dialog.vbox.pack_start(label_desc, False, False, 5)
dialog.vbox.pack_start(self.entry_desc, False, False, 5)
dialog.vbox.pack_start(btn_new, False, False, 5)
dialog.show_all()
dialog.run()
dialog.destroy()
def add_type(self, widget):
msg_type = self.entry_type.get_text()
desc = self.entry_desc.get_text()
if not msg_type:
str_error = "You need to enter the name of the new message type"
self.messager.error_dialog(str_error)
return
if not desc:
str_error = "You need to enter a description of the new message type"
self.messager.error_dialog(str_error)
return
if not self.check_type(msg_type):
return
self.make_dir(msg_type)
self.update_db(msg_type, desc)
self.entry_type.set_text("")
self.entry_desc.set_text("")
str_info = "Message Type '{0}' added successfully".format(msg_type)
self.messager.info_dialog(str_info)
def check_type(self, msg_type):
#check no whitespace
if (' ' in msg_type):
str_error = "Please use a name with no spaces"
self.messager.error_dialog(str_error)
return False
#check type not existing
query = "SELECT * FROM typelist WHERE type ILIKE '{0}'".format(msg_type)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
result = cur.fetchall()
if result:
str_error = "This type exists. Please choose a different type"
self.messager.error_dialog(str_error)
cur.close()
conn.close()
return False
else:
return True
def make_dir(self, msg_type):
type_12 = msg_type[0:12]
type_12 = type_12.lower()
filepath = dir_msg + type_12
if os.path.isdir(filepath):
return
else:
os.mkdir(filepath)
def update_db(self, msg_type, desc):
query = "INSERT INTO typelist (type, description) VALUES ('{0}', '{1}')".format(
msg_type, desc)
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
cur.execute(query)
conn.commit()
cur.close()
conn.close()
class DeleteType():
def __init__(self):
dialog = gtk.Dialog("Delete Message Type", None, 10, (
gtk.STOCK_CLOSE, gtk.RESPONSE_CANCEL,
gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
self.messager = Messager()
label_del = gtk.Label("Select the message type that you wish to remove")
self.cb_del = gtk.combo_box_new_text()
self.make_type_cb()
btn_del = gtk.Button("Delete")
btn_del.connect("clicked", self.del_type)
dialog.vbox.pack_start(label_del, False, False, 5)
dialog.vbox.pack_start(self.cb_del, False, False, 5)
dialog.vbox.pack_start(btn_del, False, False, 5)
dialog.show_all()
dialog.run()
dialog.destroy()
def del_type(self, widget):
msg_type = self.cb_del.get_active_text()
msg_pos = self.cb_del.get_active()
if msg_type:
result = self.query_del(msg_type)
if result:
self.remove_dir(msg_type)
self.cb_del.remove_text(msg_pos)
else:
str_error = "Select a Message Type from the drop down list"
self.messager.error_dialog(str_error)
def query_del(self, msg_type):
conn = self.messager.pg_connect_msg()
cur = conn.cursor()
query_check = "SELECT type from messagelist WHERE type = '{0}'".format(msg_type)
query_del = "DELETE from typelist WHERE type = '{0}'".format(msg_type)
cur.execute(query_check)
result = cur.fetchall()
if result:
cur.close()
conn.close()
str_error = "You can not delete a message type which contains messages"
self.messager.error_dialog(str_error)
return False
else:
cur.execute(query_del)
conn.commit()
cur.close()
conn.close()
str_info = "Message Type {0} deleted"
self.messager.info_dialog(str_info)
return (True)
def make_type_cb(self):
type_rows = self.messager.get_types()
for item in type_rows:
self.cb_del.append_text(item[0])
def remove_dir(self, msg_type):
type_12 = msg_type[0:12]
type_12 = type_12.lower()
dirpath = "{0}/{1}/".format (dir_msg, type_12)
contents = os.listdir(dirpath)
if contents:
dt = datetime.datetime.now()
dir_date = datetime.datetime.strftime(dt, "%Y%m")
path_backup = dir_backup + dir_date
if not os.path.isdir(path_backup):
os.mkdir(path_backup)
for item in contents:
filepath = dirpath + item
shutil.move(filepath, path_backup)
os.rmdir(dirpath)
class Preview_Player:
'''
adapted from Benny Malev's DamnSimplePlayer
'''
def __init__(self, time_label, hscale, reset_playbutton):
self.player = gst.element_factory_make("playbin", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
#sink_pre = gst.element_factory_make("alsasink", "preview_sink")
#sink_pre.set_property("device", "preview")
self.player.set_property("video-sink", fakesink)
#self.player.set_property("audio-sink", sink_pre)
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
self.time_format = gst.Format(gst.FORMAT_TIME)
#set statusbar ref.
self.time_label = time_label
self.hscale = hscale
self.reset_playbutton = reset_playbutton
#to hold place on change event in gui
self.place_in_file = None
self.progress_updatable = True
def set_place_in_file(self,place_in_file):
self.place_in_file = place_in_file
def start(self, filepath):
self.player.set_property("uri", "file://" + filepath)
self.player.set_state(gst.STATE_PLAYING)
self.play_thread_id = thread.start_new_thread(self.play_thread, ())
def stop(self):
self.play_thread_id = None
self.player.set_state(gst.STATE_NULL)
self.reset_components()
def pause(self):
self.player.set_state(gst.STATE_PAUSED)
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.play_thread_id = None
self.player.set_state(gst.STATE_NULL)
self.reset_components()
elif t == gst.MESSAGE_ERROR:
self.play_thread_id = None
self.player.set_state(gst.STATE_NULL)
err, debug = message.parse_error()
print ("Error: {0}").format (err, debug)
self.reset_components()
def convert_ns(self, time_int):
time_int = time_int / 1000000000
time_str = ""
if time_int >= 3600:
_hours = time_int / 3600
time_int = time_int - (_hours * 3600)
time_str = str(_hours) + ":"
if time_int >= 600:
_mins = time_int / 60
time_int = time_int - (_mins * 60)
time_str = time_str + str(_mins) + ":"
elif time_int >= 60:
_mins = time_int / 60
time_int = time_int - (_mins * 60)
time_str = time_str + "0" + str(_mins) + ":"
else:
time_str = time_str + "00:"
if time_int > 9:
time_str = time_str + str(time_int)
else:
time_str = time_str + "0" + str(time_int)
return time_str
def get_duration(self):
dur_int = self.player.query_duration(self.time_format, None)[0]
return self.convert_ns(dur_int)
def set_updateable_progress(self,flag):
self.progress_updatable = flag
def rewind_callback(self):
pos_int = self.player.query_position(self.time_format, None)[0]
seek_ns = pos_int - (10 * 1000000000)
self.player.seek_simple(self.time_format, gst.SEEK_FLAG_FLUSH, seek_ns)
def forward_callback(self):
pos_int = self.player.query_position(self.time_format, None)[0]
seek_ns = pos_int + (10 * 1000000000)