-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_manager.py
1708 lines (1320 loc) · 68.7 KB
/
pp_manager.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/env python
import remi.gui as gui
from remi import start, App
from remi_plus import OKDialog,LinkDialog, OKCancelDialog, AdaptableDialog, append_with_label,FileSelectionDialog
import time
import subprocess
import sys, os, shutil
import configparser
import zipfile
from threading import Timer
from time import sleep
from pp_network import Mailer, Network
class PPManager(App):
def __init__(self, *args):
super(PPManager, self).__init__(*args)
def read_options(self,manager_options_file_path):
self.options=Options()
self.options.read_options(manager_options_file_path)
self.pp_home_dir=self.options.pp_home_dir
self.pp_profiles_offset=self.options.pp_profiles_offset
self.pp_profiles_dir=self.options.pp_profiles_dir
self.top_dir=self.options.top_dir
self.media_dir=self.options.media_dir
self.media_offset=self.options.media_offset
self.livetracks_dir=self.options.livetracks_dir
self.livetracks_offset=self.options.livetracks_offset
self.pp_options=self.options.pp_options
self.unit=self.options.unit
self.editor_port=self.options.editor_port
def main(self,pp_dir,upload_dir,download_dir,ip):
self.pp_dir=pp_dir
self.manager_options_file_path=self.pp_dir+os.sep+'pp_config'+os.sep+'pp_web.cfg'
self.upload_dir= upload_dir
self.download_dir = download_dir
self.ip=ip
#init variables
self.profile_objects=[]
self.current_profile=''
# Initialise an instance of the Pi Presents and Web Editor driver classes
self.pp=PiPresents()
self.ed = WebEditor()
self.ed.init(self.pp_dir)
self.read_options(self.pp_dir+'/pp_config/pp_web.cfg')
#Init Mailer to save it config
self.mailer=Mailer()
#build gui
mww=550
# root and frames
root = gui.VBox(width=mww, margin='0px auto') #the margin 0px auto centers the main container
root.style['display'] = 'block'
root.style['overflow'] = 'hidden'
middle_frame=gui.VBox(width=mww,height=500) #5
button_frame=gui.HBox(width=280,height=30) #10
menubar=gui.MenuBar(width='100%', height='30px')
# menu
# menu = gui.Menu(width=mww-20, height=30)
menu=gui.Menu(width='100%', height='30px')
miw=70
# media menu
media_menu = gui.MenuItem('Media',width=miw, height=30)
media_import_menu = gui.MenuItem('Import',width=miw, height=30)
media_upload_menu = gui.MenuItem('Upload',width=miw, height=30)
media_manage_menu = gui.MenuItem('Manage',width=miw, height=30)
media_manage_menu.set_on_click_listener(self.on_media_manage_clicked)
media_import_menu.set_on_click_listener(self.on_media_import_clicked)
media_upload_menu.set_on_click_listener(self.on_media_upload_clicked)
# livetracks menu
livetracks_menu = gui.MenuItem('Live Tracks',width=miw, height=30)
livetracks_import_menu = gui.MenuItem('Import',width=miw, height=30)
livetracks_upload_menu = gui.MenuItem('Upload',width=miw, height=30)
livetracks_import_menu.set_on_click_listener(self.on_livetracks_import_clicked)
livetracks_upload_menu.set_on_click_listener(self.on_livetracks_upload_clicked)
livetracks_manage_menu = gui.MenuItem('Manage',width=miw, height=30)
livetracks_manage_menu.set_on_click_listener(self.on_livetracks_manage_clicked)
#profile menu
profile_menu = gui.MenuItem( 'Profile',width=miw, height=30)
profile_import_menu = gui.MenuItem('Import',width=miw, height=30)
profile_import_menu.set_on_click_listener(self.on_profile_import_clicked)
profile_upload_menu = gui.MenuItem('Upload',width=miw, height=30)
profile_upload_menu.set_on_click_listener(self.on_profile_upload_clicked)
profile_download_menu = gui.MenuItem('Download',width=miw, height=30)
profile_download_menu.set_on_click_listener(self.on_profile_download_clicked)
profile_manage_menu = gui.MenuItem('Manage',width=miw, height=30)
profile_manage_menu.set_on_click_listener(self.on_profiles_manage_clicked)
#logs menu
logs_menu = gui.MenuItem( 'Logs',width=miw, height=30)
log_download_menu = gui.MenuItem('Download Log',width=miw + 80, height=30)
log_download_menu.set_on_click_listener(self.on_log_download_clicked)
stats_download_menu = gui.MenuItem('Download Stats',width=miw + 80, height=30)
stats_download_menu.set_on_click_listener(self.on_stats_download_clicked)
# editor menu
editor_menu=gui.MenuItem('Editor',width=miw,height=30)
editor_run_menu=gui.MenuItem('Run',width=miw,height=30)
editor_run_menu.set_on_click_listener(self.on_editor_run_menu_clicked)
editor_exit_menu=gui.MenuItem('Exit',width=miw,height=30)
editor_exit_menu.set_on_click_listener(self.on_editor_exit_menu_clicked)
#options menu
options_menu=gui.MenuItem('Options',width=miw,height=30)
options_manager_menu=gui.MenuItem('Manager',width=miw,height=30)
options_manager_menu.set_on_click_listener(self.on_options_manager_menu_clicked)
options_autostart_menu=gui.MenuItem('Autostart',width=miw,height=30)
options_autostart_menu.set_on_click_listener(self.on_options_autostart_menu_clicked)
options_email_menu=gui.MenuItem('Email',width=miw,height=30)
options_email_menu.set_on_click_listener(self.on_options_email_menu_clicked)
# Pi menu
pi_menu=gui.MenuItem('Pi',width=miw,height=30)
pi_reboot_menu=gui.MenuItem('Reboot Pi',width=miw+30,height=30)
pi_reboot_menu.set_on_click_listener(self.pi_reboot_menu_clicked)
pi_shutdown_menu=gui.MenuItem('Shutdown Pi',width=miw+30,height=30)
pi_shutdown_menu.set_on_click_listener(self.pi_shutdown_menu_clicked)
pi_close_manager_menu=gui.MenuItem('Exit PPManager',width=miw+30,height=30)
pi_close_manager_menu.set_on_click_listener(self.pi_close_manager_menu_clicked)
# list of profiles
self.profile_list = gui.ListView(width=250, height=300)
self.profile_list.set_on_selection_listener(self.on_profile_selected)
#status and buttons
self.profile_name = gui.Label('Selected Profile: ',width=400, height=20)
self.pp_state_display = gui.Label('',width=400, height=20)
self.pp_state_display.css_font_weight='bold'
self.run_pp = gui.Button('Run',width=80, height=30)
self.run_pp.set_on_click_listener(self.on_run_button_pressed)
self.exit_pp = gui.Button('Exit',width=80, height=30)
self.exit_pp.set_on_click_listener(self.on_exit_button_pressed)
self.refresh = gui.Button('Refresh List',width=120, height=30)
self.refresh.set_on_click_listener(self.on_refresh_profiles_pressed)
# Build the layout
# buttons
button_frame.append(self.run_pp)
button_frame.append(self.exit_pp)
button_frame.append(self.refresh)
# middle frame
middle_frame.append(menubar)
middle_frame.append(self.pp_state_display)
middle_frame.append(self.profile_list)
middle_frame.append(button_frame)
middle_frame.append(self.profile_name)
# menus
profile_menu.append(profile_import_menu)
profile_menu.append(profile_upload_menu)
profile_menu.append(profile_download_menu)
profile_menu.append(profile_manage_menu)
media_menu.append(media_import_menu)
media_menu.append(media_upload_menu)
media_menu.append(media_manage_menu)
livetracks_menu.append(livetracks_import_menu)
livetracks_menu.append(livetracks_upload_menu)
livetracks_menu.append(livetracks_manage_menu)
logs_menu.append(log_download_menu)
logs_menu.append(stats_download_menu)
editor_menu.append(editor_run_menu)
editor_menu.append(editor_exit_menu)
options_menu.append(options_manager_menu)
options_menu.append(options_autostart_menu)
options_menu.append(options_email_menu)
pi_menu.append(pi_reboot_menu)
pi_menu.append(pi_shutdown_menu)
pi_menu.append(pi_close_manager_menu)
menu.append(profile_menu)
menu.append(media_menu)
menu.append(livetracks_menu)
menu.append(logs_menu)
menu.append(editor_menu)
menu.append(options_menu)
menu.append(pi_menu)
menubar.append(menu)
root.append(middle_frame)
# display the initial list of profiles
self.display_profiles()
# kick of regular display of Pi Presents running state
self.display_state()
# returning the root widget
return root
# ******************
# Pi Menu
# ******************
def pi_reboot_menu_clicked(self,widget):
subprocess.call (['sudo','reboot'])
def pi_shutdown_menu_clicked(self,widget):
subprocess.call (['sudo','shutdown','now','SHUTTING DOWN'])
# listener function
def pi_close_manager_menu_clicked(self, _):
self.close() # closes the application
def on_close(self):
""" Overloading App.on_close event allows to perform some
activities before app termination.
"""
self.state_timer.cancel()
print("PPManager Closing")
super(PPManager, self).on_close()
# ******************
# MANAGER OPTIONS
# ******************
def on_options_autostart_menu_clicked(self,widget):
self.options_autostart_dialog=AutostartOptionsDialog(self.pp_home_dir,self.pp_profiles_dir)
self.options_autostart_dialog.show(self)
def on_options_manager_menu_clicked(self,widget):
self.options_manager_dialog=ManagerOptionsDialog(self.pp_home_dir,self.pp_profiles_dir,
callback=self.options_manager_callback)
self.options_manager_dialog.show(self)
def options_manager_callback(self):
# and display the new list of profiles after editing options
self.read_options(self.manager_options_file_path)
self.display_profiles()
def on_options_email_menu_clicked(self,widget):
self.options_email_dialog=EmailOptionsDialog(callback=self.options_email_callback)
self.options_email_dialog.show(self)
def options_email_callback(self):
self.mailer.read_config()
# ******************
#MEDIA
# ******************
# import
def on_media_import_clicked(self,widget):
if not os.path.exists(self.top_dir):
OKDialog('Import Media','Cannot find import starting directory: '+ self.top_dir).show(self)
return
fileselectionDialog = FileSelectionDialog('Import Media', 'Select files to import',True, self.top_dir,
allow_file_selection=True, allow_folder_selection=False,
callback=self.on_media_import_dialog_confirm)
fileselectionDialog.show(self)
def on_media_import_dialog_confirm(self,filelist):
if len(filelist)==0:
OKDialog('Import Media','No file selected').show(self)
return
self.import_list=filelist
import_from1=filelist[0]
self.import_to=self.media_dir
if not import_from1.startswith(self.top_dir):
OKDialog('Import Media','Access to import source prohibited: ' + import_from1).show(self)
return
if not os.path.exists(self.import_to):
OKDialog('Import Media','Media directory does not exist: ' + self.import_to).show(self)
return
for item in self.import_list:
self.current_item=item
if os.path.isdir(item):
OKDialog('Import Media','Cannot import a directory, ignoring: '+ item).show(self)
continue
from_head,from_tail=os.path.split(item)
to_path=os.path.join(self.import_to,from_tail)
if os.path.exists(to_path):
OKCancelDialog('Import Media','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_import_media_item).show(self)
else:
self.do_import_media_item(True)
def do_import_media_item(self,result):
if result is True:
shutil.copy2(self.current_item, self.import_to)
return
#upload
def on_media_upload_clicked(self,widget):
self.media_upload_dialog=AdaptableDialog(width=300,height=200,title='Upload Media',
message='Select Media to Upload',
cancel_name='Done')
self.media_upload_button=gui.FileUploader(self.upload_dir+'/',width=250,height=30,multiple_selection_allowed=False)
self.media_upload_button.set_on_success_listener(self.on_media_upload_success)
self.media_upload_button.set_on_failed_listener(self.on_media_upload_failed)
self.media_upload_status=gui.Label('', width=450, height=30)
self.media_upload_dialog.append_field(self.media_upload_button)
self.media_upload_dialog.append_field(self.media_upload_status)
self.media_upload_dialog.show(self)
def on_media_upload_success(self,widget,filelist):
if len(filelist)==0:
OKDialog('Upload Media','No file selected').show(self)
return
self.upload_list=filelist
self.upload_to=self.media_dir
if not os.path.exists(self.upload_to):
OKDialog('Upload Media','Media directory does not exist: ' + self.upload_to).show(self)
return
item=self.upload_list
self.current_item=self.upload_list
if os.path.isdir(item):
OKDialog('Upload Media','Cannot upload a directory, ignoring: '+ item).show(self)
return
from_head,from_tail=os.path.split(item)
self.to_path=os.path.join(self.upload_to,from_tail)
if os.path.exists(self.to_path):
OKCancelDialog('Upload Media','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_upload_media_item).show(self)
else:
self.do_upload_media_item(True)
## for item in self.upload_list:
## self.current_item=item
## if os.path.isdir(item):
## OKDialog('Upload Media','Cannot upload a directory, ignoring: '+ item).show(self)
## continue
## from_head,from_tail=os.path.split(item)
## to_path=os.path.join(self.upload_to,from_tail)
## if os.path.exists(to_path):
## OKCancelDialog('Upload Media','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_upload_media_item).show(self)
## else:
## self.do_upload_media_item(True)
def do_upload_media_item(self,result):
if result is True:
if os.path.exists(self.to_path):
os.remove(self.to_path)
shutil.move(self.upload_dir+os.sep+self.current_item, self.upload_to)
self.media_upload_status.set_text('File upload successful')
else:
os.remove(self.upload_dir+os.sep+self.current_item)
def on_media_upload_failed(self,widget,result):
self.media_upload_status.set_text('ERROR: File upload failed')
OKDialog('Import Media','File Upload Failed').show(self._base_app_instance)
#manage
def on_media_manage_clicked(self,widget):
self.manage_media_dialog=FileManager("Manage Media",self.media_dir,False,self.finished_manage_media)
self.manage_media_dialog.show(self)
def finished_manage_media(self):
pass
# *********************
# LIVE TRACKS
# *********************
# import
def on_livetracks_import_clicked(self,widget):
if not os.path.exists(self.top_dir):
OKDialog('Import Live Tracks','Cannot find import starting directory: '+self.top_dir).show(self)
return
fileselectionDialog = FileSelectionDialog('Import Live Tracks', 'Select files to import',True, self.top_dir,
allow_file_selection=True, allow_folder_selection=False,
callback=self.on_livetracks_import_dialog_confirm)
fileselectionDialog.show(self)
def on_livetracks_import_dialog_confirm(self,filelist):
if len(filelist)==0:
OKDialog('Import Live Tracks','No file selected').show(self)
return
self.import_list=filelist
import_from1=filelist[0]
self.import_to=self.livetracks_dir
if not import_from1.startswith(self.top_dir):
OKDialog('Import Live Tracks','Access to source prohibited: ' + import_from1).show(self)
return
if not os.path.exists(self.import_to):
OKDialog('Import Live Tracks','Live Tracks directory does not exist: ' + self.import_to).show(self)
return
for item in self.import_list:
self.current_item=item
if os.path.isdir(item):
OKDialog('Import Live Tracks','Cannot import a directory, ignoring: '+ item).show(self)
continue
from_head,from_tail=os.path.split(item)
to_path=os.path.join(self.import_to,from_tail)
if os.path.exists(to_path):
OKCancelDialog('Import Live Tracks','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_import_livetracks_item).show(self)
else:
self.do_import_livetracks_item(True)
def do_import_livetracks_item(self,result):
if result is True:
shutil.copy2(self.current_item, self.import_to)
return
#upload
def on_livetracks_upload_clicked(self,widget):
self.livetracks_upload_dialog=AdaptableDialog(width=500,height=200,title='Upload Live Tracks',
message='Select Live Tracks to Upload',
cancel_name='Done')
self.livetracks_upload_button=gui.FileUploader(self.upload_dir+'/',width=250,height=30,
multiple_selection_allowed=False)
self.livetracks_upload_button.set_on_success_listener(self.on_livetracks_upload_success)
self.livetracks_upload_button.set_on_failed_listener(self.on_livetracks_upload_failed)
self.livetracks_upload_status=gui.Label('', width=450, height=30)
self.livetracks_upload_dialog.append_field(self.livetracks_upload_button)
self.livetracks_upload_dialog.append_field(self.livetracks_upload_status)
self.livetracks_upload_dialog.show(self)
def on_livetracks_upload_success(self,widget,filelist):
if len(filelist)==0:
OKDialog('Upload Livetracks','No file selected').show(self)
return
self.upload_list=filelist
self.upload_to=self.livetracks_dir
if not os.path.exists(self.upload_to):
OKDialog('Upload Livetracks','Livetracks directory does not exist: ' + self.upload_to).show(self)
return
item=self.upload_list
self.current_item=self.upload_list
if os.path.isdir(item):
OKDialog('Upload Livetracks','Cannot upload a directory, ignoring: '+ item).show(self)
return
from_head,from_tail=os.path.split(item)
self.to_path=os.path.join(self.upload_to,from_tail)
if os.path.exists(self.to_path):
OKCancelDialog('Upload Livetracks','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_upload_livetracks_item).show(self)
else:
self.do_upload_livetracks_item(True)
## for item in self.upload_list:
## self.current_item=item
## if os.path.isdir(item):
## OKDialog('Upload Livetracks','Cannot upload a directory, ignoring: '+ item).show(self)
## continue
## from_head,from_tail=os.path.split(item)
## to_path=os.path.join(self.upload_to,from_tail)
## if os.path.exists(to_path):
## OKCancelDialog('Upload Livetracks','Item already exists: ' + self.current_item +'Overwrite?',callback=self.do_upload_media_item).show(self)
## else:
## self.do_upload_livetracks_item(True)
def do_upload_livetracks_item(self,result):
if result is True:
if os.path.exists(self.to_path):
os.remove(self.to_path)
shutil.move(self.upload_dir+os.sep+self.current_item, self.upload_to)
self.livetracks_upload_status.set_text('File upload successful')
else:
os.remove(self.upload_dir+os.sep+self.current_item)
def on_livetracks_upload_failed(self,result):
self.livetracks_upload_status.set_text('ERROR: File upload failed')
OKDialog('Upload Live Tracks','File upload failed').show(self._base_app_instance)
#manage
def on_livetracks_manage_clicked(self,widget):
self.manage_livetracks_dialog=FileManager("Manage Live Tracks",self.livetracks_dir,False,
self.finished_manage_livetracks)
self.manage_livetracks_dialog.show(self)
def finished_manage_livetracks(self):
pass
# ******************
#PROFILES
# ******************
# import
def on_profile_import_clicked(self,widget):
if not os.path.exists(self.top_dir):
OKDialog('Import Profile','Cannot find import directory').show(self)
return
fileselectionDialog = FileSelectionDialog('Import Profile', 'Select a Profile to import',False,self.top_dir,
allow_file_selection=False,allow_folder_selection=True,
callback=self.on_profile_import_dialog_confirm)
fileselectionDialog.show(self)
def on_profile_import_dialog_confirm(self,filelist):
if len(filelist)==0:
OKDialog('Import Profile','No profile selected').show(self)
return
self.import_from=filelist[0]
self.from_basename=os.path.basename(self.import_from)
self.import_to=self.pp_profiles_dir+os.sep+self.from_basename
# print self.import_from, self.import_to, self.top_dir
if not self.import_from.startswith(self.top_dir):
OKDialog('Import Profile','Access to import source prohibited: ' + self.import_from).show(self)
return
if not os.path.isdir(self.import_from):
OKDialog('Import Profile','Profile is not a directory: ' + self.import_from).show(self)
return
if not os.path.exists(self.import_from + os.sep + 'pp_showlist.json'):
OKDialog('Import Profile','Profile does not have pp_showlist.json: ' + self.import_from).show(self)
return
if os.path.exists(self.import_to):
OKCancelDialog('Import Profile','Profile already exists, overwrite?',self.import_profile_confirm).show(self)
else:
self.import_profile_confirm(True)
def import_profile_confirm(self,result):
if result is True:
if os.path.exists(self.import_to):
shutil.rmtree(self.import_to)
# print self.import_from,self.import_to
shutil.copytree(self.import_from, self.import_to)
self.profile_count=self.display_profiles()
# download
def on_profile_download_clicked(self,widget):
if self.current_profile != '':
dest=self.download_dir+os.sep+self.current_profile_name
# print 'temp',dest
base=self.pp_home_dir+os.sep+'pp_profiles/'+self.current_profile
# print 'proflie',base
shutil.make_archive(dest,'zip',base)
self.profile_download_dialog=AdaptableDialog(width=500,height=200,title='Download Profile',
message='',confirm_name='Done')
self.profile_download_button = gui.FileDownloader('Click Link to Download', dest+'.zip', width=200, height=80)
self.profile_download_status=gui.Label('', width=450, height=30)
self.profile_download_status.set_text('Selected Profile: '+ self.current_profile)
self.profile_download_dialog.append_field(self.profile_download_status)
self.profile_download_dialog.append_field(self.profile_download_button)
self.profile_download_dialog.show(self)
self.profile_download_dialog.set_on_confirm_dialog_listener(self.on_profile_download_dialog_done)
else:
OKDialog('Download Profile', 'No profile selected').show(self)
# NO WAY TO DELETE THE ZIP except on _done
def on_profile_download_dialog_done(self,widget):
shutil.rmtree(self.download_dir)
os.makedirs(self.download_dir)
self.profile_download_dialog.hide()
# upload
def on_profile_upload_clicked(self,widget):
self.profile_upload_dialog=AdaptableDialog(width=500,height=200,title='Upload Profile',
message='Select Profile to Upload',
cancel_name='Done')
self.profile_upload_button=gui.FileUploader(self.upload_dir+os.sep,width=250,height=30,multiple_selection_allowed=False)
self.profile_upload_button.set_on_success_listener(self.on_profile_upload_success)
self.profile_upload_button.set_on_failed_listener(self.on_profile_upload_failed)
self.profile_upload_status=gui.Label('', width=450, height=30)
self.profile_upload_dialog.append_field(self.profile_upload_button)
self.profile_upload_dialog.append_field(self.profile_upload_status)
self.profile_upload_dialog.show(self)
def on_profile_upload_success(self,widget,filename):
#filename is leafname of uploaded file
# uploaded zip file goes into as specified in the widget constructor
self.profile_upload_filename=filename # xxxx.zip
self.profile_upload_directory='/'+self.profile_upload_filename.split('.')[0] # /xxxx
source_file_path =self.upload_dir+os.sep+self.profile_upload_filename
# unzip it into a directory in pp_temp with the uploaded filename
if not zipfile.is_zipfile(source_file_path):
self.profile_upload_status.set_text('ERROR: Uploaded file is not a Zip archive')
os.remove(source_file_path)
return
zzip=zipfile.ZipFile(source_file_path)
zzip.extractall(self.upload_dir+self.profile_upload_directory)
os.remove(source_file_path)
# check temp directory is a profile
if not os.path.exists(self.upload_dir+self.profile_upload_directory+os.sep+'pp_showlist.json'):
self.profile_upload_status.set_text('ERROR: Uploaded Zip is not a profile')
shutil.rmtree(self.upload_dir+self.profile_upload_directory)
else:
# warn if profile already exists
if os.path.exists(self.pp_profiles_dir+self.profile_upload_directory):
OKCancelDialog('Profile Upload','Profile already exists, overwrite?',self.on_profile_replace_ok).show(self)
else:
self.on_profile_replace_ok(True)
def on_profile_replace_ok(self,result=False):
if result:
if os.path.exists(self.pp_profiles_dir+self.profile_upload_directory):
shutil.rmtree(self.pp_profiles_dir+self.profile_upload_directory)
shutil.move(self.upload_dir+self.profile_upload_directory, self.pp_profiles_dir)
self.profile_count=self.display_profiles()
self.profile_upload_status.set_text('Profile upload successful: '+self.profile_upload_filename)
else:
self.profile_upload_status.set_text('Profile upload cancelled')
shutil.rmtree(self.upload_dir+self.profile_upload_directory)
def on_profile_upload_failed(self,filename):
self.profile_upload_status.set_text(' Upload of Zip File Failed: ' + filename )
self.profile_upload_filename=filename # xxxx.zip
source_file_path =self.upload_dir+os.sep+self.profile_upload_filename
if os.path.exists(source_file_path):
os.remove(source_file_path)
#manage
def on_profiles_manage_clicked(self,widget):
self.manage_media_dialog=FileManager("Manage Profiles",self.pp_profiles_dir,True,self.finished_manage_profiles)
self.manage_media_dialog.show(self)
def finished_manage_profiles(self):
self.display_profiles()
# ******************
# PROFILES LIST
# ******************
def on_refresh_profiles_pressed(self,widget):
self.display_profiles()
self.profile_name.set_text('Selected Profile:')
def display_profiles(self):
self.current_profile=''
self.profile_list.empty()
us_items = os.listdir(self.pp_profiles_dir)
items=sorted(us_items)
i=0
for item in items:
if os.path.isdir(self.pp_profiles_dir+ os.sep+item) is True and os.path.exists(self.pp_profiles_dir+ os.sep + item + os.sep + 'pp_showlist.json') is True:
obj= gui.ListItem(item,width=200, height=20)
self.profile_objects.append(obj)
self.profile_list.append(obj,key=i)
i+=1
return
def on_profile_selected(self,widget,key):
self.current_profile_name=self.profile_list.children[key].get_text()
if self.pp_profiles_offset !='':
self.current_profile=self.pp_profiles_offset + os.sep + self.current_profile_name
else:
self.current_profile=self.current_profile_name
self.profile_name.set_text('Selected Profile: '+ self.current_profile_name)
# ******************
# LOGS
# ******************
def on_log_download_clicked(self,widget):
self.on_logs_download_clicked('pp_log.txt','Log')
def on_stats_download_clicked(self,widget):
self.on_logs_download_clicked('pp_stats.txt','Statistics')
# download
def on_logs_download_clicked(self,log_file,name):
self.logs_dir=self.pp_dir+os.sep+'pp_logs'
self.logs_download_dialog=AdaptableDialog(width=500,height=200,title='Download ' + name,
message='',confirm_name='Done')
self.logs_download_button = gui.FileDownloader('Click Link to Start Download',self.logs_dir+os.sep+log_file, width=200, height=80)
self.logs_download_status=gui.Label('', width=450, height=30)
self.logs_download_dialog.append_field(self.logs_download_status)
self.logs_download_status.set_text('Download: '+log_file)
self.logs_download_dialog.append_field(self.logs_download_button)
self.logs_download_dialog.show(self)
self.logs_download_dialog.set_on_confirm_dialog_listener(self.on_logs_download_dialog_confirm)
def on_logs_download_dialog_confirm(self,widget):
self.logs_download_dialog.hide()
# ******************
# RUNNING Pi Presents
# ******************
def display_state(self):
if os.name== 'nt':
self.pp_state_display.set_text('Server on Windows')
else:
my_state=self.pp.am_i_running()
# Poll state of Pi Presents
pid,user,profile=self.pp.is_pp_running()
if pid!=-1:
self.pp_state_display.set_text(self.unit + ': RUNNING '+ profile)
else:
self.pp_state_display.set_text(self.unit + ': STOPPED (' + self.pp.lookup_state(my_state)+')')
self.state_timer=Timer(0.5,self.display_state)
self.state_timer.start()
def on_run_button_pressed(self,widget):
if os.name== 'nt':
OKDialog('Run Pi Presents','Failed, server on Windows').show(self)
return
if self.current_profile != '':
command = self.pp_dir+'/pipresents.py'
success=self.pp.run_pp(command,self.pp_home_dir,self.current_profile,self.pp_options)
if success is False:
OKDialog('Run Pi Presents','Error: Pi Presents already Running').show(self)
return
else:
OKDialog('Run Pi Presents','Error: No profile selected').show(self)
def on_exit_button_pressed(self,widget):
if os.name== 'nt':
OKDialog('Run Pi Presents','Failed, server on Windows').show(self)
return
success=self.pp.exit_pp()
if success is False:
OKDialog('Exit Pi Presents','Pi Presents Not Running').show(self)
# ******************
# RUNNING Editor
# ******************
def ed_display_state(self):
if os.name== 'nt':
OKDialog('Run Editor','Failed, server on Windows').show(self)
else:
my_state=self.ed.am_i_running()
# Poll state of Editor
pid,user=self.pp.is_ed_running()
if pid!=-1:
self.ed_state_display.set_text(self.unit + ': RUNNING Web Editor as '+user)
else:
self.ed_state_display.set_text(self.unit + ': STOPPED (' + self.ed.lookup_state(my_state)+')')
Timer(0.5,self.ed_display_state).start()
def on_editor_run_menu_clicked(self,widget):
if os.name== 'nt':
OKDialog('Run Pi Presents','Failed, server on Windows').show(self)
return
command = self.pp_dir+'/pp_web_editor.py'
self.ed_options=''
# run editor if it is not already running
self.ed.run_ed(command,self.ed_options)
# and show a dialog to open a browser tab
LinkDialog('Open Editor Page','','http://' + self.ip + ':'+self.editor_port +'"target="_blank"','Click to Open Editor Page').show(self)
def on_editor_exit_menu_clicked(self,widget):
if os.name== 'nt':
OKDialog('Run Pi Presents','Failed, server on Windows').show(self)
return
success=self.ed.exit_ed()
if success is False:
OKDialog('Exit Editor','Error: Editor Not Running').show(self)
# ******************
# File Manager
# ******************
class RenameDialog(AdaptableDialog):
def __init__(self, title,file_dir,filename,callback):
self.filename=filename
self.file_dir=file_dir
self.callback=callback
self.width=400
self.height=200
super(RenameDialog, self).__init__(title,'',width=self.width,height=self.height,
confirm_name='Ok',cancel_name='Cancel')
self.spacer=gui.Label('',width=400, height=30)
self.append_field(self.spacer,key='spacer')
self.name_field=gui.TextInput(single_line=True,width=self.width-100,height=30)
self.name_field.set_text(self.filename)
self.append_field(self.name_field,'name_field')
@gui.decorate_event
def confirm_dialog(self,emitter):
new_name=self.get_field('name_field').get_value()
files_in_dir = os.listdir(self.file_dir)
if new_name in files_in_dir:
OKDialog('Rename File','File already exists').show(self._base_app_instance)
return
else:
# print self.file_dir + os.sep+ self.filename
os.rename(self.file_dir + os.sep+ self.filename,self.file_dir + os.sep + new_name)
self.hide()
self.callback()
class FileManager(AdaptableDialog):
def __init__(self, title, media_dir, is_profile, callback):
self.title=title
self.is_profile=is_profile
self.current_media=media_dir
root_width= 550
root_height=500
self.callback=callback
self.current_media_name=''
super(FileManager, self).__init__(title,'',
width=root_width,height=root_height,
cancel_name='Done')
self.style['display'] = 'block'
self.style['overflow'] = 'hidden'
self.style['margin'] = '0px auto'
self._frame=gui.VBox(width=root_width, height=400)
self.append_field(self._frame,key='frame')
self.spacer=gui.Label('\n',width=400, height=20)
self._frame.append(self.spacer,key='spacer')
self.media_list = gui.ListView(width=250, height=300)
self.media_list.set_on_selection_listener(self.on_media_selected)
self._frame.append(self.media_list,key='media_list')
self.buttons_frame= gui.HBox(width=280, height=30)
self._frame.append(self.buttons_frame,key='buttons_frame')
self.media_name=gui.Label('Selected Item: ',width=400, height=20)
self._frame.append(self.media_name,key='media_name')
self.delete_media = gui.Button('Delete',width=80, height=30)
self.delete_media.set_on_click_listener(self.on_delete_media_button_pressed)
self.buttons_frame.append(self.delete_media,key='delete_media')
self.rename_media = gui.Button('Rename',width=80, height=30)
self.rename_media.set_on_click_listener(self.on_rename_media_button_pressed)
self.buttons_frame.append(self.rename_media,key='rename_media')
self.deleteall_media = gui.Button('Delete All',width=80, height=30)
self.deleteall_media.set_on_click_listener(self.on_deleteall_media_button_pressed)
self.buttons_frame.append(self.deleteall_media,key='deleteall_media')
self.display_media()
def on_refresh_media_pressed(self,widget):
self.display_media()
def display_media(self):
self.media_list.empty()
self.current_media_name=''
items=sorted(os.listdir(self.current_media))
i=0
for item in items:
if (self.is_profile is False and os.path.isdir(self.current_media+ os.sep+item) is False) or (self.is_profile is True and os.path.isdir(self.current_media+ os.sep+item) is True and os.path.exists(self.current_media+ os.sep + item + os.sep + 'pp_showlist.json') is True):
obj= gui.ListItem(item,width=200, height=20)
self.media_list.append(obj,key=i)
i+=1
return
def on_media_selected(self,widget,key):
self.current_media_name=self.media_list.children[key].get_text()
self.media_name.set_text('Selected File: '+ self.current_media_name)
@gui.decorate_event
def cancel_dialog(self,emitter):
self.hide()
self.callback()
def on_delete_media_button_pressed(self,widget):
if self.current_media_name=='':
OKDialog('Delete','No File Selected').show(self._base_app_instance )
return
OKCancelDialog('Delete Item','Delete '+self.current_media_name +' Are you sure?',callback=self.on_delete_media_confirm).show(self._base_app_instance)
return
def on_deleteall_media_button_pressed(self,widget):
OKCancelDialog('DELETE ALL ITEMS','DELETE ALL ITEMS, Are you sure?',callback=self.on_deleteall_media_confirm).show(self._base_app_instance)
return
def on_deleteall_media_confirm(self,result):
if result is False:
return
#print ('delete ALL ')
for item in os.listdir(self.current_media):
#print ('delete one of all '+self.current_media+os.sep+item)
os.remove(self.current_media+os.sep+item)
self.display_media()
def on_delete_media_confirm(self,result):
if result is False:
return
print ('delete ONE' +self.current_media+os.sep+self.current_media_name)
if self.is_profile is True:
pass
#print ('delete one profile '+self.current_media+os.sep+self.current_media_name)
shutil.rmtree(self.current_media+os.sep+self.current_media_name)
else:
pass
#print ('delete one media '+self.current_media+os.sep+self.current_media_name)
os.remove(self.current_media+os.sep+self.current_media_name)
self.display_media()
self.media_name.set_text('Selected File:')