Пример #1
0
class LiveShow:


    NEW_TRACKS={'image':{'title':'New Image','track-ref':'','type':'image','location':'','duration':'','transition':'',
                              'track-text':'','track-text-font':'','track-text-colour':'','track-text-x':'0','track-text-y':'0'},
                'video':{'title':'New Video','track-ref':'','type':'video','location':'','omx-audio':''}}
    
    IMAGE_FILES=('.gif','.jpg','.jpeg','.bmp','.png','.tif')
    VIDEO_FILES=('.mp4','.mkv','.avi','.mp2','.wmv', '.vob')
    AUDIO_FILES=('.mp3','.wav','.ogg')
                         
# *******************
# External interface
# ********************

    def __init__(self,
                            show,
                            canvas,
                            showlist,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the show is to be written on
            showlist - used jus to check the issue of medialist against showlist
            show - the dictionary for the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show =show
        self.showlist=showlist
        self.canvas=canvas
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

        # Init variables
        self.player=None
        self.shower=None
        self._end_liveshow_signal=False
        self._play_child_signal = False
        self.error=False
        
        self._livelist=None
        self._new_livelist= None


    def play(self,end_callback,ready_callback=None, top=False,command='nil'):

        """ displays the liveshow
              end_callback - function to be called when the liveshow exits
              ready_callback - callback when liveshow is ready to display
              top is True when the show is top level (i.e. run from start show)
        """

        #instantiate the arguments
        self._end_callback=end_callback
        self._ready_callback=ready_callback
        self.top=top
        self.mon.log(self,"Starting show: " + self.show['show-ref'])

        # check  data files are available.
        self.media_file = self.pp_profile + os.sep + self.show['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self,"Medialist file not found: "+ self.media_file)
            self._stop("Medialist file not found")
            
        self.options=command_options()
               
        self._pp_live_dir1 = self.pp_home + os.sep + 'pp_live_tracks'
        if not os.path.exists(self._pp_live_dir1):
            os.mkdir(self._pp_live_dir1)

        self._pp_live_dir2=''   
        if self.options['liveshow'] <>"":
            self._pp_live_dir2 = self.options['liveshow']
            if not os.path.exists(self._pp_live_dir2):
                self.mon.err(self,"live tracks directory not found " + self._pp_live_dir2)
                self._end('error',"live tracks directory not found")

        #create a medialist for the liveshow and read it.
        # it should be empty of anonymous tracks but read it to check its version.
        self.medialist=MediaList()
        if self.medialist.open_list(self.media_file,self.showlist.sissue())==False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self._end('error',"Version of medialist different to Pi Presents")
        
        if self.ready_callback<>None:
             self.ready_callback()
             
        self._play_first_track()
        
 


   # respond to key presses.
    def key_pressed(self,key_name):
        self.mon.log(self,"received key: " + key_name)
        
        if key_name=='':
            pass
        
        elif key_name=='escape':
            # if next lower show eor player is running pass down to stop the show/track
            # ELSE stop this show except for exceptions
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
            else:
                # not at top so stop the show
                if  self.top == False:
                    self._stop("exit show to higher level")
                else:
                    pass
    
        elif key_name in ('up','down'):
        # if child or sub-show is running and is a show pass to show, track does not use up/down
            if self.shower<>None:
                self.shower.key_pressed(key_name)

                
        elif key_name=='return':
            # if child show or sub-show is running and is show - pass down
            # ELSE use Return to start child
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self.show['has-child']=="yes":
                    self._play_child()
              
        elif key_name in ('p',' '):
            # pass down if show or track running.
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
 

    def button_pressed(self,button,edge):
        if button=='play': self.key_pressed("return")
        elif  button =='up': self.key_pressed("up")
        elif button=='down': self.key_pressed("down")
        elif button=='stop': self.key_pressed("escape")
        elif button=='pause': self.key_pressed('p')


       
    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.mon.log(self,"sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player<>None:
            self.mon.log(self,"sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,'terminated without terminating shower or player')

 
    def _tidy_up(self):
        pass


    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            self.terminate("error",'Cannot find resource')
        else:
            return value
        

# ***************************
# Respond to key/button presses
# ***************************

    def _stop(self,message):
        self._end_liveshow_signal=True

        
    def _play_child(self):
        self._play_child_signal=True
        if self.player<>None:
            self.player.key_pressed("escape")
      
        
# ***************************
# end of show functions
# ***************************

    def _end(self,reason,message):
        self._end_liveshow_signal=False
        self.mon.log(self,"Ending Liveshow: "+ self.show['show-ref'])
        self._tidy_up()
        self._end_callback(reason,message)
        self=None
        return
        
    def _nend(self):
        self._end('normal','end from state machine')
  

# ***************************
# Livelist
# ***************************       
        
    def _livelist_add_track(self,afile):
        (root,title)=os.path.split(afile)
        (root,ext)= os.path.splitext(afile)
        if ext.lower() in LiveShow.IMAGE_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['image'],{'title':title,'track-ref':'','location':afile})
        if ext.lower() in LiveShow.VIDEO_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['video'],{'title':title,'track-ref':'','location':afile})
        if ext.lower() in LiveShow.AUDIO_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['video'],{'title':title,'track-ref':'','location':afile})
           


        
    def _livelist_new_track(self,fields,values):
        new_track=fields
        self._new_livelist.append(copy.deepcopy(new_track))
        last = len(self._new_livelist)-1
        self._new_livelist[last].update(values)        
    

        
    def _new_livelist_create(self):
     
        self._new_livelist=[]
        if os.path.exists(self._pp_live_dir1):
            for file in os.listdir(self._pp_live_dir1):
                file = self._pp_live_dir1 + os.sep + file
                (root_file,ext_file)= os.path.splitext(file)
                if ext_file.lower() in LiveShow.IMAGE_FILES+LiveShow.VIDEO_FILES+LiveShow.AUDIO_FILES:
                    self._livelist_add_track(file)
                    
        if os.path.exists(self._pp_live_dir2):
            for file in os.listdir(self._pp_live_dir2):
                file = self._pp_live_dir2 + os.sep + file
                (root_file,ext_file)= os.path.splitext(file)
                if ext_file.lower() in LiveShow.IMAGE_FILES+LiveShow.VIDEO_FILES+LiveShow.AUDIO_FILES:
                    self._livelist_add_track(file)
                    

        self._new_livelist= sorted(self._new_livelist, key= lambda track: os.path.basename(track['location']).lower())
#       for it in self._new_livelist:
#          print it['location']
#      print ''


    
    def _livelist_replace_if_changed(self):
        self._new_livelist_create()
        if  self._new_livelist<>self._livelist:
            self._livelist=copy.deepcopy(self._new_livelist)
            self._livelist_index=0
   
   
    def _livelist_next(self):
        if self._livelist_index== len(self._livelist)-1:
            self._livelist_index=0
        else:
            self._livelist_index +=1


# ***************************
# Play Loop
# ***************************
 
    def _play_first_track(self):
        self._new_livelist_create()
        self._livelist = copy.deepcopy(self._new_livelist)
        self._livelist_index = 0
        self._play_track()

        
    def _play_track(self):        
        self._livelist_replace_if_changed()
        if len(self._livelist)>0:
            self._play_selected_track(self._livelist[self._livelist_index])
        else:
            self.display_message(self.canvas,None,self.resource('liveshow','m01'),5,self._what_next)
     
    def _what_next(self):   
        # user wants to end 
        if self._end_liveshow_signal==True:
            self._end_liveshow_signal=False
            self._end('normal',"show ended by user")
        
        # play child?
        elif self._play_child_signal == True:
            self._play_child_signal=False
            index = self.medialist.index_of_track('pp-child-show')
            if index >=0:
                #don't select the track as need to preserve mediashow sequence.
                child_track=self.medialist.track(index)
                self._display_eggtimer(self.resource('liveshow','m02'))
                self._play_selected_track(child_track)
            else:
                self.mon.err(self,"Child show not found in medialist: "+ self.show['pp-child-show'])
                self._end('error',"child show not found in medialist")
                
        # otherwise loop to next track                       
        else:
            self._livelist_next()
            self._play_track()
          
      
# ***************************
# Dispatching to Players/Shows 
# ***************************

    # used to display internal messages in situations where a medialist entry could not be used.
    def display_message(self,canvas,source,content,duration,_display_message_callback):
            self._display_message_callback=_display_message_callback
            tp={'duration':duration,'message-colour':'white','message-font':'Helvetica 20 bold'}
            self.player=MessagePlayer(canvas,tp,tp)
            self.player.play(content,self._display_message_end,None)

            
    def  _display_message_end(self,reason,message):
        self.player=None
        if reason in ("killed",'error'):
            self._end(reason,message)
        else:
            self._display_message_callback()


    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     
         

    def _play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected_track is a dictionary for the track/show
        """
        # self.canvas.delete(ALL)
        
        # is menu required
        if self.show['has-child']=="yes":
            enable_child=True
        else:
            enable_child=False

        #dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
                                      
        if track_type=="image":
            track_file=self.complete_path(selected_track)
            # images played from menus don't have children
            self.player=ImagePlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=enable_child)
        elif track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=enable_child)
            
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self._stop("Unknown show")
                
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.end_shower,top=False,command='nil')

            
            elif selected_show['type']=="menu":
                self.shower= MenuShow(selected_show,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.end_shower,top=False,command='nil')
                
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self._stop("Unknown show type")  
                                                                            
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self._stop("Unknown track type")            


    def ready_callback(self):
        self._delete_eggtimer()
        
        
    def end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self._end(reason,message)
        else:
            self._what_next()

    def end_shower(self,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if reason in("killed","error"):
            self._end(reason,message)
        else:
            self._what_next()  
        
        
    def _display_eggtimer(self,text):
        self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold")
        self.canvas.update_idletasks( )


    def _delete_eggtimer(self):
            self.canvas.delete(ALL)
Пример #2
0
class MediaShow:

    # *******************
    # External interface
    # ********************

    def __init__(self, show, canvas, showlist, pp_home, pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the dictionary fo the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show = show
        self.showlist = showlist
        self.canvas = canvas
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        # Init variables
        self.player = None
        self.shower = None
        self._poll_for_interval_timer = None
        self._poll_for_continue_timer = None
        self._waiting_for_interval = False
        self._interval_timer = None
        self.error = False

        self._interval_timer_signal = False
        self._end_mediashow_signal = False
        self._next_track_signal = False
        self._previous_track_signal = False
        self._play_child_signal = False
        self._req_next = 'nil'

        self._state = 'closed'

    def play(self,
             end_callback,
             ready_callback=None,
             top=False,
             command='nil'):
        """ displays the mediashow
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """

        #instantiate the arguments
        self._end_callback = end_callback
        self._ready_callback = ready_callback
        self.top = top
        self.command = command
        self.mon.log(self, "Starting show: " + self.show['show-ref'])

        # check  data files are available.
        self.media_file = self.pp_profile + "/" + self.show['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self, "Medialist file not found: " + self.media_file)
            self._end('error', "Medialist file not found")

        #create a medialist for the mediashow and read it.
        self.medialist = MediaList()
        if self.medialist.open_list(self.media_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self._end('error', "Version of medialist different to Pi Presents")

        self._wait_for_trigger()

# respond to key presses.

    def key_pressed(self, key_name):
        self.mon.log(self, "received key: " + key_name)

        if key_name == '':
            pass

        elif key_name == 'escape':
            # if next lower show is running pass down to stop the show and lower level
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            # if not at top stop the show
            else:
                if self.top == False:
                    self._end_mediashow_signal = True
                    # and if a track is running stop that first
                    if self.player <> None:
                        self.player.key_pressed(key_name)
                else:
                    # at top level in a manual presentation stop the track
                    if self.show['progress'] == 'manual':
                        if self.player <> None:
                            self.player.key_pressed(key_name)

        elif key_name in ('up', 'down'):
            # if child or sub-show is running and is a show pass to show, track does not use up/down
            # otherwise use keys for next or previous
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            else:
                if key_name == 'up':
                    self._previous()
                else:
                    self._next()

        elif key_name == 'return':
            # if child show or sub-show is running and is show - pass down- player does not use return
            # ELSE use Return to start child or to start the show if waiting
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            else:
                if self._state == 'playing':
                    if self.show['has-child'] == 'yes':
                        self._play_child_signal = True
                        # and stop the current track if its running
                        if self.player <> None:
                            self.player.key_pressed("escape")
                else:
                    self._start_show()

        elif key_name == 'pir':
            self._start_show()

        elif key_name in ('p', ' '):
            # pass down if show or track running.
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            elif self.player <> None:
                self.player.key_pressed(key_name)

    def button_pressed(self, button, edge):
        if button == 'play': self.key_pressed("return")
        elif button == 'up': self.key_pressed("up")
        elif button == 'down': self.key_pressed("down")
        elif button == 'stop': self.key_pressed("escape")
        elif button == 'pause': self.key_pressed('p')
        elif button == 'PIR': self.key_pressed('pir')

    # kill or error
    def terminate(self, reason):
        if self.shower <> None:
            self.mon.log(self, "sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player <> None:
            self.mon.log(self, "sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,
                      'terminated without terminating shower or player')

    def _tidy_up(self):
        if self._poll_for_continue_timer <> None:
            self.canvas.after_cancel(self._poll_for_continue_timer)
            self._poll_for_continue_timer = None
        if self._poll_for_interval_timer <> None:
            self.canvas.after_cancel(self._poll_for_interval_timer)
            self._poll_for_interval_timer = None
        if self._interval_timer <> None:
            self.canvas.after_cancel(self._interval_timer)
            self._interval_timer = None

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            self.terminate("error")
        else:
            return value

# ***************************
# Respond to key/button presses
# ***************************

    def _stop(self, message):
        self._end_mediashow_signal = True
        if self._interval_timer <> None:
            self.canvas.after_cancel(self._interval_timer)

    def _next(self):
        # stop track if running and set signal
        self._next_track_signal = True
        if self.shower <> None:
            self.shower.key_pressed("escape")
        else:
            if self.player <> None:
                self.player.key_pressed("escape")

    def _previous(self):
        self._previous_track_signal = True
        if self.shower <> None:
            self.shower.key_pressed("escape")
        else:
            if self.player <> None:
                self.player.key_pressed("escape")

# ***************************
# end of show functions
# ***************************

    def _end(self, reason, message):
        self._end_mediashow_signal = False
        self.mon.log(self, "Ending Mediashow: " + self.show['show-ref'])
        self._tidy_up()
        self._end_callback(reason, message)
        self = None
        return

# ***************************
# Show sequencer
# ***************************

    def _wait_for_trigger(self):
        self._state = 'waiting'
        if self.ready_callback <> None:
            self.ready_callback()

        self.mon.log(self, "Waiting for trigger: " + self.show['trigger'])

        if self.show['trigger'] == "button":
            # blank screen waiting for trigger if auto, otherwise display something
            if self.show['progress'] == "manual":
                text = self.resource('mediashow', 'm01')
            else:
                text = ""
            self.display_message(self.canvas, 'text', text, 0,
                                 self._start_show)

        elif self.show['trigger'] == "PIR":
            # blank screen waiting for trigger
            text = self.resource('mediashow', 'm02')
            self.display_message(self.canvas, 'text', text, 0,
                                 self._start_show)

        elif self.show['trigger'] == "start":
            self._start_show()

        else:
            self.mon.err(self, "Unknown trigger: " + self.show['trigger'])
            self._end('error', "Unknown trigger type")

    def _start_show(self):
        self._state = 'playing'
        self._direction = 'forward'
        # start interval timer
        if self.show[
                'repeat'] == "interval" and self.show['repeat-interval'] <> 0:
            self._interval_timer_signal = False
            self._interval_timer = self.canvas.after(
                int(self.show['repeat-interval']) * 1000,
                self._end_interval_timer)
        # and play the first track unless commanded otherwise
        if self.command == 'backward':
            self.medialist.finish()
        else:
            self.medialist.start()
        self._play_selected_track(self.medialist.selected_track())

    def _what_next(self):
        self._direction = 'forward'

        # user wants to end, wait for any shows or tracks to have ended then end show
        if self._end_mediashow_signal == True:
            if self.player == None and self.shower == None:
                self._end_mediashow_signal = False
                self._end('normal', "show ended by user")
            else:
                pass

        #returning from a subshow needing to move onward
        elif self._req_next == 'do-next':
            self._req_next = 'nil'
            self.medialist.next()
            self._play_selected_track(self.medialist.selected_track())

        #returning from a subshow needing to move backward
        elif self._req_next == 'do-previous':
            self._req_next = 'nil'
            self._direction = 'backward'
            self.medialist.previous()
            self._play_selected_track(self.medialist.selected_track())

        # user wants to play child
        elif self._play_child_signal == True:
            self._play_child_signal = False
            index = self.medialist.index_of_track('pp-child-show')
            if index >= 0:
                #don't select the track as need to preserve mediashow sequence.
                child_track = self.medialist.track(index)
                self._display_eggtimer(self.resource('mediashow', 'm07'))
                self._play_selected_track(child_track)
            else:
                self.mon.err(
                    self, "Child show not found in medialist: " +
                    self.show['pp-child-show'])
                self._end('error', "child show not found in medialist")

        # skip to next track on user input
        elif self._next_track_signal == True:
            self._next_track_signal = False
            if self.medialist.at_end() == True:
                if self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'oneshot' and self.top == False:
                    self._end('do-next', "Return from Sub Show")
                else:
                    self.medialist.next()
                    self._play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.next()
                self._play_selected_track(self.medialist.selected_track())

        # skip to previous track on user input
        elif self._previous_track_signal == True:
            self._previous_track_signal = False
            self._direction = 'backward'
            if self.medialist.at_start() == True:
                if self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'oneshot' and self.top == False:
                    self._end('do-previous', "Return from Sub Show")
                else:
                    self.medialist.previous()
                    self._play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.previous()
                self._play_selected_track(self.medialist.selected_track())

        # track is finished and we are on auto
        elif self.show['progress'] == "auto":
            if self.medialist.at_end() == True:
                if self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'oneshot' and self.top == False:
                    self._end('do-next', "Return from Sub Show")

                #### elif
                elif self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'oneshot' and self.top == True:
                    self._wait_for_trigger()

                elif self._waiting_for_interval == True:
                    if self._interval_timer_signal == True:
                        self._interval_timer_signal = False
                        self._waiting_for_interval = False
                        self._start_show()
                    else:
                        self._poll_for_interval_timer = self.canvas.after(
                            1000, self._what_next)

                elif self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'interval' and int(
                            self.show['repeat-interval']) > 0:
                    self._waiting_for_interval = True
                    self._poll_for_interval_timer = self.canvas.after(
                        1000, self._what_next)

                elif self.show['sequence'] == "ordered" and self.show[
                        'repeat'] == 'interval' and int(
                            self.show['repeat-interval']) == 0:
                    self.medialist.next()
                    self._play_selected_track(self.medialist.selected_track())

                else:
                    self.mon.err(self, "Unhandled playing event: ")
                    self._end('error', "Unhandled playing event")

            else:
                self.medialist.next()
                self._play_selected_track(self.medialist.selected_track())

        # track has finished and we are on manual progress
        elif self.show['progress'] == "manual":
            self._delete_eggtimer()
            self._display_eggtimer(self.resource('mediashow', 'm03'))
            self._poll_for_continue_timer = self.canvas.after(
                500, self._what_next)

        else:
            #unhandled state
            self.mon.err(self, "Unhandled playing event: ")
            self._end('error', "Unhandled playing event")

    def _end_interval_timer(self):
        self._interval_timer_signal = True


# ***************************
# Dispatching to Players/Shows
# ***************************

# used to display internal messages in situations where a medialist entry could be used.

    def display_message(self, canvas, source, content, duration,
                        _display_message_callback):
        self._display_message_callback = _display_message_callback
        tp = {
            'duration': duration,
            'message-colour': 'white',
            'message-font': 'Helvetica 20 bold'
        }
        self.player = MessagePlayer(canvas, tp, tp)
        self.player.play(content, self._display_message_end, None)

    def _display_message_end(self, reason, message):
        self.player = None
        if reason in ('error', 'killed'):
            self._end(reason, message)
        else:
            self._display_message_callback()

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def _play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
        self.canvas.delete(ALL)
        if self.show['progress'] == "manual":
            self._display_eggtimer(self.resource('mediashow', 'm04'))

        # is menu required
        if self.show['has-child'] == "yes":
            enable_child = True
        else:
            enable_child = False

        #dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.canvas, self.show, selected_track)
            self.player.play(track_file,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "image":
            track_file = self.complete_path(selected_track)
            # images played from menus don't have children
            self.player = ImagePlayer(self.canvas, self.show, selected_track)
            self.player.play(track_file,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track['text']
            self.player = MessagePlayer(self.canvas, self.show, selected_track)
            self.player.play(text,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self._end('error', "Unknown show")

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.canvas,
                                        self.showlist, self.pp_home,
                                        self.pp_profile)
                self.shower.play(self.end_shower,
                                 top=False,
                                 command=self._direction)

            elif selected_show['type'] == "liveshow":
                self.shower = LiveShow(selected_show, self.canvas,
                                       self.showlist, self.pp_home,
                                       self.pp_profile)
                self.shower.play(self.end_shower, top=False, command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.canvas,
                                       self.showlist, self.pp_home,
                                       self.pp_profile)
                self.shower.play(self.end_shower, top=False, command='nil')

            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self._end('error' "Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self._end('error', "Unknown track type")

    def ready_callback(self):
        self._delete_eggtimer()

    def end_player(self, reason, message):
        self._req_next = 'nil'
        self.mon.log(self, "Returned from player with message: " + message)
        self.player = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        elif self.show['progress'] == "manual":
            self._display_eggtimer(self.resource('mediashow', 'm05'))
            self._req_next = reason
            self._what_next()
        else:
            self._req_next = reason
            self._what_next()

    def end_shower(self, reason, message):
        self._req_next = 'nil'
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        elif self.show['progress'] == "manual":
            self._display_eggtimer(self.resource('mediashow', 'm06'))
            self._req_next = reason
            self._what_next()
        else:
            self._req_next = reason
            self._what_next()

    def _display_eggtimer(self, text):
        self.canvas.create_text(int(self.canvas['width']) / 2,
                                int(self.canvas['height']) / 2,
                                text=text,
                                fill='white',
                                font="Helvetica 20 bold")
        self.canvas.update_idletasks()

    def _delete_eggtimer(self):
        self.canvas.delete(ALL)
Пример #3
0
class MenuShow:
    """ Displays a menu with optional hint below it. User can traverse the menu and
              select a track using key or button presses.
        Interface:
         * play - displays the menu and selects the first entry
         * input_pressed,  - receives user events passes them to a Player if a track is playing,
                otherwise actions them with _next, _previous, _play_selected_track, _end
         Optional display of eggtimer by means of Players ready_callback
         Supports imageplayer, videoplayer,messagplayer,audioplayer,menushow,mediashow
         Destroys itself on exit
    """

# *********************
# external interface
# ********************

    def __init__(self,
                            show_params,
                             root,
                            canvas,
                            showlist,
                             pp_dir,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the name of the configuration dictionary section for the menu
            showlist  - the showlist
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory"""
        
        self.mon=Monitor()
        self.mon.on()
        
        self.display_guidelines_command=show_params['menu-guidelines']
        self.display_guidelines=self.display_guidelines_command

        
        #instantiate arguments
        self.show_params=show_params
        self.root=root
        self.canvas=canvas
        self.showlist=showlist
        self.pp_dir=pp_dir
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()
        
        # init variables
        self.drawn  = None
        self.player=None
        self.shower=None
        self.menu_timeout_running=None
        self.error=False




    def play(self,show_id,end_callback,ready_callback,top=False,command='nil'):
        """ displays the menu 
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """
        
        #instantiate arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.command=command

        # check  data files are available.
        self.menu_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.menu_file):
            self.mon.err(self,"Medialist file not found: "+ self.menu_file)
            self.end('error',"Medialist file not found")
        
        #create a medialist for the menu and read it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.menu_file,self.showlist.sissue()) == False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self.end('error',"Version of medialist different to Pi Presents")

        #get control bindings for this show if top level
        controlsmanager=ControlsManager()
        if self.top==True:
            self.controls_list=controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list=controlsmanager.merge_show_controls(self.controls_list,self.show_params['controls'])

           
        if self.show_params['has-background']=="yes":
            background_index=self.medialist.index_of_track ('pp-menu-background')
            if background_index>=0:
                self.menu_img_file = self.complete_path(self.medialist.track(background_index)['location'])
                if not os.path.exists(self.menu_img_file):
                    self.mon.err(self,"Menu background file not found: "+ self.menu_img_file)
                    self.end('error',"Menu background file not found")
            else:
                self.mon.err(self,"Menu background not found in medialist")
                self.end('error',"Menu background not found")
                               
        self.end_menushow_signal= False
        if self.ready_callback<>None:
            self.ready_callback()

        self.menu_timeout_value=int(self.show_params['timeout'])*1000
        self.do_menu()


    def do_menu(self):
        #start timeout alarm if required
        if int(self.show_params['timeout'])<>0:
            self.menu_timeout_running=self.canvas.after(self.menu_timeout_value,self.timeout_menu)

        if self.show_params['menu-background-colour']<>'':
            self.canvas.config(bg=self.show_params['menu-background-colour'])
            
        self.canvas.delete('pp-content')
        self.canvas.update()
        
        # display background image
        if self.show_params['has-background']=="yes":
            self.display_background()

        self.delete_eggtimer()
        self.display_new_menu()
        self.canvas.tag_raise('pp-click-area')            
        self.canvas.update_idletasks( )

        # display menu text if enabled
        if self.show_params['menu-text']<> '':
            self.canvas.create_text(int(self.show_params['menu-text-x']),int(self.show_params['menu-text-y']),
                                                    anchor=NW,
                                                  text=self.show_params['menu-text'],
                                                  fill=self.show_params['menu-text-colour'],
                                                  font=self.show_params['menu-text-font'],
                                                  tag='pp-content')

        self.canvas.update_idletasks( )
        
        # display instructions (hint)
        hint_text=self.show_params['hint-text']
        if hint_text<>'':
            self.canvas.create_text(int(self.show_params['hint-x']),int(self.show_params['hint-y']),
                                                    anchor=NW,
                                text=hint_text,
                                fill=self.show_params['hint-colour'],
                                font=self.show_params['hint-font'],
                                tag='pp-content')

        self.canvas.update_idletasks( )


    #stop received from another concurrent show
    def managed_stop(self):
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        if self.shower<>None:
            self.shower.managed_stop()
        elif self.player<>None:
            self.end_menushow_signal=True
            self.player.input_pressed('stop')
        else:
            self.end('normal','stopped by ShowManager')
            

    # kill or error received
    def terminate(self,reason):
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        if self.shower<>None:
            self.shower.terminate(reason)
        elif self.player<>None:
            self.player.terminate(reason)
        else:
            self.end(reason,'Terminated no shower or player running')



   # respond to user inputs.
    def input_pressed(self,symbol,edge,source):
        self.mon.log(self,"Show Id: "+str(self.show_id)+" received key or operation: " + symbol)
        if self.show_params['disable-controls']=='yes':
            return 

        # if at top convert symbolic name to operation otherwise lower down we have received an operation
        # look through list of standard symbols to find match (symbolic-name, function name) operation =lookup (symbol
        if self.top==True:
            operation=self.lookup_control(symbol,self.controls_list)
        else:
            operation=symbol
            
        # print 'operation',operation
        # if no match for symbol against standard operations then return
        if operation=='':
            return
        else:
            if self.shower<>None:
                # if next lower show is running pass down operatin to  the show and lower levels
                self.shower.input_pressed(operation,source,edge) 
            else:
                #service the standard inputs for this show
                if operation=='stop':
                    if self.menu_timeout_running<>None:
                        self.canvas.after_cancel(self.menu_timeout_running)
                        self.menu_timeout_running=None
                    if self.shower<>None:
                        self.shower.input_pressed('stop',edge,source)
                    elif self.player<>None:
                        self.player.input_pressed('stop')
                    else:
                        # not at top so end the show
                        if  self.top == False:
                            self.end('normal',"exit from stop command")
                        else:
                            pass
              
                elif operation in ('up','down'):
                # if child or sub-show running and is a show pass down
                # if  child not running - move
                    if self.shower<>None:
                        self.shower.input_pressed(operation,edge,source)
                    else:
                        if self.player==None:
                            if self.menu_timeout_running<>None:
                                self.canvas.after_cancel(self.menu_timeout_running)
                                self.menu_timeout_running=self.canvas.after(self.menu_timeout_value,self.timeout_menu)
                            if operation=='up':
                                self.previous()
                            else:
                                self.next()
                        
                elif operation =='play':
                    # if child running and is show - pass down
                    # if no track already running  - play
                    if self.shower<>None:
                        self.shower.input_pressed(operation,edge,source)
                    else:
                        if self.player==None:
                            self.play_selected_track(self.medialist.selected_track())

                elif operation == 'pause':
                    # pass down if show or track running.
                    if self.shower<>None:
                        self.shower.input_pressed(operation,edge,source)
                    elif self.player<>None:
                        self.player.input_pressed(operation)
                        
                elif operation[0:4]=='omx-' or operation[0:6]=='mplay-'or operation[0:5]=='uzbl-':
                    if self.player<>None:
                        self.player.input_pressed(operation)


 
    def lookup_control(self,symbol,controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''


# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Sequencing
# *********************

    def timeout_menu(self):
        self.end('normal','menu timeout')
        return
        
    def next(self):     
        self.highlight_menu_entry(self.menu_index,False)
        self.medialist.next('ordered')
        if self.menu_index==self.menu_length-1:
            self.menu_index=0
        else:
            self.menu_index+=1
        self.highlight_menu_entry(self.menu_index,True)     


    def previous(self):   
        self.highlight_menu_entry(self.menu_index,False)
        if self.menu_index==0:
            self.menu_index=self.menu_length-1
        else:
            self.menu_index-=1
        self.medialist.previous('ordered')
        self.highlight_menu_entry(self.menu_index,True)
        

     # at the end of a track just re-display the menu with the original callback from the menu       
    def what_next(self,message):
        # user wants to end
        if self.end_menushow_signal==True:
            self.end_menushow_signal=False
            self.end('normal',"show ended by user")
        else:
            self.do_menu()


# *********************
# Dispatching to Players
# *********************

    def play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
         #remove menu and show working.....        

        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
            
        self.canvas.delete('pp-content')
        self.display_eggtimer(self.resource('menushow','m01'))
    
        # dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track['location'])
            self.player=VideoPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.delete_eggtimer,
                                        enable_menu=False)
                                        
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track['location'])
            self.player=AudioPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.delete_eggtimer,
                                        enable_menu=False)
                                        
        elif track_type=="image":
            # images played from menus don't have children
            track_file=self.complete_path(selected_track['location'])
            self.player=ImagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                    self.showlist,
                                    self.end_player,
                                    self.delete_eggtimer,
                                    enable_menu=False,
                                    )
            
        elif track_type=="web":
            # create a browser
            track_file=self.complete_path(selected_track['location'])
            self.player=BrowserPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.delete_eggtimer,
                                        enable_menu=False)

                                    
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(text,
                                    self.showlist,
                                    self.end_player,
                                    self.delete_eggtimer,
                                    enable_menu=False
                                    )
 
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self.end("Unknown show")
          
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                                self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.delete_eggtimer,top=False,command='nil')

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                 self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.delete_eggtimer,top=False,command='nil')

            elif selected_show['type']=="radiobuttonshow":
                self.shower= RadioButtonShow(selected_show,
                                                       self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                         self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="hyperlinkshow":
                self.shower= HyperlinkShow(selected_show,
                                                        self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')


            elif selected_show['type']=="menu": 
                self.shower= MenuShow(selected_show,
                                                        self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.delete_eggtimer,top=False,command='nil')                    
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self.end("Unknown show type")  
                
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self.end("Unknown track type")
    
    # callback from when player ends
    def end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            self.display_eggtimer(self.resource('menushow','m02'))
            self.what_next(message)

    # callback from when shower ends
    def end_shower(self,show_id,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if reason in ("killed","error"):
            self.end(reason,message)
        else:
            self.display_eggtimer(self.resource('menushow','m03'))
            self.what_next(message)  
   


# *********************
# Ending the show
# *********************
    # finish the player for killing, error or normally
    # this may be called directly if sub/child shows or players are not running
    # if they might be running then need to call terminate?????
    
    def end(self,reason,message):
        self.mon.log(self,"Ending menushow: "+ self.show_params['show-ref'])  
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        self.end_callback(self.show_id,reason,message)
        self=None
        return


# *********************
# Displaying things
# *********************

    def display_background(self):
        pil_menu_img=PIL.Image.open(self.menu_img_file)
        self.menu_background = PIL.ImageTk.PhotoImage(pil_menu_img)
        self.drawn = self.canvas.create_image(int(self.canvas['width'])/2,
                                      int(self.canvas['height'])/2,
                                      image=self.menu_background,
                                      anchor=CENTER,
                                      tag='pp-content')


   
    
    def display_new_menu(self):

        # calculate menu geometry
        error,reason=self.calculate_geometry()
        if error<>'normal':
            self.mon.err(self,"Menu geometry error: "+ reason)
            self.end('error',"Menu geometry error")
        else:
            # display the menu entries
            self.display_menu_entries()


    def display_menu_entries(self):
        # init the loop
        column_index=0
        row_index=0
        self.menu_length=1
        
        # id store is a list of elements each being a list of the three ids of the elements of the entry
        self.menu_entry_id=[]
        # offsets for the above
        self.icon_id_index=0 # rectangle around the icon
        self.image_id_index=1 # icon image - needed for tkinter
        self.text_id_index=2 # the text - need whn no icon is displayed

        #select the startof the medialist
        self.medialist.start()

        #loop through menu entries
        while True:
            #display the entry
            #calculate top left corner of entry
            self.calculate_entry_position(column_index,row_index)


            # display the button strip
            self.display_entry_strip()

            #display the selected entry highlight
            icon_id=self.display_icon_rectangle()

            #display the image in the icon
            image_id=self.display_icon_image()

            if self.show_params['menu-text-mode']<>'none':
                text_id=self.display_icon_text()
            else:
                text_id=None

            #append id's to the list
            self.menu_entry_id.append([icon_id,image_id,text_id])

            self.canvas.update_idletasks( )            
            #and loop
            if self.medialist.at_end():
                break
            self.menu_length+=1
            self.medialist.next('ordered')

            if self.direction=='horizontal':
                column_index+=1
                if column_index>=self.menu_columns:
                    column_index=0
                    row_index+=1
            else:
                row_index+=1
                if row_index>=self.menu_rows:
                    row_index=0
                    column_index+=1
                    
        # finally select and highlight the first entry
        self.medialist.start()
        self.menu_index=0
        self.highlight_menu_entry(self.menu_index,True)


    def print_geometry(self,total_width,total_height):
        print 'menu width: ', self.menu_width
        print 'columns', self.menu_columns
        print 'icon width: ', self.icon_width
        print 'horizontal padding: ', self.menu_horizontal_padding
        print 'text width: ', self.text_width
        print 'entry width: ', self.entry_width
        print 'total width: ', total_width
        print 'x separation: ', self.x_separation
        print ''
        print 'menu height', self.menu_height
        print 'rows: ', self.menu_rows
        print 'icon height', self.icon_height
        print 'vertical padding: ', self.menu_vertical_padding        
        print 'text height', self.text_height
        print 'entry height', self.entry_height
        print 'total height', total_height
        print 'y separation', self.y_separation

        
    # ------------------------------------------------------------------
    #calculate menu entry size and separation between menu entries
    # ------------------------------------------------------------------
    def calculate_geometry(self):

        self.display_strip=self.show_params['menu-strip']
        self.screen_width=int(self.canvas['width'])
        self.screen_height=int(self.canvas['height'])
        
        if self.display_strip=='yes':
            self.strip_padding=int(self.show_params['menu-strip-padding'])
        else:
            self.strip_padding=0

        # parse the menu window
        error,reason,self.menu_x_left,self.menu_y_top,self.menu_x_right,self.menu_y_bottom=self.parse_menu_window(self.show_params['menu-window'])
        if error<>'normal':
            return 'error',"Menu Window error: "+ reason

        if self.show_params['menu-icon-mode']=='none' and self.show_params['menu-text-mode']=='none':
            return 'error','Icon and Text are both None'
        if self.show_params['menu-icon-mode']=='none' and self.show_params['menu-text-mode']=='overlay':
            return 'error','cannot overlay none icon'

        self.direction=self.show_params['menu-direction']
        
        self.menu_width=self.menu_x_right - self.menu_x_left
        self.menu_height=self.menu_y_bottom - self.menu_y_top

        self.list_length=self.medialist.display_length()

        # get or calculate rows and columns
        if self.direction=='horizontal':
            if self.show_params['menu-columns']=='':
                return 'error','blank columns for horizontal direction'
            self.menu_columns=int(self.show_params['menu-columns'])
            self.menu_rows=self.list_length//self.menu_columns
            if self.list_length % self.menu_columns<>0:
                self.menu_rows+=1
        else:
            if self.show_params['menu-rows']=='':
                return 'error','blank rows for vertical direction'
            self.menu_rows=int(self.show_params['menu-rows'])
            self.menu_columns=self.list_length//self.menu_rows
            if self.list_length % self.menu_rows<>0:
                self.menu_columns+=1
                
        self.x_separation=int(self.show_params['menu-horizontal-separation'])
        self.y_separation=int(self.show_params['menu-vertical-separation'])

        # get size of padding depending on exitence of icon and text
        if self.show_params['menu-icon-mode'] in ('thumbnail','bullet') and self.show_params['menu-text-mode'] == 'right':
            self.menu_horizontal_padding=int(self.show_params['menu-horizontal-padding'])
        else:
            self.menu_horizontal_padding=0

        if self.show_params['menu-icon-mode'] in ('thumbnail','bullet') and self.show_params['menu-text-mode'] == 'below':
            self.menu_vertical_padding=int(self.show_params['menu-vertical-padding'])
        else:
            self.menu_vertical_padding=0
            
        #calculate size of icon depending on use
        if self.show_params['menu-icon-mode'] in ('thumbnail','bullet'):
            self.icon_width=int(self.show_params['menu-icon-width'])
            self.icon_height=int(self.show_params['menu-icon-height'])
        else:
            self.icon_width=0
            self.icon_height=0

        #calculate size of text box depending on mode
        if self.show_params['menu-text-mode']<>'none':
            self.text_width=int(self.show_params['menu-text-width'])
            self.text_height=int(self.show_params['menu-text-height'])
        else:
            self.text_width=0
            self.text_height=0
            
        # calculate size of entry box by combining text and icon sizes
        if self.show_params['menu-text-mode'] == 'right':
            self.entry_width=self.icon_width+self.menu_horizontal_padding+self.text_width
            self.entry_height=max(self.text_height,self.icon_height)
        elif self.show_params['menu-text-mode']=='below':
            self.entry_width=max(self.text_width,self.icon_width)
            self.entry_height=self.icon_height + self.menu_vertical_padding + self.text_height 
        else:
            # no text or overlaid text
            if self.show_params['menu-icon-mode'] in ('thumbnail','bullet'):
                # icon only
                self.entry_width=self.icon_width
                self.entry_height=self.icon_height
            else:
                #text only
                self.entry_width=self.text_width
                self.entry_height=self.text_height

        if self.entry_width<=self.menu_horizontal_padding:
            return 'error','entry width is zero'

        if self.entry_height<=self.menu_vertical_padding:
            return 'error','entry height is zero'

        # calculate totals for debugging puropses
        total_width=self.menu_columns * self.entry_width +(self.menu_columns-1)*self.x_separation
        total_height=self.menu_rows * self.entry_height + (self.menu_rows-1)*self.y_separation
        
        # self.print_geometry(total_width,total_height)   


        # display guidelines and debgging text if there is a problem     
        if total_width>self.menu_width and self.display_guidelines<>'never':
                self.display_guidelines='always'
                self.mon.log(self,'\nMENU IS WIDER THAN THE WINDOW')
                self.print_geometry(total_width,total_height)


        if total_height>self.menu_height and self.display_guidelines<>'never':
                self.display_guidelines='always'
                self.mon.log(self,'\nMENU IS TALLER THAN THE WINDOW')
                self.print_geometry(total_width,total_height)            

        # display calculated total rectangle guidelines for debugging
        if self.display_guidelines=='always':
            points=[self.menu_x_left,self.menu_y_top, self.menu_x_left+total_width,self.menu_y_top+total_height]

            # and display the icon rectangle
            self.canvas.create_rectangle(points,
                                           outline='red',
                                           fill='',
                                           tag='pp-content')

        
        # display menu rectangle guidelines for debugging
        if self.display_guidelines=='always':
            points=[self.menu_x_left,self.menu_y_top, self.menu_x_right,self.menu_y_bottom]
            self.canvas.create_rectangle(points,
                                           outline='blue',
                                           fill='',
                                           tag='pp-content')
                
        return 'normal',''

    def calculate_entry_position(self,column_index,row_index):
            self.entry_x=self.menu_x_left+ column_index*(self.x_separation+self.entry_width)
            self.entry_y=self.menu_y_top+ row_index*(self.y_separation+self.entry_height)

            
    def display_entry_strip(self):
        if self.display_strip=='yes':
            if self.direction=='vertical':
                    #display the strip
                    strip_points=[self.entry_x - self.strip_padding -1 ,
                                  self.entry_y - self.strip_padding - 1,
                                  self.entry_x+ self.entry_width + self.strip_padding - 1,
                                  self.entry_y+self.entry_height+ self.strip_padding - 1]
                    self.canvas.create_rectangle(strip_points,
                                                       outline='',
                                                        fill='gray',
                                                       stipple='gray12',                                 
                                                       tag='pp-content')

                    top_l_points=[self.entry_x - self.strip_padding,
                                  self.entry_y - self.strip_padding,
                                  self.entry_x + self.entry_width + self.strip_padding ,
                                  self.entry_y - self.strip_padding]
                    
                    self.canvas.create_line(top_l_points,
                                            fill='light gray',
                                            tag='pp-content')
                    
                    bottom_l_points=[self.entry_x - self.strip_padding,
                                     self.entry_y + self.entry_height + self.strip_padding,
                                     self.entry_x+ self.entry_width + self.strip_padding ,
                                     self.entry_y+ self.entry_height + self.strip_padding]
                    
                    self.canvas.create_line(bottom_l_points,
                                            fill='dark gray',
                                            tag='pp-content')

                    left_l_points=[self.entry_x - self.strip_padding,
                                   self.entry_y - self.strip_padding,
                                   self.entry_x - self.strip_padding,
                                   self.entry_y + self.entry_height + self.strip_padding]
                    
                    self.canvas.create_line(left_l_points,
                                            fill='gray',
                                            tag='pp-content')

            else:
                    #display the strip vertically
                    strip_points=[self.entry_x - self.strip_padding +1 ,
                                  self.entry_y - self.strip_padding +1,
                                  self.entry_x+self.entry_width + self.strip_padding -1,
                                  self.entry_y + self.entry_height+ self.strip_padding -1]
                    
                    self.canvas.create_rectangle(strip_points,
                                                       outline='',
                                                        fill='gray',
                                                       stipple='gray12',                                 
                                                       tag='pp-content')

                    top_l_points=[self.entry_x - self.strip_padding,
                                  self.entry_y - self.strip_padding,
                                  self.entry_x + self.entry_width + self.strip_padding,
                                  self.entry_y - self.strip_padding]
                    
                    self.canvas.create_line(top_l_points,
                                            fill='light gray',
                                            tag='pp-content')
                    
                    left_l_points=[self.entry_x - self.strip_padding,
                                   self.entry_y - self.strip_padding,
                                   self.entry_x - self.strip_padding,
                                   self.entry_y + self.entry_height+ self.strip_padding]
                    
                    self.canvas.create_line(left_l_points,
                                            fill='gray',
                                            tag='pp-content')

                    right_l_points=[self.entry_x +self.entry_width + self.strip_padding,
                                     self.entry_y - self.strip_padding,
                                     self.entry_x +self.entry_width + self.strip_padding,
                                     self.entry_y + self.entry_height+ self.strip_padding]
                    
                    self.canvas.create_line(right_l_points,
                                            fill='dark gray',
                                            tag='pp-content')


    # display the rectangle that goes arond the icon when the entry is selected
    def display_icon_rectangle(self):
            if self.show_params['menu-icon-mode'] in ('thumbnail','bullet'):

                #calculate icon parameters
                if self.icon_width<self.text_width and self.show_params['menu-text-mode']=='below':
                        self.icon_x_left=self.entry_x+abs(self.icon_width-self.text_width)/2
                else:
                        self.icon_x_left=self.entry_x
                self.icon_x_right=self.icon_x_left+self.icon_width

                if self.icon_height<self.text_height and self.show_params['menu-text-mode']=='right':
                        self.icon_y_top=self.entry_y+abs(self.icon_height-self.text_height)/2
                else:
                        self.icon_y_top=self.entry_y
                self.icon_y_bottom=self.icon_y_top+self.icon_height

                
                req_horiz_sep=self.menu_horizontal_padding
                req_vert_sep=self.menu_vertical_padding

                
                points=[self.icon_x_left,self.icon_y_top,self.icon_x_right,self.icon_y_top,self.icon_x_right,self.icon_y_bottom,self.icon_x_left,self.icon_y_bottom]

                # display guidelines make it white when not selctedfor debugging
                if self.display_guidelines=='always':
                    outline='white'
                else:
                    outline=''

                # and display the icon rectangle
                icon_id=self.canvas.create_polygon(points,
                                                   outline=outline,
                                                   fill='',
                                                   tag='pp-content')


            else:
                # not using icon so set starting point for text to zero icon size
                self.icon_x_right=self.entry_x
                self.icon_y_bottom=self.entry_y
                req_horiz_sep=0
                req_vert_sep=0
                icon_id=None
            return icon_id
        

    #display the image in a menu entry
    def  display_icon_image(self):
            image_id=None
            if self.show_params['menu-icon-mode'] == 'thumbnail':
                # try for the thumbnail
                if self.medialist.selected_track()['thumbnail']<>'' and os.path.exists(self.complete_path(self.medialist.selected_track()['thumbnail'])):
                    self.pil_image=PIL.Image.open(self.complete_path(self.medialist.selected_track()['thumbnail']))
                else:
                    #cannot find thumbnail get the image if its an image track
                    if self.medialist.selected_track()['type'] =='image':
                        self.track=self.complete_path(self.medialist.selected_track()['location'])
                    else:
                        self.track=''
                    if self.medialist.selected_track()['type']=='image' and os.path.exists(self.track)==True: 
                        self.pil_image=PIL.Image.open(self.track)
                    else:
                        #use a standard thumbnail
                        type=self.medialist.selected_track()['type']
                        standard=self.pp_dir+os.sep+'pp_home'+os.sep+'pp_resources'+os.sep+type+'.png'
                        if os.path.exists(standard)==True:
                            self.pil_image=PIL.Image.open(standard)
                            self.mon.log(self,'WARNING: default thumbnail used for '+self.medialist.selected_track()['title'])
                        else:
                            self.pil_image=None

                # display the image                
                if self.pil_image<>None:
                    self.pil_image=self.pil_image.resize((self.icon_width-2,self.icon_height-2))                 
                    image_id=PIL.ImageTk.PhotoImage(self.pil_image)
                    self.canvas.create_image(self.icon_x_left+1, self.icon_y_top+1,
                                                image=image_id, anchor=NW,
                                                 tag='pp-content')
                else:
                        image_id=None
                        
            elif self.show_params['menu-icon-mode'] =='bullet':
                    bullet=self.complete_path(self.show_params['menu-bullet'])                  
                    if os.path.exists(bullet)==False:
                        self.pil_image=None                          
                    else:
                        self.pil_image=PIL.Image.open(bullet)
                    if self.pil_image<>None:
                        self.pil_image=self.pil_image.resize((self.icon_width-2,self.icon_height-2))                 
                        image_id=PIL.ImageTk.PhotoImage(self.pil_image)
                        self.canvas.create_image(self.icon_x_left+1, self.icon_y_top+1,
                                                      image=image_id, anchor=NW,
                                                      tag='pp-content')                                      
            else:
                image_id=None
            return image_id

            
    #display the text of a menu entry
    def display_icon_text(self):
            text_mode=self.show_params['menu-text-mode']
            if self.show_params['menu-icon-mode'] in ('thumbnail','bullet'):
                if text_mode=='right':
                    if self.icon_height>self.text_height:
                        text_y_top=self.entry_y+abs(self.icon_height-self.text_height)/2
                    else:
                        text_y_top=self.entry_y
                    text_y_bottom=text_y_top+self.text_height
                    
                    text_x_left=self.icon_x_right+self.menu_horizontal_padding
                    text_x_right=text_x_left+self.text_width
                    
                    text_x=text_x_left
                    text_y=text_y_top+(self.text_height/2)

                elif text_mode=='below':
                    text_y_top=self.icon_y_bottom+self.menu_vertical_padding
                    text_y_bottom=text_y_top+self.text_height
                    
                    if self.icon_width>self.text_width:
                        text_x_left=self.entry_x+abs(self.icon_width-self.text_width)/2
                    else:
                        text_x_left=self.entry_x
                    text_x_right=text_x_left+self.text_width
                    
                    text_x=text_x_left+(self.text_width/2)
                    text_y=text_y_top

                else:
                    # icon with text_mode=overlay or none
                    text_x_left=self.icon_x_left
                    text_x_right= self.icon_x_right
                    text_y_top=self.icon_y_top
                    text_y_bottom=self.icon_y_bottom
                    text_x=(text_x_left+text_x_right)/2
                    text_y=(text_y_top+text_y_bottom)/2                    

            else:
                    #no icon text only
                    text_y_top=self.entry_y
                    text_y_bottom=text_y_top+self.text_height
                    text_x_left=self.entry_x
                    text_x_right=text_x_left+self.text_width
                    text_x=self.entry_x
                    text_y=self.entry_y+self.text_height/2


            #display the guidelines for debugging
            if self.display_guidelines=='always':
                points=[text_x_left,text_y_top,text_x_right,text_y_top,text_x_right,text_y_bottom,text_x_left,text_y_bottom]
                self.canvas.create_polygon(points,fill= '' ,
                                              outline='white',
                                              tag='pp-content')

            # display the text
            if text_mode=='below' and self.show_params['menu-icon-mode']  in ('thumbnail','bullet'):
                anchor=N
                justify=CENTER
            elif text_mode=='overlay' and self.show_params['menu-icon-mode']  in ('thumbnail','bullet'):
                anchor=CENTER
                justify=CENTER
            else:
                anchor=W
                justify=LEFT
            text_id=self.canvas.create_text(text_x,text_y,
                                       text=self.medialist.selected_track()['title'],
                                       anchor=anchor,
                                       fill=self.show_params['entry-colour'],
                                       font=self.show_params['entry-font'],
                                       width=self.text_width,
                                       justify=justify,
                                       tag='pp-content')
            return text_id
        

    def highlight_menu_entry(self,index,state):
        if self.show_params['menu-icon-mode']<>'none':
            if state==True:
                self.canvas.itemconfig(self.menu_entry_id[index][self.icon_id_index],
                                       outline=self.show_params['entry-select-colour'],
                                       width=4,
                                       )
            else:
                self.canvas.itemconfig(self.menu_entry_id[index][self.icon_id_index],
                                        outline='',
                                       width=1
                                       )
        else:
            if state==True:
                self.canvas.itemconfig(self.menu_entry_id[index][self.text_id_index],
                                       fill=self.show_params['entry-select-colour'])
            else:
                self.canvas.itemconfig(self.menu_entry_id[index][self.text_id_index],
                                    fill=self.show_params['entry-colour'])
                

    
    def display_eggtimer(self,text):
        # print "display eggtimer"
        self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold",
                                                   tag='pp-eggtimer')
        self.canvas.update_idletasks( )


    def delete_eggtimer(self):
        # print"delete eggtimer"
        self.canvas.delete('pp-eggtimer')
        self.canvas.update_idletasks( )


# *********************
# utilities
# *********************

    def complete_path(self,track_file):
        #  complete path of the filename of the selected entry
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        return track_file     

    def parse_menu_window(self,line):
            if line<>'':
                fields = line.split()
                if len(fields) not in  (1, 2,4):
                    return 'error','wrong number of fields',0,0,0,0
                if len(fields)==1:
                    if fields[0]=='fullscreen':
                        return 'normal','',0,0,self.screen_width - 1, self.screen_height - 1
                    else:
                        return 'error','single field is not fullscreen',0,0,0,0
                if len(fields)==2:                    
                    if fields[0].isdigit() and fields[1].isdigit():
                        return 'normal','',int(fields[0]),int(fields[1]),self.screen_width, self.screen_height
                    else:
                        return 'error','field is not a digit',0,0,0,0
                if len(fields)==4:                    
                    if fields[0].isdigit() and fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit():
                        return 'normal','',int(fields[0]),int(fields[1]),int(fields[2]),int(fields[3])
                else:
                     return 'error','field is not a digit',0,0,0,0
            else:
                     return 'error','line is blank',0,0,0,0


    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            # timers may be running so need terminate
            self.terminate("error")
        else:
            return value
Пример #4
0
class LiveShow:
    """ plays a set of tracks the content of which is dynamically specified by plaacing track files
                in one of two directories. Tracks are played in file leafname alphabetical order.
                Can be interrupted
    """
            
# *******************
# External interface
# ********************

    def __init__(self,
                            show_params,
                             root,
                            canvas,
                            showlist,
                             pp_dir,
                            pp_home,
                            pp_profile):

        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show_params =show_params
        self.showlist=showlist
        self.root=root
        self.canvas=canvas
        self.pp_dir=pp_dir
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

        #create and instance of TimeOfDay scheduler so we can add events
        self.tod=TimeOfDay()


        # Init variables
        self.player=None
        self.shower=None
        self.end_liveshow_signal=False
        self.end_trigger_signal= False
        self.play_child_signal = False
        self.error=False
        self.egg_timer=None
        self.duration_timer=None
        self.state='closed'
        self.livelist=None
        self.new_livelist= None



    def play(self,show_id,end_callback,ready_callback, top=False,command='nil'):
	global defaultDur
	if defaultDur == None:
		defaultDur = self.showlist.get_dur()

        #instantiate the arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.mon.log(self,"Starting show: " + self.show_params['show-ref'])



        # check  data files are available.
        self.media_file = self.pp_profile + os.sep + self.show_params['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self,"Medialist file not found: "+ self.media_file)
            self.end_liveshow_signal=True

            
        self.options=command_options()
               
        self.pp_live_dir1 = self.pp_home + os.sep + 'pp_live_tracks'
        if not os.path.exists(self.pp_live_dir1):
            os.mkdir(self.pp_live_dir1)
            os.mkdir(self.pp_live_dir1+os.sep+ 'Archive')

        self.pp_live_dir2=''   
        if self.options['liveshow'] <>"":
            self.pp_live_dir2 = self.options['liveshow']
            if not os.path.exists(self.pp_live_dir2):
                self.mon.err(self,"live tracks directory not found " + self.pp_live_dir2)
                self.end('error',"live tracks directory not found")

        #create a medialist for the liveshow and read it.
        # it should be empty of anonymous tracks but read it to check its version.
        self.medialist=MediaList()

        if self.medialist.open_list(self.media_file,self.showlist.sissue())==False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self.end('error',"Version of medialist different to Pi Presents")

        #get control bindings for this show if top level
        controlsmanager=ControlsManager()
        if self.top==True:
            self.controls_list=controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list=controlsmanager.merge_show_controls(self.controls_list,self.show_params['controls'])

        #set up the time of day triggers for the show
        if self.show_params['trigger-start']in('time','time-quiet'):
            error_text=self.tod.add_times(self.show_params['trigger-start-time'],id(self),self.tod_start_callback,self.show_params['trigger-start'])
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)
                
        if self.show_params['trigger-end']=='time':
            error_text=self.tod.add_times(self.show_params['trigger-end-time'],id(self),self.tod_end_callback,'n/a')
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)

        if self.show_params['trigger-end']=='duration':
            error_text=self.calculate_duration(self.show_params['trigger-end-time'])
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)       

        self.wait_for_trigger()                


    def managed_stop(self):
        # if next lower show eor player is running pass down to stop the show/track
        if self.shower<>None:
            self.shower.managed_stop()
        else:
            self.end_liveshow_signal=True
            if self.player<>None:
                self.player.input_pressed('stop')

                
    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.shower.terminate(reason)
        elif self.player<>None:
            self.player.terminate(reason)
        else:
            self.end(reason,'terminated without terminating shower or player')

 
   # respond to key presses.
    def input_pressed(self,symbol,edge,source):
        self.mon.log(self,"received key: " + symbol)
        if self.show_params['disable-controls']=='yes':
            return 

       # if at top convert symbolic name to operation otherwise lower down we have received an operation
        # look through list of standard symbols to find match (symbolic-name, function name) operation =lookup (symbol
        if self.top==True:
            operation=self.lookup_control(symbol,self.controls_list)
        else:
            operation=symbol
        # print 'operation',operation
        # if no match for symbol against standard operations then return
        if operation=='':
            return

        else:
            #service the standard inputs for this show
            if operation=='stop':
                # if next lower show eor player is running pass down to stop the show/track
                # ELSE stop this show except for exceptions
                if self.shower<>None:
                    self.shower.input_pressed('stop',edge,source)
                elif self.player<>None:
                    self.player.input_pressed('stop')
                else:
                    # not at top so stop the show
                    if  self.top == False:
                        self.end_liveshow_signal=True
                    else:
                        pass
        
            elif operation in ('up','down'):
            # if child or sub-show is running and is a show pass to show, track does not use up/down
                if self.shower<>None:
                    self.shower.input_pressed(operation,edge,source)

                    
            elif operation=='play':
                # if child show or sub-show is running and is show - pass down
                # ELSE use Return to start child
                if self.shower<>None:
                    self.shower.input_pressed(operation,edge,source)
                else:
                    if self.show_params['has-child']=="yes":
                        self.play_child_signal=True
                        if self.player<>None:
                            self.player.input_pressed("stop")
                  
            elif operation == 'pause':
                # pass down if show or track running.
                if self.shower<>None:
                    self.shower.input_pressed(operation,edge,source)
                elif self.player<>None:
                    self.player.input_pressed(operation)

            elif operation[0:4]=='omx-' or operation[0:6]=='mplay-':
                if self.player<>None:
                    self.player.input_pressed(operation)
     
    def lookup_control(self,symbol,controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

# ***************************
# Constructing Livelist
# ***************************       
        
    def livelist_add_track(self,afile):

        (root,title)=os.path.split(afile)
        (root_plus,ext)= os.path.splitext(afile)
        if ext.lower() in PPdefinitions.IMAGE_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['image'],{'title':title,'track-ref':'','location':afile})
        if ext.lower() in PPdefinitions.VIDEO_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['video'],{'title':title,'track-ref':'','location':afile})
        if ext.lower() in PPdefinitions.AUDIO_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['audio'],{'title':title,'track-ref':'','location':afile})
        if ext.lower() in PPdefinitions.WEB_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['web'],{'title':title,'track-ref':'','location':afile})
        if ext.lower()=='.cfg':
            self.livelist_new_plugin(afile,title)
           

    def livelist_new_plugin(self,plugin_cfg,title):

        # read the file which is a plugin cfg file into a dictionary
        self.plugin_config = ConfigParser.ConfigParser()
        self.plugin_config.read(plugin_cfg)
        self.plugin_params =  dict(self.plugin_config.items('plugin'))
        # create a new livelist entry of a type specified in the config file with plugin
        self.livelist_new_track(PPdefinitions.new_tracks[self.plugin_params['type']],{'title':title,'track-ref':'','plugin':plugin_cfg,'location':plugin_cfg})        

        
    def livelist_new_track(self,fields,values):
        new_track=fields
        self.new_livelist.append(copy.deepcopy(new_track))
        last = len(self.new_livelist)-1
        self.new_livelist[last].update(values)        
    

        
    def new_livelist_create(self):
     
        self.new_livelist=[]
        if os.path.exists(self.pp_live_dir1):
            for file in os.listdir(self.pp_live_dir1):
                file = self.pp_live_dir1 + os.sep + file
                (root_file,ext_file)= os.path.splitext(file)
                if (ext_file.lower() in PPdefinitions.IMAGE_FILES+PPdefinitions.VIDEO_FILES+PPdefinitions.AUDIO_FILES+PPdefinitions.WEB_FILES) or (ext_file.lower()=='.cfg'):
                    self.livelist_add_track(file)
                    
        if os.path.exists(self.pp_live_dir2):
            for file in os.listdir(self.pp_live_dir2):
                file = self.pp_live_dir2 + os.sep + file
                (root_file,ext_file)= os.path.splitext(file)
                if ext_file.lower() in PPdefinitions.IMAGE_FILES+PPdefinitions.VIDEO_FILES+PPdefinitions.AUDIO_FILES+PPdefinitions.WEB_FILES or (ext_file.lower()=='.cfg'):
                    self.livelist_add_track(file)
                    

        self.new_livelist= sorted(self.new_livelist, key= lambda track: os.path.basename(track['location']).lower())
        # print 'LIVELIST'
        # for it in self.new_livelist:
            # print 'type: ', it['type'], 'loc: ',it['location'],'\nplugin cfg: ', it['plugin']
        # print ''


    
    def livelist_replace_if_changed(self):
        self.new_livelist_create()
        if  self.new_livelist<>self.livelist:
            self.livelist=copy.deepcopy(self.new_livelist)
            self.livelist_index = 1
   
   
    def livelist_next(self):

	skip = False
        if self.livelist_index== len(self.livelist)-1:
            self.livelist_index=0
        else:
            self.livelist_index +=1

	#Author Joe Houng 


	#get properties from file name if it exists
	runningFileName = self.livelist[self.livelist_index]['title']
	fileNameTupel = ProcessFileName(runningFileName)
	dur = fileNameTupel[0]
	startDate = fileNameTupel[1]
	endDate = fileNameTupel[2]

	if dur == "":
		#duration not specified in filename
		global defaultDur
		dur = defaultDur

	if startDate != "":
		curDate = time.strftime('%Y-%m-%d-%H-%M-%S')
		if startDate > curDate:
			print dur
			self.livelist_index +=1
			skip = True
			dur = defaultDur

	if skip == False:
	        if endDate != "":
			if endDate <= time.strftime('%Y-%m-%d-%H-%M-%S'):
				try:
					toArchive(runningFileName, self.pp_home)
				except IOError:
					None
	self.showlist.assign_dur(dur);
	skip = False


# ***************************
# Sequencing
# ***************************


    def wait_for_trigger(self):
        self.state='waiting'
        if self.ready_callback<>None:
            self.ready_callback()

        self.mon.log(self,"Waiting for trigger: "+ self.show_params['trigger-start'])

        if self.show_params['trigger-start'] in ('time','time-quiet'):
            # if next show is this one display text
            next_show=self.tod.next_event_time()
            if next_show[3]<>True:
                if next_show[1]=='tomorrow':
                    text = self.resource('liveshow','m04')
                else:
                    text = self.resource('liveshow','m03')                     
                text=text.replace('%tt',next_show[0])
                self.display_message(self.canvas,'text',text,0,self.play_first_track)  
            
        elif self.show_params['trigger-start']=="start":
            self.play_first_track()            
        else:
            self.mon.err(self,"Unknown trigger: "+ self.show_params['trigger-start'])
            self.end('error',"Unknown trigger type")

    # callbacks from time of day scheduler
    def tod_start_callback(self):
        if self.state=='waiting' and self.show_params['trigger-start']in('time','time-quiet'):
            self.play_first_track()      

    def tod_end_callback(self):
        if self.state=='playing' and self.show_params['trigger-end'] in ('time','duration'):
            self.end_trigger_signal=True
            if self.shower<>None:
                self.shower.input_pressed('stop','front','')
            elif self.player<>None:
                self.player.input_pressed('stop')

    def play_first_track(self):
        self.state='playing'
        skip = False
        # start duration timer
        if self.show_params['trigger-end']=='duration':
            # print 'set alarm ', self.duration
            self.duration_timer = self.canvas.after(self.duration*1000,self.tod_end_callback)
        self.new_livelist_create()
        self.livelist = copy.deepcopy(self.new_livelist)
        self.livelist_index = 0
	
	#Author Joe Houng

	#get properties from file name if it exists
	runningFileName = self.livelist[self.livelist_index]['title']

	fileNameTupel = ProcessFileName(runningFileName)
	dur = fileNameTupel[0]
	startDate = fileNameTupel[1]
	endDate = fileNameTupel[2]
	

	if dur == "":
		#duration not specified in filename
		global defaultDur
		dur = defaultDur
		
	if startDate != "":
		curDate = time.strftime('%Y-%m-%d')
		if startDate > curDate:
			print dur
			self.livelist_index +=1
			skip = True

	if skip == False:
		if endDate != "":
			if endDate <= time.strftime('%Y-%m-%d'):
				toArchive(runningFileName)
		self.showlist.assign_dur(dur);
	skip = False

	
	
	self.play_track()




        
    def play_track(self):        
        self.livelist_replace_if_changed()
        if len(self.livelist)>0:
            self.play_selected_track(self.livelist[self.livelist_index])
        else:
            self.display_message(self.canvas,None,self.resource('liveshow','m01'),5,self.what_next)


     
    def what_next(self):
        # end of show time trigger
        if self.end_trigger_signal==True:
            self.end_trigger_signal=False
            if self.top==True:
                self.state='waiting'
                self.wait_for_trigger()
            else:
                # not at top so stop the show
                self.end('normal','sub-show end time trigger')
                    
        # user wants to end 
        elif self.end_liveshow_signal==True:
            self.end_liveshow_signal=False
            self.end('normal',"show ended by user")
        
        # play child?
        elif self.play_child_signal == True:
            self.play_child_signal=False
            index = self.medialist.index_of_track('pp-child-show')
            if index >=0:
                #don't select the track as need to preserve mediashow sequence.
                child_track=self.medialist.track(index)
                self.display_eggtimer(self.resource('liveshow','m02'))
                self.play_selected_track(child_track)
            else:
                self.mon.err(self,"Child show not found in medialist: "+ self.show_params['pp-child-show'])
                self.end('error',"child show not found in medialist")
                
        # otherwise loop to next track                       
        else:
            self.livelist_next()
            self.play_track()
          
      
# ***************************
# Dispatching to Players/Shows 
# ***************************

    def ready_callback(self):
        self.delete_eggtimer()

    def play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected_track is a dictionary for the track/show
        """
        self.canvas.delete('pp-content')
        
        # is menu required
        if self.show_params['has-child']=="yes":
            enable_child=True
        else:
            enable_child=False

        #dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
                                      
        if track_type=="image":
            track_file=self.complete_path(selected_track)
            # images played from menus don't have children
            self.player=ImagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                    self.showlist,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=enable_child)
            
        elif track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=enable_child)
                   
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track)
            self.player=AudioPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=enable_child)
                                                
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(text,
                                    self.showlist,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=enable_child
                                    )

        elif track_type=="web":
            # create a browser
            track_file=self.complete_path(selected_track)
            self.player=BrowserPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=enable_child)
                                        
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self.end_liveshow_signal=True
                
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                               self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            
            elif selected_show['type']=="menu":
                self.shower= MenuShow(selected_show,
                                                          self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                          self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="radiobuttonshow":
                self.shower= RadioButtonShow(selected_show,
                                                         self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                         self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="hyperlinkshow":
                self.shower= HyperlinkShow(selected_show,
                                                       sef.root,
                                                        self.canvas,
                                                        self.showlist,
                                                       self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')
                
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self.end_liveshow_signal=True
                                                                            
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self.end_liveshow_signal=True
            

    def end_shower(self,show_id,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            self.what_next()  
        
    def end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            self.what_next()


# ***************************
# end of show 
# ***************************

    def end(self,reason,message):
        self.end_liveshow_signal=False
        self.mon.log(self,"Ending Liveshow: "+ self.show_params['show-ref'])
        self.tidy_up()
        self.end_callback(self.show_id,reason,message)
        self=None

    
    def tidy_up(self):
        if self.duration_timer<>None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer=None
        #clear outstanding time of day events for this show
        # self.tod.clear_times_list(id(self))     


# ******************************
# Displaying things
# *********************************
        
    def display_eggtimer(self,text):
        self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold",
                                                   tag='pp-eggtimer')
        self.canvas.update_idletasks( )


    def delete_eggtimer(self):
        self.canvas.delete('pp-eggtimer')
        self.canvas.update_idletasks( )

    # used to display internal messages in situations where a medialist entry could not be used.
    def display_message(self,canvas,source,content,duration,display_message_callback):
            self.display_message_callback=display_message_callback
            tp={'duration':duration,'message-colour':'white','message-font':'Helvetica 20 bold','message-justify':'left',
                'background-colour':'','background-image':'','show-control-begin':'','show-control-end':'',
                'animate-begin':'','animate-clear':'','animate-end':'','message-x':'','message-y':'',
                'display-show-background':'no','display-show-text':'no','show-text':'','track-text':'',
                'plugin':''}
            self.player=MessagePlayer(self.show_id,self.root,canvas,tp,tp,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(content,self.showlist,self.display_message_end,None)

   
    def  display_message_end(self,reason,message):
        self.player=None
        if reason in ("killed",'error'):
            self.end(reason,message)
        else:
            self.display_message_callback()


# ******************************
# utilities
# *********************************

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            self.terminate("error",'Cannot find resource')
        else:
            return value
        

    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     
         

    def calculate_duration(self,line):
        fields=line.split(':')
        if len(fields)==1:
            secs=fields[0]
            minutes='0'
            hours='0'
        if len(fields)==2:
            secs=fields[1]
            minutes=fields[0]
            hours='0'
        if len(fields)==3:
            secs=fields[2]
            minutes=fields[1]
            hours=fields[0]
        self.duration=3600*long(hours)+60*long(minutes)+long(secs)
        return ''
Пример #5
0
class MediaShow:

    # *******************
    # External interface
    # ********************

    def __init__(self, show_params, canvas, showlist, pp_home, pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the dictionary fo the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        # instantiate arguments
        self.show_params = show_params
        self.showlist = showlist
        self.canvas = canvas
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        # Init variables
        self.player = None
        self.shower = None
        self._poll_for_interval_timer = None
        self._poll_for_continue_timer = None
        self._waiting_for_interval = False
        self._interval_timer = None
        self.duration_timer = None
        self.error = False

        self._interval_timer_signal = False
        self._end_trigger_signal = False
        self._end_mediashow_signal = False
        self._next_track_signal = False
        self._previous_track_signal = False
        self._play_child_signal = False
        self._req_next = "nil"

        # create and instance of TimeOfDay scheduler so we can add events
        self.tod = TimeOfDay()

        self._state = "closed"

    def play(self, show_id, end_callback, ready_callback=None, top=False, command="nil"):

        """ displays the mediashow
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """

        # instantiate the arguments
        self.show_id = show_id
        self._end_callback = end_callback
        self._ready_callback = ready_callback
        self.top = top
        self.command = command
        self.mon.log(self, "Starting show: Id= " + str(self.show_id) + "  " + self.show_params["show-ref"])

        # check  data files are available.
        self.media_file = self.pp_profile + "/" + self.show_params["medialist"]
        if not os.path.exists(self.media_file):
            self.mon.err(self, "Medialist file not found: " + self.media_file)
            self._end("error", "Medialist file not found")

        # create a medialist for the mediashow and read it.
        self.medialist = MediaList()
        if self.medialist.open_list(self.media_file, self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self._end("error", "Version of medialist different to Pi Presents")

        # set up the time of day triggers for the show
        if self.show_params["trigger"] in ("time", "time-quiet"):
            error_text = self.tod.add_times(
                self.show_params["trigger-input"], id(self), self.tod_start_callback, self.show_params["trigger"]
            )
            if error_text <> "":
                self.mon.err(self, error_text)
                self._end("error", error_text)

        if self.show_params["trigger-end"] == "time":
            # print self.show_params['trigger-end-time']
            error_text = self.tod.add_times(
                self.show_params["trigger-end-time"], id(self), self.tod_end_callback, "n/a"
            )
            if error_text <> "":
                self.mon.err(self, error_text)
                self._end("error", error_text)

        if self.show_params["trigger-end"] == "duration":
            error_text = self.calculate_duration(self.show_params["trigger-end-time"])
            if error_text <> "":
                self.mon.err(self, error_text)
                self._end("error", error_text)

        self._state = "closed"
        self.egg_timer = None
        self._wait_for_trigger()

    def calculate_duration(self, line):
        fields = line.split(":")
        if len(fields) == 1:
            secs = fields[0]
            minutes = "0"
            hours = "0"
        if len(fields) == 2:
            secs = fields[1]
            minutes = fields[0]
            hours = "0"
        if len(fields) == 3:
            secs = fields[2]
            minutes = fields[1]
            hours = fields[0]
        self.duration = 3600 * long(hours) + 60 * long(minutes) + long(secs)
        return ""

    # ********************************
    # Respond to external events
    # ********************************

    # respond to key presses
    def key_pressed(self, key_name):
        self.mon.log(self, "received key: " + key_name)
        if self.show_params["disable-controls"] == "yes":
            return
        if key_name == "":
            pass

        elif key_name == "escape":
            # if next lower show is running pass down to stop the show and lower level
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            # if not at top stop the show
            else:
                if self.top == False:
                    self._end_mediashow_signal = True
                    # and if a track is running stop that first
                    if self.player <> None:
                        self.player.key_pressed(key_name)
                else:
                    # at top level in a manual presentation stop the track
                    if self.show_params["progress"] == "manual":
                        if self.player <> None:
                            self.player.key_pressed(key_name)

        elif key_name in ("up", "down"):
            # if child or sub-show is running and is a show pass to show, track does not use up/down
            # otherwise use keys for next or previous
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            else:
                if key_name == "up" and self._state == "playing":
                    self._previous()
                else:
                    self._next()

        elif key_name == "return":
            # if child show or sub-show is running and is show - pass down- player does not use return
            # ELSE use Return to start child or to start the show if waiting
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            else:
                if self._state == "playing":
                    if self.show_params["has-child"] == "yes":
                        self._play_child_signal = True
                        # and stop the current track if its running
                        if self.player <> None:
                            self.player.key_pressed("escape")
                else:
                    if self._state == "waiting":
                        self._start_show()

        elif key_name in ("p", " "):
            # pass down if show or track running.
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            elif self.player <> None:
                self.player.key_pressed(key_name)

    def button_pressed(self, button, edge):
        # print 'mediashow button pressed', button
        if button == "play":
            self.key_pressed("return")
        elif button == "up":
            self.key_pressed("up")
        elif button == "down":
            self.key_pressed("down")
        elif button == "stop":
            self.key_pressed("escape")
        elif button == "pause":
            self.key_pressed("p")
        else:
            self.input_pressed(button)

    def input_pressed(self, xinput):
        # print self._state, self.show_params['trigger-next'], self.show_params['next-input']
        if (
            self._state == "waiting"
            and self.show_params["trigger"] == "GPIO"
            and xinput == self.show_params["trigger-input"]
        ):
            self.key_pressed("return")
        elif (
            self._state == "playing"
            and self.show_params["trigger-next"] == "GPIO"
            and xinput == self.show_params["next-input"]
        ):
            self.key_pressed("down")

    # callback from time of day scheduler
    def tod_start_callback(self):
        if self._state == "waiting" and self.show_params["trigger"] in ("time", "time-quiet"):
            self._start_show()

    def tod_end_callback(self):
        if self._state == "playing" and self.show_params["trigger-end"] in ("time", "duration"):
            self._end_trigger_signal = True
            if self.shower <> None:
                self.shower.key_pressed("escape")
            elif self.player <> None:
                self.player.key_pressed("escape")

    # kill or error
    def terminate(self, reason):
        if self.shower <> None:
            self.mon.log(self, "sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player <> None:
            self.mon.log(self, "sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason, "terminated without terminating shower or player")

    def _tidy_up(self):
        # clear outstanding time of day events for this show
        self.tod.clear_times_list(id(self))
        if self._poll_for_continue_timer <> None:
            self.canvas.after_cancel(self._poll_for_continue_timer)
            self._poll_for_continue_timer = None
        if self._poll_for_interval_timer <> None:
            self.canvas.after_cancel(self._poll_for_interval_timer)
            self._poll_for_interval_timer = None
        if self._interval_timer <> None:
            self.canvas.after_cancel(self._interval_timer)
            self._interval_timer = None
        if self.duration_timer <> None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer = None

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self, "resource: " + section + ": " + item + " not found")
            self.terminate("error")
        else:
            return value

    # ***************************
    # Do actions as a result of events
    # ***************************

    def _stop(self, message):
        self._end_mediashow_signal = True
        if self._interval_timer <> None:
            self.canvas.after_cancel(self._interval_timer)

    def _next(self):
        # stop track if running and set signal
        self._next_track_signal = True
        if self.shower <> None:
            self.shower.key_pressed("escape")
        else:
            if self.player <> None:
                self.player.key_pressed("escape")

    def _previous(self):
        self._previous_track_signal = True
        if self.shower <> None:
            self.shower.key_pressed("escape")
        else:
            if self.player <> None:
                self.player.key_pressed("escape")

    # ***************************
    # end of show functions
    # ***************************

    def _end(self, reason, message):
        self._end_mediashow_signal = False
        self.mon.log(self, "Ending Mediashow: " + self.show_params["show-ref"])
        self._tidy_up()
        self._end_callback(self.show_id, reason, message)
        self = None
        return

    # ***************************
    # Show sequencer
    # ***************************

    # wait for trigger sets the state to waiting so that key/button presses can do a start show.

    def _wait_for_trigger(self):
        self._state = "waiting"
        if self.ready_callback <> None:
            self.ready_callback()

        self.mon.log(self, "Waiting for trigger: " + self.show_params["trigger"])

        if self.show_params["trigger"] == "button":
            # blank screen waiting for trigger if auto, otherwise display something
            if self.show_params["progress"] == "manual":
                text = self.resource("mediashow", "m01")
            else:
                text = ""
            self.display_message(self.canvas, "text", text, 0, self._start_show)

        elif self.show_params["trigger"] == "GPIO":
            # blank screen waiting for trigger
            # text = self.resource('mediashow','m02')
            # self.display_message(self.canvas,'text',text,0,self._start_show)
            pass

        elif self.show_params["trigger"] in ("time", "time-quiet"):
            # show next show notice
            quiet = 3
            # if next show is this one display text
            next_show = self.tod.next_event_time()
            if next_show[quiet] == False:
                if next_show[1] == "tomorrow":
                    text = self.resource("mediashow", "m09")
                else:
                    text = self.resource("mediashow", "m08")
                text = text.replace("%tt", next_show[0])
                self.display_message(self.canvas, "text", text, 0, self._start_show)

        elif self.show_params["trigger"] == "start":
            self._start_show()

        else:
            self.mon.err(self, "Unknown trigger: " + self.show_params["trigger"])
            self._end("error", "Unknown trigger type")

    def _start_show(self):
        self._state = "playing"
        self._direction = "forward"
        # self.canvas.delete(ALL)
        # start interval timer
        if self.show_params["repeat"] == "interval" and self.show_params["repeat-interval"] <> 0:
            self._interval_timer_signal = False
            self._interval_timer = self.canvas.after(
                int(self.show_params["repeat-interval"]) * 1000, self._end_interval_timer
            )

        # start duration timer
        if self.show_params["trigger-end"] == "duration":
            # print 'set alarm ', self.duration
            self.duration_timer = self.canvas.after(self.duration * 1000, self.tod_end_callback)

        # and play the first track unless commanded otherwise
        if self.command == "backward":
            self.medialist.finish()
        else:
            self.medialist.start()
        self._play_selected_track(self.medialist.selected_track())

    def _what_next(self):
        self._direction = "forward"

        # end of show trigger
        if self._end_trigger_signal == True:
            self._end_trigger_signal = False
            if self.top == True:
                self._state = "waiting"
                self._wait_for_trigger()
            else:
                # not at top so stop the show
                self._end("normal", "sub-show end time trigger")

        # user wants to end, wait for any shows or tracks to have ended then end show
        elif self._end_mediashow_signal == True:
            if self.player == None and self.shower == None:
                self._end_mediashow_signal = False
                self._end("normal", "show ended by user")
            else:
                pass

        # returning from a subshow needing to move onward
        elif self._req_next == "do-next":
            self._req_next = "nil"
            self.medialist.next(self.show_params["sequence"])
            self._play_selected_track(self.medialist.selected_track())

        # returning from a subshow needing to move backward
        elif self._req_next == "do-previous":
            self._req_next = "nil"
            self._direction = "backward"
            self.medialist.previous(self.show_params["sequence"])
            self._play_selected_track(self.medialist.selected_track())

        # user wants to play child
        elif self._play_child_signal == True:
            self._play_child_signal = False
            index = self.medialist.index_of_track("pp-child-show")
            if index >= 0:
                # don't select the track as need to preserve mediashow sequence.
                child_track = self.medialist.track(index)
                self._display_eggtimer(self.resource("mediashow", "m07"))
                self._play_selected_track(child_track)
            else:
                self.mon.err(self, "Child show not found in medialist: " + self.show_params["pp-child-show"])
                self._end("error", "child show not found in medialist")

        # skip to next track on user input
        elif self._next_track_signal == True:
            self._next_track_signal = False
            if self.medialist.at_end() == True:
                if (
                    self.show_params["sequence"] == "ordered"
                    and self.show_params["repeat"] == "oneshot"
                    and self.top == False
                ):
                    self._end("do-next", "Return from Sub Show")
                else:
                    self.medialist.next(self.show_params["sequence"])
                    self._play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.next(self.show_params["sequence"])
                self._play_selected_track(self.medialist.selected_track())

        # skip to previous track on user input
        elif self._previous_track_signal == True:
            self._previous_track_signal = False
            self._direction = "backward"
            if self.medialist.at_start() == True:
                if (
                    self.show_params["sequence"] == "ordered"
                    and self.show_params["repeat"] == "oneshot"
                    and self.top == False
                ):
                    self._end("do-previous", "Return from Sub Show")
                else:
                    self.medialist.previous(self.show_params["sequence"])
                    self._play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.previous(self.show_params["sequence"])
                self._play_selected_track(self.medialist.selected_track())

        # track is finished and we are on auto
        elif self.show_params["progress"] == "auto":

            if self.medialist.at_end() == True:

                if (
                    self.show_params["sequence"] == "ordered"
                    and self.show_params["repeat"] == "oneshot"
                    and self.top == False
                ):
                    self._end("do-next", "Return from Sub Show")

                elif (
                    self.show_params["sequence"] == "ordered"
                    and self.show_params["repeat"] == "oneshot"
                    and self.top == True
                ):
                    self._wait_for_trigger()

                elif self._waiting_for_interval == True:
                    if self._interval_timer_signal == True:
                        self._interval_timer_signal = False
                        self._waiting_for_interval = False
                        self._start_show()
                    else:
                        self._poll_for_interval_timer = self.canvas.after(1000, self._what_next)

                elif (
                    self.show_params["sequence"] == "ordered"
                    and self.show_params["repeat"] == "interval"
                    and int(self.show_params["repeat-interval"]) > 0
                ):
                    self._waiting_for_interval = True
                    self._poll_for_interval_timer = self.canvas.after(1000, self._what_next)

                # elif self.show_params['sequence']=="ordered" and self.show_params['repeat']=='interval' and int(self.show_params['repeat-interval'])==0:
                elif self.show_params["repeat"] == "interval" and int(self.show_params["repeat-interval"]) == 0:
                    self.medialist.next(self.show_params["sequence"])
                    self._play_selected_track(self.medialist.selected_track())

                # shffling so there is no end condition
                elif self.show_params["sequence"] == "shuffle":
                    self.medialist.next(self.show_params["sequence"])
                    self._play_selected_track(self.medialist.selected_track())

                else:
                    self.mon.err(
                        self,
                        "Unhandled playing event: "
                        + self.show_params["sequence"]
                        + " with "
                        + self.show_params["repeat"]
                        + " of "
                        + self.show_params["repeat-interval"],
                    )
                    self._end("error", "Unhandled playing event")

            else:
                self.medialist.next(self.show_params["sequence"])
                self._play_selected_track(self.medialist.selected_track())

        # track has finished and we are on manual progress
        elif self.show_params["progress"] == "manual":
            self._delete_eggtimer()
            self.canvas.delete(ALL)
            if self.show_params["trigger-next"] == "button":
                self._display_eggtimer(self.resource("mediashow", "m03"))
            self._poll_for_continue_timer = self.canvas.after(2000, self._what_next)

        else:
            # unhandled state
            self.mon.err(self, "Unhandled playing event: ")
            self._end("error", "Unhandled playing event")

    def _end_interval_timer(self):
        self._interval_timer_signal = True

    # ***************************
    # Dispatching to Players/Shows
    # ***************************

    # used to display internal messages in situations where a medialist entry could be used.
    def display_message(self, canvas, source, content, duration, _display_message_callback):
        self._display_message_callback = _display_message_callback
        tp = {
            "duration": duration,
            "message-colour": "white",
            "message-font": "Helvetica 20 bold",
            "background-colour": "",
            "background-image": "",
        }
        self.player = MessagePlayer(self.show_id, canvas, self.pp_home, tp, tp)
        self.player.play(content, self._display_message_end, None)

    def _display_message_end(self, reason, message):
        self.player = None
        if reason in ("error", "killed"):
            self._end(reason, message)
        else:
            self._display_message_callback()

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track["location"]
        if track_file <> "" and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def _play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
        # self.canvas.delete(ALL)
        if self.show_params["progress"] == "manual":
            self._display_eggtimer(self.resource("mediashow", "m04"))

        # is menu required
        if self.show_params["has-child"] == "yes":
            enable_child = True
        else:
            enable_child = False

        # dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track["type"]
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.show_id, self.canvas, self.pp_home, self.show_params, selected_track)
            self.player.play(track_file, self.end_player, self.ready_callback, enable_menu=enable_child)

        elif track_type == "audio":
            # create a audioplayer
            track_file = self.complete_path(selected_track)
            self.player = AudioPlayer(self.show_id, self.canvas, self.pp_home, self.show_params, selected_track)
            self.player.play(track_file, self.end_player, self.ready_callback, enable_menu=enable_child)

        elif track_type == "image":
            track_file = self.complete_path(selected_track)
            # images played from menus don't have children
            self.player = ImagePlayer(self.show_id, self.canvas, self.pp_home, self.show_params, selected_track)
            self.player.play(track_file, self.end_player, self.ready_callback, enable_menu=enable_child)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track["text"]
            self.player = MessagePlayer(self.show_id, self.canvas, self.pp_home, self.show_params, selected_track)
            self.player.play(text, self.end_player, self.ready_callback, enable_menu=enable_child)

        elif track_type == "show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track["sub-show"])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(self, "Show not found in showlist: " + selected_track["sub-show"])
                self._end("error", "Unknown show")

            if selected_show["type"] == "mediashow":
                self.shower = MediaShow(selected_show, self.canvas, self.showlist, self.pp_home, self.pp_profile)
                self.shower.play(self.show_id, self.end_shower, top=False, command=self._direction)

            elif selected_show["type"] == "liveshow":
                self.shower = LiveShow(selected_show, self.canvas, self.showlist, self.pp_home, self.pp_profile)
                self.shower.play(self.show_id, self.end_shower, top=False, command="nil")

            elif selected_show["type"] == "menu":
                self.shower = MenuShow(selected_show, self.canvas, self.showlist, self.pp_home, self.pp_profile)
                self.shower.play(self.show_id, self.end_shower, top=False, command="nil")

            else:
                self.mon.err(self, "Unknown Show Type: " + selected_show["type"])
                self._end("error" "Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self._end("error", "Unknown track type")

    def ready_callback(self):
        self._delete_eggtimer()

    def end_player(self, reason, message):
        self._req_next = "nil"
        self.mon.log(self, " Show Id: " + str(self.show_id) + " Returned from player with message: " + message)
        self.player = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        elif self.show_params["progress"] == "manual":
            self._display_eggtimer(self.resource("mediashow", "m05"))
            self._req_next = reason
            self._what_next()
        else:
            self._req_next = reason
            self._what_next()

    def end_shower(self, show_id, reason, message):
        self._req_next = "nil"
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        elif self.show_params["progress"] == "manual":
            self._display_eggtimer(self.resource("mediashow", "m06"))
            self._req_next = reason
            self._what_next()
        else:
            self._req_next = reason
            self._what_next()

    def _display_eggtimer(self, text):
        self.egg_timer = self.canvas.create_text(
            int(self.canvas["width"]) / 2,
            int(self.canvas["height"]) / 2,
            text=text,
            fill="white",
            font="Helvetica 20 bold",
        )
        self.canvas.update_idletasks()

    def _delete_eggtimer(self):
        if self.egg_timer != None:
            self.canvas.delete(self.egg_timer)
            self.canvas.update_idletasks()
Пример #6
0
class MediaShow:


# *******************
# External interface
# ********************

    def __init__(self,
                            show_params,
                             root,
                            canvas,
                            showlist,
                            pp_dir,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the dictionary fo the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show_params =show_params
        self.showlist=showlist
        self.root=root
        self.canvas=canvas
        self.pp_dir=pp_dir
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

        # Init variables
        self.player=None
        self.shower=None
        self.poll_for_interval_timer=None
        self.poll_for_continue_timer=None
        self.waiting_for_interval=False
        self.interval_timer=None
        self.duration_timer=None
        self.error=False
        
        self.interval_timer_signal=False
        self.end_trigger_signal=False
        self.end_mediashow_signal=False
        self.next_track_signal=False
        self.previous_track_signal=False
        self.play_child_signal = False
        self.req_next='nil'

        #create and instance of TimeOfDay scheduler so we can add events
        self.tod=TimeOfDay()

        self.state='closed'


    def play(self,show_id,end_callback,show_ready_callback, top=False,command='nil'):

        """ displays the mediashow
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """

        #instantiate the arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.show_ready_callback=show_ready_callback
        self.top=top
        self.command=command
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Starting show")

        # check  data files are available.
        self.media_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self,"Medialist file not found: "+ self.media_file)
            self.end('error',"Medialist file not found")


        #create a medialist for the mediashow and read it.
        self.medialist=MediaList(self.show_params['sequence'])
        if self.medialist.open_list(self.media_file,self.showlist.sissue())==False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self.end('error',"Version of medialist different to Pi Presents")

        #get controls for this show if top level
        controlsmanager=ControlsManager()
        if self.top==True:
            self.controls_list=controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list=controlsmanager.merge_show_controls(self.controls_list,self.show_params['controls'])


        #set up the time of day triggers for the show
        if self.show_params['trigger']in('time','time-quiet'):
            error_text=self.tod.add_times(self.show_params['trigger-input'],id(self),self.tod_start_callback,self.show_params['trigger'])
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)

        if self.show_params['trigger-end']=='time':
            # print self.show_params['trigger-end-time']
            error_text=self.tod.add_times(self.show_params['trigger-end-time'],id(self),self.tod_end_callback,'n/a')
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)
                
        if self.show_params['trigger-end']=='duration':
            error_text=self.calculate_duration(self.show_params['trigger-end-time'])
            if error_text<>'':
                self.mon.err(self,error_text)
                self.end('error',error_text)
                
        self.state='closed'
        self.egg_timer=None
        self.wait_for_trigger()


# ********************************
# Respond to external events
# ********************************

    #stop received from another concurrent show
    def managed_stop(self):
           # if next lower show is running pass down to stop the show and lower level
            if self.shower<>None:
                self.shower.managed_stop()
            else:
                #stop the show if not at top
                self.end_mediashow_signal=True
                # and if track is runing stop that first
                if self.player<>None:
                    self.player.input_pressed('stop')

    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.shower.terminate(reason)
        elif self.player<>None:
            self.player.terminate(reason)
        else:
            self.end(reason,' terminated with no shower or player to terminate')


   
   # respond to input events
    def input_pressed(self,symbol,edge,source):
        self.mon.log(self, self.show_params['show-ref']+ ' '+ str(self.show_id)+": received input: " + symbol)

        
        #  check symbol against mediashow triggers, triggers can be used at top or lower level
        # and not affected by disable-controls

        if self.state=='waiting' and self.show_params['trigger'] in ('input','input-quiet')and symbol == self.show_params['trigger-input']:
            self.start_show()
        elif self.state=='playing' and self.show_params['trigger-next']=='input' and symbol == self.show_params['next-input']:
            self.next()

       # internal functions are triggered only when disable-controls is  'no'
        if self.show_params['disable-controls']=='yes':
            return

        # if at top convert symbolic name to operation otherwise lower down we have received an operation
        # look through list of standard symbols to find match (symbolic-name, function name) operation =lookup (symbol
        if self.top==True:
            operation=self.lookup_control(symbol,self.controls_list)
        else:
            operation=symbol
            
   
        # print 'operation',operation
        self.do_operation(operation,edge,source)


    #service the standard inputs for this show
    def do_operation(self,operation,edge,source):
        if self.shower<>None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation,edge,source) 
        else:        
            # control this show and its tracks
            # print 'operation',operation
            if operation=='stop':
                if self.top == False:
                    # not at top so stop the current show 
                    self.end_mediashow_signal=True
                    # and if a track is running stop that first
                    if self.player<>None:
                        self.player.input_pressed('stop')
                else:
                    # top = True, just stop track if running
                    if self.player<>None:
                        self.player.input_pressed('stop')

            elif operation in ('up','down'):
                #if playing rather than waiting use keys for next or previous
                if operation=='up' and self.state=='playing':
                    self.previous()
                else:
                    self.next()

            elif operation=='play':
                # use 'play' to start child if state=playing or to trigger the show if waiting for trigger
                if self.state=='playing':
                    if self.show_params['has-child']=='yes':
                        self.play_child_signal=True
                        self.child_track_ref='pp-child-show'
                        # and stop the current track if its running
                        if self.player<>None:
                            self.player.input_pressed('stop')
                else:
                    if self.state=='waiting':
                        self.start_show()

            elif operation == 'pause':
                if self.player<>None:
                    self.player.input_pressed(operation)
                    
            #if the operation is omxplayer or mplayer runtime control then pass it to player if running
            elif operation[0:4]=='omx-' or operation[0:6]=='mplay-'or operation[0:5]=='uzbl-':
                if self.player<>None:
                    self.player.input_pressed(operation)



    def lookup_control(self,symbol,controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''


# ***************************
# Show sequencer
# ***************************

    def end_interval_timer(self):
        self.interval_timer_signal=True

    # callback from time of day scheduler
    def tod_start_callback(self):
         if self.state=='waiting' and self.show_params['trigger']in('time','time-quiet'):
            self.start_show()

    def tod_end_callback(self):
        if self.state=='playing' and self.show_params['trigger-end'] in ('time','duration'):
            self.end_trigger_signal=True
            if self.shower<>None:
                self.shower.input_pressed('stop')
            elif self.player<>None:
                self.player.input_pressed('stop')
                

    def stop(self,message):
        self.end_mediashow_signal=True
        if self.interval_timer<>None:
            self.canvas.after_cancel(self.interval_timer)

   
    def next(self):
        # stop track if running and set signal
        self.next_track_signal=True
        if self.shower<>None:
            self.shower.input_pressed("stop")
        else:
            if self.player<>None:
                self.player.input_pressed("stop")

    def previous(self):
        self.previous_track_signal=True
        if self.shower<>None:
            self.shower.input_pressed("stop")
        else:
            if self.player<>None:
                self.player.input_pressed("stop")
    
        
    # wait for trigger sets the state to waiting so that events can do a start show.    
    def wait_for_trigger(self):
        self.state='waiting'
        if self.show_ready_callback<>None:
            self.show_ready_callback()

        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Waiting for trigger: "+ self.show_params['trigger'])
        
        if self.show_params['trigger']=="input":
            # blank screen waiting for trigger if auto, otherwise display something
            if self.show_params['progress']=="manual":
                text= self.resource('mediashow','m01')
            else:
                text= self.resource('mediashow','m02')
            self.display_message(self.canvas,'text',text,0,self.start_show)


        elif self.show_params['trigger']=="input-quiet":
            # blank screen waiting for trigger
            text = self.resource('mediashow','m10')
            self.display_message(self.canvas,'text',text,0,self.start_show)
            pass

        elif self.show_params['trigger'] in ('time','time-quiet'):
            # show next show notice
            quiet=3
            # if next show is this one display text
            next_show=self.tod.next_event_time()
            if next_show[quiet]==False:
                if next_show[1]=='tomorrow':
                    text = self.resource('mediashow','m09')
                else:
                    text = self.resource('mediashow','m08')                     
                text=text.replace('%tt',next_show[0])
                self.display_message(self.canvas,'text',text,0,self.start_show)  
            
        elif self.show_params['trigger']=="start":
            self.start_show()
            
        else:
            self.mon.err(self,"Unknown trigger: "+ self.show_params['trigger'])
            self.end('error',"Unknown trigger type")



  
    def start_show(self):
        self.state='playing'
        self.direction='forward'
        # self.canvas.delete(ALL)
        # start interval timer
        if self.show_params['repeat']=="interval" and self.show_params['repeat-interval']<>0:
            self.interval_timer_signal=False
            self.interval_timer=self.canvas.after(int(self.show_params['repeat-interval'])*1000,self.end_interval_timer)
            
        # start duration timer
        if self.show_params['trigger-end']=='duration':
            # print 'set alarm ', self.duration
            self.duration_timer = self.canvas.after(self.duration*1000,self.tod_end_callback)
        
        # and play the first track unless commanded otherwise
        if self.command=='backward':
            self.medialist.finish()
        else:
            self.medialist.start()
        self.play_selected_track(self.medialist.selected_track())
 
 
    def what_next(self):
        self.direction='forward'

        # end of show trigger caused by tod
        if self.end_trigger_signal==True:
            self.end_trigger_signal=False
            if self.top==True:
                self.state='waiting'
                self.wait_for_trigger()
            else:
                # not at top so stop the show
                self.end('normal','sub-show end time trigger')
        
        # user wants to end, wait for any shows or tracks to have ended then end show
        # probalby will get here with end_m set when player and shower has finished
        elif self.end_mediashow_signal==True:
            if self.player==None and self.shower==None:
                self.end_mediashow_signal=False
                self.end('normal',"show ended by user")

            
        #returning from a subshow needing to move onward 
        elif self.req_next=='do-next':
            self.req_next='nil'
            self.medialist.next(self.show_params['sequence'])
            self.play_selected_track(self.medialist.selected_track())
            
        #returning from a subshow needing to move backward 
        elif self.req_next=='do-previous':
            self.req_next='nil'
            self.direction='backward'
            self.medialist.previous(self.show_params['sequence'])
            self.play_selected_track(self.medialist.selected_track())         
               
        # user wants to play child
        elif self.play_child_signal == True:
            self.play_child_signal=False
            index = self.medialist.index_of_track(self.child_track_ref)
            if index >=0:
                #don't use select the track as need to preserve mediashow sequence.
                child_track=self.medialist.track(index)
                self.display_eggtimer(self.resource('mediashow','m07'))
                self.play_selected_track(child_track)
            else:
                self.mon.err(self,"Child show not found in medialist: "+ self.show_params['pp-child-show'])
                self.end('error',"child show not found in medialist")
        
        # skip to next track on user input
        elif self.next_track_signal==True:
            self.next_track_signal=False
            if self.medialist.at_end()==True:
                if  self.show_params['sequence']=="ordered" and self.show_params['repeat']=='oneshot' and self.top==False:
                    self.end('do-next',"Return from Sub Show")
                elif  self.show_params['sequence']=="ordered" and self.show_params['repeat']=='single-run' and self.top==False:
                    self.end('do-next',"Return from Sub Show")
                else:
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())               
            else:
                self.medialist.next(self.show_params['sequence'])
                self.play_selected_track(self.medialist.selected_track())
                
        # skip to previous track on user input
        elif self.previous_track_signal==True:
            self.previous_track_signal=False
            self.direction='backward'
            if self.medialist.at_start()==True:
                if  self.show_params['sequence']=="ordered" and self.show_params['repeat']=='oneshot' and self.top==False:
                    self.end('do-previous',"Return from Sub Show")
                elif  self.show_params['sequence']=="ordered" and self.show_params['repeat']=='single-run' and self.top==False:
                    self.end('do-previous',"Return from Sub Show")
                else:
                    self.medialist.previous(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())               
            else:
                self.medialist.previous(self.show_params['sequence'])              
                self.play_selected_track(self.medialist.selected_track())
        

        # track is finished and we are on auto        
        elif self.show_params['progress']=="auto":
            
            if self.medialist.at_end()==True:

                # oneshot
                if self.show_params['repeat']=='oneshot' and self.top==False:
                    self.end('normal',"End of Oneshot in subshow")
                    
                elif self.show_params['sequence']=="ordered" and self.show_params['repeat']=='oneshot' and self.top==True:
                    self.wait_for_trigger()

                # single run
                elif self.show_params['repeat']=='single-run' and self.top==True:
                   self.end('normal',"End of Single Run")

                elif self.show_params['repeat']=='single-run' and self.top==False:
                   self.end('do-next',"End of single run - Return from Sub Show")

                # repeating and waiting to restart 
                elif self.waiting_for_interval==True:
                    if self.interval_timer_signal==True:
                        self.interval_timer_signal=False
                        self.waiting_for_interval=False
                        self.start_show()
                    else:
                        self.poll_for_interval_timer=self.canvas.after(1000,self.what_next)
 
                elif self.show_params['repeat']=='interval' and int(self.show_params['repeat-interval'])>0:
                    self.waiting_for_interval=True
                    self.poll_for_interval_timer=self.canvas.after(1000,self.what_next) 
                    
                #elif self.show_params['sequence']=="ordered" and self.show_params['repeat']=='interval' and int(self.show_params['repeat-interval'])==0:
                elif self.show_params['repeat']=='interval' and int(self.show_params['repeat-interval'])==0:
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())

                # shuffling so there is no end condition
                elif self.show_params['sequence']=="shuffle":
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())
                    
                else:
                    self.mon.err(self,"Unhandled playing event: "+self.show_params['sequence'] +' with ' + self.show_params['repeat']+" of "+ self.show_params['repeat-interval'])
                    self.end('error',"Unhandled playing event")
                    
            else:
                self.medialist.next(self.show_params['sequence'])
                self.play_selected_track(self.medialist.selected_track())
                    
        # track has finished and we are on manual progress               
        elif self.show_params['progress']=="manual":
                    self.delete_eggtimer()
                    self.canvas.delete('pp-content')
                    if self.show_params['trigger-next']=='input':
                        self.display_eggtimer(self.resource('mediashow','m03'))
                    self.poll_for_continue_timer=self.canvas.after(2000,self.what_next)
                    
        else:
            #unhandled state
            self.mon.err(self,"Unhandled playing event: ")
            self.end('error',"Unhandled playing event")           


# ***************************
# Dispatching to Players/Shows 
# ***************************
   
    def ready_callback(self):
        self.delete_eggtimer()

    def play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
        self.delete_eggtimer()
        if self.show_params['progress']=="manual":
            self.display_eggtimer(self.resource('mediashow','m04'))

        # is menu required
        if self.show_params['has-child']=="yes":
            self.enable_child=True
        else:
            self.enable_child=False

        #dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Track type is: "+ track_type)
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=self.enable_child)
  
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track)
            self.player=AudioPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=self.enable_child)
 
        elif track_type=="web":
            # create a browser
            track_file=self.complete_path(selected_track)
            self.player=BrowserPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=self.enable_child)
  


 
        elif track_type=="image":
            track_file=self.complete_path(selected_track)
            # images played from menus don't have children
            self.player=ImagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                    self.showlist,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=self.enable_child)
                                    
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(text,
                                    self.showlist,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=self.enable_child
                                    )

 
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self.end('error',"Unknown show")
                
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                               self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                print "Starting MediaShow: {0}".format(selected_track['sub-show'])
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command=self.direction)

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="radiobuttonshow":
                self.shower= RadioButtonShow(selected_show,
                                                         self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="hyperlinkshow":
                self.shower= HyperlinkShow(selected_show,
                                                       self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                       self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')
            
            elif selected_show['type']=="menu":
                self.shower= MenuShow(selected_show,
                                                        self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                          self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')
                
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self.end('error'"Unknown show type")  
            
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self.end('error',"Unknown track type")            


    def end_player(self,reason,message):
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Returned from player with message: "+ message)
        self.player=None
        self.req_next='nil'
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            # elif>else move to what-next?
            if self.show_params['progress']=="manual":
                self.display_eggtimer(self.resource('mediashow','m05'))
                self.req_next=reason
                self.what_next()
            else:
                self.req_next=reason
                self.what_next()
                

    def end_shower(self,show_id,reason,message):
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Returned from shower with message: "+ message)
        self.shower=None
        self.req_next='nil'
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            if self.show_params['progress']=="manual":
                self.display_eggtimer(self.resource('mediashow','m06'))
                self.req_next=reason
                self.what_next() 
            else:
                self.req_next=reason
                self.what_next() 



# ***************************
# end of show 
# ***************************

    def end(self,reason,message):
        self.end_mediashow_signal=False
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Ending Mediashow")
        self.tidy_up()
        self.end_callback(self.show_id,reason,message)
        self=None
        return

    def tidy_up(self):
        #clear outstanding time of day events for this show
        # self.tod.clear_times_list(id(self))
        if self.poll_for_continue_timer<>None:
                self.canvas.after_cancel(self.poll_for_continue_timer)
                self.poll_for_continue_timer=None
        if self.poll_for_interval_timer<>None:
                self.canvas.after_cancel(self.poll_for_interval_timer)
                self.poll_for_interval_timer=None
        if self.interval_timer<>None:
            self.canvas.after_cancel(self.interval_timer)
            self.interval_timer=None
        if self.duration_timer<>None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer=None


# ***************************
# displaying things
# ***************************
    
    def display_eggtimer(self,text):
        self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold",
                                                tag='pp-eggtimer')
        self.canvas.update_idletasks( )


    def delete_eggtimer(self):
        self.canvas.delete('pp-eggtimer')
        self.canvas.update_idletasks( )


    # used to display internal messages in situations where a medialist entry could not be used.
    def display_message(self,canvas,source,content,duration,_display_message_callback):
            self.display_message_callback=_display_message_callback
            tp={'duration':duration,'message-colour':'white','message-font':'Helvetica 20 bold','background-colour':'',
                'message-justify':'left','background-image':'','show-control-begin':'','show-control-end':'',
                'animate-begin':'','animate-clear':'','animate-end':'','message-x':'','message-y':'',
                'display-show-background':'no','display-show-text':'no','show-text':'','track-text':'',
                'plugin':''}
            self.player=MessagePlayer(self.show_id,self.root,canvas,tp,tp,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(content,self.showlist,self.display_message_end,None,False)

    def   display_message_end(self,reason,message):
        self.player=None
        if reason in ('error','killed'):
            self.end(reason,message)
        else:
            self.display_message_callback()



# ***************************
# utilities
# ***************************

    def calculate_duration(self,line):
        fields=line.split(':')
        if len(fields)==1:
            secs=fields[0]
            minutes='0'
            hours='0'
        if len(fields)==2:
            secs=fields[1]
            minutes=fields[0]
            hours='0'
        if len(fields)==3:
            secs=fields[2]
            minutes=fields[1]
            hours=fields[0]
        self.duration=3600*long(hours)+60*long(minutes)+long(secs)
        return ''

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            self.terminate("error")
        else:
            return value
        
    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,self.show_params['show-ref']+ ' '+ str(self.show_id)+ ": Track to play is: "+ track_file)
        return track_file     
Пример #7
0
class HyperlinkShow:
    """
        Aimed at touchscreens but can be used for any purpose where the user is required to follow hyperlinks between tracks
        Profiles for media tracks (message, image, video, audio ) specify links to other tracks
        in a link a symbolic name of an input is associated with a track-reference
        The show begins at first-track and then uses events (GPIO, keypresses etc.) to link to other tracks via their symbolic names
        If using 'call' keeps a record of the tracks it has visited so the 'return' command can go back.
        Executes timeout-track if no user input is received.

        links are of the form:
           symbolic-name command [track-ref]
        
        link commands:
          call <track-ref> play track-ref and add it to the path
          return - return 1 back up the path removing the track from the path, stops at home-track.
          return n - return n tracks back up the path removing the track from the path, stops at home-track.
          return <track-ref> return to <track-ref> removing tracks from the path
          home  - return to home-track removing tracks from the path
          jump <track-ref-> - play trck-ref forgetting the path back to home-track
          goto <track-ref> - play track-ref, forget the path 
          exit - end the hyperlink show
          null - inhibits the link defined in the show with the same symbolic name.

          reserved symbolic names
          pp-onend command  - pseudo symbolic name for end of a track

        interface:
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
    """

    # *********************
    # external interface
    # ********************

    def __init__(self, show_params, root, canvas, showlist, pp_dir, pp_home,
                 pp_profile):
        """ canvas - the canvas that the tracks of the event show are to be written on
            show_params - the name of the configuration dictionary section for the hyperlinkshow
            showlist  - the showlist, to enable runningnof show type tracks.
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show_params = show_params
        self.root = root
        self.showlist = showlist
        self.canvas = canvas
        self.pp_dir = pp_dir
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        #create a path stack
        self.path = PathManager()

        # init variables
        self.drawn = None
        self.player = None
        self.shower = None
        self.timeout_running = None
        self.error = False

    def play(self,
             show_id,
             end_callback,
             ready_callback,
             top=False,
             command='nil'):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              ready_callback - callback when event-show is ready to display its forst track (not used?)
              top is True when the show is top level (run from [start] or from show control)
              command is not used
        """

        #instantiate arguments
        self.show_id = show_id
        self.end_callback = end_callback
        self.ready_callback = ready_callback
        self.top = top
        self.command = command

        # check data files are available.
        self.medialist_file = self.pp_profile + "/" + self.show_params[
            'medialist']
        if not os.path.exists(self.medialist_file):
            self.mon.err(self,
                         "Medialist file not found: " + self.medialist_file)
            self.end('error', "Medialist file not found")

        #create a medialist object for the hyperlinkshow and read the file into it.
        self.medialist = MediaList()
        if self.medialist.open_list(self.medialist_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self.end('error', "Version of medialist different to Pi Presents")

        #get controls for this show if top level
        controlsmanager = ControlsManager()
        if self.top == True:
            self.controls_list = controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list = controlsmanager.merge_show_controls(
                self.controls_list, self.show_params['controls'])

        # read show  links and destinations
        self.first_track_ref = self.show_params['first-track-ref']
        self.home_track_ref = self.show_params['home-track-ref']
        self.timeout_track_ref = self.show_params['timeout-track-ref']

        # state variables and signals
        self.end_hyperlinkshow_signal = False
        self.egg_timer = None
        self.next_track_signal = False
        self.next_track_ref = ''
        self.current_track_ref = ''
        self.current_track_type = ''

        # ready callback for show
        if self.ready_callback <> None:
            self.ready_callback()

        self.canvas.delete('pp-content')
        self.canvas.config(bg='black')

        self.do_first_track()

#stop received from another concurrent show via ShowManager

    def managed_stop(self):
        # set signal to stop the hyperlinkshow when all  sub-shows and players have ended
        self.end_hyperlinkshow_signal = True
        # then stop and shows or tracks.
        if self.shower <> None:
            self.shower.managed_stop()
        elif self.player <> None:
            self.player.input_pressed('stop')
        else:
            self.end('normal', 'stopped by ShowManager')

    # kill or error
    def terminate(self, reason):
        self.end_hyperlinkshow_signal = True
        if self.shower <> None:
            self.shower.terminate(reason)
        elif self.player <> None:
            self.player.terminate(reason)
        else:
            self.end(reason, 'terminated without terminating shower or player')

# respond to inputs

    def input_pressed(self, symbol, edge, source):

        self.mon.log(self, "received symbol: " + symbol)

        #does the symbol match a link, if so execute it
        if self.is_link(symbol, edge, source) == True:
            return

        # controls are disabled so ignore anything else
        if self.show_params['disable-controls'] == 'yes':
            return

        # does it match a control
        # if at top convert symbolic name to operation otherwise lower down we have received an operatio
        # look through list of controls to find match
        if self.top == True:
            operation = self.lookup_control(symbol, self.controls_list)
        else:
            operation = symbol
        # print 'operation',operation
        if operation <> '':
            self.do_operation(operation, edge, source)

    def do_operation(self, operation, edge, source):
        if self.shower <> None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation, edge, source)
        else:
            # control this show and its tracks
            if operation == 'stop':
                if self.player <> None:
                    if self.current_track_ref == self.first_track_ref and self.top == False:
                        self.end_radiobuttonshow_signal = True
                    self.player.input_pressed('stop')

            elif operation == 'pause':
                if self.player <> None:
                    self.player.input_pressed(operation)

            elif operation[0:4] == 'omx-' or operation[
                    0:6] == 'mplay-' or operation[0:5] == 'uzbl-':
                if self.player <> None:
                    self.player.input_pressed(operation)

    def is_link(self, symbol, edge, source):
        # find the first entry in links that matches the symbol and execute its operation
        # print 'hyperlinkshow ',symbol
        found = False
        for link in self.links:
            #print link
            if symbol == link[0]:
                found = True
                if link[1] <> 'null':
                    # print 'match',link[0]
                    link_operation = link[1]
                    if link_operation == 'home':
                        self.do_home(edge, source)
                    elif link_operation == 'return':
                        self.do_return(link[2], edge, source)
                    elif link_operation == 'call':
                        self.do_call(link[2], edge, source)
                    elif link_operation == 'goto':
                        self.do_goto(link[2], edge, source)
                    elif link_operation == 'jump':
                        self.do_jump(link[2], edge, source)
                    elif link_operation == 'exit':
                        self.end('normal', 'executed exit command')
        return found

    def lookup_control(self, symbol, controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************

    def timeout_callback(self):
        self.do_call(self.timeout_track_ref, 'front', 'timeout')

    def do_call(self, track_ref, edge, source):
        if track_ref <> self.current_track_ref:
            self.mon.log(self, 'call: ' + track_ref)
            self.next_track_signal = True
            self.next_track_op = 'call'
            self.next_track_arg = track_ref
            if self.shower <> None:
                self.shower.input_pressed('stop', edge, source)
            elif self.player <> None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_goto(self, to, edge, source):
        if to <> self.current_track_ref:
            self.mon.log(self, 'goto: ' + to)
            self.next_track_signal = True
            self.next_track_op = 'goto'
            self.next_track_arg = to
            if self.shower <> None:
                self.shower.input_pressed('stop', edge, source)
            elif self.player <> None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_jump(self, to, edge, source):
        if to <> self.current_track_ref:
            self.mon.log(self, 'jump to: ' + to)
            self.next_track_signal = True
            self.next_track_op = 'jump'
            self.next_track_arg = to
            if self.shower <> None:
                self.shower.input_pressed('stop', edge, source)
            elif self.player <> None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_return(self, to, edge, source):
        self.next_track_signal = True
        if to.isdigit() or to == '':
            self.mon.log(self, 'hyperlink command - return by: ' + to)
            self.next_track_op = 'return-by'
            if to == '':
                self.next_track_arg = '1'
            else:
                self.next_track_arg = to
        else:
            self.mon.log(self, 'hyperlink command - return to: ' + to)
            self.next_track_op = 'return-to'
            self.next_track_arg = to
        if self.shower <> None:
            self.shower.input_pressed('stop', edge, source)
        elif self.player <> None:
            self.player.input_pressed('stop')
        else:
            self.what_next()

    def do_home(self, edge, source):
        if self.current_track_ref <> self.home_track_ref:
            self.mon.log(self, 'hyperlink command - home')
            self.next_track_signal = True
            self.next_track_op = 'home'
            if self.shower <> None:
                self.shower.input_pressed('stop', edge, source)
            elif self.player <> None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_first_track(self):
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >= 0:
            #don't use select the track as not using selected_track in hyperlinkshow
            first_track = self.medialist.track(index)
            self.current_track_ref = first_track['track-ref']
            self.path.append(first_track['track-ref'])
            self.play_selected_track(first_track)
        else:
            self.mon.err(
                self, "first-track not found in medialist: " +
                self.show_params['first-track-ref'])
            self.end('error', "first track not found in medialist")

    def what_next(self):
        # user wants to end the show
        if self.end_hyperlinkshow_signal == True:
            self.end_hyperlinkshow_signal = False
            self.end('normal', "show ended by user")

        # user has selected another track
        elif self.next_track_signal == True:
            self.next_track_signal = False

            # home
            if self.next_track_op in ('home'):
                # back to 1 before home
                back_ref = self.path.back_to(self.home_track_ref)
                if back_ref == '':
                    self.mon.err(
                        self, "home - home track not in path: " +
                        self.home_track_ref)
                    self.end('error', "home - home track not in path")
                # play home
                self.next_track_ref = self.home_track_ref
                self.path.append(self.next_track_ref)

            # return-by
            elif self.next_track_op in ('return-by'):
                if self.current_track_ref <> self.home_track_ref:
                    # back n stopping at home
                    # back one more and return it
                    back_ref = self.path.back_by(self.home_track_ref,
                                                 self.next_track_arg)
                    # use returned track
                    self.next_track_ref = back_ref
                    self.path.append(self.next_track_ref)

            # return-to
            elif self.next_track_op in ('return-to'):
                #back to one before return-to track
                back_ref = self.path.back_to(self.next_track_arg)
                if back_ref == '':
                    self.mon.err(
                        self, "return-to - track not in path: " +
                        self.next_track_arg)
                    self.end('error', "return-to - track not in path")
                # and append the return to track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_ref)

            # call
            elif self.next_track_op in ('call'):
                # append the required track
                self.path.append(self.next_track_arg)
                self.next_track_ref = self.next_track_arg

            # goto
            elif self.next_track_op in ('goto'):
                self.path.pop_for_sibling()
                ##                    #back to first track and remove it
                ##                    back_ref=self.path.back_to(self.first_track_ref)
                ##                    if back_ref=='':
                ##                        self.mon.err(self,"goto - first track not in path: "+self.first_track_ref)
                ##                        self.end('error',"goto - first track not in path")
                #add the goto track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_arg)

            # jump
            elif self.next_track_op in ('jump'):
                # back to home and remove it
                back_ref = self.path.back_to(self.home_track_ref)
                if back_ref == '':
                    self.mon.err(
                        self, "jump - home track not in path: " +
                        self.home_track_ref)
                    self.end('error', "jump - track not in path")
                # add back the home track without playing it
                self.path.append(self.home_track_ref)
                # append the jumped to track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_ref)

            else:
                self.mon.err(
                    self, "unaddressed what next: " + self.next_track_op +
                    ' ' + self.next_track_arg)
                self.end('error', "unaddressed what next")

            self.current_track_ref = self.next_track_ref
            index = self.medialist.index_of_track(self.next_track_ref)
            if index >= 0:
                #don't use select the track as not using selected_track in hyperlinkshow
                next_track = self.medialist.track(index)
                self.play_selected_track(next_track)
            else:
                self.mon.err(
                    self, "next-track not found in medialist: " +
                    self.next_track_ref)
                self.end('error', "next track not found in medialist")

        else:
            #track ends naturally
            #then input pp-onend symbolic name
            self.input_pressed('pp-onend', 'front', 'key')

# *********************
# Dispatching to Players
# *********************

    def page_callback(self):
        # called from a Player when ready to play, merge the links from the track with those from the show
        # and then enable the click areas
        self.delete_eggtimer()
        links_text = self.player.get_links()
        reason, message, track_links = self.path.parse_links(links_text)
        if reason == 'error':
            self.mon.err(self, message + " in page")
            self.end('error', message)
        self.path.merge_links(self.links, track_links)

        # enable the click-area that are in the list of links
        self.enable_click_areas()

    def enable_click_areas(self):
        for link in self.links:
            #print 'trying link ',link[0]
            if not ('key-'
                    in link[0]) and link[1] <> 'null' and link[0] <> 'pp-auto':
                # print 'enabling link ',link[0]
                self.canvas.itemconfig(link[0], state='normal')

    def play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """

        if self.timeout_running <> None:
            self.canvas.after_cancel(self.timeout_running)
            self.timeout_running = None

        # self.canvas.delete(ALL)
        self.display_eggtimer(self.resource('hyperlinkshow', 'm01'))

        self.current_track_type = selected_track['type']

        #read the show links. Track links will be added by ready_callback
        links_text = self.show_params['links']
        reason, message, self.links = self.path.parse_links(links_text)
        if reason == 'error':
            self.mon.err(self, message + " in show")
            self.end('error', message)

        #start timeout for the track if required

        if self.current_track_ref <> self.first_track_ref and int(
                self.show_params['timeout']) <> 0:
            self.timeout_running = self.canvas.after(
                int(self.show_params['timeout']) * 1000, self.timeout_callback)

        # dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "audio":
            # create a audioplayer
            track_file = self.complete_path(selected_track)
            self.player = AudioPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "image":
            track_file = self.complete_path(selected_track)
            self.player = ImagePlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(
                track_file,
                self.showlist,
                self.end_player,
                self.page_callback,
                enable_menu=False,
            )

        elif track_type == "web":
            # create a browser
            track_file = self.complete_path(selected_track)
            self.player = BrowserPlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track['text']
            self.player = MessagePlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(text,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "show":
            self.enable_click_areas()
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self.end("Unknown show")

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.root, self.canvas,
                                        self.showlist, self.pp_dir,
                                        self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "liveshow":
                self.shower = LiveShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "radiobuttonshow":
                self.shower = RadioButtonShow(selected_show, self.root,
                                              self.canvas, self.showlist,
                                              self.pp_dir, self.pp_home,
                                              self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "hyperlinkshow":
                self.shower = HyperlinkShow(selected_show, self.root,
                                            self.canvas, self.showlist, self,
                                            pp_dir, self.pp_home,
                                            self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')
            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self.end("Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self.end("Unknown track type")

    # callback from when player ends
    def end_player(self, reason, message):
        self.mon.log(self, "Returned from player with message: " + message)
        self.player = None
        # this does not seem to change the colour of the polygon
        self.canvas.itemconfig('pp-click-area', state='hidden')
        self.canvas.update_idletasks()
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            self.display_eggtimer(self.resource('hyperlinkshow', 'm02'))
            self.what_next()

    # callback from when shower ends
    def end_shower(self, show_id, reason, message):
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        self.canvas.itemconfig('pp-click-area', state='hidden')
        self.canvas.update_idletasks()
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            self.display_eggtimer(self.resource('hyperlinkshow', 'm03'))
            self.what_next()

# *********************
# End the show
# *********************
# finish the player for killing, error or normally
# this may be called directly sub/child shows or players are not running
# if they might be running then need to call terminate.

    def end(self, reason, message):
        self.mon.log(self,
                     "Ending hyperlinkshow: " + self.show_params['show-ref'])
        self.end_callback(self.show_id, reason, message)
        self = None
        return

# *********************
# displaying things
# *********************

    def display_eggtimer(self, text):
        #self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
        #int(self.canvas['height'])/2,
        #text= text,
        # fill='white',
        # font="Helvetica 20 bold")
        #self.canvas.update_idletasks( )
        pass

    def delete_eggtimer(self):
        if self.egg_timer != None:
            self.canvas.delete(self.egg_timer)


# *********************
# utilities
# *********************

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file <> '' and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            # players or showers may be running so need terminate
            self.terminate("error")
        else:
            return value
Пример #8
0
class MediaShow:


# *******************
# External interface
# ********************

    def __init__(self,
                            show,
                            canvas,
                            showlist,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the dictionary fo the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show =show
        self.showlist=showlist
        self.canvas=canvas
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

        # Init variables
        self.player=None
        self.shower=None
        self._poll_for_interval_timer=None
        self._poll_for_continue_timer=None
        self._waiting_for_interval=False
        self._interval_timer=None
        self.error=False
        
        self._interval_timer_signal=False
        self._end_mediashow_signal=False
        self._next_track_signal=False
        self._previous_track_signal=False
        self._play_child_signal = False
        self._req_next='nil'

        self._state='closed'


    def play(self,end_callback,ready_callback=None, top=False,command='nil'):

        """ displays the mediashow
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """

        #instantiate the arguments
        self._end_callback=end_callback
        self._ready_callback=ready_callback
        self.top=top
        self.command=command
        self.mon.log(self,"Starting show: " + self.show['show-ref'])

        # check  data files are available.
        self.media_file = self.pp_profile + "/" + self.show['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self,"Medialist file not found: "+ self.media_file)
            self._end('error',"Medialist file not found")


        #create a medialist for the mediashow and read it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.media_file,self.showlist.sissue())==False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self._end('error',"Version of medialist different to Pi Presents")

        self._wait_for_trigger()


   # respond to key presses.
    def key_pressed(self,key_name):
        self.mon.log(self,"received key: " + key_name)
        
        if key_name=='':
            pass
        
        elif key_name=='escape':
            # if next lower show is running pass down to stop the show and lower level
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            # if not at top stop the show
            else:
                if self.top == False:
                    self._end_mediashow_signal=True
                    # and if a track is running stop that first
                    if self.player<>None:
                        self.player.key_pressed(key_name)
                else:
                    # at top level in a manual presentation stop the track
                    if self.show['progress']=='manual':
                        if self.player<>None:
                            self.player.key_pressed(key_name)
    
        elif key_name in ('up','down'):
        # if child or sub-show is running and is a show pass to show, track does not use up/down
        # otherwise use keys for next or previous
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if key_name=='up':
                    self._previous()
                else:
                    self._next()
                
        elif key_name=='return':
            # if child show or sub-show is running and is show - pass down- player does not use return
            # ELSE use Return to start child or to start the show if waiting
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self._state=='playing':
                    if self.show['has-child']=='yes':
                        self._play_child_signal=True
                        # and stop the current track if its running
                        if self.player<>None:
                            self.player.key_pressed("escape")
                else:
                    self._start_show()

        elif key_name=='pir':
                self._start_show()
                
        elif key_name in ('p',' '):
            # pass down if show or track running.
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
 

    def button_pressed(self,button,edge):
        if button=='play': self.key_pressed("return")
        elif  button =='up': self.key_pressed("up")
        elif button=='down': self.key_pressed("down")
        elif button=='stop': self.key_pressed("escape")
        elif button=='pause': self.key_pressed('p')
        elif button=='PIR': self.key_pressed('pir')


    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.mon.log(self,"sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player<>None:
            self.mon.log(self,"sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,'terminated without terminating shower or player')

 
    def _tidy_up(self):
        if self._poll_for_continue_timer<>None:
                self.canvas.after_cancel(self._poll_for_continue_timer)
                self._poll_for_continue_timer=None
        if self._poll_for_interval_timer<>None:
                self.canvas.after_cancel(self._poll_for_interval_timer)
                self._poll_for_interval_timer=None
        if self._interval_timer<>None:
            self.canvas.after_cancel(self._interval_timer)
            self._interval_timer=None

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            self.terminate("error")
        else:
            return value

# ***************************
# Respond to key/button presses
# ***************************

    def _stop(self,message):
        self._end_mediashow_signal=True
        if self._interval_timer<>None:
            self.canvas.after_cancel(self._interval_timer)

   
    def _next(self):
        # stop track if running and set signal
        self._next_track_signal=True
        if self.shower<>None:
            self.shower.key_pressed("escape")
        else:
            if self.player<>None:
                self.player.key_pressed("escape")

    def _previous(self):
        self._previous_track_signal=True
        if self.shower<>None:
            self.shower.key_pressed("escape")
        else:
            if self.player<>None:
                self.player.key_pressed("escape")
     
        
# ***************************
# end of show functions
# ***************************

    def _end(self,reason,message):
        self._end_mediashow_signal=False
        self.mon.log(self,"Ending Mediashow: "+ self.show['show-ref'])
        self._tidy_up()
        self._end_callback(reason,message)
        self=None
        return
        



# ***************************
# Show sequencer
# ***************************
 
    def _wait_for_trigger(self):
        self._state='waiting'
        if self.ready_callback<>None:
            self.ready_callback()

        self.mon.log(self,"Waiting for trigger: "+ self.show['trigger'])
        
        if self.show['trigger']=="button":
            # blank screen waiting for trigger if auto, otherwise display something
            if self.show['progress']=="manual":
                text= self.resource('mediashow','m01')
            else:
                text=""
            self.display_message(self.canvas,'text',text,0,self._start_show)


        elif self.show['trigger']=="PIR":
            # blank screen waiting for trigger
            text = self.resource('mediashow','m02')
            self.display_message(self.canvas,'text',text,0,self._start_show)      
            
        elif self.show['trigger']=="start":
            self._start_show()
            
        else:
            self.mon.err(self,"Unknown trigger: "+ self.show['trigger'])
            self._end('error',"Unknown trigger type")
  
        

    def _start_show(self):
        self._state='playing'
        self._direction='forward'
        # start interval timer
        if self.show['repeat']=="interval" and self.show['repeat-interval']<>0:
            self._interval_timer_signal=False
            self._interval_timer=self.canvas.after(int(self.show['repeat-interval'])*1000,self._end_interval_timer)
        # and play the first track unless commanded otherwise
        if self.command=='backward':
            self.medialist.finish()
        else:
            self.medialist.start()
        self._play_selected_track(self.medialist.selected_track())
 
 
    def _what_next(self):
        self._direction='forward'
        
        # user wants to end, wait for any shows or tracks to have ended then end show
        if self._end_mediashow_signal==True:
            if self.player==None and self.shower==None:
                self._end_mediashow_signal=False
                self._end('normal',"show ended by user")
            else:
                pass
            
        #returning from a subshow needing to move onward 
        elif self._req_next=='do-next':
            self._req_next='nil'
            self.medialist.next()
            self._play_selected_track(self.medialist.selected_track())
            
        #returning from a subshow needing to move backward 
        elif self._req_next=='do-previous':
            self._req_next='nil'
            self._direction='backward'
            self.medialist.previous()
            self._play_selected_track(self.medialist.selected_track())         
               
        # user wants to play child
        elif self._play_child_signal == True:
            self._play_child_signal=False
            index = self.medialist.index_of_track('pp-child-show')
            if index >=0:
                #don't select the track as need to preserve mediashow sequence.
                child_track=self.medialist.track(index)
                self._display_eggtimer(self.resource('mediashow','m07'))
                self._play_selected_track(child_track)
            else:
                self.mon.err(self,"Child show not found in medialist: "+ self.show['pp-child-show'])
                self._end('error',"child show not found in medialist")
        
        # skip to next track on user input
        elif self._next_track_signal==True:
            self._next_track_signal=False
            if self.medialist.at_end()==True:
                if  self.show['sequence']=="ordered" and self.show['repeat']=='oneshot' and self.top==False:
                    self._end('do-next',"Return from Sub Show")
                else:
                    self.medialist.next()
                    self._play_selected_track(self.medialist.selected_track())               
            else:
                self.medialist.next()
                self._play_selected_track(self.medialist.selected_track())
                
        # skip to previous track on user input
        elif self._previous_track_signal==True:
            self._previous_track_signal=False
            self._direction='backward'
            if self.medialist.at_start()==True:
                if  self.show['sequence']=="ordered" and self.show['repeat']=='oneshot' and self.top==False:
                    self._end('do-previous',"Return from Sub Show")
                else:
                    self.medialist.previous()
                    self._play_selected_track(self.medialist.selected_track())               
            else:
                self.medialist.previous()              
                self._play_selected_track(self.medialist.selected_track())
        

        # track is finished and we are on auto        
        elif self.show['progress']=="auto":
            if self.medialist.at_end()==True:
                if self.show['sequence']=="ordered" and self.show['repeat']=='oneshot' and self.top==False:
                    self._end('do-next',"Return from Sub Show")
                    
                #### elif    
                elif self.show['sequence']=="ordered" and self.show['repeat']=='oneshot' and self.top==True:
                    self._wait_for_trigger()

                elif self._waiting_for_interval==True:
                    if self._interval_timer_signal==True:
                        self._interval_timer_signal=False
                        self._waiting_for_interval=False
                        self._start_show()
                    else:
                        self._poll_for_interval_timer=self.canvas.after(1000,self._what_next)
 
                elif self.show['sequence']=="ordered" and self.show['repeat']=='interval' and int(self.show['repeat-interval'])>0:
                    self._waiting_for_interval=True
                    self._poll_for_interval_timer=self.canvas.after(1000,self._what_next) 
                    
                elif self.show['sequence']=="ordered" and self.show['repeat']=='interval' and int(self.show['repeat-interval'])==0:
                    self.medialist.next()
                    self._play_selected_track(self.medialist.selected_track())
                           
                else:
                    self.mon.err(self,"Unhandled playing event: ")
                    self._end('error',"Unhandled playing event")
                    
            else:
                self.medialist.next()
                self._play_selected_track(self.medialist.selected_track())
                    
        # track has finished and we are on manual progress               
        elif self.show['progress']=="manual":
                    self._delete_eggtimer()
                    self._display_eggtimer(self.resource('mediashow','m03'))
                    self._poll_for_continue_timer=self.canvas.after(500,self._what_next)
                    
        else:
            #unhandled state
            self.mon.err(self,"Unhandled playing event: ")
            self._end('error',"Unhandled playing event")           



    def _end_interval_timer(self):
        self._interval_timer_signal=True
 

        

# ***************************
# Dispatching to Players/Shows 
# ***************************

    # used to display internal messages in situations where a medialist entry could be used.
    def display_message(self,canvas,source,content,duration,_display_message_callback):
            self._display_message_callback=_display_message_callback
            tp={'duration':duration,'message-colour':'white','message-font':'Helvetica 20 bold'}
            self.player=MessagePlayer(canvas,tp,tp)
            self.player.play(content,self._display_message_end,None)

    def   _display_message_end(self,reason,message):
        self.player=None
        if reason in ('error','killed'):
            self._end(reason,message)
        else:
            self._display_message_callback()


    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     
         
        


    def _play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
        self.canvas.delete(ALL)
        if self.show['progress']=="manual":
            self._display_eggtimer(self.resource('mediashow','m04'))

        # is menu required
        if self.show['has-child']=="yes":
            enable_child=True
        else:
            enable_child=False

        #dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                        self.end_player,
                                        self.ready_callback,
                                        enable_menu=enable_child)
                                        
        elif track_type=="image":
            track_file=self.complete_path(selected_track)
            # images played from menus don't have children
            self.player=ImagePlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=enable_child)
                                    
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.canvas,self.show,selected_track)
            self.player.play(text,
                                    self.end_player,
                                    self.ready_callback,
                                    enable_menu=enable_child
                                    )
         
 
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self._end('error',"Unknown show")
                
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.end_shower,top=False,command=self._direction)

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.end_shower,top=False,command='nil')
            
            elif selected_show['type']=="menu":
                self.shower= MenuShow(selected_show,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.end_shower,top=False,command='nil')
                
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self._end('error'"Unknown show type")  
            
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self._end('error',"Unknown track type")            


    def ready_callback(self):
        self._delete_eggtimer()
        
        
    def end_player(self,reason,message):
        self._req_next='nil'
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self._end(reason,message)
        elif self.show['progress']=="manual":
            self._display_eggtimer(self.resource('mediashow','m05'))
            self._req_next=reason
            self._what_next()
        else:
            self._req_next=reason
            self._what_next()

    def end_shower(self,reason,message):
        self._req_next='nil'
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if reason in("killed","error"):
            self._end(reason,message)
        elif self.show['progress']=="manual":
            self._display_eggtimer(self.resource('mediashow','m06'))
            self._req_next=reason
            self._what_next() 
        else:
            self._req_next=reason
            self._what_next() 
        
        
    def _display_eggtimer(self,text):
        self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold")
        self.canvas.update_idletasks( )


    def _delete_eggtimer(self):
            self.canvas.delete(ALL)
Пример #9
0
class MenuShow:
    """ Displays a menu with optional hint below it. User can traverse the menu and
              select a track using key or button presses.
        Interface:
         * play - displays the menu and selects the first entry
         * key_pressed, button_pressed - receives user events passes them to a Player if a track is playing,
                otherwise actions them with _next, _previous, _play_selected_track, _end
         Optional display of eggtimer by means of Players ready_callback
         Supports imageplayer, videoplayer,messagplayer,audioplayer,menushow,mediashow
         Destroys itself on exit
    """

# *********************
# external interface
# ********************

    def __init__(self,
                            show_params,
                            canvas,
                            showlist,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the name of the configuration dictionary section for the menu
            showlist  - the showlist
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory"""
        
        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show_params=show_params
        self.showlist=showlist
        self.canvas=canvas
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()
        
        # init variables
        self.drawn  = None
        self.player=None
        self.shower=None
        self.menu_timeout_running=None
        self.error=False



    def play(self,show_id,end_callback,ready_callback=None,top=False,command='nil'):
        """ displays the menu 
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """
        
        #instantiate arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.command=command

        # check  data files are available.
        self.menu_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.menu_file):
            self.mon.err(self,"Medialist file not found: "+ self.menu_file)
            self._end('error',"Medialist file not found")
        
        #create a medialist for the menu and read it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.menu_file,self.showlist.sissue()) == False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self._end('error',"Version of medialist different to Pi Presents")
           
        if self.show_params['has-background']=="yes":
            background_index=self.medialist.index_of_track ('pp-menu-background')
            if background_index>=0:
                self.menu_img_file = self.complete_path(self.medialist.track(background_index))
                if not os.path.exists(self.menu_img_file):
                    self.mon.err(self,"Menu background file not found: "+ self.menu_img_file)
                    self._end('error',"Menu background file not found")
            else:
                self.mon.err(self,"Menu background not found in medialist")
                self._end('error',"Menu background not found")

        self.egg_timer=None

        #start timeout alarm if required
        if int(self.show_params['timeout'])<>0:
            self.menu_timeout_running=self.canvas.after(int(self.show_params['timeout'])*1000,self._timeout_menu)
        
        if self.ready_callback<>None:
            self.ready_callback()
        
        self.canvas.delete(ALL)
        
        # display background image
        if self.show_params['has-background']=="yes":
            self._display_background()
 
       #display the list of video titles
        self._display_video_titles()

        # display instructions (hint)
        self.canvas.create_text(int(self.canvas['width'])/2,
                                int(self.canvas['height']) - int(self.show_params['hint-y']),
                                text=self.show_params['hint-text'],
                                fill=self.show_params['hint-colour'],
                                font=self.show_params['hint-font'])
        self.canvas.update( )



   # respond to key presses.
    def key_pressed(self,key_name):
        self.mon.log(self,"received key: " + key_name)
        if self.show_params['disable-controls']=='yes':
            return 
        if key_name=='':
            pass
        
        elif key_name=='escape':
            # if next lower show eor player is running pass down to stop bottom level
            # ELSE stop this show if not at top
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
            else:
                # not at top so stop the show
                if  self.top == False:
                    self._end('normal',"exit from stop command")
                else:
                    pass
      
        elif key_name in ('up','down'):
        # if child or sub-show running and is a show pass down
        # if  child not running - move
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self.player==None:
                    if key_name=='up':
                        self._previous()
                    else:
                        self._next()
                
        elif key_name=='return':
            # if child running and is show - pass down
            # if no track already running  - play
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self.player==None:
                    self._play_selected_track(self.medialist.selected_track())

        elif key_name in ('p',' '):
            # pass down if show or track running.
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
 
 
    def button_pressed(self,button,edge):
        if button=='play': self.key_pressed("return")
        elif  button =='up': self.key_pressed("up")
        elif button=='down': self.key_pressed("down")
        elif button=='stop': self.key_pressed("escape")
        elif button=='pause': self.key_pressed('p')

        
    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.mon.log(self,"sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player<>None:
            self.mon.log(self,"sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,'terminated without terminating shower or player')



# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# language resources
# *********************

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            # timers may be running so need terminate
            self.terminate("error")
        else:
            return value


# *********************
# Sequencing
# *********************

    def _timeout_menu(self):
        self._end('normal','menu timeout')
        return
        
    
    # finish the player for killing, error or normally
    # this may be called directly sub/child shows or players are not running
    # if they might be running then need to call terminate.
    
    def _end(self,reason,message):
        # self.canvas.delete(ALL)
        # self.canvas.update_idletasks( )
        self.mon.log(self,"Ending menushow: "+ self.show_params['show-ref'])  
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        self.end_callback(self.show_id,reason,message)
        self=None
        return


    def _next(self):     
        self._highlight_menu_entry(self.menu_index,False)
        self.medialist.next('ordered')
        if self.menu_index==self.menu_length-1:
            self.menu_index=0
        else:
            self.menu_index+=1
        self._highlight_menu_entry(self.menu_index,True)     


    def _previous(self):   
        self._highlight_menu_entry(self.menu_index,False)
        if self.menu_index==0:
            self.menu_index=self.menu_length-1
        else:
            self.menu_index-=1
        self.medialist.previous('ordered')
        self._highlight_menu_entry(self.menu_index,True)    


# *********************
# Dispatching to Players
# *********************

    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     
         


    def _play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
         #remove menu and show working.....        

        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        self.canvas.delete(ALL)
        self._display_eggtimer(self.resource('menushow','m01'))
    
        # dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.show_id,self.canvas,self.pp_home,self.show_params,selected_track)
            self.player.play(track_file,
                                        self._end_player,
                                        self._delete_eggtimer,
                                        enable_menu=False)
                                        
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track)
            self.player=AudioPlayer(self.show_id,self.canvas,self.pp_home,self.show_params,selected_track)
            self.player.play(track_file,
                                        self._end_player,
                                        self._delete_eggtimer,
                                        enable_menu=False)
                                        
        elif track_type=="image":
            # images played from menus don't have children
            track_file=self.complete_path(selected_track)
            self.player=ImagePlayer(self.show_id,self.canvas,self.pp_home,self.show_params,selected_track)
            self.player.play(track_file,
                                    self._end_player,
                                    self._delete_eggtimer,
                                    enable_menu=False,
                                    )
                                    
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.canvas,self.pp_home,self.show_params,selected_track)
            self.player.play(text,
                                    self._end_player,
                                    self._delete_eggtimer,
                                    enable_menu=False
                                    )
 
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self._end("Unknown show")
            
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self._end_shower,top=False,command='nil')

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,top=False,command='nil')

            elif selected_show['type']=="menu": 
                self.shower= MenuShow(selected_show,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self._end_shower,top=False,command='nil')                    
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self._end("Unknown show type")  
                
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self._end("Unknown track type")
    
    # callback from when player ends
    def _end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self._end(reason,message)
        self._display_eggtimer(self.resource('menushow','m02'))
        self._what_next(message)

    # callback from when shower ends
    def _end_shower(self,show_id,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if message in ("killed","error"):
            self._end(reason,message)
        self._display_eggtimer(self.resource('menushow','m03'))
        self._what_next(message)  
   

     # at the end of a track just re-display the menu with the original callback from the menu       
    def _what_next(self,message):
        self.mon.log(self,"Re-displaying menu")
        self.play(self.show_id,self.end_callback,top=self.top)



# *********************
# Displaying things
# *********************

    def _display_background(self):
        pil_menu_img=PIL.Image.open(self.menu_img_file)
        # adjust brightness and rotate (experimental)
        # enh=PIL.ImageEnhance.Brightness(pil_menu_img)
        # pil_menu_img=enh.enhance(0.1)
        # pil_menu_img=pil_menu_img.rotate(45)
        self.menu_background = PIL.ImageTk.PhotoImage(pil_menu_img)
        self.drawn = self.canvas.create_image(int(self.canvas['width'])/2,
                                      int(self.canvas['height'])/2,
                                      image=self.menu_background,
                                      anchor=CENTER)


    def _display_video_titles(self):
        self.menu_length=1
        self.menu_entry_id=[]
        x=int(self.show_params['menu-x'])
        y=int(self.show_params['menu-y'])
        self.medialist.start()
        while True:
            id=self.canvas.create_text(x,y,anchor=NW,
                                       text="* "+self.medialist.selected_track()['title'],
                                       fill=self.show_params['entry-colour'],
                                       font=self.show_params['entry-font'])
            self.menu_entry_id.append(id)
            y=y + int(self.show_params['menu-spacing'])
            if self.medialist.at_end():
                break
            self.menu_length+=1
            self.medialist.next('ordered')
            
        # select and highlight the first entry
        self.medialist.start()
        self.menu_index=0
        self._highlight_menu_entry(self.menu_index,True)
        # self.medialist.print_list()

    def _highlight_menu_entry(self,index,state):
        if state==True:
            self.canvas.itemconfig(self.menu_entry_id[index],fill=self.show_params['entry-select-colour'])
        else:
            self.canvas.itemconfig(self.menu_entry_id[index],fill=self.show_params['entry-colour'])
    
    
    def _display_eggtimer(self,text):
        self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold")
        self.canvas.update_idletasks( )


    def _delete_eggtimer(self):
        if self.egg_timer!=None:
            self.canvas.delete(self.egg_timer)
Пример #10
0
class LiveShow:
    """ plays a set of tracks the content of which is dynamically specified by plaacing track files
                in one of two directories. Tracks are played in file leafname alphabetical order.
                Can be interrupted
    """

    # *******************
    # External interface
    # ********************

    def __init__(self, show_params, root, canvas, showlist, pp_dir, pp_home,
                 pp_profile):

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show_params = show_params
        self.showlist = showlist
        self.root = root
        self.canvas = canvas
        self.pp_dir = pp_dir
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        #create and instance of TimeOfDay scheduler so we can add events
        self.tod = TimeOfDay()

        # Init variables
        self.player = None
        self.shower = None
        self.end_liveshow_signal = False
        self.end_trigger_signal = False
        self.play_child_signal = False
        self.error = False
        self.egg_timer = None
        self.duration_timer = None
        self.state = 'closed'
        self.livelist = None
        self.new_livelist = None

    def play(self,
             show_id,
             end_callback,
             ready_callback,
             top=False,
             command='nil'):

        #instantiate the arguments
        self.show_id = show_id
        self.end_callback = end_callback
        self.ready_callback = ready_callback
        self.top = top
        self.mon.log(self, "Starting show: " + self.show_params['show-ref'])

        # check  data files are available.
        self.media_file = self.pp_profile + os.sep + self.show_params[
            'medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self, "Medialist file not found: " + self.media_file)
            self.end_liveshow_signal = True

        self.options = command_options()

        self.pp_live_dir1 = self.pp_home + os.sep + 'pp_live_tracks'
        if not os.path.exists(self.pp_live_dir1):
            os.mkdir(self.pp_live_dir1)

        self.pp_live_dir2 = ''
        if self.options['liveshow'] <> "":
            self.pp_live_dir2 = self.options['liveshow']
            if not os.path.exists(self.pp_live_dir2):
                self.mon.err(
                    self,
                    "live tracks directory not found " + self.pp_live_dir2)
                self.end('error', "live tracks directory not found")

        #create a medialist for the liveshow and read it.
        # it should be empty of anonymous tracks but read it to check its version.
        self.medialist = MediaList()
        if self.medialist.open_list(self.media_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self.end('error', "Version of medialist different to Pi Presents")

        #get control bindings for this show if top level
        controlsmanager = ControlsManager()
        if self.top == True:
            self.controls_list = controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list = controlsmanager.merge_show_controls(
                self.controls_list, self.show_params['controls'])

        #set up the time of day triggers for the show
        if self.show_params['trigger-start'] in ('time', 'time-quiet'):
            error_text = self.tod.add_times(
                self.show_params['trigger-start-time'], id(self),
                self.tod_start_callback, self.show_params['trigger-start'])
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        if self.show_params['trigger-end'] == 'time':
            error_text = self.tod.add_times(
                self.show_params['trigger-end-time'], id(self),
                self.tod_end_callback, 'n/a')
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        if self.show_params['trigger-end'] == 'duration':
            error_text = self.calculate_duration(
                self.show_params['trigger-end-time'])
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        self.wait_for_trigger()

    def managed_stop(self):
        # if next lower show eor player is running pass down to stop the show/track
        if self.shower <> None:
            self.shower.managed_stop()
        else:
            self.end_liveshow_signal = True
            if self.player <> None:
                self.player.input_pressed('stop')

    # kill or error
    def terminate(self, reason):
        if self.shower <> None:
            self.shower.terminate(reason)
        elif self.player <> None:
            self.player.terminate(reason)
        else:
            self.end(reason, 'terminated without terminating shower or player')

# respond to key presses.

    def input_pressed(self, symbol, edge, source):
        self.mon.log(self, "received key: " + symbol)
        if self.show_params['disable-controls'] == 'yes':
            return

    # if at top convert symbolic name to operation otherwise lower down we have received an operation
    # look through list of standard symbols to find match (symbolic-name, function name) operation =lookup (symbol
        if self.top == True:
            operation = self.lookup_control(symbol, self.controls_list)
        else:
            operation = symbol
        # print 'operation',operation
        # if no match for symbol against standard operations then return
        if operation == '':
            return

        else:
            #service the standard inputs for this show
            if operation == 'stop':
                # if next lower show eor player is running pass down to stop the show/track
                # ELSE stop this show except for exceptions
                if self.shower <> None:
                    self.shower.input_pressed('stop', edge, source)
                elif self.player <> None:
                    self.player.input_pressed('stop')
                else:
                    # not at top so stop the show
                    if self.top == False:
                        self.end_liveshow_signal = True
                    else:
                        pass

            elif operation in ('up', 'down'):
                # if child or sub-show is running and is a show pass to show, track does not use up/down
                if self.shower <> None:
                    self.shower.input_pressed(operation, edge, source)

            elif operation == 'play':
                # if child show or sub-show is running and is show - pass down
                # ELSE use Return to start child
                if self.shower <> None:
                    self.shower.input_pressed(operation, edge, source)
                else:
                    if self.show_params['has-child'] == "yes":
                        self.play_child_signal = True
                        if self.player <> None:
                            self.player.input_pressed("stop")

            elif operation == 'pause':
                # pass down if show or track running.
                if self.shower <> None:
                    self.shower.input_pressed(operation, edge, source)
                elif self.player <> None:
                    self.player.input_pressed(operation)

            elif operation[0:4] == 'omx-' or operation[0:6] == 'mplay-':
                if self.player <> None:
                    self.player.input_pressed(operation)

    def lookup_control(self, symbol, controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

# ***************************
# Constructing Livelist
# ***************************

    def livelist_add_track(self, afile):
        (root, title) = os.path.split(afile)
        (root_plus, ext) = os.path.splitext(afile)
        if ext.lower() in PPdefinitions.IMAGE_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['image'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() in PPdefinitions.VIDEO_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['video'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() in PPdefinitions.AUDIO_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['audio'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() in PPdefinitions.WEB_FILES:
            self.livelist_new_track(PPdefinitions.new_tracks['web'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() == '.cfg':
            self.livelist_new_plugin(afile, title)

    def livelist_new_plugin(self, plugin_cfg, title):

        # read the file which is a plugin cfg file into a dictionary
        self.plugin_config = ConfigParser.ConfigParser()
        self.plugin_config.read(plugin_cfg)
        self.plugin_params = dict(self.plugin_config.items('plugin'))
        # create a new livelist entry of a type specified in the config file with plugin
        self.livelist_new_track(
            PPdefinitions.new_tracks[self.plugin_params['type']], {
                'title': title,
                'track-ref': '',
                'plugin': plugin_cfg,
                'location': plugin_cfg
            })

    def livelist_new_track(self, fields, values):
        new_track = fields
        self.new_livelist.append(copy.deepcopy(new_track))
        last = len(self.new_livelist) - 1
        self.new_livelist[last].update(values)

    def new_livelist_create(self):

        self.new_livelist = []
        if os.path.exists(self.pp_live_dir1):
            for file in os.listdir(self.pp_live_dir1):
                file = self.pp_live_dir1 + os.sep + file
                (root_file, ext_file) = os.path.splitext(file)
                if (ext_file.lower() in PPdefinitions.IMAGE_FILES +
                        PPdefinitions.VIDEO_FILES + PPdefinitions.AUDIO_FILES +
                        PPdefinitions.WEB_FILES) or (ext_file.lower()
                                                     == '.cfg'):
                    self.livelist_add_track(file)

        if os.path.exists(self.pp_live_dir2):
            for file in os.listdir(self.pp_live_dir2):
                file = self.pp_live_dir2 + os.sep + file
                (root_file, ext_file) = os.path.splitext(file)
                if ext_file.lower(
                ) in PPdefinitions.IMAGE_FILES + PPdefinitions.VIDEO_FILES + PPdefinitions.AUDIO_FILES + PPdefinitions.WEB_FILES or (
                        ext_file.lower() == '.cfg'):
                    self.livelist_add_track(file)

        self.new_livelist = sorted(
            self.new_livelist,
            key=lambda track: os.path.basename(track['location']).lower())
        # print 'LIVELIST'
        # for it in self.new_livelist:
        # print 'type: ', it['type'], 'loc: ',it['location'],'\nplugin cfg: ', it['plugin']
        # print ''

    def livelist_replace_if_changed(self):
        self.new_livelist_create()
        if self.new_livelist <> self.livelist:
            self.livelist = copy.deepcopy(self.new_livelist)
            self.livelist_index = 0

    def livelist_next(self):
        if self.livelist_index == len(self.livelist) - 1:
            self.livelist_index = 0
        else:
            self.livelist_index += 1

# ***************************
# Sequencing
# ***************************

    def wait_for_trigger(self):
        self.state = 'waiting'
        if self.ready_callback <> None:
            self.ready_callback()

        self.mon.log(
            self, "Waiting for trigger: " + self.show_params['trigger-start'])

        if self.show_params['trigger-start'] in ('time', 'time-quiet'):
            # if next show is this one display text
            next_show = self.tod.next_event_time()
            if next_show[3] <> True:
                if next_show[1] == 'tomorrow':
                    text = self.resource('liveshow', 'm04')
                else:
                    text = self.resource('liveshow', 'm03')
                text = text.replace('%tt', next_show[0])
                self.display_message(self.canvas, 'text', text, 0,
                                     self.play_first_track)

        elif self.show_params['trigger-start'] == "start":
            self.play_first_track()
        else:
            self.mon.err(
                self, "Unknown trigger: " + self.show_params['trigger-start'])
            self.end('error', "Unknown trigger type")

    # callbacks from time of day scheduler
    def tod_start_callback(self):
        if self.state == 'waiting' and self.show_params['trigger-start'] in (
                'time', 'time-quiet'):
            self.play_first_track()

    def tod_end_callback(self):
        if self.state == 'playing' and self.show_params['trigger-end'] in (
                'time', 'duration'):
            self.end_trigger_signal = True
            if self.shower <> None:
                self.shower.input_pressed('stop', 'front', '')
            elif self.player <> None:
                self.player.input_pressed('stop')

    def play_first_track(self):
        self.state = 'playing'
        # start duration timer
        if self.show_params['trigger-end'] == 'duration':
            # print 'set alarm ', self.duration
            self.duration_timer = self.canvas.after(self.duration * 1000,
                                                    self.tod_end_callback)
        self.new_livelist_create()
        self.livelist = copy.deepcopy(self.new_livelist)
        self.livelist_index = 0
        self.play_track()

    def play_track(self):
        self.livelist_replace_if_changed()
        if len(self.livelist) > 0:
            self.play_selected_track(self.livelist[self.livelist_index])
        else:
            self.display_message(self.canvas, None,
                                 self.resource('liveshow', 'm01'), 5,
                                 self.what_next)

    def what_next(self):
        # end of show time trigger
        if self.end_trigger_signal == True:
            self.end_trigger_signal = False
            if self.top == True:
                self.state = 'waiting'
                self.wait_for_trigger()
            else:
                # not at top so stop the show
                self.end('normal', 'sub-show end time trigger')

        # user wants to end
        elif self.end_liveshow_signal == True:
            self.end_liveshow_signal = False
            self.end('normal', "show ended by user")

        # play child?
        elif self.play_child_signal == True:
            self.play_child_signal = False
            index = self.medialist.index_of_track('pp-child-show')
            if index >= 0:
                #don't select the track as need to preserve mediashow sequence.
                child_track = self.medialist.track(index)
                self.display_eggtimer(self.resource('liveshow', 'm02'))
                self.play_selected_track(child_track)
            else:
                self.mon.err(
                    self, "Child show not found in medialist: " +
                    self.show_params['pp-child-show'])
                self.end('error', "child show not found in medialist")

        # otherwise loop to next track
        else:
            self.livelist_next()
            self.play_track()

# ***************************
# Dispatching to Players/Shows
# ***************************

    def ready_callback(self):
        self.delete_eggtimer()

    def play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected_track is a dictionary for the track/show
        """
        self.canvas.delete('pp-content')

        # is menu required
        if self.show_params['has-child'] == "yes":
            enable_child = True
        else:
            enable_child = False

        #dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "image":
            track_file = self.complete_path(selected_track)
            # images played from menus don't have children
            self.player = ImagePlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "audio":
            # create a audioplayer
            track_file = self.complete_path(selected_track)
            self.player = AudioPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track['text']
            self.player = MessagePlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(text,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "web":
            # create a browser
            track_file = self.complete_path(selected_track)
            self.player = BrowserPlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self.end_liveshow_signal = True

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.root, self.canvas,
                                        self.showlist, self.pp_dir,
                                        self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "radiobuttonshow":
                self.shower = RadioButtonShow(selected_show, self.root,
                                              self.canvas, self.showlist,
                                              self.pp_dir, self.pp_home,
                                              self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "hyperlinkshow":
                self.shower = HyperlinkShow(selected_show, sef.root,
                                            self.canvas, self.showlist,
                                            self.pp_dir, self.pp_home,
                                            self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self.end_liveshow_signal = True

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self.end_liveshow_signal = True

    def end_shower(self, show_id, reason, message):
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            self.what_next()

    def end_player(self, reason, message):
        self.mon.log(self, "Returned from player with message: " + message)
        self.player = None
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            self.what_next()

# ***************************
# end of show
# ***************************

    def end(self, reason, message):
        self.end_liveshow_signal = False
        self.mon.log(self, "Ending Liveshow: " + self.show_params['show-ref'])
        self.tidy_up()
        self.end_callback(self.show_id, reason, message)
        self = None

    def tidy_up(self):
        if self.duration_timer <> None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer = None
        #clear outstanding time of day events for this show
        # self.tod.clear_times_list(id(self))

# ******************************
# Displaying things
# *********************************

    def display_eggtimer(self, text):
        self.egg_timer = self.canvas.create_text(int(self.canvas['width']) / 2,
                                                 int(self.canvas['height']) /
                                                 2,
                                                 text=text,
                                                 fill='white',
                                                 font="Helvetica 20 bold",
                                                 tag='pp-eggtimer')
        self.canvas.update_idletasks()

    def delete_eggtimer(self):
        self.canvas.delete('pp-eggtimer')
        self.canvas.update_idletasks()

    # used to display internal messages in situations where a medialist entry could not be used.
    def display_message(self, canvas, source, content, duration,
                        display_message_callback):
        self.display_message_callback = display_message_callback
        tp = {
            'duration': duration,
            'message-colour': 'white',
            'message-font': 'Helvetica 20 bold',
            'message-justify': 'left',
            'background-colour': '',
            'background-image': '',
            'show-control-begin': '',
            'show-control-end': '',
            'animate-begin': '',
            'animate-clear': '',
            'animate-end': '',
            'message-x': '',
            'message-y': '',
            'display-show-background': 'no',
            'display-show-text': 'no',
            'show-text': '',
            'track-text': '',
            'plugin': ''
        }
        self.player = MessagePlayer(self.show_id, self.root, canvas, tp, tp,
                                    self.pp_dir, self.pp_home, self.pp_profile)
        self.player.play(content, self.showlist, self.display_message_end,
                         None)

    def display_message_end(self, reason, message):
        self.player = None
        if reason in ("killed", 'error'):
            self.end(reason, message)
        else:
            self.display_message_callback()


# ******************************
# utilities
# *********************************

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            self.terminate("error", 'Cannot find resource')
        else:
            return value

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file <> '' and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def calculate_duration(self, line):
        fields = line.split(':')
        if len(fields) == 1:
            secs = fields[0]
            minutes = '0'
            hours = '0'
        if len(fields) == 2:
            secs = fields[1]
            minutes = fields[0]
            hours = '0'
        if len(fields) == 3:
            secs = fields[2]
            minutes = fields[1]
            hours = fields[0]
        self.duration = 3600 * long(hours) + 60 * long(minutes) + long(secs)
        return ''
Пример #11
0
class PPWebEditor(App):

    def __init__(self, *args):
        # print 'DOIING _INIT do not use'
        super(PPWebEditor, self).__init__(*args)
        

    def main(self):
        # print 'DOING MAIN executed once when server starts'
        # ***************************************
        # INIT
        # ***************************************
        self.editor_issue="1.3"

        # get directory holding the code
        self.editor_dir=sys.path[0]

        ColourMap().init()
        
        # initialise editor options OSC config class, and OSC editors
        self.eo=Options()
        self.eo.init_options(self.editor_dir)
        self.osc_config=OSCConfig()
        self.osc_ute= OSCUnitType()

        
        # initialise variables
        self.init() 

        # BUILD THE GUI
        # frames
        root = gui.Widget(width=900,height=500) #1
        root.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)         
        top_frame=gui.Widget(width=900,height=40)#1
        top_frame.set_layout_orientation(gui.Widget.LAYOUT_HORIZONTAL)
        bottom_frame=gui.Widget(width=900,height=300)#1
        bottom_frame.set_layout_orientation(gui.Widget.LAYOUT_HORIZONTAL)       
        root.append(top_frame)
        root.append(bottom_frame)

        left_frame=gui.Widget(width=350,height=400)#1
        left_frame.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)
        left_frame.style['margin']='10px'
        middle_frame=gui.VBox(width=50,height=300)#1
        middle_frame.style['margin']='10px'
        # middle_frame.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)   
        right_frame=gui.Widget(width=350,height=400)#1
        right_frame.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)   
        updown_frame=gui.VBox(width=50,height=300)#1
        updown_frame.style['margin']='10px'
        # updown_frame.set_layout_orientation(gui.Widget.LAYOUT_VERTICAL)   

        bottom_frame.append(left_frame)
        bottom_frame.append(middle_frame)
        bottom_frame.append(right_frame)
        bottom_frame.append(updown_frame)

        #menu
        menu = gui.Menu(width=700, height=30)
        top_frame.append(menu)

        #profile menu
        profile_menu = gui.MenuItem('Profile',width=80, height=30)
        profile_open_menu = gui.MenuItem('Open',width=120, height=30)
        profile_open_menu.set_on_click_listener(self,'open_existing_profile')
        profile_validate_menu = gui.MenuItem('Validate',width=120, height=30)
        profile_validate_menu.set_on_click_listener(self, 'validate_profile')
        profile_new_menu = gui.MenuItem('New',width=120, height=30)
        profile_menu.append(profile_open_menu)
        profile_menu.append(profile_validate_menu)
        profile_menu.append(profile_new_menu)

        pmenu = gui.MenuItem('Exhibit',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_exhibit_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Media Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_mediashow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Art Media Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_artmediashow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Menu',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_menu_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Presentation',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_presentation_profile')
        profile_new_menu.append(pmenu)        
        pmenu = gui.MenuItem('Interactive',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_interactive_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Live Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_liveshow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('Art Live Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_artliveshow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem('RadioButton Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_radiobuttonshow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem( 'Hyperlink Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_hyperlinkshow_profile')
        profile_new_menu.append(pmenu)
        pmenu = gui.MenuItem( 'Blank',width=150, height=30)
        pmenu.set_on_click_listener(self, 'new_blank_profile')
        profile_new_menu.append(pmenu)
        
        # shows menu              
        show_menu = gui.MenuItem( 'Show',width=80, height=30)
        show_delete_menu = gui.MenuItem('Delete',width=120, height=30)
        show_delete_menu.set_on_click_listener(self, 'remove_show')    
        show_edit_menu = gui.MenuItem('Edit',width=120, height=30)
        show_edit_menu.set_on_click_listener(self, 'm_edit_show')
        show_copy_to_menu = gui.MenuItem( 'Copy To',width=120, height=30)
        show_copy_to_menu.set_on_click_listener(self, 'copy_show')
        show_add_menu = gui.MenuItem( 'Add',width=120, height=30)
        show_menu.append(show_delete_menu)
        show_menu.append(show_edit_menu)
        show_menu.append(show_copy_to_menu)
        show_menu.append(show_add_menu)


        pmenu = gui.MenuItem('Menu',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_menushow')
        show_add_menu.append(pmenu)
        
        pmenu = gui.MenuItem( 'Media Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_mediashow')
        show_add_menu.append(pmenu)
        
        pmenu = gui.MenuItem('Live Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_liveshow')
        show_add_menu.append(pmenu)

        pmenu = gui.MenuItem('Hyperlink Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_hyperlinkshow')
        show_add_menu.append(pmenu)

        pmenu = gui.MenuItem('RadioButton Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_radiobuttonshow')
        show_add_menu.append(pmenu)

        pmenu = gui.MenuItem( 'Art Mediashow Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_artmediashow')
        show_add_menu.append(pmenu)

        pmenu = gui.MenuItem( 'Art Liveshow Show',width=150, height=30)
        pmenu.set_on_click_listener(self, 'add_artliveshow')
        show_add_menu.append(pmenu)

        # medialists menu
        medialist_menu = gui.MenuItem( 'Medialist',width=80, height=30)
        
        medialist_delete_menu = gui.MenuItem( 'Delete',width=120, height=30)
        medialist_delete_menu.set_on_click_listener(self, 'remove_medialist')

        
        medialist_add_menu = gui.MenuItem( 'Add',width=120, height=30)
        medialist_add_menu.set_on_click_listener(self, 'add_medialist')
        
        medialist_copy_to_menu = gui.MenuItem('Copy To',width=120, height=30)
        medialist_copy_to_menu.set_on_click_listener(self, 'copy_medialist')
        
        medialist_menu.append(medialist_add_menu)
        medialist_menu.append(medialist_delete_menu)
        medialist_menu.append(medialist_copy_to_menu)

        # tracks menu
        track_menu = gui.MenuItem('Track',width=80, height=30)

        track_delete_menu = gui.MenuItem('Delete',width=120, height=30)
        track_delete_menu.set_on_click_listener(self, 'remove_track')
        track_edit_menu = gui.MenuItem( 'Edit',width=120, height=30)
        track_edit_menu.set_on_click_listener(self, 'm_edit_track')
        track_add_from_dir_menu = gui.MenuItem('Add Directory',width=120, height=30)
        track_add_from_dir_menu.set_on_click_listener(self, 'add_tracks_from_dir')
        track_add_from_file_menu = gui.MenuItem('Add File',width=120, height=30)
        track_add_from_file_menu.set_on_click_listener(self, 'add_track_from_file')
        track_new_menu = gui.MenuItem('New',width=120, height=30)

        track_new_video_menu = gui.MenuItem('Video',width=120, height=30)
        track_new_video_menu.set_on_click_listener(self, 'new_video_track')
        track_new_audio_menu = gui.MenuItem('Audio',width=120,height=30)
        track_new_audio_menu.set_on_click_listener(self, 'new_audio_track')
        track_new_image_menu = gui.MenuItem( 'Image',width=120, height=30)
        track_new_image_menu.set_on_click_listener(self, 'new_image_track')
        track_new_web_menu = gui.MenuItem( 'Web',width=120, height=30)
        track_new_web_menu.set_on_click_listener(self, 'new_web_track')
        track_new_message_menu = gui.MenuItem('Message',width=120, height=30)
        track_new_message_menu.set_on_click_listener(self, 'new_message_track')
        track_new_show_menu = gui.MenuItem('Show',width=120, height=30)
        track_new_show_menu.set_on_click_listener(self, 'new_show_track')
        track_new_menu_menu = gui.MenuItem('Menu',width=120, height=30)
        track_new_menu_menu.set_on_click_listener(self, 'new_menu_track')

        track_new_menu.append(track_new_video_menu)
        track_new_menu.append(track_new_audio_menu)
        track_new_menu.append(track_new_image_menu)
        track_new_menu.append(track_new_web_menu)        
        track_new_menu.append(track_new_message_menu)
        track_new_menu.append(track_new_show_menu)
        track_new_menu.append(track_new_menu_menu)
        
        track_menu.append(track_delete_menu)
        track_menu.append(track_edit_menu)
        track_menu.append(track_add_from_dir_menu)
        track_menu.append(track_add_from_file_menu)
        track_menu.append(track_new_menu)


      
        options_menu = gui.MenuItem('Options',width=80, height=30)
        options_edit_menu=gui.MenuItem('Edit',width=80, height=30)
        options_edit_menu.set_on_click_listener(self, 'edit_options')
        options_menu.append(options_edit_menu)

        # osc menu
        osc_menu = gui.MenuItem( 'OSC',width=80, height=30)  
        osc_create_menu = gui.MenuItem( 'Create',width=120, height=30)
        osc_create_menu.set_on_click_listener(self, 'create_osc')
        osc_edit_menu = gui.MenuItem( 'Edit',width=120, height=30)
        osc_edit_menu.set_on_click_listener(self, 'edit_osc')
        osc_delete_menu = gui.MenuItem( 'Delete',width=120, height=30)
        osc_delete_menu.set_on_click_listener(self, 'delete_osc')
        osc_menu.append(osc_create_menu)
        osc_menu.append(osc_edit_menu)
        osc_menu.append(osc_delete_menu)
        
        # help menu
        help_menu = gui.MenuItem( 'Help',width=80, height=30)
        help_text_menu = gui.MenuItem( 'Help',width=80, height=30)
        help_text_menu.set_on_click_listener(self, 'show_help')
        about_menu = gui.MenuItem( 'About',width=80, height=30)
        about_menu.set_on_click_listener(self, 'show_about')
        help_menu.append(help_text_menu)
        help_menu.append(about_menu)

        menu.append(profile_menu)
        menu.append(show_menu)
        menu.append(medialist_menu)
        menu.append(track_menu)
        menu.append(osc_menu)
        menu.append(options_menu)
        menu.append(help_menu)


        
        #shows and medialists
        shows_label=gui.Label('<b>Shows</b>',width=300, height=20)
        shows_label.style['margin']='5px'
        self.shows_display= gui.ListView(width=350, height=150)
        self.shows_display.set_on_selection_listener(self,'show_selected')
        
        medialists_label=gui.Label('<b>Medialists</b>',width=300, height=20)
        medialists_label.style['margin']='5px'
        self.medialists_display= gui.ListView(width=350, height=150)
        self.medialists_display.set_on_selection_listener(self,'medialist_selected')

        left_frame.append(shows_label)
        left_frame.append(self.shows_display)         
        left_frame.append(medialists_label)
        left_frame.append(self.medialists_display)

        #edit show button
        edit_show = gui.Button('Edit\nShow',width=50, height=50)
        edit_show.set_on_click_listener(self, 'm_edit_show')
        middle_frame.append(edit_show)

        #tracks
        tracks_label=gui.Label('<b>Tracks in Selected Medialist</b>',width=300, height=20)
        tracks_label.style['margin']='5px'
        self.tracks_display= gui.ListView(width=350, height=300)
        self.tracks_display.set_on_selection_listener(self,'track_selected')

        right_frame.append(tracks_label)
        right_frame.append(self.tracks_display)  

        #tracks buttons
        add_track = gui.Button('Add',width=50, height=50)
        add_track.set_on_click_listener(self, 'add_track_from_file')
        updown_frame.append(add_track)
        
        edit_track = gui.Button('Edit',width=50, height=50)
        edit_track.set_on_click_listener(self, 'm_edit_track')
        updown_frame.append(edit_track)        

        up_track = gui.Button('Up',width=50, height=50)
        up_track.set_on_click_listener(self, 'move_track_up')
        updown_frame.append(up_track)

        down_track = gui.Button('Down',width=50, height=50)
        down_track.set_on_click_listener(self, 'move_track_down')
        updown_frame.append(down_track)
        return root

        
    def init(self):
        # print 'init'
        self.eo.read_options()
        self.pp_home_dir = self.eo.pp_home_dir
        self.pp_profiles_offset = self.eo.pp_profiles_offset
        self.initial_media_dir = self.eo.initial_media_dir
        self.pp_profile_dir=''
        self.current_medialist=None
        self.current_showlist=None
        self.current_show=None

    def empty_lists(self):
        # print 'empty lists'
        self.shows_display.empty()
        self.medialists_display.empty()
        self.tracks_display.empty()

    def show_help (self):
        OKDialog("Help","Please Read 'manual.pdf'",width=400,height=200).show(self)

  
    def show_about (self):
        OKDialog("About","Web Editor for Pi Presents Profiles<br>"
                              +"For profiles of version: " + self.editor_issue + "<br>Author: Ken Thompson"
                              +"<br>Website: http://pipresents.wordpress.com/<br>",width=400,height=200).show(self)

    def validate_profile(self):
        if self.current_showlist != None:
            val =Validator('Validation Result')
            val.show(self)
            val.validate_profile(self.editor_dir,self.pp_home_dir,self.pp_profile_dir,self.editor_issue,True)


    # **************
    # OPTIONS
    # **************


    def edit_options(self):
        self.eo.edit(self.edit_options_callback)
        self.eo.show(self)

    def edit_options_callback(self):
        # self.eo.show(self)
        self.eo.read_options()
        self.init()
        self.empty_lists()
        



    # **************
    # OSC CONFIGURATION
    # **************

    def create_osc(self):
        if self.pp_profile_dir=='':
            return
        # print 'create',OSCConfig.options_file
        if self.osc_config.read() is False:
            iodir=self.pp_profile_dir+os.sep+'pp_io_config'
            if not os.path.exists(iodir):
                os.makedirs(iodir)
            self.osc_config.create()

    def edit_osc(self):
        # print 'edit',OSCConfig.options_file
        if self.osc_config.read() is False:
            # print 'no config file'
            return
        self.osc_ute.edit(self.edit_osc_callback)
        self.osc_ute.show(self)

    def edit_osc_callback(self):
        # self.osc_ute.hide()
        # print 'edit callback', OSCConfig.current_unit_type
        self.osc_editor=OSCWebEditor()
        if OSCConfig.current_unit_type != '':
            self.osc_editor.edit()
            self.osc_editor.show(self)
   
    def delete_osc(self):
        if self.osc_config.read() is False:
            return
        self.osc_config.delete()
        



    
    # **************
    # PROFILES
    # **************

    def open_existing_profile(self):
        initial_dir=self.pp_home_dir+os.sep+"pp_profiles"+self.pp_profiles_offset
        if os.path.exists(initial_dir) is False:
            OKDialog('Open Profile',"Profiles directory not found: " + initial_dir + "<br><br>Hint: Data Home option must end in pp_home").show(self)
            return
        open_existing_profile_dialog = gui.FileSelectionDialog('Open Profile','Select profile',False, initial_dir) #width=600,height=200,
        open_existing_profile_dialog.set_on_confirm_value_listener(self, 'open_existing_profile_dialog_confirm')
        open_existing_profile_dialog.show(self)


    def open_existing_profile_dialog_confirm(self, filelist):
        if len(filelist)==0:
            OKDialog('Open Profile',"Nothing Selected").show(self)
            return
        # print 'filelist',filelist[0]
        self.open_profile(filelist[0])
        

    def open_profile(self,dir_path):
        showlist_file = dir_path + os.sep + "pp_showlist.json"
        #print 'open profile',showlist_file
        if os.path.exists(showlist_file) is False:
            OKDialog('Open Profile',"Not a Profile: " + dir_path).show(self)
            return
        self.pp_profile_dir = dir_path
        OSCConfig.options_file=self.pp_profile_dir+ os.sep+'pp_io_config'+os.sep+'osc.cfg'
        # print 'profile direcotry',self.pp_profile_dir
        # self.root.title("Editor for Pi Presents - "+ self.pp_profile_dir)
        if self.open_showlist(self.pp_profile_dir) is False:
            self.init()
            self.empty_lists()
            return
        
        self.open_medialists(self.pp_profile_dir)
        self.refresh_tracks_display()
        self.osc_config_file=self.pp_profile_dir+os.sep+'pp_io_config'+os.sep+'osc.cfg'


    def new_profile(self,profile):
        d = gui.InputDialog("New Profile","Name",width=400,height=250)
        self.new_profile_template=profile
        d.set_on_confirm_value_listener(self, 'new_profile_confirm')
        d.show(self)

    def new_profile_confirm(self,name):
        if name == "":
            OKDialog("New Profile","Name is blank").show(self)
            return
        to = self.pp_home_dir + os.sep + "pp_profiles"+ self.pp_profiles_offset + os.sep + name
        if os.path.exists(to) is  True:
            OKDialog( "New Profile","Profile exists\n(%s)" % to ).show(self)
            return
        shutil.copytree(self.new_profile_template, to, symlinks=False, ignore=None)
        self.open_profile(to)


        
    def new_exhibit_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_exhibit_1p3'
        self.new_profile(profile)

    def new_interactive_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_interactive_1p3'
        self.new_profile(profile)

    def new_menu_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_menu_1p3'
        self.new_profile(profile)

    def new_presentation_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_presentation_1p3'
        self.new_profile(profile)

    def new_blank_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep +"ppt_blank_1p3"
        self.new_profile(profile)

    def new_mediashow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_mediashow_1p3'
        self.new_profile(profile)
        
    def new_liveshow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_liveshow_1p3'
        self.new_profile(profile)

    def new_artmediashow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_artmediashow_1p3'
        self.new_profile(profile)
        
    def new_artliveshow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_artliveshow_1p3'
        self.new_profile(profile)

    def new_radiobuttonshow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_radiobuttonshow_1p3'
        self.new_profile(profile)

    def new_hyperlinkshow_profile(self):
        profile = self.editor_dir+os.sep+'pp_resources'+os.sep+'pp_templates'+os.sep + 'ppt_hyperlinkshow_1p3'
        self.new_profile(profile)


    # ***************************************
    # Shows
    # ***************************************

# !!!!! changed app_exit to return
    def open_showlist(self,profile_dir):
        showlist_file = profile_dir + os.sep + "pp_showlist.json"
        if os.path.exists(showlist_file) is False:
            OKDialog('Open Profile',"showlist file not found at " + profile_dir + "<br><br>Hint: Have you opened the profile directory?").show(self)
            return False
        self.current_showlist=ShowList()
        self.current_showlist.open_json(showlist_file)
        if float(self.current_showlist.sissue())<float(self.editor_issue):
            self.update_profile()
            OKDialog('Open Profile',"Version of profile has been updated to "+self.editor_issue+", please re-open").show(self)
            return False
        if float(self.current_showlist.sissue())>float(self.editor_issue):
            OKDialog('Open Profile',"Version of profile is greater than editor").show(self)
            return False
        self.refresh_shows_display()
        return True


    def save_showlist(self,showlist_dir):
        if self.current_showlist is not None:
            showlist_file = showlist_dir + os.sep + "pp_showlist.json"
            self.current_showlist.save_list(showlist_file)
            
    def add_mediashow(self):
        self.add_show(PPdefinitions.new_shows['mediashow'])

    def add_liveshow(self):
        self.add_show(PPdefinitions.new_shows['liveshow'])

    def add_radiobuttonshow(self):
        self.add_show(PPdefinitions.new_shows['radiobuttonshow'])

    def add_hyperlinkshow(self):
        self.add_show(PPdefinitions.new_shows['hyperlinkshow'])

    def add_artliveshow(self):
        self.add_show(PPdefinitions.new_shows['artliveshow'])

    def add_artmediashow(self):
        self.add_show(PPdefinitions.new_shows['artmediashow'])
        
    def add_menushow(self):
        self.add_show(PPdefinitions.new_shows['menu'])

    def add_start(self):  
        self.add_show(PPdefinitions.new_shows['start'])


    def add_show(self,default):
        # append it to the showlist and then add the medialist
        if self.current_showlist is not None:
            self.default_show=default
            d = gui.InputDialog("Add Show","Show Reference",width=400,height=250)
            d.set_on_confirm_value_listener(self, 'add_show_confirm')
            d.show(self)

    def add_show_confirm(self,name):
        # print 'show name',name
        if name == "":
            OKDialog("Add Show","Name is blank").show(self)
            return             
        if self.current_showlist.index_of_show(name) != -1:
            OKDialog("Add Show","A Show with this name already exists").show(self)
            return
        # print 'copy show template',self.default_show,name
        copied_show=self.current_showlist.copy(self.default_show,name)
        # print 'add mediafile from show',name
        mediafile=self.add_medialist(name)
        # print 'mediafile added',mediafile
        if mediafile != '':
            copied_show['medialist']=mediafile
        self.current_showlist.append(copied_show)
        self.save_showlist(self.pp_profile_dir)
        self.refresh_shows_display()

            
    def remove_show(self):
        if  self.current_showlist is not None and self.current_showlist.length()>0 and self.current_showlist.show_is_selected():
            OKCancelDialog("Delete Show","Are you sure?",self.remove_show_confirm).show(self)

    def remove_show_confirm(self,result):
        if result is True:
            index= self.current_showlist.selected_show_index()
            self.current_showlist.remove(index)
            self.save_showlist(self.pp_profile_dir)
            self.refresh_shows_display()


    def show_refs(self):
        _show_refs=[]
        for index in range(self.current_showlist.length()):
            if self.current_showlist.show(index)['show-ref'] != "start":
                _show_refs.append(copy.deepcopy(self.current_showlist.show(index)['show-ref']))
        return _show_refs
 
    def refresh_shows_display(self):
        self.shows_display.empty()
        key=0
        for index in range(self.current_showlist.length()):
            value= self.current_showlist.show(index)['title']+"   ["+self.current_showlist.show(index)['show-ref']+"]"
            obj = gui.ListItem(value,width=340, height=20)
            self.shows_display.append(obj,key=key)
            key+=1
        if self.current_showlist.show_is_selected():
            self.shows_display.select_by_key(self.current_showlist.selected_show_index())            
            # self.shows_display.show()

            
    def show_selected(self,event):
        if self.current_showlist is not None and self.current_showlist.length()>0:
            mouse_item_index=self.shows_display.get_key()
            self.current_showlist.select(mouse_item_index)
            self.refresh_shows_display()

    def copy_show(self):
        if  self.current_showlist is not None and self.current_showlist.show_is_selected():
            self.add_show(self.current_showlist.selected_show())

        
    def m_edit_show(self):
        self.edit_show(PPdefinitions.show_types,PPdefinitions.show_field_specs)
        

    def edit_show(self,show_types,field_specs):
        if self.current_showlist is not None and self.current_showlist.show_is_selected():
            self.edit_show_dialog=WebEditItem("Edit Show",self.current_showlist.selected_show(),show_types,field_specs,self.show_refs(),
                       self.initial_media_dir,self.pp_home_dir,'show',self.finished_edit_show)
            self.edit_show_dialog.show(self)
            self.edit_show_dialog.show_tab('show')

    def finished_edit_show(self):
        self.save_showlist(self.pp_profile_dir)
        self.refresh_shows_display()



    # ***************************************
    #   Medialists
    # ***************************************

    def open_medialists(self,profile_dir):
        self.medialists = []
        for this_file in os.listdir(profile_dir):
            if this_file.endswith(".json") and this_file not in ('pp_showlist.json','schedule.json'):
                self.medialists = self.medialists + [this_file]
        self.medialists_display.empty()
        key=0
        for index in range (len(self.medialists)):
            obj = gui.ListItem(self.medialists[index],width=340, height=20)
            self.medialists_display.append(obj, key=key)
            key+=1
        self.current_medialists_index=-1
        self.current_medialist=None


    def add_medialist(self,name=None):
        if self.current_showlist != None:
            if name is None:
                d = gui.InputDialog("Add Medialist","File",width=400,height=250)
                d.set_on_confirm_value_listener(self, 'add_medialist_confirm')
                d.show(self)
            else:
                medialist_name=self.add_medialist_confirm(name)
                return medialist_name

    def add_medialist_confirm(self,name):
        # print 'add medialist',name
        if name == "":
            OKDialog("Add Medialist","Name is blank").show(self)
            return  ''           
        if self.current_showlist.index_of_show(name) != -1:
            OKDialog("Add Medialist","A medialist with this name already exists").show(self)
            return ''
        if not name.endswith(".json"):
            name=name+(".json")
               
        path = self.pp_profile_dir + os.sep + name
        if os.path.exists(path) is  True:
            OKDialog("Add Medialist","Medialist file exists<br>(%s)" % path).show(self)
            return ''
        nfile = open(path,'wb')
        nfile.write("{")
        nfile.write("\"issue\":  \""+self.editor_issue+"\",\n")
        nfile.write("\"tracks\": [")
        nfile.write("]")
        nfile.write("}")
        nfile.close()
        # append it to the list
        self.medialists.append(copy.deepcopy(name))
        # print 'medialists',self.medialists
        # add title to medialists display
        # self.medialists_display.insert(END, name)  
        # and set it as the selected medialist
        self.refresh_medialists_display()
        # print 'returning medilaist name',name
        return name


    def copy_medialist(self,to_file=None):
        if self.current_showlist != None:
            if self.current_medialist is not None:
                #from_file= self.current_medialist
                self.from_file= self.medialists[self.current_medialists_index]
                if to_file is None:
                    d = gui.InputDialog("Copy Medialist","File",width=400,height=250)
                d.set_on_confirm_value_listener(self, 'copy_medialist_confirm')
                d.show(self)
            else:
                self.copy_medialist_confirm(to_file)

    def copy_medialist_confirm(self,to_file):
        # print self.from_file,to_file
        if to_file == "":
            OKDialog("Copy Medialist","Name is blank").show(self)
            return ''

        success_file = self.copy_medialist_file(self.from_file,to_file)
        if success_file =='':
            return ''

        # append it to the list
        self.medialists.append(copy.deepcopy(success_file))
        # add title to medialists display
        # self.medialists_display.insert(END, success_file)
        # and reset  selected medialist
        self.current_medialist=None
        self.refresh_medialists_display()
        self.refresh_tracks_display()
        return success_file


    def copy_medialist_file(self,from_file,to_file):
        if not to_file.endswith(".json"):
            to_file+=(".json")
                
        to_path = self.pp_profile_dir + os.sep + to_file
        if os.path.exists(to_path) is  True:
            OKDialog("Copy Medialist","Medialist file exists\n(%s)" % to_path).show(self)
            return ''
        
        from_path= self.pp_profile_dir + os.sep + from_file
        if os.path.exists(from_path) is  False:
            OKDialog("Copy Medialist","Medialist file not found\n(%s)" % from_path).show(self)
            return ''

        shutil.copy(from_path,to_path)
        return to_file


    def remove_medialist(self):
        if self.current_medialist is not None:
            OKCancelDialog("Delete Medialist","Are you sure?",self.remove_medialist_confirm).show(self)

    def remove_medialist_confirm(self,result):
        if result is True:
            os.remove(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index])
            self.open_medialists(self.pp_profile_dir)
            self.refresh_medialists_display()
            self.refresh_tracks_display()


# removed appexit
    def medialist_selected(self,key):
        """
        user clicks on a medialst in a profile so try and select it.
        """
        # print 'selected',type(self.medialists_display.get_key()),self.medialists_display.get_key()
        if len(self.medialists)>0:
            self.current_medialists_index=self.medialists_display.get_key()

            self.current_medialist=MediaList('ordered')
            if not self.current_medialist.open_list(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index],self.current_showlist.sissue()):
                OKDialog(self,"medialist is a different version to showlist: "+ self.medialists[self.current_medialists_index]).show(self)
                #self.app_exit()
                return
            self.refresh_tracks_display()
            self.refresh_medialists_display()


    def refresh_medialists_display(self):
        # print 'refresh medialists'
        self.medialists_display.empty()
        key=0
        for index in range (len(self.medialists)):
            obj = gui.ListItem(self.medialists[index],width=340, height=20)
            self.medialists_display.append(obj,key=key)
            key+=1
                    
        if self.current_medialist is not None:
            self.medialists_display.select_by_key(self.current_medialists_index)
            # self.medialists_display.show(self)

  

    def save_medialist(self):
        basefile=self.medialists[self.current_medialists_index]
        # print type(basefile)
        # basefile=str(basefile)
        # print type(basefile)
        medialist_file = self.pp_profile_dir+ os.sep + basefile
        self.current_medialist.save_list(medialist_file)



    # ***************************************
    #   Tracks
    # ***************************************
          
    def refresh_tracks_display(self):
        self.tracks_display.empty()
        if self.current_medialist is not None:
            key=0
            for index in range(self.current_medialist.length()):
                if self.current_medialist.track(index)['track-ref'] != '':
                    track_ref_string="  ["+self.current_medialist.track(index)['track-ref']+"]"
                else:
                    track_ref_string=""
                obj = gui.ListItem(self.current_medialist.track(index)['title']+track_ref_string,width=340, height=20)
                self.tracks_display.append(obj,key=key)
                key+=1                
            if self.current_medialist.track_is_selected():
                self.tracks_display.select_by_key(self.current_medialist.selected_track_index())  

            
    def  track_selected(self,key):
        # print 'track sel', type(self.tracks_display.get_key())
        if self.current_medialist is not None and self.current_medialist.length()>0:
            mouse_item_index=self.tracks_display.get_key()
            self.current_medialist.select(mouse_item_index)
            self.refresh_tracks_display()

    def m_edit_track(self):
        self.edit_track(PPdefinitions.track_types,PPdefinitions.track_field_specs)

    def edit_track(self,track_types,field_specs):      
        if self.current_medialist is not None and self.current_medialist.track_is_selected():
            self.edit_track_dialog=WebEditItem("Edit Track",self.current_medialist.selected_track(),track_types,field_specs,
                       self.show_refs(),self.initial_media_dir,self.pp_home_dir,'track',self.finished_edit_track)
            self.edit_track_dialog.show(self)
            self.edit_track_dialog.show_tab('track')

    def finished_edit_track(self):
        self.refresh_tracks_display()
        self.save_medialist()        
            
    def move_track_up(self):
        if self.current_medialist is not None and self.current_medialist.track_is_selected():
            self.current_medialist.move_up()
            self.refresh_tracks_display()
            self.save_medialist()

    def move_track_down(self):
        if self.current_medialist is not None and self.current_medialist.track_is_selected():
            self.current_medialist.move_down()
            self.refresh_tracks_display()
            self.save_medialist()
        
    def new_track(self,fields,values):
        if self.current_medialist is not None:
            # print '\nfields ', fields
            # print '\nvalues ', values
            new_track=copy.deepcopy(fields)
            # print ',\new track ',new_track
            self.current_medialist.append(new_track)
            # print '\nbefore values ',self.current_medialist.print_list()
            if values is not None:
                self.current_medialist.update(self.current_medialist.length()-1,values)
            self.current_medialist.select(self.current_medialist.length()-1)
            self.refresh_tracks_display()
            self.save_medialist()

    def new_message_track(self):
        self.new_track(PPdefinitions.new_tracks['message'],None)
            
    def new_video_track(self):
        self.new_track(PPdefinitions.new_tracks['video'],None)
  
    def new_audio_track(self):
        self.new_track(PPdefinitions.new_tracks['audio'],None)

    def new_web_track(self):
        self.new_track(PPdefinitions.new_tracks['web'],None)
        
    def new_image_track(self):
        self.new_track(PPdefinitions.new_tracks['image'],None)

    def new_show_track(self):
        self.new_track(PPdefinitions.new_tracks['show'],None)

    def new_menu_track(self):
        # print 'new menu track'
        self.new_track(PPdefinitions.new_tracks['menu'],None)
 
    def remove_track(self):
        if  self.current_medialist is not None and self.current_medialist.length()>0 and self.current_medialist.track_is_selected():
            OKCancelDialog("Delete Track","Are you sure?",self.remove_track_confirm).show(self)

    def remove_track_confirm(self,result):
        # print 'confirm',result
        if result is True:
            index= self.current_medialist.selected_track_index()
            self.current_medialist.remove(index)
            self.save_medialist()
            self.refresh_tracks_display()
            
    def add_track_from_file(self):
        if self.current_medialist is None:
            return
        add_track_from_file_dialog = gui.FileSelectionDialog('Add Track', 'Select Tracks',True, self.eo.initial_media_dir)#600,200,
        add_track_from_file_dialog.set_on_confirm_value_listener(self, 'add_track_from_file_dialog_confirm')
        add_track_from_file_dialog.show(self)


    def add_track_from_file_dialog_confirm(self, filelist):
        if len(filelist)==0:
            OKDialog('Add Track',"Nothing Selected").show(self)
            return
        for file_path in filelist:
            file_path=os.path.normpath(file_path)
            # print "file path ", file_path
            self.add_track(file_path)
        self.save_medialist()

    def add_tracks_from_dir(self):
        if self.current_medialist is None: return
        add_tracks_from_dir_dialog = gui.FileSelectionDialog('Add Directory', 'Select Directory',
                                    multiple_selection = False, allow_file_selection=False, selection_folder = self.eo.initial_media_dir)
        add_tracks_from_dir_dialog.set_on_confirm_value_listener(self, 'add_tracks_from_dir_dialog_confirm')
        add_tracks_from_dir_dialog.show(self)


    def add_tracks_from_dir_dialog_confirm(self, result):
        image_specs =[PPdefinitions.IMAGE_FILES,PPdefinitions.VIDEO_FILES,PPdefinitions.AUDIO_FILES,
                      PPdefinitions.WEB_FILES,('All files', '*')]
        # last one is ignored in finding files in directory, for dialog box only
        if len(result) == 0:
            OKDialog('Add Tracks',"Nothing Selected").show(self)
            return
        directory=result[0]
        # make list of exts we recognise
        exts = []
        for image_spec in image_specs[:-1]:
            image_list=image_spec[1:]
            for ext in image_list:
                exts.append(copy.deepcopy(ext))
        for this_file in os.listdir(directory):
            (root_file,ext_file)= os.path.splitext(this_file)
            if ext_file.lower() in exts:
                file_path=directory+os.sep+this_file
                # print "file path before ", file_path
                file_path=os.path.normpath(file_path)
                # print "file path after ", file_path
                self.add_track(file_path)
        self.save_medialist()


    def add_track(self,afile):
        relpath = os.path.relpath(afile,self.pp_home_dir)
        # print "relative path ",relpath
        common = os.path.commonprefix([afile,self.pp_home_dir])
        # print "common ",common
        if common.endswith("pp_home")  is  False:
            location = afile
        else:
            location = "+" + os.sep + relpath
            location = string.replace(location,'\\','/')
            # print "location ",location
        (root,title)=os.path.split(afile)
        (root,ext)= os.path.splitext(afile)
        if ext.lower() in PPdefinitions.IMAGE_FILES:
            self.new_track(PPdefinitions.new_tracks['image'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.VIDEO_FILES:
            self.new_track(PPdefinitions.new_tracks['video'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.AUDIO_FILES:
            self.new_track(PPdefinitions.new_tracks['audio'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.WEB_FILES:
            self.new_track(PPdefinitions.new_tracks['web'],{'title':title,'track-ref':'','location':location})
        else:
            OKDialog('Add Track',afile + " - cannot determine track type, use menu track>new").show(self)
Пример #12
0
class MenuShow:
    """ Displays a menu with optional hint below it. User can traverse the menu and
              select a track using key or button presses.
        Interface:
         * play - displays the menu and selects the first entry
         * key_pressed, button_pressed - receives user events passes them to a Player if a track is playing,
                otherwise actions them with _next, _previous, _play_selected_track, _end
         Optional display of eggtimer by means of Players ready_callback
         Supports imageplayer, videoplayer,messagplayer,menushow,mediashow
         Destroys itself on exit
    """

# *********************
# external interface
# ********************

    def __init__(self,
                            show,
                            canvas,
                            showlist,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the name of the configuration dictionary section for the menu
            cf  - the configuration object
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory"""
        
        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show=show
        self.showlist=showlist
        self.canvas=canvas
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()
        
        # init variables
        self.drawn  = None
        self.player=None
        self.shower=None
        self.menu_timeout_running=None
        self.error=False



    def play(self,end_callback,ready_callback=None,top=False,command='nil'):
        """ displays the menu 
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """
        
        #instantiate arguments
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.command=command

        
        # check  data files are available.
        self.menu_file = self.pp_profile + "/" + self.show['medialist']
        if not os.path.exists(self.menu_file):
            self.mon.err(self,"Medialist file not found: "+ self.menu_file)
            self._end('error',"Medialist file not found")
        
        #create a medialist for the menu and read it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.menu_file,self.showlist.sissue()) == False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self._end('error',"Version of medialist different to Pi Presents")
           
        if self.show['has-background']=="yes":
            background_index=self.medialist.index_of_track ('pp-menu-background')
            if background_index>=0:
                self.menu_img_file = self.complete_path(self.medialist.track(background_index))
                if not os.path.exists(self.menu_img_file):
                    self.mon.err(self,"Menu background file not found: "+ self.menu_img_file)
                    self._end('error',"Menu background file not found")
            else:
                self.mon.err(self,"Menu background not found in medialist")
                self._end('error',"Menu background not found")

        #start timeout alarm if required
        if int(self.show['timeout'])<>0:
            self.menu_timeout_running=self.canvas.after(int(self.show['timeout'])*1000,self._timeout_menu)
        
        if self.ready_callback<>None:
            self.ready_callback()
        
        self.canvas.delete(ALL)
        
        # display background image
        if self.show['has-background']=="yes":
            self._display_background()
 
       #display the list of video titles
        self._display_video_titles()

        # display instructions (hint)
        self.canvas.create_text(int(self.canvas['width'])/2,
                                int(self.canvas['height']) - int(self.show['hint-y']),
                                text=self.show['hint-text'],
                                fill=self.show['hint-colour'],
                                font=self.show['hint-font'])
        self.canvas.update( )



   # respond to key presses.
    def key_pressed(self,key_name):
        self.mon.log(self,"received key: " + key_name)
        
        if key_name=='':
            pass
        
        elif key_name=='escape':
            # if next lower show eor player is running pass down to stop bottom level
            # ELSE stop this show if not at top
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
            else:
                # not at top so stop the show
                if  self.top == False:
                    self._end('normal',"exit from stop command")
                else:
                    pass
      
        elif key_name in ('up','down'):
        # if child or sub-show running and is a show pass down
        # if  child not running - move
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self.player==None:
                    if key_name=='up':
                        self._previous()
                    else:
                        self._next()
                
        elif key_name=='return':
            # if child running and is show - pass down
            # if no track already running  - play
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            else:
                if self.player==None:
                    self._play_selected_track(self.medialist.selected_track())

        elif key_name in ('p',' '):
            # pass down if show or track running.
            if self.shower<>None:
                self.shower.key_pressed(key_name)
            elif self.player<>None:
                self.player.key_pressed(key_name)
 
 
    def button_pressed(self,button,edge):
        if button=='play': self.key_pressed("return")
        elif  button =='up': self.key_pressed("up")
        elif button=='down': self.key_pressed("down")
        elif button=='stop': self.key_pressed("escape")
        elif button=='pause': self.key_pressed('p')

        
    # kill or error
    def terminate(self,reason):
        if self.shower<>None:
            self.mon.log(self,"sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player<>None:
            self.mon.log(self,"sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,'terminated without terminating shower or player')



# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# language resources
# *********************

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            # timers may be running so need terminate
            self.terminate("error")
        else:
            return value


# *********************
# Sequencing
# *********************

    def _timeout_menu(self):
        self._end('normal','menu timeout')
        return
        
    
    # finish the player for killing, error or normally
    # this may be called directly sub/child shows or players are not running
    # if they might be running then need to call terminate.
    
    def _end(self,reason,message):
        self.canvas.delete(ALL)
        self.canvas.update_idletasks( )
        self.mon.log(self,"Ending menushow: "+ self.show['show-ref'])  
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        self.end_callback(reason,message)
        self=None
        return


    def _next(self):     
        self._highlight_menu_entry(self.menu_index,False)
        self.medialist.next()
        if self.menu_index==self.menu_length-1:
            self.menu_index=0
        else:
            self.menu_index+=1
        self._highlight_menu_entry(self.menu_index,True)     


    def _previous(self):   
        self._highlight_menu_entry(self.menu_index,False)
        if self.menu_index==0:
            self.menu_index=self.menu_length-1
        else:
            self.menu_index-=1
        self.medialist.previous()
        self._highlight_menu_entry(self.menu_index,True)    


# *********************
# Dispatching to Players
# *********************

    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     
         


    def _play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
         #remove menu and show working.....        
        self.canvas.delete(ALL)
        if self.menu_timeout_running<>None:
            self.canvas.after_cancel(self.menu_timeout_running)
            self.menu_timeout_running=None
        self._display_eggtimer(self.resource('menushow','m01'))
    
        # dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                        self._end_player,
                                        self._delete_eggtimer,
                                        enable_menu=False)
                                        
        elif track_type=="image":
            # images played from menus don't have children
            enable_child=False
            track_file=self.complete_path(selected_track)
            self.player=ImagePlayer(self.canvas,self.show,selected_track)
            self.player.play(track_file,
                                    self._end_player,
                                    self._delete_eggtimer,
                                    enable_menu=enable_child,
                                    )
                                    
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.canvas,self.show,selected_track)
            self.player.play(text,
                                    self._end_player,
                                    self._delete_eggtimer,
                                    enable_menu=False
                                    )
 
        elif track_type=="show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self._end("Unknown show")
            
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self._end_shower,top=False,command='nil')

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                self.canvas,
                                                                self.showlist,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.end_shower,top=False,command='nil')

            elif selected_show['type']=="menu": 
                self.shower= MenuShow(selected_show,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self._end_shower,top=False,command='nil')                    
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self._end("Unknown show type")  
                
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self._end("Unknown track type")
    
    # callback from when player ends
    def _end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        if reason in("killed","error"):
            self._end(reason,message)
        self._display_eggtimer(self.resource('menushow','m02'))
        self._what_next(message)

    # callback from when shower ends
    def _end_shower(self,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        if message in ("killed","error"):
            self._end(reason,message)
        self._display_eggtimer(self.resource('menushow','m03'))
        self._what_next(message)  
   

     # at the end of a track just re-display the menu with the original callback from the menu       
    def _what_next(self,message):
        self.mon.log(self,"Re-displaying menu")
        self.play(self.end_callback,top=self.top)



# *********************
# Displaying things
# *********************

    def _display_background(self):
        pil_menu_img=PIL.Image.open(self.menu_img_file)
        # adjust brightness and rotate (experimental)
        # enh=PIL.ImageEnhance.Brightness(pil_menu_img)
        # pil_menu_img=enh.enhance(0.1)
        # pil_menu_img=pil_menu_img.rotate(45)
        self.menu_background = PIL.ImageTk.PhotoImage(pil_menu_img)
        self.drawn = self.canvas.create_image(int(self.canvas['width'])/2,
                                      int(self.canvas['height'])/2,
                                      image=self.menu_background,
                                      anchor=CENTER)


    def _display_video_titles(self):
        self.menu_length=1
        self.menu_entry_id=[]
        x=int(self.show['menu-x'])
        y=int(self.show['menu-y'])
        self.medialist.start()
        while True:
            id=self.canvas.create_text(x,y,anchor=NW,
                                       text="* "+self.medialist.selected_track()['title'],
                                       fill=self.show['entry-colour'],
                                       font=self.show['entry-font'])
            self.menu_entry_id.append(id)
            y=y + int(self.show['menu-spacing'])
            if self.medialist.at_end():
                break
            self.menu_length+=1
            self.medialist.next()
            
        # select and highlight the first entry
        self.medialist.start()
        self.menu_index=0
        self._highlight_menu_entry(self.menu_index,True)
        # self.medialist.print_list()

    def _highlight_menu_entry(self,index,state):
        if state==True:
            self.canvas.itemconfig(self.menu_entry_id[index],fill=self.show['entry-select-colour'])
        else:
            self.canvas.itemconfig(self.menu_entry_id[index],fill=self.show['entry-colour'])
    
    
    def _display_eggtimer(self,text):
        self.canvas.create_text(int(self.canvas['width'])/2,
                                              int(self.canvas['height'])/2,
                                                  text= text,
                                                  fill='white',
                                                  font="Helvetica 20 bold")
        self.canvas.update_idletasks( )


    def _delete_eggtimer(self):
            self.canvas.delete(ALL)
Пример #13
0
class RadioButtonShow:
    """
        starts at 'first-track' which can be any type of track or a show
        The show has links of the form symbolic-name play track-ref
        key, gpio or click area will play the referenced track
        at the end of that track control will return to first-track
        links in the tracks are ignored. Links are inherited from the show.
        timeout returns to first-track

        interface:
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
    """

    # *********************
    # external interface
    # ********************

    def __init__(self, show_params, root, canvas, showlist, pp_dir, pp_home,
                 pp_profile):
        """ canvas - the canvas that the tracks of the event show are to be written on
            show_params - the name of the configuration dictionary section for the radiobuttonshow
            showlist  - the showlist, to enable runningnof show type tracks.
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show_params = show_params
        self.showlist = showlist
        self.root = root
        self.canvas = canvas
        self.pp_dir = pp_dir
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        #create a path stack - only used to parse the links.
        self.path = PathManager()

        # init variables
        self.drawn = None
        self.player = None
        self.shower = None
        self.timeout_running = None
        self.error = False

    def play(self,
             show_id,
             end_callback,
             ready_callback,
             top=False,
             command='nil'):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              ready_callback - callback when event-show is ready to display its forst track (not used?)
              top is True when the show is top level (run from [start] or from show control)
              command is not used
        """

        #instantiate arguments
        self.show_id = show_id
        self.end_callback = end_callback
        self.ready_callback = ready_callback
        self.top = top
        self.command = command

        # check data files are available.
        self.medialist_file = self.pp_profile + "/" + self.show_params[
            'medialist']
        if not os.path.exists(self.medialist_file):
            self.mon.err(self,
                         "Medialist file not found: " + self.medialist_file)
            self.end('error', "Medialist file not found")

        #create a medialist object for the radiobuttonshow and read the file into it.
        self.medialist = MediaList()
        if self.medialist.open_list(self.medialist_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self.end('error', "Version of medialist different to Pi Presents")

        # read show destinations
        self.first_track_ref = self.show_params['first-track-ref']

        #get control bindings for this show if top level
        controlsmanager = ControlsManager()
        if self.top == True:
            self.controls_list = controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list = controlsmanager.merge_show_controls(
                self.controls_list, self.show_params['controls'])

        #read the show links. Track links will be added by ready_callback
        links_text = self.show_params['links']
        reason, message, self.links = self.path.parse_links(links_text)
        if reason == 'error':
            self.mon.err(self, message + " in show")
            self.end('error', message)

        # state variables and signals
        self.end_radiobuttonshow_signal = False
        self.egg_timer = None
        self.next_track_signal = False
        self.next_track_ref = ''
        self.current_track_ref = ''
        self.current_track_type = ''

        # ready callback for show
        if self.ready_callback <> None:
            self.ready_callback()

        self.canvas.delete('pp-content')
        self.canvas.config(bg='black')

        self.do_first_track()

#stop received from another concurrent show via ShowManager

    def managed_stop(self):
        # set signal to stop the radiobuttonshow when all  sub-shows and players have ended
        self.end_radiobuttonshow_signal = True
        # then stop and shows or tracks.
        if self.shower <> None:
            self.shower.managed_stop()
        elif self.player <> None:
            self.player.input_pressed('stop')
        else:
            self.end('normal', 'stopped by ShowManager')

    # kill or error
    def terminate(self, reason):
        self.end_radiobuttonshow_signal = True
        if self.shower <> None:
            self.shower.terminate(reason)
        elif self.player <> None:
            self.player.terminate(reason)
        else:
            self.end(reason, 'terminated without terminating shower or player')

# respond to inputs

    def input_pressed(self, symbol, edge, source):

        self.mon.log(self, "received symbol: " + symbol)

        #does the symbol match a link, if so execute it
        if self.is_link(symbol, edge, source) == True:
            return

        # controls are disabled so ignore inputs
        if self.show_params['disable-controls'] == 'yes':
            return

        # does it match a control
        # if at top convert symbolic name to operation otherwise lower down we have received an operatio
        # look through list of controls to find match
        if self.top == True:
            operation = self.lookup_control(symbol, self.controls_list)
        else:
            operation = symbol
        # print 'operation',operation
        if operation <> '':
            self.do_operation(operation, edge, source)

    def do_operation(self, operation, edge, source):
        if self.shower <> None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation, edge, source)
        else:
            #control this show and its tracks
            if operation == 'stop':
                if self.player <> None:
                    if self.current_track_ref == self.first_track_ref and self.top == False:
                        self.end_radiobuttonshow_signal = True
                    self.player.input_pressed('stop')

            elif operation == 'pause':
                if self.player <> None:
                    self.player.input_pressed(operation)

            elif operation[0:4] == 'omx-' or operation[
                    0:6] == 'mplay-' or operation[0:5] == 'uzbl-':
                if self.player <> None:
                    self.player.input_pressed(operation)

    def lookup_control(self, symbol, controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

    def is_link(self, symbol, edge, source):
        # we have links which locally define symbolic names to be converted to radiobuttonshow operations
        # find the first entry in links that matches the symbol and execute its operation
        #print 'radiobuttonshow ',symbol
        found = False
        for link in self.links:
            #print link
            if symbol == link[0]:
                found = True
                if link[1] <> 'null':
                    #print 'match',link[0]
                    link_operation = link[1]
                    if link_operation == 'play':
                        self.do_play(link[2], edge, source)
        return found

# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************

    def timeout_callback(self):
        self.do_play(self.first_track_ref, 'front', 'timeout')

    def do_play(self, track_ref, edge, source):
        if track_ref <> self.current_track_ref:
            # print 'executing play ',track_ref
            self.next_track_signal = True
            self.next_track_op = 'play'
            self.next_track_arg = track_ref
            if self.shower <> None:
                self.shower.input_pressed('stop', edge, source)
            elif self.player <> None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_first_track(self):
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >= 0:
            #don't use select the track as not using selected_track in radiobuttonshow
            first_track = self.medialist.track(index)
            self.path.append(first_track['track-ref'])
            self.current_track_ref = self.first_track_ref
            self.play_selected_track(first_track)
        else:
            self.mon.err(
                self, "first-track not found in medialist: " +
                self.show_params['first-frack-ref'])
            self.end('error', "first track not found in medialist")

    def what_next(self):
        # user wants to end the show
        if self.end_radiobuttonshow_signal == True:
            self.end_radiobuttonshow_signal = False
            self.end('normal', "show ended by user")

        # user has selected another track
        elif self.next_track_signal == True:
            self.next_track_signal = False
            self.next_track_ref = self.next_track_arg
            self.current_track_ref = self.next_track_ref
            index = self.medialist.index_of_track(self.next_track_ref)
            if index >= 0:
                #don't use select the track as not using selected_track in radiobuttonshow
                next_track = self.medialist.track(index)
                self.play_selected_track(next_track)
            else:
                self.mon.err(
                    self, "next-track not found in medialist: " +
                    self.next_track_ref)
                self.end('error', "next track not found in medialist")

        else:
            #track ends naturally
            self.next_track_ref = self.first_track_ref
            self.current_track_ref = self.next_track_ref
            index = self.medialist.index_of_track(self.next_track_ref)
            if index >= 0:
                #don't use select the track as not using selected_track in radiobuttonshow
                next_track = self.medialist.track(index)
                self.play_selected_track(next_track)
            else:
                self.mon.err(
                    self, "next-track not found in medialist: " +
                    self.next_track_ref)
                self.end('error', "next track not found in medialist")

# *********************
# Dispatching to Players
# *********************

    def page_callback(self):
        # called from a Player when ready to play, if first-track merge the links from the track with those from the show
        self.delete_eggtimer()
        if self.current_track_ref == self.first_track_ref:
            #links_text=self.player.get_links()
            #reason,message,track_links=self.path.parse_links(links_text)
            #if reason=='error':
            #self.mon.err(self,message + " in page")
            #self.end('error',message)
            #self.path.merge_links(self.links,track_links)
            pass

    def play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """

        if self.timeout_running <> None:
            self.canvas.after_cancel(self.timeout_running)
            self.timeout_running = None

        # self.display_eggtimer(self.resource('radiobuttonshow','m01'))

        self.current_track_type = selected_track['type']

        #start timeout for the track if required

        if self.current_track_ref <> self.first_track_ref and int(
                self.show_params['timeout']) <> 0:
            self.timeout_running = self.canvas.after(
                int(self.show_params['timeout']) * 1000, self.timeout_callback)

        # dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "audio":
            # create a audioplayer
            track_file = self.complete_path(selected_track)
            self.player = AudioPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "image":
            track_file = self.complete_path(selected_track)
            self.player = ImagePlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(
                track_file,
                self.showlist,
                self.end_player,
                self.page_callback,
                enable_menu=False,
            )

        elif track_type == "web":
            # create a browser
            track_file = self.complete_path(selected_track)
            self.player = BrowserPlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track['text']
            self.player = MessagePlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(text,
                             self.showlist,
                             self.end_player,
                             self.page_callback,
                             enable_menu=False)

        elif track_type == "show":
            # self.enable_click_areas()
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self.end("Unknown show")

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.root, self.canvas,
                                        self.showlist, self.pp_dir,
                                        self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "liveshow":
                self.shower = LiveShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "radiobuttonshow":
                self.shower = RadioButtonShow(selected_show, self.root,
                                              self.canvas, self.showlist,
                                              self.pp_dir, self.pp_home,
                                              self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "hyperlinkshow":
                self.shower = HyperlinkShow(selected_show, self.root,
                                            self.canvas, self.showlist,
                                            self.pp_dir, self.pp_home,
                                            self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')
            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self.end("Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self.end("Unknown track type")

    # callback from when player ends
    def end_player(self, reason, message):
        self.mon.log(self, "Returned from player with message: " + message)
        self.player = None
        # this does not seem to change the colour of the polygon
        # self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks()
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            #self.display_eggtimer(self.resource('radiobuttonshow','m02'))
            self.what_next()

    # callback from when shower ends
    def end_shower(self, show_id, reason, message):
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        # self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks()
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            #self.display_eggtimer(self.resource('radiobuttonshow','m03'))
            self.what_next()

# *********************
# End the show
# *********************
# finish the player for killing, error or normally
# this may be called directly sub/child shows or players are not running
# if they might be running then need to call terminate.

    def end(self, reason, message):
        self.mon.log(self,
                     "Ending radiobuttonshow: " + self.show_params['show-ref'])
        self.end_callback(self.show_id, reason, message)
        self = None
        return

# *********************
# displaying things
# *********************

    def display_eggtimer(self, text):
        #self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
        #int(self.canvas['height'])/2,
        #text= text,
        # fill='white',
        # font="Helvetica 20 bold")
        #self.canvas.update_idletasks( )
        pass

    def delete_eggtimer(self):
        if self.egg_timer != None:
            self.canvas.delete(self.egg_timer)


# *********************
# utilities
# *********************

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file <> '' and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            # players or showers may be running so need terminate
            self.terminate("error")
        else:
            return value
Пример #14
0
class MediaShow:

    # *******************
    # External interface
    # ********************

    def __init__(self, show_params, root, canvas, showlist, pp_dir, pp_home,
                 pp_profile):
        """ canvas - the canvas that the menu is to be written on
            show - the dictionary fo the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show_params = show_params
        self.showlist = showlist
        self.root = root
        self.canvas = canvas
        self.pp_dir = pp_dir
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        # Init variables
        self.player = None
        self.shower = None
        self.poll_for_interval_timer = None
        self.poll_for_continue_timer = None
        self.waiting_for_interval = False
        self.interval_timer = None
        self.duration_timer = None
        self.error = False

        self.interval_timer_signal = False
        self.end_trigger_signal = False
        self.end_mediashow_signal = False
        self.next_track_signal = False
        self.previous_track_signal = False
        self.play_child_signal = False
        self.req_next = 'nil'

        #create and instance of TimeOfDay scheduler so we can add events
        self.tod = TimeOfDay()

        self.state = 'closed'

    def play(self,
             show_id,
             end_callback,
             show_ready_callback,
             top=False,
             command='nil'):
        """ displays the mediashow
              end_callback - function to be called when the menu exits
              ready_callback - callback when menu is ready to display (not used)
              top is True when the show is top level (run from [start])
        """

        #instantiate the arguments
        self.show_id = show_id
        self.end_callback = end_callback
        self.show_ready_callback = show_ready_callback
        self.top = top
        self.command = command
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Starting show")

        # check  data files are available.
        self.media_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self, "Medialist file not found: " + self.media_file)
            self.end('error', "Medialist file not found")

        #create a medialist for the mediashow and read it.
        self.medialist = MediaList()
        if self.medialist.open_list(self.media_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self.end('error', "Version of medialist different to Pi Presents")

        #get controls for this show if top level
        controlsmanager = ControlsManager()
        if self.top == True:
            self.controls_list = controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list = controlsmanager.merge_show_controls(
                self.controls_list, self.show_params['controls'])

        #set up the time of day triggers for the show
        if self.show_params['trigger'] in ('time', 'time-quiet'):
            error_text = self.tod.add_times(self.show_params['trigger-input'],
                                            id(self), self.tod_start_callback,
                                            self.show_params['trigger'])
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        if self.show_params['trigger-end'] == 'time':
            # print self.show_params['trigger-end-time']
            error_text = self.tod.add_times(
                self.show_params['trigger-end-time'], id(self),
                self.tod_end_callback, 'n/a')
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        if self.show_params['trigger-end'] == 'duration':
            error_text = self.calculate_duration(
                self.show_params['trigger-end-time'])
            if error_text <> '':
                self.mon.err(self, error_text)
                self.end('error', error_text)

        self.state = 'closed'
        self.egg_timer = None
        self.wait_for_trigger()

# ********************************
# Respond to external events
# ********************************

#stop received from another concurrent show

    def managed_stop(self):
        # if next lower show is running pass down to stop the show and lower level
        if self.shower <> None:
            self.shower.managed_stop()
        else:
            #stop the show if not at top
            self.end_mediashow_signal = True
            # and if track is runing stop that first
            if self.player <> None:
                self.player.input_pressed('stop')

    # kill or error
    def terminate(self, reason):
        if self.shower <> None:
            self.shower.terminate(reason)
        elif self.player <> None:
            self.player.terminate(reason)
        else:
            self.end(reason,
                     ' terminated with no shower or player to terminate')

# respond to input events

    def input_pressed(self, symbol, edge, source):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": received input: " + symbol)

        #  check symbol against mediashow triggers, triggers can be used at top or lower level
        # and not affected by disable-controls

        if self.state == 'waiting' and self.show_params['trigger'] in (
                'input',
                'input-quiet') and symbol == self.show_params['trigger-input']:
            self.start_show()
        elif self.state == 'playing' and self.show_params[
                'trigger-next'] == 'input' and symbol == self.show_params[
                    'next-input']:
            self.next()

    # internal functions are triggered only when disable-controls is  'no'
        if self.show_params['disable-controls'] == 'yes':
            return

        # if at top convert symbolic name to operation otherwise lower down we have received an operation
        # look through list of standard symbols to find match (symbolic-name, function name) operation =lookup (symbol
        if self.top == True:
            operation = self.lookup_control(symbol, self.controls_list)
        else:
            operation = symbol

        # print 'operation',operation
        self.do_operation(operation, edge, source)

    #service the standard inputs for this show

    def do_operation(self, operation, edge, source):
        if self.shower <> None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation, edge, source)
        else:
            # control this show and its tracks
            # print 'operation',operation
            if operation == 'stop':
                if self.top == False:
                    # not at top so stop the current show
                    self.end_mediashow_signal = True
                    # and if a track is running stop that first
                    if self.player <> None:
                        self.player.input_pressed('stop')
                else:
                    # top = True, just stop track if running
                    if self.player <> None:
                        self.player.input_pressed('stop')

            elif operation in ('up', 'down'):
                #if playing rather than waiting use keys for next or previous
                if operation == 'up' and self.state == 'playing':
                    self.previous()
                else:
                    self.next()

            elif operation == 'play':
                # use 'play' to start child if state=playing or to trigger the show if waiting for trigger
                if self.state == 'playing':
                    if self.show_params['has-child'] == 'yes':
                        self.play_child_signal = True
                        self.child_track_ref = 'pp-child-show'
                        # and stop the current track if its running
                        if self.player <> None:
                            self.player.input_pressed('stop')
                else:
                    if self.state == 'waiting':
                        self.start_show()

            elif operation == 'pause':
                if self.player <> None:
                    self.player.input_pressed(operation)

            #if the operation is omxplayer or mplayer runtime control then pass it to player if running
            elif operation[0:4] == 'omx-' or operation[
                    0:6] == 'mplay-' or operation[0:5] == 'uzbl-':
                if self.player <> None:
                    self.player.input_pressed(operation)

    def lookup_control(self, symbol, controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

# ***************************
# Show sequencer
# ***************************

    def end_interval_timer(self):
        self.interval_timer_signal = True

    # callback from time of day scheduler
    def tod_start_callback(self):
        if self.state == 'waiting' and self.show_params['trigger'] in (
                'time', 'time-quiet'):
            self.start_show()

    def tod_end_callback(self):
        if self.state == 'playing' and self.show_params['trigger-end'] in (
                'time', 'duration'):
            self.end_trigger_signal = True
            if self.shower <> None:
                self.shower.input_pressed('stop')
            elif self.player <> None:
                self.player.input_pressed('stop')

    def stop(self, message):
        self.end_mediashow_signal = True
        if self.interval_timer <> None:
            self.canvas.after_cancel(self.interval_timer)

    def next(self):
        # stop track if running and set signal
        self.next_track_signal = True
        if self.shower <> None:
            self.shower.input_pressed("stop")
        else:
            if self.player <> None:
                self.player.input_pressed("stop")

    def previous(self):
        self.previous_track_signal = True
        if self.shower <> None:
            self.shower.input_pressed("stop")
        else:
            if self.player <> None:
                self.player.input_pressed("stop")

    # wait for trigger sets the state to waiting so that events can do a start show.
    def wait_for_trigger(self):
        self.state = 'waiting'
        if self.show_ready_callback <> None:
            self.show_ready_callback()

        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Waiting for trigger: " + self.show_params['trigger'])

        if self.show_params['trigger'] == "input":
            # blank screen waiting for trigger if auto, otherwise display something
            if self.show_params['progress'] == "manual":
                text = self.resource('mediashow', 'm01')
            else:
                text = self.resource('mediashow', 'm02')
            self.display_message(self.canvas, 'text', text, 0, self.start_show)

        elif self.show_params['trigger'] == "input-quiet":
            # blank screen waiting for trigger
            text = self.resource('mediashow', 'm10')
            self.display_message(self.canvas, 'text', text, 0, self.start_show)
            pass

        elif self.show_params['trigger'] in ('time', 'time-quiet'):
            # show next show notice
            quiet = 3
            # if next show is this one display text
            next_show = self.tod.next_event_time()
            if next_show[quiet] == False:
                if next_show[1] == 'tomorrow':
                    text = self.resource('mediashow', 'm09')
                else:
                    text = self.resource('mediashow', 'm08')
                text = text.replace('%tt', next_show[0])
                self.display_message(self.canvas, 'text', text, 0,
                                     self.start_show)

        elif self.show_params['trigger'] == "start":
            self.start_show()

        else:
            self.mon.err(self,
                         "Unknown trigger: " + self.show_params['trigger'])
            self.end('error', "Unknown trigger type")

    def start_show(self):
        self.state = 'playing'
        self.direction = 'forward'
        # self.canvas.delete(ALL)
        # start interval timer
        if self.show_params['repeat'] == "interval" and self.show_params[
                'repeat-interval'] <> 0:
            self.interval_timer_signal = False
            self.interval_timer = self.canvas.after(
                int(self.show_params['repeat-interval']) * 1000,
                self.end_interval_timer)

        # start duration timer
        if self.show_params['trigger-end'] == 'duration':
            # print 'set alarm ', self.duration
            self.duration_timer = self.canvas.after(self.duration * 1000,
                                                    self.tod_end_callback)

        # and play the first track unless commanded otherwise
        if self.command == 'backward':
            self.medialist.finish()
        else:
            self.medialist.start()
        self.play_selected_track(self.medialist.selected_track())

    def what_next(self):
        self.direction = 'forward'

        # end of show trigger caused by tod
        if self.end_trigger_signal == True:
            self.end_trigger_signal = False
            if self.top == True:
                self.state = 'waiting'
                self.wait_for_trigger()
            else:
                # not at top so stop the show
                self.end('normal', 'sub-show end time trigger')

        # user wants to end, wait for any shows or tracks to have ended then end show
        # probalby will get here with end_m set when player and shower has finished
        elif self.end_mediashow_signal == True:
            if self.player == None and self.shower == None:
                self.end_mediashow_signal = False
                self.end('normal', "show ended by user")

        #returning from a subshow needing to move onward
        elif self.req_next == 'do-next':
            self.req_next = 'nil'
            self.medialist.next(self.show_params['sequence'])
            self.play_selected_track(self.medialist.selected_track())

        #returning from a subshow needing to move backward
        elif self.req_next == 'do-previous':
            self.req_next = 'nil'
            self.direction = 'backward'
            self.medialist.previous(self.show_params['sequence'])
            self.play_selected_track(self.medialist.selected_track())

        # user wants to play child
        elif self.play_child_signal == True:
            self.play_child_signal = False
            index = self.medialist.index_of_track(self.child_track_ref)
            if index >= 0:
                #don't use select the track as need to preserve mediashow sequence.
                child_track = self.medialist.track(index)
                self.display_eggtimer(self.resource('mediashow', 'm07'))
                self.play_selected_track(child_track)
            else:
                self.mon.err(
                    self, "Child show not found in medialist: " +
                    self.show_params['pp-child-show'])
                self.end('error', "child show not found in medialist")

        # skip to next track on user input
        elif self.next_track_signal == True:
            self.next_track_signal = False
            if self.medialist.at_end() == True:
                if self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'oneshot' and self.top == False:
                    self.end('do-next', "Return from Sub Show")
                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'single-run' and self.top == False:
                    self.end('do-next', "Return from Sub Show")
                else:
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.next(self.show_params['sequence'])
                self.play_selected_track(self.medialist.selected_track())

        # skip to previous track on user input
        elif self.previous_track_signal == True:
            self.previous_track_signal = False
            self.direction = 'backward'
            if self.medialist.at_start() == True:
                if self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'oneshot' and self.top == False:
                    self.end('do-previous', "Return from Sub Show")
                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'single-run' and self.top == False:
                    self.end('do-previous', "Return from Sub Show")
                else:
                    self.medialist.previous(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())
            else:
                self.medialist.previous(self.show_params['sequence'])
                self.play_selected_track(self.medialist.selected_track())

        # track is finished and we are on auto
        elif self.show_params['progress'] == "auto":

            if self.medialist.at_end() == True:

                # oneshot
                if self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'oneshot' and self.top == False:
                    self.end('normal', "End of Oneshot in subshow")

                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'oneshot' and self.top == True:
                    self.wait_for_trigger()

                # single run
                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'single-run' and self.top == True:
                    self.end('normal', "End of Single Run")

                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'single-run' and self.top == False:
                    self.end('do-next',
                             "End of single run - Return from Sub Show")

                # repeating and waiting to restart
                elif self.waiting_for_interval == True:
                    if self.interval_timer_signal == True:
                        self.interval_timer_signal = False
                        self.waiting_for_interval = False
                        self.start_show()
                    else:
                        self.poll_for_interval_timer = self.canvas.after(
                            1000, self.what_next)

                elif self.show_params[
                        'sequence'] == "ordered" and self.show_params[
                            'repeat'] == 'interval' and int(
                                self.show_params['repeat-interval']) > 0:
                    self.waiting_for_interval = True
                    self.poll_for_interval_timer = self.canvas.after(
                        1000, self.what_next)

                #elif self.show_params['sequence']=="ordered" and self.show_params['repeat']=='interval' and int(self.show_params['repeat-interval'])==0:
                elif self.show_params['repeat'] == 'interval' and int(
                        self.show_params['repeat-interval']) == 0:
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())

                # shuffling so there is no end condition
                elif self.show_params['sequence'] == "shuffle":
                    self.medialist.next(self.show_params['sequence'])
                    self.play_selected_track(self.medialist.selected_track())

                else:
                    self.mon.err(
                        self, "Unhandled playing event: " +
                        self.show_params['sequence'] + ' with ' +
                        self.show_params['repeat'] + " of " +
                        self.show_params['repeat-interval'])
                    self.end('error', "Unhandled playing event")

            else:
                self.medialist.next(self.show_params['sequence'])
                self.play_selected_track(self.medialist.selected_track())

        # track has finished and we are on manual progress
        elif self.show_params['progress'] == "manual":
            self.delete_eggtimer()
            self.canvas.delete('pp-content')
            if self.show_params['trigger-next'] == 'input':
                self.display_eggtimer(self.resource('mediashow', 'm03'))
            self.poll_for_continue_timer = self.canvas.after(
                2000, self.what_next)

        else:
            #unhandled state
            self.mon.err(self, "Unhandled playing event: ")
            self.end('error', "Unhandled playing event")

# ***************************
# Dispatching to Players/Shows
# ***************************

    def ready_callback(self):
        self.delete_eggtimer()

    def play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """
        self.delete_eggtimer()
        if self.show_params['progress'] == "manual":
            self.display_eggtimer(self.resource('mediashow', 'm04'))

        # is menu required
        if self.show_params['has-child'] == "yes":
            self.enable_child = True
        else:
            self.enable_child = False

        #dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Track type is: " + track_type)
        if track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=self.enable_child)

        elif track_type == "audio":
            # create a audioplayer
            track_file = self.complete_path(selected_track)
            self.player = AudioPlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=self.enable_child)

        elif track_type == "web":
            # create a browser
            track_file = self.complete_path(selected_track)
            self.player = BrowserPlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=self.enable_child)

        elif track_type == "image":
            track_file = self.complete_path(selected_track)
            # images played from menus don't have children
            self.player = ImagePlayer(self.show_id, self.root, self.canvas,
                                      self.show_params, selected_track,
                                      self.pp_dir, self.pp_home,
                                      self.pp_profile)
            self.player.play(track_file,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=self.enable_child)

        elif track_type == "message":
            # bit odd because MessagePlayer is used internally to display text.
            text = selected_track['text']
            self.player = MessagePlayer(self.show_id, self.root, self.canvas,
                                        self.show_params, selected_track,
                                        self.pp_dir, self.pp_home,
                                        self.pp_profile)
            self.player.play(text,
                             self.showlist,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=self.enable_child)

        elif track_type == "show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self.end('error', "Unknown show")

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.root, self.canvas,
                                        self.showlist, self.pp_dir,
                                        self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command=self.direction)

            elif selected_show['type'] == "liveshow":
                self.shower = LiveShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "radiobuttonshow":
                self.shower = RadioButtonShow(selected_show, self.root,
                                              self.canvas, self.showlist,
                                              self.pp_dir, self.pp_home,
                                              self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "hyperlinkshow":
                self.shower = HyperlinkShow(selected_show, self.root,
                                            self.canvas, self.showlist,
                                            self.pp_dir, self.pp_home,
                                            self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.root, self.canvas,
                                       self.showlist, self.pp_dir,
                                       self.pp_home, self.pp_profile)
                self.shower.play(self.show_id,
                                 self.end_shower,
                                 self.ready_callback,
                                 top=False,
                                 command='nil')

            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self.end('error' "Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self.end('error', "Unknown track type")

    def end_player(self, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Returned from player with message: " + message)
        self.player = None
        self.req_next = 'nil'
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            # elif>else move to what-next?
            if self.show_params['progress'] == "manual":
                self.display_eggtimer(self.resource('mediashow', 'm05'))
                self.req_next = reason
                self.what_next()
            else:
                self.req_next = reason
                self.what_next()

    def end_shower(self, show_id, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Returned from shower with message: " + message)
        self.shower = None
        self.req_next = 'nil'
        if reason in ("killed", "error"):
            self.end(reason, message)
        else:
            if self.show_params['progress'] == "manual":
                self.display_eggtimer(self.resource('mediashow', 'm06'))
                self.req_next = reason
                self.what_next()
            else:
                self.req_next = reason
                self.what_next()

# ***************************
# end of show
# ***************************

    def end(self, reason, message):
        self.end_mediashow_signal = False
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Ending Mediashow")
        self.tidy_up()
        self.end_callback(self.show_id, reason, message)
        self = None
        return

    def tidy_up(self):
        #clear outstanding time of day events for this show
        # self.tod.clear_times_list(id(self))
        if self.poll_for_continue_timer <> None:
            self.canvas.after_cancel(self.poll_for_continue_timer)
            self.poll_for_continue_timer = None
        if self.poll_for_interval_timer <> None:
            self.canvas.after_cancel(self.poll_for_interval_timer)
            self.poll_for_interval_timer = None
        if self.interval_timer <> None:
            self.canvas.after_cancel(self.interval_timer)
            self.interval_timer = None
        if self.duration_timer <> None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer = None

# ***************************
# displaying things
# ***************************

    def display_eggtimer(self, text):
        self.canvas.create_text(int(self.canvas['width']) / 2,
                                int(self.canvas['height']) / 2,
                                text=text,
                                fill='white',
                                font="Helvetica 20 bold",
                                tag='pp-eggtimer')
        self.canvas.update_idletasks()

    def delete_eggtimer(self):
        self.canvas.delete('pp-eggtimer')
        self.canvas.update_idletasks()

    # used to display internal messages in situations where a medialist entry could not be used.
    def display_message(self, canvas, source, content, duration,
                        _display_message_callback):
        self.display_message_callback = _display_message_callback
        tp = {
            'duration': duration,
            'message-colour': 'white',
            'message-font': 'Helvetica 20 bold',
            'background-colour': '',
            'message-justify': 'left',
            'background-image': '',
            'show-control-begin': '',
            'show-control-end': '',
            'animate-begin': '',
            'animate-clear': '',
            'animate-end': '',
            'message-x': '',
            'message-y': '',
            'display-show-background': 'no',
            'display-show-text': 'no',
            'show-text': '',
            'track-text': '',
            'plugin': ''
        }
        self.player = MessagePlayer(self.show_id, self.root, canvas, tp, tp,
                                    self.pp_dir, self.pp_home, self.pp_profile)
        self.player.play(content, self.showlist, self.display_message_end,
                         None, False)

    def display_message_end(self, reason, message):
        self.player = None
        if reason in ('error', 'killed'):
            self.end(reason, message)
        else:
            self.display_message_callback()


# ***************************
# utilities
# ***************************

    def calculate_duration(self, line):
        fields = line.split(':')
        if len(fields) == 1:
            secs = fields[0]
            minutes = '0'
            hours = '0'
        if len(fields) == 2:
            secs = fields[1]
            minutes = fields[0]
            hours = '0'
        if len(fields) == 3:
            secs = fields[2]
            minutes = fields[1]
            hours = fields[0]
        self.duration = 3600 * long(hours) + 60 * long(minutes) + long(secs)
        return ''

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            self.terminate("error")
        else:
            return value

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file <> '' and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Track to play is: " + track_file)
        return track_file
Пример #15
0
class RadioButtonShow:
    """
        starts at 'first-track' which can be any type of track or a show
        The show has links of the form symbolic-name play track-ref
        key, gpio or click area will play the referenced track
        at the end of that track control will return to first-track
        links in the tracks are ignored. Links are inherited from the show.
        timeout returns to first-track

        interface:
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
    """

# *********************
# external interface
# ********************

    def __init__(self,
                            show_params,
                             root,
                            canvas,
                            showlist,
                             pp_dir,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the tracks of the event show are to be written on
            show_params - the name of the configuration dictionary section for the radiobuttonshow
            showlist  - the showlist, to enable runningnof show type tracks.
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """
        
        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show_params=show_params
        self.showlist=showlist
        self.root=root
        self.canvas=canvas
        self.pp_dir=pp_dir
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

      
        #create a path stack - only used to parse the links.
        self.path = PathManager()
        
        # init variables
        self.drawn  = None
        self.player=None
        self.shower=None
        self.timeout_running=None
        self.error=False



    def play(self,show_id,end_callback,ready_callback,top=False,command='nil'):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              ready_callback - callback when event-show is ready to display its forst track (not used?)
              top is True when the show is top level (run from [start] or from show control)
              command is not used
        """
        
        #instantiate arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.command=command

        # check data files are available.
        self.medialist_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.medialist_file):
            self.mon.err(self,"Medialist file not found: "+ self.medialist_file)
            self.end('error',"Medialist file not found")
        
        #create a medialist object for the radiobuttonshow and read the file into it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.medialist_file,self.showlist.sissue()) == False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self.end('error',"Version of medialist different to Pi Presents")
        
        # read show destinations
        self.first_track_ref=self.show_params['first-track-ref']

        #get control bindings for this show if top level
        controlsmanager=ControlsManager()
        if self.top==True:
            self.controls_list=controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list=controlsmanager.merge_show_controls(self.controls_list,self.show_params['controls'])


        #read the show links. Track links will be added by ready_callback
        links_text=self.show_params['links']
        reason,message,self.links=self.path.parse_links(links_text)
        if reason=='error':
            self.mon.err(self,message + " in show")
            self.end('error',message)
        
        # state variables and signals   
        self.end_radiobuttonshow_signal= False
        self.egg_timer=None
        self.next_track_signal=False
        self.next_track_ref=''
        self.current_track_ref=''
        self.current_track_type=''

        # ready callback for show
        if self.ready_callback<>None:
            self.ready_callback()
                    
        self.canvas.delete('pp-content')
        self.canvas.config(bg='black')
        
        self.do_first_track()

        
#stop received from another concurrent show via ShowManager

    def managed_stop(self):
            # set signal to stop the radiobuttonshow when all  sub-shows and players have ended
            self.end_radiobuttonshow_signal=True
            # then stop and shows or tracks.
            if self.shower<>None:
                self.shower.managed_stop()
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.end('normal','stopped by ShowManager')
                

    # kill or error
    def terminate(self,reason):
        self.end_radiobuttonshow_signal=True
        if self.shower<>None:
            self.shower.terminate(reason)
        elif self.player<>None:
            self.player.terminate(reason)
        else:
            self.end(reason,'terminated without terminating shower or player')


   # respond to inputs
    def input_pressed(self,symbol,edge,source):

        self.mon.log(self,"received symbol: " + symbol)

        #does the symbol match a link, if so execute it
        if self.is_link(symbol,edge,source)==True:
            return

        # controls are disabled so ignore inputs
        if self.show_params['disable-controls']=='yes':
            return

        # does it match a control       
        # if at top convert symbolic name to operation otherwise lower down we have received an operatio    
        # look through list of controls to find match
        if self.top==True:
            operation=self.lookup_control(symbol,self.controls_list)
        else:
            operation=symbol
        # print 'operation',operation 
        if operation<>'':
            self.do_operation(operation,edge,source)


    def do_operation(self,operation,edge,source):
        if self.shower<>None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation,edge,source)
        else:
            #control this show and its tracks
            if operation=='stop':
                if self.player<>None:
                    if self.current_track_ref==self.first_track_ref and self.top==False:
                        self.end_radiobuttonshow_signal=True
                    self.player.input_pressed('stop')
                    
            elif operation == 'pause':
                if self.player<>None:
                    self.player.input_pressed(operation)
                    
            elif operation[0:4]=='omx-' or operation[0:6]=='mplay-'or operation[0:5]=='uzbl-':
                if self.player<>None:
                    self.player.input_pressed(operation)


    def lookup_control(self,symbol,controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''


    def is_link(self,symbol,edge,source):
        # we have links which locally define symbolic names to be converted to radiobuttonshow operations
        # find the first entry in links that matches the symbol and execute its operation
        #print 'radiobuttonshow ',symbol
        found=False
        for link in self.links:
            #print link
            if symbol==link[0]:
                found=True
                if link[1]<>'null':
                    #print 'match',link[0]
                    link_operation=link[1]
                    if link_operation=='play':
                        self.do_play(link[2],edge,source)
        return found



# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************


    def timeout_callback(self):
        self.do_play(self.first_track_ref,'front','timeout')

    def do_play(self,track_ref,edge,source):
        if track_ref<>self.current_track_ref:
            # print 'executing play ',track_ref
            self.next_track_signal=True
            self.next_track_op='play'
            self.next_track_arg=track_ref
            if self.shower<>None:
                self.shower.input_pressed('stop',edge,source)
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.what_next()



    def do_first_track(self):
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >=0:
            #don't use select the track as not using selected_track in radiobuttonshow
            first_track=self.medialist.track(index)
            self.path.append(first_track['track-ref'])
            self.current_track_ref=self.first_track_ref
            self.play_selected_track(first_track)
        else:
            self.mon.err(self,"first-track not found in medialist: "+ self.show_params['first-frack-ref'])
            self.end('error',"first track not found in medialist")

            

    def what_next(self):
        # user wants to end the show 
        if self.end_radiobuttonshow_signal==True:
            self.end_radiobuttonshow_signal=False
            self.end('normal',"show ended by user")

        # user has selected another track
        elif self.next_track_signal==True:
                self.next_track_signal=False
                self.next_track_ref=self.next_track_arg        
                self.current_track_ref=self.next_track_ref                    
                index = self.medialist.index_of_track(self.next_track_ref)
                if index >=0:
                    #don't use select the track as not using selected_track in radiobuttonshow
                    next_track=self.medialist.track(index)
                    self.play_selected_track(next_track)
                else:
                    self.mon.err(self,"next-track not found in medialist: "+ self.next_track_ref)
                    self.end('error',"next track not found in medialist")
                    
        else:
            #track ends naturally
            self.next_track_ref=self.first_track_ref
            self.current_track_ref=self.next_track_ref                    
            index = self.medialist.index_of_track(self.next_track_ref)
            if index >=0:
                #don't use select the track as not using selected_track in radiobuttonshow
                next_track=self.medialist.track(index)
                self.play_selected_track(next_track)
            else:
                self.mon.err(self,"next-track not found in medialist: "+ self.next_track_ref)
                self.end('error',"next track not found in medialist")



# *********************
# Dispatching to Players
# *********************


    def page_callback(self):
        # called from a Player when ready to play, if first-track merge the links from the track with those from the show
        self.delete_eggtimer()
        if self.current_track_ref==self.first_track_ref:
            #links_text=self.player.get_links()
            #reason,message,track_links=self.path.parse_links(links_text)
            #if reason=='error':
                #self.mon.err(self,message + " in page")
                #self.end('error',message)
            #self.path.merge_links(self.links,track_links)
            pass

           
    def play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """     

        if self.timeout_running<>None:
            self.canvas.after_cancel(self.timeout_running)
            self.timeout_running=None
            
        # self.display_eggtimer(self.resource('radiobuttonshow','m01'))

        self.current_track_type = selected_track['type']
        

        #start timeout for the track if required           
             
        if self.current_track_ref<>self.first_track_ref and int(self.show_params['timeout'])<>0:
            self.timeout_running=self.canvas.after(int(self.show_params['timeout'])*1000,self.timeout_callback)
        

        # dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                        
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track)
            self.player=AudioPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                        
        elif track_type=="image":
            track_file=self.complete_path(selected_track)
            self.player=ImagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                    self.showlist,
                                    self.end_player,
                                    self.page_callback,
                                    enable_menu=False,
                                    )

        elif track_type=="web":
            # create a browser
            track_file=self.complete_path(selected_track)
            self.player=BrowserPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                    
                         
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(text,
                                    self.showlist,
                                    self.end_player,
                                    self.page_callback,
                                    enable_menu=False
                                    )

 
        elif track_type=="show":
            # self.enable_click_areas()
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self.end("Unknown show")
            
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                               self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                  self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                                  self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="radiobuttonshow":
                self.shower= RadioButtonShow(selected_show,
                                                         self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                         self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="hyperlinkshow":
                self.shower= HyperlinkShow(selected_show,
                                                       self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                       self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="menu": 
                self.shower= MenuShow(selected_show,
                                                          self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')                    
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self.end("Unknown show type")  
                
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self.end("Unknown track type")

    
    # callback from when player ends
    def end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        # this does not seem to change the colour of the polygon
        # self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks( )
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            #self.display_eggtimer(self.resource('radiobuttonshow','m02'))
            self.what_next()

    # callback from when shower ends
    def end_shower(self,show_id,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        # self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks( )
        if reason in ("killed","error"):
            self.end(reason,message)
        else:
            #self.display_eggtimer(self.resource('radiobuttonshow','m03'))
            self.what_next()  


# *********************
# End the show
# *********************
    # finish the player for killing, error or normally
    # this may be called directly sub/child shows or players are not running
    # if they might be running then need to call terminate.

    def end(self,reason,message):
        self.mon.log(self,"Ending radiobuttonshow: "+ self.show_params['show-ref'])  
        self.end_callback(self.show_id,reason,message)
        self=None
        return




# *********************
# displaying things
# *********************

    def display_eggtimer(self,text):
        #self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
                                              #int(self.canvas['height'])/2,
                                                  #text= text,
                                                # fill='white',
                                               # font="Helvetica 20 bold")
        #self.canvas.update_idletasks( )
        pass


    def delete_eggtimer(self):
        if self.egg_timer!=None:
            self.canvas.delete(self.egg_timer)

# *********************
# utilities
# *********************

    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     


    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            # players or showers may be running so need terminate
            self.terminate("error")
        else:
            return value
Пример #16
0
class PPEditor:



# ***************************************
# INIT
# ***************************************

    def __init__(self):
    
        self.editor_issue="1.2"

# get command options
        self.command_options=ed_options()

# get directory holding the code
        self.pp_dir=sys.path[0]
            
        if not os.path.exists(self.pp_dir+os.sep+"pp_editor.py"):
            tkMessageBox.showwarning("Pi Presents","Bad Application Directory")
            exit()
            
          
#Initialise logging
        Monitor.log_path=self.pp_dir
        self.mon=Monitor()
        self.mon.on()

        if self.command_options['debug'] == True:
            Monitor.global_enable=True
        else:
            Monitor.global_enable=False

        self.mon.log (self, "Pi Presents Editor is starting")

        self.mon.log (self," OS and separator " + os.name +'  ' + os.sep)
        self.mon.log(self,"sys.path[0] -  location of code: code "+sys.path[0])


 # set up the gui
 
        #root is the Tkinter root widget
        self.root = tk.Tk()
        self.root.title("Editor for Pi Presents")

        # self.root.configure(background='grey')

        self.root.resizable(False,False)

        #define response to main window closing
        self.root.protocol ("WM_DELETE_WINDOW", self.app_exit)

        # bind some display fields
        self.filename = tk.StringVar()
        self.display_selected_track_title = tk.StringVar()
        self.display_show = tk.StringVar()


# define menu
        menubar = Menu(self.root)

        profilemenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        profilemenu.add_command(label='Open', command = self.open_existing_profile)
        profilemenu.add_command(label='Validate', command = self.validate_profile)
        menubar.add_cascade(label='Profile', menu = profilemenu)

        ptypemenu = Menu(profilemenu, tearoff=0, bg="grey", fg="black")
        ptypemenu.add_command(label='Exhibit', command = self.new_exhibit_profile)
        ptypemenu.add_command(label='Media Show', command = self.new_mediashow_profile)
        ptypemenu.add_command(label='Menu', command = self.new_menu_profile)
        ptypemenu.add_command(label='Presentation', command = self.new_presentation_profile)
        ptypemenu.add_command(label='Interactive', command = self.new_interactive_profile)
        ptypemenu.add_command(label='Live Show', command = self.new_liveshow_profile)
        ptypemenu.add_command(label='RadioButton Show', command = self.new_radiobuttonshow_profile)
        ptypemenu.add_command(label='Hyperlink Show', command = self.new_hyperlinkshow_profile)
        ptypemenu.add_command(label='Blank', command = self.new_blank_profile)
        profilemenu.add_cascade(label='New from Template', menu = ptypemenu)
        
        showmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        showmenu.add_command(label='Delete', command = self.remove_show)
        showmenu.add_command(label='Edit', command = self.m_edit_show)
        showmenu.add_command(label='Copy To', command = self.copy_show)
        menubar.add_cascade(label='Show', menu = showmenu)

        stypemenu = Menu(showmenu, tearoff=0, bg="grey", fg="black")
        stypemenu.add_command(label='Menu', command = self.add_menu)
        stypemenu.add_command(label='MediaShow', command = self.add_mediashow)
        stypemenu.add_command(label='LiveShow', command = self.add_liveshow)
        stypemenu.add_command(label='HyperlinkShow', command = self.add_hyperlinkshow)
        stypemenu.add_command(label='RadioButtonShow', command = self.add_radiobuttonshow)
        showmenu.add_cascade(label='Add', menu = stypemenu)
        
        medialistmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='MediaList', menu = medialistmenu)
        medialistmenu.add_command(label='Add', command = self.add_medialist)
        medialistmenu.add_command(label='Delete', command = self.remove_medialist)
      
        trackmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        trackmenu.add_command(label='Delete', command = self.remove_track)
        trackmenu.add_command(label='Edit', command = self.m_edit_track)
        trackmenu.add_command(label='Add from Dir', command = self.add_tracks_from_dir)
        trackmenu.add_command(label='Add from File', command = self.add_track_from_file)


        menubar.add_cascade(label='Track', menu = trackmenu)

        typemenu = Menu(trackmenu, tearoff=0, bg="grey", fg="black")
        typemenu.add_command(label='Video', command = self.new_video_track)
        typemenu.add_command(label='Audio', command = self.new_audio_track)
        typemenu.add_command(label='Image', command = self.new_image_track)
        typemenu.add_command(label='Web', command = self.new_web_track)
        typemenu.add_command(label='Message', command = self.new_message_track)
        typemenu.add_command(label='Show', command = self.new_show_track)
        typemenu.add_command(label='Menu Background', command = self.new_menu_background_track)
        typemenu.add_command(label='Child Show', command = self.new_child_show_track)
        trackmenu.add_cascade(label='New', menu = typemenu)

        toolsmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Tools', menu = toolsmenu)
        toolsmenu.add_command(label='Update All', command = self.update_all)
        
        optionsmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Options', menu = optionsmenu)
        optionsmenu.add_command(label='Edit', command = self.edit_options)

        helpmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Help', menu = helpmenu)
        helpmenu.add_command(label='Help', command = self.show_help)
        helpmenu.add_command(label='About', command = self.about)
         
        self.root.config(menu=menubar)

        top_frame=Frame(self.root)
        top_frame.pack(side=TOP)
        bottom_frame=Frame(self.root)
        bottom_frame.pack(side=TOP, fill=BOTH, expand=1)        

        left_frame=Frame(bottom_frame, padx=5)
        left_frame.pack(side=LEFT)
        middle_frame=Frame(bottom_frame,padx=5)
        middle_frame.pack(side=LEFT)              
        right_frame=Frame(bottom_frame,padx=5,pady=10)
        right_frame.pack(side=LEFT)
        updown_frame=Frame(bottom_frame,padx=5)
        updown_frame.pack(side=LEFT)
        
        tracks_title_frame=Frame(right_frame)
        tracks_title_frame.pack(side=TOP)
        tracks_label = Label(tracks_title_frame, text="Tracks in Selected Medialist")
        tracks_label.pack()
        tracks_frame=Frame(right_frame)
        tracks_frame.pack(side=TOP)
        shows_title_frame=Frame(left_frame)
        shows_title_frame.pack(side=TOP)
        shows_label = Label(shows_title_frame, text="Shows")
        shows_label.pack()
        shows_frame=Frame(left_frame)
        shows_frame.pack(side=TOP)
        shows_title_frame=Frame(left_frame)
        shows_title_frame.pack(side=TOP)
        medialists_title_frame=Frame(left_frame)
        medialists_title_frame.pack(side=TOP)
        medialists_label = Label(medialists_title_frame, text="Medialists")
        medialists_label.pack()
        medialists_frame=Frame(left_frame)
        medialists_frame.pack(side=LEFT)
        
 # define buttons 

        add_button = Button(middle_frame, width = 5, height = 2, text='Edit\nShow',
                              fg='black', command = self.m_edit_show, bg="light grey")
        add_button.pack(side=RIGHT)
        
        add_button = Button(updown_frame, width = 5, height = 1, text='Add',
                              fg='black', command = self.add_track_from_file, bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame, width = 5, height = 1, text='Edit',
                              fg='black', command = self.m_edit_track, bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame, width = 5, height = 1, text='Up',
                              fg='black', command = self.move_track_up, bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame, width = 5, height = 1, text='Down',
                              fg='black', command = self.move_track_down, bg="light grey")
        add_button.pack(side=TOP)



# define display of showlist 
        scrollbar = Scrollbar(shows_frame, orient=tk.VERTICAL)
        self.shows_display = Listbox(shows_frame, selectmode=SINGLE, height=7,
                                    width = 40, bg="white",activestyle=NONE,
                                    fg="black", yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.shows_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.shows_display.pack(side=LEFT, fill=BOTH, expand=1)
        self.shows_display.bind("<ButtonRelease-1>", self.e_select_show)

    
# define display of medialists
        scrollbar = Scrollbar(medialists_frame, orient=tk.VERTICAL)
        self.medialists_display = Listbox(medialists_frame, selectmode=SINGLE, height=7,
                                    width = 40, bg="white",activestyle=NONE,
                                    fg="black",yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.medialists_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.medialists_display.pack(side=LEFT,  fill=BOTH, expand=1)
        self.medialists_display.bind("<ButtonRelease-1>", self.select_medialist)


# define display of tracks
        scrollbar = Scrollbar(tracks_frame, orient=tk.VERTICAL)
        self.tracks_display = Listbox(tracks_frame, selectmode=SINGLE, height=15,
                                    width = 40, bg="white",activestyle=NONE,
                                    fg="black",yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.tracks_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.tracks_display.pack(side=LEFT,fill=BOTH, expand=1)
        self.tracks_display.bind("<ButtonRelease-1>", self.e_select_track)


# initialise editor options class
        self.options=Options(self.pp_dir) #creates options file in code directory if necessary
        
# initialise variables      
        self.init()
        
#and enter Tkinter event loop
        self.root.mainloop()        


# ***************************************
# INIT AND EXIT
# ***************************************
    def app_exit(self):
        self.root.destroy()
        exit()


    def init(self):
        self.options.read()
        self.pp_home_dir = self.options.pp_home_dir
        self.initial_media_dir = self.options.initial_media_dir
        self.mon.log(self,"Data Home from options is "+self.pp_home_dir)
        self.mon.log(self,"Initial Media from options is "+self.initial_media_dir)
        self.current_medialist=None
        self.current_showlist=None
        self.current_show=None
        self.shows_display.delete(0,END)
        self.medialists_display.delete(0,END)
        self.tracks_display.delete(0,END)



# ***************************************
# MISCELLANEOUS
# ***************************************

    def edit_options(self):
        """edit the options then read them from file"""
        eo = OptionsDialog(self.root, self.options.options_file,'Edit Options')
        if eo.result==True: self.init()


    def show_help (self):
        tkMessageBox.showinfo("Help",
       "Read 'manual.pdf'")
  

    def about (self):
        tkMessageBox.showinfo("About","Editor for Pi Presents Profiles\n"
                   +"For profile version: " + self.editor_issue + "\nAuthor: Ken Thompson"+
                              "\nWebsite: http://pipresents.wordpress.com/")

    def validate_profile(self):
        val =Validator()
        val.validate_profile(self.root,self.pp_dir,self.pp_home_dir,self.pp_profile_dir,self.editor_issue,True)


    
# **************
# PROFILES
# **************

    def open_existing_profile(self):
        initial_dir=self.pp_home_dir+os.sep+"pp_profiles"
        if os.path.exists(initial_dir)==False:
            self.mon.err(self,"Home directory not found: " + initial_dir + "\n\nHint: Data Home option must end in pp_home")
            return
        dir_path=tkFileDialog.askdirectory(initialdir=initial_dir)
        # dir_path="C:\Users\Ken\pp_home\pp_profiles\\ttt"
        if len(dir_path)>0:
            self.open_profile(dir_path)
        

    def open_profile(self,dir_path):
        showlist_file = dir_path + os.sep + "pp_showlist.json"
        if os.path.exists(showlist_file)==False:
            self.mon.err(self,"Not a Profile: " + dir_path + "\n\nHint: Have you opened the profile directory?")
            return
        self.pp_profile_dir = dir_path
        self.root.title("Editor for Pi Presents - "+ self.pp_profile_dir)
        if self.open_showlist(self.pp_profile_dir)==False:
            self.init()
            return
        self.open_medialists(self.pp_profile_dir)
        self.refresh_tracks_display()


    def new_profile(self,profile):
        d = Edit1Dialog(self.root,"New Profile","Name", "")
        if d .result == None:
            return
        name=str(d.result)
        if name=="":
            tkMessageBox.showwarning("New Profile","Name is blank")
            return
        to = self.pp_home_dir + os.sep + "pp_profiles"+ os.sep + name
        if os.path.exists(to)== True:
            tkMessageBox.showwarning( "New Profile","Profile exists\n(%s)" % to )
            return
        shutil.copytree(profile, to, symlinks=False, ignore=None)
        self.open_profile(to)


        
    def new_exhibit_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_exhibit"
        self.new_profile(profile)

    def new_interactive_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_interactive"
        self.new_profile(profile)

    def new_menu_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_menu"
        self.new_profile(profile)

    def new_presentation_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_presentation"
        self.new_profile(profile)

    def new_blank_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_blank"
        self.new_profile(profile)

    def new_mediashow_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_mediashow"
        self.new_profile(profile)
        
    def new_liveshow_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_liveshow"
        self.new_profile(profile)

    def new_radiobuttonshow_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_radiobuttonshow"
        self.new_profile(profile)

    def new_hyperlinkshow_profile(self):
        profile = self.pp_dir+"/pp_home/pp_profiles/ppt_hyperlinkshow"
        self.new_profile(profile)

# ***************************************
# Shows
# ***************************************

    def open_showlist(self,dir):
        showlist_file = dir + os.sep + "pp_showlist.json"
        if os.path.exists(showlist_file)==False:
            self.mon.err(self,"showlist file not found at " + dir + "\n\nHint: Have you opened the profile directory?")
            self.app_exit()
        self.current_showlist=ShowList()
        self.current_showlist.open_json(showlist_file)
        if float(self.current_showlist.sissue())<float(self.editor_issue) or  (self.command_options['forceupdate'] == True and float(self.current_showlist.sissue())==float(self.editor_issue)):
            self.update_profile()
            self.mon.err(self,"Version of profile has been updated to "+self.editor_issue+", please re-open")
            return False
        if float(self.current_showlist.sissue())>float(self.editor_issue):
            self.mon.err(self,"Version of profile is greater than editor, must exit")
            self.app_exit()
        self.refresh_shows_display()
        return True


    def save_showlist(self,dir):
        if self.current_showlist<>None:
            showlist_file = dir + os.sep + "pp_showlist.json"
            self.current_showlist.save_list(showlist_file)
            
    def add_eventshow(self):
        self.add_show(PPdefinitions.new_shows['eventshow'])

    def add_mediashow(self):
        self.add_show(PPdefinitions.new_shows['mediashow'])

    def add_liveshow(self):
        self.add_show(PPdefinitions.new_shows['liveshow'])

    def add_radiobuttonshow(self):
        self.add_show(PPdefinitions.new_shows['radiobuttonshow'])

    def add_hyperlinkshow(self):
        self.add_show(PPdefinitions.new_shows['hyperlinkshow'])
        
    def add_menu(self):
        self.add_show(PPdefinitions.new_shows['menu'])

    def add_start(self):  
        self.add_show(PPdefinitions.new_shows['start'])


    def add_show(self,default):
        # append it to the showlist and then add the medialist
        if self.current_showlist<>None:
            d = Edit1Dialog(self.root,"AddShow",
                                "Show Reference", "")
            if d.result == None:
                return
            name=str(d.result)
            if name=="":
                tkMessageBox.showwarning(
                                "Add Show",
                                "Name is blank"
                                        )
                return
            if self.current_showlist.index_of_show(name)<>-1:
                tkMessageBox.showwarning(
                                "Add Show",
                                "A Show with this name already exists"
                                        )
                return            
            copied_show=self.current_showlist.copy(default,name)
            mediafile=self.add_medialist(name)
            if mediafile<>'':
                copied_show['medialist']=mediafile
            self.current_showlist.append(copied_show)
            self.save_showlist(self.pp_profile_dir)
            self.refresh_shows_display()

            
    def remove_show(self):
        if  self.current_showlist<>None and self.current_showlist.length()>0 and self.current_showlist.show_is_selected():
            if tkMessageBox.askokcancel("Delete Show","Delete Show"):
                index= self.current_showlist.selected_show_index()
                self.current_showlist.remove(index)
                self.save_showlist(self.pp_profile_dir)
                self.refresh_shows_display()

    def show_refs(self):
        _show_refs=[]
        for index in range(self.current_showlist.length()):
            if self.current_showlist.show(index)['show-ref']<>"start":
                _show_refs.append(copy.deepcopy(self.current_showlist.show(index)['show-ref']))
        return _show_refs
 
    def refresh_shows_display(self):
        self.shows_display.delete(0,self.shows_display.size())
        for index in range(self.current_showlist.length()):
            self.shows_display.insert(END, self.current_showlist.show(index)['title']+"   ["+self.current_showlist.show(index)['show-ref']+"]")        
        if self.current_showlist.show_is_selected():
            self.shows_display.itemconfig(self.current_showlist.selected_show_index(),fg='red')            
            self.shows_display.see(self.current_showlist.selected_show_index())

            
    def e_select_show(self,event):
        if self.current_showlist<>None and self.current_showlist.length()>0:
            mouse_item_index=int(event.widget.curselection()[0])
            self.current_showlist.select(mouse_item_index)
            self.refresh_shows_display()

    def copy_show(self):
        if  self.current_showlist<>None and self.current_showlist.show_is_selected():
            self.add_show(self.current_showlist.selected_show())

        
    def m_edit_show(self):
        self.edit_show(PPdefinitions.show_types,PPdefinitions.show_field_specs)
        
     

    def edit_show(self,show_types,field_specs):
        if self.current_showlist<>None and self.current_showlist.show_is_selected():
            d=EditItem(self.root,"Edit Show",self.current_showlist.selected_show(),show_types,field_specs,self.show_refs(),
                       self.initial_media_dir,self.pp_home_dir,'show')
            if d.result == True:

                self.save_showlist(self.pp_profile_dir)
                self.refresh_shows_display()

 

# ***************************************
#   Medialists
# ***************************************

    def open_medialists(self,dir):
        self.medialists = []
        for file in os.listdir(dir):
            if file.endswith(".json") and file<>'pp_showlist.json':
                self.medialists = self.medialists + [file]
        self.medialists_display.delete(0,self.medialists_display.size())
        for index in range (len(self.medialists)):
            self.medialists_display.insert(END, self.medialists[index])
        self.current_medialists_index=-1
        self.current_medialist=None


    def add_medialist(self,name=None):
        if name==None:
            d = Edit1Dialog(self.root,"Add Medialist",
                                    "File", "")
            if d.result == None:
                return ''
            name=str(d.result)
            if name=="":
                tkMessageBox.showwarning(
                                    "Add medialist",
                                    "Name is blank"
                                            )
                return ''
            
        if not name.endswith(".json"):
            name=name+(".json")
                
        path = self.pp_profile_dir + os.sep + name
        if os.path.exists(path)== True:
                tkMessageBox.showwarning("Add medialist","Medialist file exists\n(%s)" % path)
                return ''
        nfile = open(path,'wb')
        nfile.write("{")
        nfile.write("\"issue\":  \""+self.editor_issue+"\",\n")
        nfile.write("\"tracks\": [")
        nfile.write("]")
        nfile.write("}")
        nfile.close()
        # append it to the list
        self.medialists.append(copy.deepcopy(name))
        # add title to medialists display
        self.medialists_display.insert(END, name)  
        # and set it as the selected medialist
        self.refresh_medialists_display()
        return name


    def remove_medialist(self):
        if self.current_medialist<>None:
            if tkMessageBox.askokcancel("Delete Medialist","Delete Medialist"):
                os.remove(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index])
                self.open_medialists(self.pp_profile_dir)
                self.refresh_medialists_display()
                self.refresh_tracks_display()


    def select_medialist(self,event):
        """
        user clicks on a medialst in a profile so try and select it.
        """
        # needs forgiving int for possible tkinter upgrade
        if len(self.medialists)>0:
            self.current_medialists_index=int(event.widget.curselection()[0])
            self.current_medialist=MediaList()
            if not self.current_medialist.open_list(self.pp_profile_dir+ os.sep + self.medialists[self.current_medialists_index],self.current_showlist.sissue()):
                self.mon.err(self,"medialist is a different version to showlist: "+ self.medialists[self.current_medialists_index])
                self.app_exit()        
            self.refresh_tracks_display()
            self.refresh_medialists_display()


    def refresh_medialists_display(self):
        self.medialists_display.delete(0,len(self.medialists))
        for index in range (len(self.medialists)):
            self.medialists_display.insert(END, self.medialists[index])
        if self.current_medialist<>None:
            self.medialists_display.itemconfig(self.current_medialists_index,fg='red')
            self.medialists_display.see(self.current_medialists_index)

    def save_medialist(self):
        basefile=self.medialists[self.current_medialists_index]
        #print type(basefile)
        # basefile=str(basefile)
        #print type(basefile)
        file = self.pp_profile_dir+ os.sep + basefile
        self.current_medialist.save_list(file)

          
# ***************************************
#   Tracks
# ***************************************
                
    def refresh_tracks_display(self):
        self.tracks_display.delete(0,self.tracks_display.size())
        if self.current_medialist<>None:
            for index in range(self.current_medialist.length()):
                if self.current_medialist.track(index)['track-ref']<>"":
                    track_ref_string="  ["+self.current_medialist.track(index)['track-ref']+"]"
                else:
                    track_ref_string=""
                self.tracks_display.insert(END, self.current_medialist.track(index)['title']+track_ref_string)        
            if self.current_medialist.track_is_selected():
                self.tracks_display.itemconfig(self.current_medialist.selected_track_index(),fg='red')            
                self.tracks_display.see(self.current_medialist.selected_track_index())
            
    def e_select_track(self,event):
        if self.current_medialist<>None and self.current_medialist.length()>0:
            mouse_item_index=int(event.widget.curselection()[0])
            self.current_medialist.select(mouse_item_index)
            self.refresh_tracks_display()

    def m_edit_track(self):
        self.edit_track(PPdefinitions.track_types,PPdefinitions.track_field_specs)

    def edit_track(self,track_types,field_specs):      
        if self.current_medialist<>None and self.current_medialist.track_is_selected():
            d=EditItem(self.root,"Edit Track",self.current_medialist.selected_track(),track_types,field_specs,self.show_refs(),
                       self.initial_media_dir,self.pp_home_dir,'track')
            if d.result == True:
                self.save_medialist()
            self.refresh_tracks_display()

    def move_track_up(self):
        if self.current_medialist<>None and self.current_medialist.track_is_selected():
            self.current_medialist.move_up()
            self.refresh_tracks_display()
            self.save_medialist()

    def move_track_down(self):
        if self.current_medialist<>None and self.current_medialist.track_is_selected():
            self.current_medialist.move_down()
            self.refresh_tracks_display()
            self.save_medialist()
        
    def new_track(self,fields,values):
        if self.current_medialist<>None:
            #print '\nfields ', fields
            #print '\nvalues ', values
            new_track=copy.deepcopy(fields)
            #print ',\new track ',new_track
            self.current_medialist.append(new_track)
            #print '\nbefore values ',self.current_medialist.print_list()
            if values<>None:
                self.current_medialist.update(self.current_medialist.length()-1,values)
            self.current_medialist.select(self.current_medialist.length()-1)
            self.refresh_tracks_display()
            self.save_medialist()

    def new_message_track(self):
        self.new_track(PPdefinitions.new_tracks['message'],None)
            
    def new_video_track(self):
        self.new_track(PPdefinitions.new_tracks['video'],None)
  
    def new_audio_track(self):
        self.new_track(PPdefinitions.new_tracks['audio'],None)

    def new_web_track(self):
        self.new_track(PPdefinitions.new_tracks['web'],None)
        
    def new_image_track(self):
        self.new_track(PPdefinitions.new_tracks['image'],None)

    def new_show_track(self):
        self.new_track(PPdefinitions.new_tracks['show'],None)
 
    def new_menu_background_track(self):
        self.new_track(PPdefinitions.new_tracks['menu-background'],None)

    def new_child_show_track(self):
        self.new_track(PPdefinitions.new_tracks['child-show'],None)

    def remove_track(self):
        if  self.current_medialist<>None and self.current_medialist.length()>0 and self.current_medialist.track_is_selected():
            if tkMessageBox.askokcancel("Delete Track","Delete Track"):
                index= self.current_medialist.selected_track_index()
                self.current_medialist.remove(index)
                self.save_medialist()
                self.refresh_tracks_display()
                
    def add_track_from_file(self):
        if self.current_medialist==None: return
        # print "initial directory ", self.options.initial_media_dir
        files_path=tkFileDialog.askopenfilename(initialdir=self.options.initial_media_dir, multiple=True)
        # fix for tkinter bug
        files_path =  self.root.tk.splitlist(files_path)
        for file_path in files_path:
            file_path=os.path.normpath(file_path)
            # print "file path ", file_path
            self.add_track(file_path)
        self.save_medialist()

    def add_tracks_from_dir(self):
        if self.current_medialist==None: return
        image_specs =[
            PPdefinitions.IMAGE_FILES,
            PPdefinitions.VIDEO_FILES,
            PPdefinitions.AUDIO_FILES,
            PPdefinitions.WEB_FILES,
          ('All files', '*')]    #last one is ignored in finding files
                                    # in directory, for dialog box only
        directory=tkFileDialog.askdirectory(initialdir=self.options.initial_media_dir)
        # deal with tuple returned on Cancel
        if len(directory)==0: return
        # make list of exts we recognise
        exts = []
        for image_spec in image_specs[:-1]:
            image_list=image_spec[1:]
            for ext in image_list:
                exts.append(copy.deepcopy(ext))
        for file in os.listdir(directory):
            (root_file,ext_file)= os.path.splitext(file)
            if ext_file.lower() in exts:
                file_path=directory+os.sep+file
                #print "file path before ", file_path
                file_path=os.path.normpath(file_path)
                #print "file path after ", file_path
                self.add_track(file_path)
        self.save_medialist()


    def add_track(self,afile):
        relpath = os.path.relpath(afile,self.pp_home_dir)
        #print "relative path ",relpath
        common = os.path.commonprefix([afile,self.pp_home_dir])
        #print "common ",common
        if common.endswith("pp_home") == False:
            location = afile
        else:
            location = "+" + os.sep + relpath
            location = string.replace(location,'\\','/')
            #print "location ",location
        (root,title)=os.path.split(afile)
        (root,ext)= os.path.splitext(afile)
        if ext.lower() in PPdefinitions.IMAGE_FILES:
            self.new_track(PPdefinitions.new_tracks['image'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.VIDEO_FILES:
            self.new_track(PPdefinitions.new_tracks['video'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.AUDIO_FILES:
            self.new_track(PPdefinitions.new_tracks['audio'],{'title':title,'track-ref':'','location':location})
        elif ext.lower() in PPdefinitions.WEB_FILES:
            self.new_track(PPdefinitions.new_tracks['web'],{'title':title,'track-ref':'','location':location})
        else:
            self.mon.err(self,afile + " - file extension not recognised")



# *********************************************
# update profile
# **********************************************

    def update_all(self):
        self.init()
        for profile_file in os.listdir(self.pp_home_dir+os.sep+'pp_profiles'):
            # self.mon.log (self,"Updating "+profile_file)
            self.pp_profile_dir = self.pp_home_dir+os.sep+'pp_profiles'+ os.sep + profile_file
            if not os.path.exists(self.pp_profile_dir+os.sep+"pp_showlist.json"):
                tkMessageBox.showwarning("Pi Presents","Not a profile, skipping "+self.pp_profile_dir)
            else:
                self.current_showlist=ShowList()
                #self.mon.log (self,"Checking version "+profile_file)
                self.current_showlist.open_json(self.pp_profile_dir+os.sep+"pp_showlist.json")
                if float(self.current_showlist.sissue())<float(self.editor_issue):
                    self.mon.log(self,"Version of profile "+profile_file+ "  is being updated to "+self.editor_issue)
                    self.update_profile()
                elif (self.command_options['forceupdate'] == True and float(self.current_showlist.sissue())==float(self.editor_issue)):
                    self.mon.log(self, "Forced updating of " + profile_file + ' to '+self.editor_issue)
                    self.update_profile()
                elif float(self.current_showlist.sissue())>float(self.editor_issue):
                    tkMessageBox.showwarning("Pi Presents", "Version of profile " +profile_file+ " is greater than editor, skipping")
                else:
                    self.mon.log(self," Profile " + profile_file + " Already up to date ")
        self.init()
        tkMessageBox.showwarning("Pi Presents","All profiles updated")
            
    def update_profile(self):
        #open showlist and update its shows
        # self.mon.log (self,"Updating show ")
        ifile  = open(self.pp_profile_dir + os.sep + "pp_showlist.json", 'rb')
        shows = json.load(ifile)['shows']
        ifile.close()
        replacement_shows=self.update_shows(shows)
        dic={'issue':self.editor_issue,'shows':replacement_shows}
        ofile  = open(self.pp_profile_dir + os.sep + "pp_showlist.json", "wb")
        json.dump(dic,ofile,sort_keys=True,indent=1)

        
        # UPDATE MEDIALISTS AND THEIR TRACKS
        for file in os.listdir(self.pp_profile_dir):
            if file.endswith(".json") and file<>'pp_showlist.json':
                # self.mon.log (self,"Updating medialist " + file)
                #open a medialist and update its tracks
                ifile  = open(self.pp_profile_dir + os.sep + file, 'rb')
                tracks = json.load(ifile)['tracks']
                ifile.close()
                replacement_tracks=self.update_tracks(tracks)
                dic={'issue':self.editor_issue,'tracks':replacement_tracks}
                ofile  = open(self.pp_profile_dir + os.sep + file, "wb")
                json.dump(dic,ofile,sort_keys=True,indent=1)


    def update_tracks(self,old_tracks):
        # get correct spec from type of field
        replacement_tracks=[]
        for old_track in old_tracks:
            #print '\nold track ',old_track
            track_type=old_track['type']
            spec_fields=PPdefinitions.new_tracks[track_type]
            left_overs=dict()
            # go through track and delete fields not in spec
            for key in old_track.keys():
                if key in spec_fields:
                        left_overs[key]=old_track[key]
            #print '\n leftovers',left_overs
            replacement_track=copy.deepcopy(PPdefinitions.new_tracks[track_type])
            #print '\n before update', replacement_track
            replacement_track.update(left_overs)
            #print '\nafter update',replacement_track
            replacement_tracks.append(copy.deepcopy(replacement_track))
        return replacement_tracks


    def update_shows(self,old_shows):
        # get correct spec from type of field
        replacement_shows=[]
        for old_show in old_shows:
            show_type=old_show['type']
            spec_fields=PPdefinitions.new_shows[show_type]
            left_overs=dict()
            # go through track and delete fields not in spec
            for key in old_show.keys():
                if key in spec_fields:
                        left_overs[key]=old_show[key]
            # print '\n leftovers',left_overs
            replacement_show=copy.deepcopy(PPdefinitions.new_shows[show_type])
            replacement_show.update(left_overs)
            replacement_shows.append(copy.deepcopy(replacement_show))
        return replacement_shows                
Пример #17
0
class PPEditor:

    # ***************************************
    # INIT
    # ***************************************

    def __init__(self):

        self.editor_issue = "1.2"

        # get command options
        self.command_options = ed_options()

        # get directory holding the code
        self.pp_dir = sys.path[0]

        if not os.path.exists(self.pp_dir + os.sep + "pp_editor.py"):
            tkMessageBox.showwarning("Pi Presents",
                                     "Bad Application Directory")
            exit()

#Initialise logging
        Monitor.log_path = self.pp_dir
        self.mon = Monitor()
        self.mon.on()

        if self.command_options['debug'] == True:
            Monitor.global_enable = True
        else:
            Monitor.global_enable = False

        self.mon.log(self, "Pi Presents Editor is starting")

        self.mon.log(self, " OS and separator " + os.name + '  ' + os.sep)
        self.mon.log(self,
                     "sys.path[0] -  location of code: code " + sys.path[0])

        # set up the gui

        #root is the Tkinter root widget
        self.root = tk.Tk()
        self.root.title("Editor for Pi Presents")

        # self.root.configure(background='grey')

        self.root.resizable(False, False)

        #define response to main window closing
        self.root.protocol("WM_DELETE_WINDOW", self.app_exit)

        # bind some display fields
        self.filename = tk.StringVar()
        self.display_selected_track_title = tk.StringVar()
        self.display_show = tk.StringVar()

        # define menu
        menubar = Menu(self.root)

        profilemenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        profilemenu.add_command(label='Open',
                                command=self.open_existing_profile)
        profilemenu.add_command(label='Validate',
                                command=self.validate_profile)
        menubar.add_cascade(label='Profile', menu=profilemenu)

        ptypemenu = Menu(profilemenu, tearoff=0, bg="grey", fg="black")
        ptypemenu.add_command(label='Exhibit',
                              command=self.new_exhibit_profile)
        ptypemenu.add_command(label='Media Show',
                              command=self.new_mediashow_profile)
        ptypemenu.add_command(label='Menu', command=self.new_menu_profile)
        ptypemenu.add_command(label='Presentation',
                              command=self.new_presentation_profile)
        ptypemenu.add_command(label='Interactive',
                              command=self.new_interactive_profile)
        ptypemenu.add_command(label='Live Show',
                              command=self.new_liveshow_profile)
        ptypemenu.add_command(label='RadioButton Show',
                              command=self.new_radiobuttonshow_profile)
        ptypemenu.add_command(label='Hyperlink Show',
                              command=self.new_hyperlinkshow_profile)
        ptypemenu.add_command(label='Blank', command=self.new_blank_profile)
        profilemenu.add_cascade(label='New from Template', menu=ptypemenu)

        showmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        showmenu.add_command(label='Delete', command=self.remove_show)
        showmenu.add_command(label='Edit', command=self.m_edit_show)
        showmenu.add_command(label='Copy To', command=self.copy_show)
        menubar.add_cascade(label='Show', menu=showmenu)

        stypemenu = Menu(showmenu, tearoff=0, bg="grey", fg="black")
        stypemenu.add_command(label='Menu', command=self.add_menu)
        stypemenu.add_command(label='MediaShow', command=self.add_mediashow)
        stypemenu.add_command(label='LiveShow', command=self.add_liveshow)
        stypemenu.add_command(label='HyperlinkShow',
                              command=self.add_hyperlinkshow)
        stypemenu.add_command(label='RadioButtonShow',
                              command=self.add_radiobuttonshow)
        showmenu.add_cascade(label='Add', menu=stypemenu)

        medialistmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='MediaList', menu=medialistmenu)
        medialistmenu.add_command(label='Add', command=self.add_medialist)
        medialistmenu.add_command(label='Delete',
                                  command=self.remove_medialist)

        trackmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        trackmenu.add_command(label='Delete', command=self.remove_track)
        trackmenu.add_command(label='Edit', command=self.m_edit_track)
        trackmenu.add_command(label='Add from Dir',
                              command=self.add_tracks_from_dir)
        trackmenu.add_command(label='Add from File',
                              command=self.add_track_from_file)

        menubar.add_cascade(label='Track', menu=trackmenu)

        typemenu = Menu(trackmenu, tearoff=0, bg="grey", fg="black")
        typemenu.add_command(label='Video', command=self.new_video_track)
        typemenu.add_command(label='Audio', command=self.new_audio_track)
        typemenu.add_command(label='Image', command=self.new_image_track)
        typemenu.add_command(label='Web', command=self.new_web_track)
        typemenu.add_command(label='Message', command=self.new_message_track)
        typemenu.add_command(label='Show', command=self.new_show_track)
        typemenu.add_command(label='Menu Background',
                             command=self.new_menu_background_track)
        typemenu.add_command(label='Child Show',
                             command=self.new_child_show_track)
        trackmenu.add_cascade(label='New', menu=typemenu)

        toolsmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Tools', menu=toolsmenu)
        toolsmenu.add_command(label='Update All', command=self.update_all)

        optionsmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Options', menu=optionsmenu)
        optionsmenu.add_command(label='Edit', command=self.edit_options)

        helpmenu = Menu(menubar, tearoff=0, bg="grey", fg="black")
        menubar.add_cascade(label='Help', menu=helpmenu)
        helpmenu.add_command(label='Help', command=self.show_help)
        helpmenu.add_command(label='About', command=self.about)

        self.root.config(menu=menubar)

        top_frame = Frame(self.root)
        top_frame.pack(side=TOP)
        bottom_frame = Frame(self.root)
        bottom_frame.pack(side=TOP, fill=BOTH, expand=1)

        left_frame = Frame(bottom_frame, padx=5)
        left_frame.pack(side=LEFT)
        middle_frame = Frame(bottom_frame, padx=5)
        middle_frame.pack(side=LEFT)
        right_frame = Frame(bottom_frame, padx=5, pady=10)
        right_frame.pack(side=LEFT)
        updown_frame = Frame(bottom_frame, padx=5)
        updown_frame.pack(side=LEFT)

        tracks_title_frame = Frame(right_frame)
        tracks_title_frame.pack(side=TOP)
        tracks_label = Label(tracks_title_frame,
                             text="Tracks in Selected Medialist")
        tracks_label.pack()
        tracks_frame = Frame(right_frame)
        tracks_frame.pack(side=TOP)
        shows_title_frame = Frame(left_frame)
        shows_title_frame.pack(side=TOP)
        shows_label = Label(shows_title_frame, text="Shows")
        shows_label.pack()
        shows_frame = Frame(left_frame)
        shows_frame.pack(side=TOP)
        shows_title_frame = Frame(left_frame)
        shows_title_frame.pack(side=TOP)
        medialists_title_frame = Frame(left_frame)
        medialists_title_frame.pack(side=TOP)
        medialists_label = Label(medialists_title_frame, text="Medialists")
        medialists_label.pack()
        medialists_frame = Frame(left_frame)
        medialists_frame.pack(side=LEFT)

        # define buttons

        add_button = Button(middle_frame,
                            width=5,
                            height=2,
                            text='Edit\nShow',
                            fg='black',
                            command=self.m_edit_show,
                            bg="light grey")
        add_button.pack(side=RIGHT)

        add_button = Button(updown_frame,
                            width=5,
                            height=1,
                            text='Add',
                            fg='black',
                            command=self.add_track_from_file,
                            bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame,
                            width=5,
                            height=1,
                            text='Edit',
                            fg='black',
                            command=self.m_edit_track,
                            bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame,
                            width=5,
                            height=1,
                            text='Up',
                            fg='black',
                            command=self.move_track_up,
                            bg="light grey")
        add_button.pack(side=TOP)
        add_button = Button(updown_frame,
                            width=5,
                            height=1,
                            text='Down',
                            fg='black',
                            command=self.move_track_down,
                            bg="light grey")
        add_button.pack(side=TOP)

        # define display of showlist
        scrollbar = Scrollbar(shows_frame, orient=tk.VERTICAL)
        self.shows_display = Listbox(shows_frame,
                                     selectmode=SINGLE,
                                     height=7,
                                     width=40,
                                     bg="white",
                                     activestyle=NONE,
                                     fg="black",
                                     yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.shows_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.shows_display.pack(side=LEFT, fill=BOTH, expand=1)
        self.shows_display.bind("<ButtonRelease-1>", self.e_select_show)

        # define display of medialists
        scrollbar = Scrollbar(medialists_frame, orient=tk.VERTICAL)
        self.medialists_display = Listbox(medialists_frame,
                                          selectmode=SINGLE,
                                          height=7,
                                          width=40,
                                          bg="white",
                                          activestyle=NONE,
                                          fg="black",
                                          yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.medialists_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.medialists_display.pack(side=LEFT, fill=BOTH, expand=1)
        self.medialists_display.bind("<ButtonRelease-1>",
                                     self.select_medialist)

        # define display of tracks
        scrollbar = Scrollbar(tracks_frame, orient=tk.VERTICAL)
        self.tracks_display = Listbox(tracks_frame,
                                      selectmode=SINGLE,
                                      height=15,
                                      width=40,
                                      bg="white",
                                      activestyle=NONE,
                                      fg="black",
                                      yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.tracks_display.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.tracks_display.pack(side=LEFT, fill=BOTH, expand=1)
        self.tracks_display.bind("<ButtonRelease-1>", self.e_select_track)

        # initialise editor options class
        self.options = Options(
            self.pp_dir)  #creates options file in code directory if necessary

        # initialise variables
        self.init()

        #and enter Tkinter event loop
        self.root.mainloop()

# ***************************************
# INIT AND EXIT
# ***************************************

    def app_exit(self):
        self.root.destroy()
        exit()

    def init(self):
        self.options.read()
        self.pp_home_dir = self.options.pp_home_dir
        self.initial_media_dir = self.options.initial_media_dir
        self.mon.log(self, "Data Home from options is " + self.pp_home_dir)
        self.mon.log(self,
                     "Initial Media from options is " + self.initial_media_dir)
        self.current_medialist = None
        self.current_showlist = None
        self.current_show = None
        self.shows_display.delete(0, END)
        self.medialists_display.delete(0, END)
        self.tracks_display.delete(0, END)

# ***************************************
# MISCELLANEOUS
# ***************************************

    def edit_options(self):
        """edit the options then read them from file"""
        eo = OptionsDialog(self.root, self.options.options_file,
                           'Edit Options')
        if eo.result == True: self.init()

    def show_help(self):
        tkMessageBox.showinfo("Help", "Read 'manual.pdf'")

    def about(self):
        tkMessageBox.showinfo(
            "About",
            "Editor for Pi Presents Profiles\n" + "For profile version: " +
            self.editor_issue + "\nAuthor: Ken Thompson" +
            "\nWebsite: http://pipresents.wordpress.com/")

    def validate_profile(self):
        val = Validator()
        val.validate_profile(self.root, self.pp_dir, self.pp_home_dir,
                             self.pp_profile_dir, self.editor_issue, True)

# **************
# PROFILES
# **************

    def open_existing_profile(self):
        initial_dir = self.pp_home_dir + os.sep + "pp_profiles"
        if os.path.exists(initial_dir) == False:
            self.mon.err(
                self, "Home directory not found: " + initial_dir +
                "\n\nHint: Data Home option must end in pp_home")
            return
        dir_path = tkFileDialog.askdirectory(initialdir=initial_dir)
        # dir_path="C:\Users\Ken\pp_home\pp_profiles\\ttt"
        if len(dir_path) > 0:
            self.open_profile(dir_path)

    def open_profile(self, dir_path):
        showlist_file = dir_path + os.sep + "pp_showlist.json"
        if os.path.exists(showlist_file) == False:
            self.mon.err(
                self, "Not a Profile: " + dir_path +
                "\n\nHint: Have you opened the profile directory?")
            return
        self.pp_profile_dir = dir_path
        self.root.title("Editor for Pi Presents - " + self.pp_profile_dir)
        if self.open_showlist(self.pp_profile_dir) == False:
            self.init()
            return
        self.open_medialists(self.pp_profile_dir)
        self.refresh_tracks_display()

    def new_profile(self, profile):
        d = Edit1Dialog(self.root, "New Profile", "Name", "")
        if d.result == None:
            return
        name = str(d.result)
        if name == "":
            tkMessageBox.showwarning("New Profile", "Name is blank")
            return
        to = self.pp_home_dir + os.sep + "pp_profiles" + os.sep + name
        if os.path.exists(to) == True:
            tkMessageBox.showwarning("New Profile",
                                     "Profile exists\n(%s)" % to)
            return
        shutil.copytree(profile, to, symlinks=False, ignore=None)
        self.open_profile(to)

    def new_exhibit_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_exhibit"
        self.new_profile(profile)

    def new_interactive_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_interactive"
        self.new_profile(profile)

    def new_menu_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_menu"
        self.new_profile(profile)

    def new_presentation_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_presentation"
        self.new_profile(profile)

    def new_blank_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_blank"
        self.new_profile(profile)

    def new_mediashow_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_mediashow"
        self.new_profile(profile)

    def new_liveshow_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_liveshow"
        self.new_profile(profile)

    def new_radiobuttonshow_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_radiobuttonshow"
        self.new_profile(profile)

    def new_hyperlinkshow_profile(self):
        profile = self.pp_dir + "/pp_home/pp_profiles/ppt_hyperlinkshow"
        self.new_profile(profile)

# ***************************************
# Shows
# ***************************************

    def open_showlist(self, dir):
        showlist_file = dir + os.sep + "pp_showlist.json"
        if os.path.exists(showlist_file) == False:
            self.mon.err(
                self, "showlist file not found at " + dir +
                "\n\nHint: Have you opened the profile directory?")
            self.app_exit()
        self.current_showlist = ShowList()
        self.current_showlist.open_json(showlist_file)
        if float(self.current_showlist.sissue()) < float(
                self.editor_issue) or (
                    self.command_options['forceupdate'] == True
                    and float(self.current_showlist.sissue()) == float(
                        self.editor_issue)):
            self.update_profile()
            self.mon.err(
                self, "Version of profile has been updated to " +
                self.editor_issue + ", please re-open")
            return False
        if float(self.current_showlist.sissue()) > float(self.editor_issue):
            self.mon.err(
                self, "Version of profile is greater than editor, must exit")
            self.app_exit()
        self.refresh_shows_display()
        return True

    def save_showlist(self, dir):
        if self.current_showlist <> None:
            showlist_file = dir + os.sep + "pp_showlist.json"
            self.current_showlist.save_list(showlist_file)

    def add_eventshow(self):
        self.add_show(PPdefinitions.new_shows['eventshow'])

    def add_mediashow(self):
        self.add_show(PPdefinitions.new_shows['mediashow'])

    def add_liveshow(self):
        self.add_show(PPdefinitions.new_shows['liveshow'])

    def add_radiobuttonshow(self):
        self.add_show(PPdefinitions.new_shows['radiobuttonshow'])

    def add_hyperlinkshow(self):
        self.add_show(PPdefinitions.new_shows['hyperlinkshow'])

    def add_menu(self):
        self.add_show(PPdefinitions.new_shows['menu'])

    def add_start(self):
        self.add_show(PPdefinitions.new_shows['start'])

    def add_show(self, default):
        # append it to the showlist and then add the medialist
        if self.current_showlist <> None:
            d = Edit1Dialog(self.root, "AddShow", "Show Reference", "")
            if d.result == None:
                return
            name = str(d.result)
            if name == "":
                tkMessageBox.showwarning("Add Show", "Name is blank")
                return
            if self.current_showlist.index_of_show(name) <> -1:
                tkMessageBox.showwarning(
                    "Add Show", "A Show with this name already exists")
                return
            copied_show = self.current_showlist.copy(default, name)
            mediafile = self.add_medialist(name)
            if mediafile <> '':
                copied_show['medialist'] = mediafile
            self.current_showlist.append(copied_show)
            self.save_showlist(self.pp_profile_dir)
            self.refresh_shows_display()

    def remove_show(self):
        if self.current_showlist <> None and self.current_showlist.length(
        ) > 0 and self.current_showlist.show_is_selected():
            if tkMessageBox.askokcancel("Delete Show", "Delete Show"):
                index = self.current_showlist.selected_show_index()
                self.current_showlist.remove(index)
                self.save_showlist(self.pp_profile_dir)
                self.refresh_shows_display()

    def show_refs(self):
        _show_refs = []
        for index in range(self.current_showlist.length()):
            if self.current_showlist.show(index)['show-ref'] <> "start":
                _show_refs.append(
                    copy.deepcopy(
                        self.current_showlist.show(index)['show-ref']))
        return _show_refs

    def refresh_shows_display(self):
        self.shows_display.delete(0, self.shows_display.size())
        for index in range(self.current_showlist.length()):
            self.shows_display.insert(
                END,
                self.current_showlist.show(index)['title'] + "   [" +
                self.current_showlist.show(index)['show-ref'] + "]")
        if self.current_showlist.show_is_selected():
            self.shows_display.itemconfig(
                self.current_showlist.selected_show_index(), fg='red')
            self.shows_display.see(self.current_showlist.selected_show_index())

    def e_select_show(self, event):
        if self.current_showlist <> None and self.current_showlist.length(
        ) > 0:
            mouse_item_index = int(event.widget.curselection()[0])
            self.current_showlist.select(mouse_item_index)
            self.refresh_shows_display()

    def copy_show(self):
        if self.current_showlist <> None and self.current_showlist.show_is_selected(
        ):
            self.add_show(self.current_showlist.selected_show())

    def m_edit_show(self):
        self.edit_show(PPdefinitions.show_types,
                       PPdefinitions.show_field_specs)

    def edit_show(self, show_types, field_specs):
        if self.current_showlist <> None and self.current_showlist.show_is_selected(
        ):
            d = EditItem(self.root, "Edit Show",
                         self.current_showlist.selected_show(), show_types,
                         field_specs, self.show_refs(), self.initial_media_dir,
                         self.pp_home_dir, 'show')
            if d.result == True:

                self.save_showlist(self.pp_profile_dir)
                self.refresh_shows_display()

# ***************************************
#   Medialists
# ***************************************

    def open_medialists(self, dir):
        self.medialists = []
        for file in os.listdir(dir):
            if file.endswith(".json") and file <> 'pp_showlist.json':
                self.medialists = self.medialists + [file]
        self.medialists_display.delete(0, self.medialists_display.size())
        for index in range(len(self.medialists)):
            self.medialists_display.insert(END, self.medialists[index])
        self.current_medialists_index = -1
        self.current_medialist = None

    def add_medialist(self, name=None):
        if name == None:
            d = Edit1Dialog(self.root, "Add Medialist", "File", "")
            if d.result == None:
                return ''
            name = str(d.result)
            if name == "":
                tkMessageBox.showwarning("Add medialist", "Name is blank")
                return ''

        if not name.endswith(".json"):
            name = name + (".json")

        path = self.pp_profile_dir + os.sep + name
        if os.path.exists(path) == True:
            tkMessageBox.showwarning("Add medialist",
                                     "Medialist file exists\n(%s)" % path)
            return ''
        nfile = open(path, 'wb')
        nfile.write("{")
        nfile.write("\"issue\":  \"" + self.editor_issue + "\",\n")
        nfile.write("\"tracks\": [")
        nfile.write("]")
        nfile.write("}")
        nfile.close()
        # append it to the list
        self.medialists.append(copy.deepcopy(name))
        # add title to medialists display
        self.medialists_display.insert(END, name)
        # and set it as the selected medialist
        self.refresh_medialists_display()
        return name

    def remove_medialist(self):
        if self.current_medialist <> None:
            if tkMessageBox.askokcancel("Delete Medialist",
                                        "Delete Medialist"):
                os.remove(self.pp_profile_dir + os.sep +
                          self.medialists[self.current_medialists_index])
                self.open_medialists(self.pp_profile_dir)
                self.refresh_medialists_display()
                self.refresh_tracks_display()

    def select_medialist(self, event):
        """
        user clicks on a medialst in a profile so try and select it.
        """
        # needs forgiving int for possible tkinter upgrade
        if len(self.medialists) > 0:
            self.current_medialists_index = int(event.widget.curselection()[0])
            self.current_medialist = MediaList('ordered')
            if not self.current_medialist.open_list(
                    self.pp_profile_dir + os.sep +
                    self.medialists[self.current_medialists_index],
                    self.current_showlist.sissue()):
                self.mon.err(
                    self, "medialist is a different version to showlist: " +
                    self.medialists[self.current_medialists_index])
                self.app_exit()
            self.refresh_tracks_display()
            self.refresh_medialists_display()

    def refresh_medialists_display(self):
        self.medialists_display.delete(0, len(self.medialists))
        for index in range(len(self.medialists)):
            self.medialists_display.insert(END, self.medialists[index])
        if self.current_medialist <> None:
            self.medialists_display.itemconfig(self.current_medialists_index,
                                               fg='red')
            self.medialists_display.see(self.current_medialists_index)

    def save_medialist(self):
        basefile = self.medialists[self.current_medialists_index]
        #print type(basefile)
        # basefile=str(basefile)
        #print type(basefile)
        file = self.pp_profile_dir + os.sep + basefile
        self.current_medialist.save_list(file)

# ***************************************
#   Tracks
# ***************************************

    def refresh_tracks_display(self):
        self.tracks_display.delete(0, self.tracks_display.size())
        if self.current_medialist <> None:
            for index in range(self.current_medialist.length()):
                if self.current_medialist.track(index)['track-ref'] <> "":
                    track_ref_string = "  [" + self.current_medialist.track(
                        index)['track-ref'] + "]"
                else:
                    track_ref_string = ""
                self.tracks_display.insert(
                    END,
                    self.current_medialist.track(index)['title'] +
                    track_ref_string)
            if self.current_medialist.track_is_selected():
                self.tracks_display.itemconfig(
                    self.current_medialist.selected_track_index(), fg='red')
                self.tracks_display.see(
                    self.current_medialist.selected_track_index())

    def e_select_track(self, event):
        if self.current_medialist <> None and self.current_medialist.length(
        ) > 0:
            mouse_item_index = int(event.widget.curselection()[0])
            self.current_medialist.select(mouse_item_index)
            self.refresh_tracks_display()

    def m_edit_track(self):
        self.edit_track(PPdefinitions.track_types,
                        PPdefinitions.track_field_specs)

    def edit_track(self, track_types, field_specs):
        if self.current_medialist <> None and self.current_medialist.track_is_selected(
        ):
            d = EditItem(self.root, "Edit Track",
                         self.current_medialist.selected_track(), track_types,
                         field_specs, self.show_refs(), self.initial_media_dir,
                         self.pp_home_dir, 'track')
            if d.result == True:
                self.save_medialist()
            self.refresh_tracks_display()

    def move_track_up(self):
        if self.current_medialist <> None and self.current_medialist.track_is_selected(
        ):
            self.current_medialist.move_up()
            self.refresh_tracks_display()
            self.save_medialist()

    def move_track_down(self):
        if self.current_medialist <> None and self.current_medialist.track_is_selected(
        ):
            self.current_medialist.move_down()
            self.refresh_tracks_display()
            self.save_medialist()

    def new_track(self, fields, values):
        if self.current_medialist <> None:
            #print '\nfields ', fields
            #print '\nvalues ', values
            new_track = copy.deepcopy(fields)
            #print ',\new track ',new_track
            self.current_medialist.append(new_track)
            #print '\nbefore values ',self.current_medialist.print_list()
            if values <> None:
                self.current_medialist.update(
                    self.current_medialist.length() - 1, values)
            self.current_medialist.select(self.current_medialist.length() - 1)
            self.refresh_tracks_display()
            self.save_medialist()

    def new_message_track(self):
        self.new_track(PPdefinitions.new_tracks['message'], None)

    def new_video_track(self):
        self.new_track(PPdefinitions.new_tracks['video'], None)

    def new_audio_track(self):
        self.new_track(PPdefinitions.new_tracks['audio'], None)

    def new_web_track(self):
        self.new_track(PPdefinitions.new_tracks['web'], None)

    def new_image_track(self):
        self.new_track(PPdefinitions.new_tracks['image'], None)

    def new_show_track(self):
        self.new_track(PPdefinitions.new_tracks['show'], None)

    def new_menu_background_track(self):
        self.new_track(PPdefinitions.new_tracks['menu-background'], None)

    def new_child_show_track(self):
        self.new_track(PPdefinitions.new_tracks['child-show'], None)

    def remove_track(self):
        if self.current_medialist <> None and self.current_medialist.length(
        ) > 0 and self.current_medialist.track_is_selected():
            if tkMessageBox.askokcancel("Delete Track", "Delete Track"):
                index = self.current_medialist.selected_track_index()
                self.current_medialist.remove(index)
                self.save_medialist()
                self.refresh_tracks_display()

    def add_track_from_file(self):
        if self.current_medialist == None: return
        # print "initial directory ", self.options.initial_media_dir
        files_path = tkFileDialog.askopenfilename(
            initialdir=self.options.initial_media_dir, multiple=True)
        # fix for tkinter bug
        files_path = self.root.tk.splitlist(files_path)
        for file_path in files_path:
            file_path = os.path.normpath(file_path)
            # print "file path ", file_path
            self.add_track(file_path)
        self.save_medialist()

    def add_tracks_from_dir(self):
        if self.current_medialist == None: return
        image_specs = [
            PPdefinitions.IMAGE_FILES, PPdefinitions.VIDEO_FILES,
            PPdefinitions.AUDIO_FILES, PPdefinitions.WEB_FILES,
            ('All files', '*')
        ]  #last one is ignored in finding files
        # in directory, for dialog box only
        directory = tkFileDialog.askdirectory(
            initialdir=self.options.initial_media_dir)
        # deal with tuple returned on Cancel
        if len(directory) == 0: return
        # make list of exts we recognise
        exts = []
        for image_spec in image_specs[:-1]:
            image_list = image_spec[1:]
            for ext in image_list:
                exts.append(copy.deepcopy(ext))
        for file in os.listdir(directory):
            (root_file, ext_file) = os.path.splitext(file)
            if ext_file.lower() in exts:
                file_path = directory + os.sep + file
                #print "file path before ", file_path
                file_path = os.path.normpath(file_path)
                #print "file path after ", file_path
                self.add_track(file_path)
        self.save_medialist()

    def add_track(self, afile):
        relpath = os.path.relpath(afile, self.pp_home_dir)
        #print "relative path ",relpath
        common = os.path.commonprefix([afile, self.pp_home_dir])
        #print "common ",common
        if common.endswith("pp_home") == False:
            location = afile
        else:
            location = "+" + os.sep + relpath
            location = string.replace(location, '\\', '/')
            #print "location ",location
        (root, title) = os.path.split(afile)
        (root, ext) = os.path.splitext(afile)
        if ext.lower() in PPdefinitions.IMAGE_FILES:
            self.new_track(PPdefinitions.new_tracks['image'], {
                'title': title,
                'track-ref': '',
                'location': location
            })
        elif ext.lower() in PPdefinitions.VIDEO_FILES:
            self.new_track(PPdefinitions.new_tracks['video'], {
                'title': title,
                'track-ref': '',
                'location': location
            })
        elif ext.lower() in PPdefinitions.AUDIO_FILES:
            self.new_track(PPdefinitions.new_tracks['audio'], {
                'title': title,
                'track-ref': '',
                'location': location
            })
        elif ext.lower() in PPdefinitions.WEB_FILES:
            self.new_track(PPdefinitions.new_tracks['web'], {
                'title': title,
                'track-ref': '',
                'location': location
            })
        else:
            self.mon.err(self, afile + " - file extension not recognised")

# *********************************************
# update profile
# **********************************************

    def update_all(self):
        self.init()
        for profile_file in os.listdir(self.pp_home_dir + os.sep +
                                       'pp_profiles'):
            # self.mon.log (self,"Updating "+profile_file)
            self.pp_profile_dir = self.pp_home_dir + os.sep + 'pp_profiles' + os.sep + profile_file
            if not os.path.exists(self.pp_profile_dir + os.sep +
                                  "pp_showlist.json"):
                tkMessageBox.showwarning(
                    "Pi Presents",
                    "Not a profile, skipping " + self.pp_profile_dir)
            else:
                self.current_showlist = ShowList()
                #self.mon.log (self,"Checking version "+profile_file)
                self.current_showlist.open_json(self.pp_profile_dir + os.sep +
                                                "pp_showlist.json")
                if float(self.current_showlist.sissue()) < float(
                        self.editor_issue):
                    self.mon.log(
                        self, "Version of profile " + profile_file +
                        "  is being updated to " + self.editor_issue)
                    self.update_profile()
                elif (self.command_options['forceupdate'] == True
                      and float(self.current_showlist.sissue()) == float(
                          self.editor_issue)):
                    self.mon.log(
                        self, "Forced updating of " + profile_file + ' to ' +
                        self.editor_issue)
                    self.update_profile()
                elif float(self.current_showlist.sissue()) > float(
                        self.editor_issue):
                    tkMessageBox.showwarning(
                        "Pi Presents", "Version of profile " + profile_file +
                        " is greater than editor, skipping")
                else:
                    self.mon.log(
                        self,
                        " Profile " + profile_file + " Already up to date ")
        self.init()
        tkMessageBox.showwarning("Pi Presents", "All profiles updated")

    def update_profile(self):
        #open showlist and update its shows
        # self.mon.log (self,"Updating show ")
        ifile = open(self.pp_profile_dir + os.sep + "pp_showlist.json", 'rb')
        shows = json.load(ifile)['shows']
        ifile.close()
        replacement_shows = self.update_shows(shows)
        dic = {'issue': self.editor_issue, 'shows': replacement_shows}
        ofile = open(self.pp_profile_dir + os.sep + "pp_showlist.json", "wb")
        json.dump(dic, ofile, sort_keys=True, indent=1)

        # UPDATE MEDIALISTS AND THEIR TRACKS
        for file in os.listdir(self.pp_profile_dir):
            if file.endswith(".json") and file <> 'pp_showlist.json':
                # self.mon.log (self,"Updating medialist " + file)
                #open a medialist and update its tracks
                ifile = open(self.pp_profile_dir + os.sep + file, 'rb')
                tracks = json.load(ifile)['tracks']
                ifile.close()
                replacement_tracks = self.update_tracks(tracks)
                dic = {
                    'issue': self.editor_issue,
                    'tracks': replacement_tracks
                }
                ofile = open(self.pp_profile_dir + os.sep + file, "wb")
                json.dump(dic, ofile, sort_keys=True, indent=1)

    def update_tracks(self, old_tracks):
        # get correct spec from type of field
        replacement_tracks = []
        for old_track in old_tracks:
            #print '\nold track ',old_track
            track_type = old_track['type']
            spec_fields = PPdefinitions.new_tracks[track_type]
            left_overs = dict()
            # go through track and delete fields not in spec
            for key in old_track.keys():
                if key in spec_fields:
                    left_overs[key] = old_track[key]
            #print '\n leftovers',left_overs
            replacement_track = copy.deepcopy(
                PPdefinitions.new_tracks[track_type])
            #print '\n before update', replacement_track
            replacement_track.update(left_overs)
            #print '\nafter update',replacement_track
            replacement_tracks.append(copy.deepcopy(replacement_track))
        return replacement_tracks

    def update_shows(self, old_shows):
        # get correct spec from type of field
        replacement_shows = []
        for old_show in old_shows:
            show_type = old_show['type']
            spec_fields = PPdefinitions.new_shows[show_type]
            left_overs = dict()
            # go through track and delete fields not in spec
            for key in old_show.keys():
                if key in spec_fields:
                    left_overs[key] = old_show[key]
            # print '\n leftovers',left_overs
            replacement_show = copy.deepcopy(
                PPdefinitions.new_shows[show_type])
            replacement_show.update(left_overs)
            replacement_shows.append(copy.deepcopy(replacement_show))
        return replacement_shows
Пример #18
0
class LiveShow:

    NEW_TRACKS = {
        'image': {
            'title': 'New Image',
            'track-ref': '',
            'type': 'image',
            'location': '',
            'duration': '',
            'transition': '',
            'track-text': '',
            'track-text-font': '',
            'track-text-colour': '',
            'track-text-x': '0',
            'track-text-y': '0'
        },
        'video': {
            'title': 'New Video',
            'track-ref': '',
            'type': 'video',
            'location': '',
            'omx-audio': ''
        }
    }

    IMAGE_FILES = ('.gif', '.jpg', '.jpeg', '.bmp', '.png', '.tif')
    VIDEO_FILES = ('.mp4', '.mkv', '.avi', '.mp2', '.wmv')
    AUDIO_FILES = ('.mp3', '.wav', '.ogg')

    # *******************
    # External interface
    # ********************

    def __init__(self, show, canvas, showlist, pp_home, pp_profile):
        """ canvas - the canvas that the show is to be written on
            showlist - used jus to check the issue of medialist against showlist
            show - the dictionary for the show to be played
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        self.mon = Monitor()
        self.mon.on()

        #instantiate arguments
        self.show = show
        self.showlist = showlist
        self.canvas = canvas
        self.pp_home = pp_home
        self.pp_profile = pp_profile

        # open resources
        self.rr = ResourceReader()

        # Init variables
        self.player = None
        self.shower = None
        self._end_liveshow_signal = False
        self._play_child_signal = False
        self.error = False

        self._livelist = None
        self._new_livelist = None

    def play(self,
             end_callback,
             ready_callback=None,
             top=False,
             command='nil'):
        """ displays the liveshow
              end_callback - function to be called when the liveshow exits
              ready_callback - callback when liveshow is ready to display
              top is True when the show is top level (i.e. run from start show)
        """

        #instantiate the arguments
        self._end_callback = end_callback
        self._ready_callback = ready_callback
        self.top = top
        self.mon.log(self, "Starting show: " + self.show['show-ref'])

        # check  data files are available.
        self.media_file = self.pp_profile + os.sep + self.show['medialist']
        if not os.path.exists(self.media_file):
            self.mon.err(self, "Medialist file not found: " + self.media_file)
            self._stop("Medialist file not found")

        self.options = command_options()

        self._pp_live_dir1 = self.pp_home + os.sep + 'pp_live_tracks'
        if not os.path.exists(self._pp_live_dir1):
            os.mkdir(self._pp_live_dir1)

        self._pp_live_dir2 = ''
        if self.options['liveshow'] <> "":
            self._pp_live_dir2 = self.options['liveshow']
            if not os.path.exists(self._pp_live_dir2):
                self.mon.err(
                    self,
                    "live tracks directory not found " + self._pp_live_dir2)
                self._end('error', "live tracks directory not found")

        #create a medialist for the liveshow and read it.
        # it should be empty of anonymous tracks but read it to check its version.
        self.medialist = MediaList()
        if self.medialist.open_list(self.media_file,
                                    self.showlist.sissue()) == False:
            self.mon.err(self, "Version of medialist different to Pi Presents")
            self._end('error', "Version of medialist different to Pi Presents")

        if self.ready_callback <> None:
            self.ready_callback()

        self._play_first_track()

# respond to key presses.

    def key_pressed(self, key_name):
        self.mon.log(self, "received key: " + key_name)

        if key_name == '':
            pass

        elif key_name == 'escape':
            # if next lower show eor player is running pass down to stop the show/track
            # ELSE stop this show except for exceptions
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            elif self.player <> None:
                self.player.key_pressed(key_name)
            else:
                # not at top so stop the show
                if self.top == False:
                    self._stop("exit show to higher level")
                else:
                    pass

        elif key_name in ('up', 'down'):
            # if child or sub-show is running and is a show pass to show, track does not use up/down
            if self.shower <> None:
                self.shower.key_pressed(key_name)

        elif key_name == 'return':
            # if child show or sub-show is running and is show - pass down
            # ELSE use Return to start child
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            else:
                if self.show['has-child'] == "yes":
                    self._play_child()

        elif key_name in ('p', ' '):
            # pass down if show or track running.
            if self.shower <> None:
                self.shower.key_pressed(key_name)
            elif self.player <> None:
                self.player.key_pressed(key_name)

    def button_pressed(self, button, edge):
        if button == 'play': self.key_pressed("return")
        elif button == 'up': self.key_pressed("up")
        elif button == 'down': self.key_pressed("down")
        elif button == 'stop': self.key_pressed("escape")
        elif button == 'pause': self.key_pressed('p')

    # kill or error
    def terminate(self, reason):
        if self.shower <> None:
            self.mon.log(self, "sent terminate to shower")
            self.shower.terminate(reason)
        elif self.player <> None:
            self.mon.log(self, "sent terminate to player")
            self.player.terminate(reason)
        else:
            self._end(reason,
                      'terminated without terminating shower or player')

    def _tidy_up(self):
        pass

    def resource(self, section, item):
        value = self.rr.get(section, item)
        if value == False:
            self.mon.err(self,
                         "resource: " + section + ': ' + item + " not found")
            self.terminate("error", 'Cannot find resource')
        else:
            return value

# ***************************
# Respond to key/button presses
# ***************************

    def _stop(self, message):
        self._end_liveshow_signal = True

    def _play_child(self):
        self._play_child_signal = True
        if self.player <> None:
            self.player.key_pressed("escape")

# ***************************
# end of show functions
# ***************************

    def _end(self, reason, message):
        self._end_liveshow_signal = False
        self.mon.log(self, "Ending Liveshow: " + self.show['show-ref'])
        self._tidy_up()
        self._end_callback(reason, message)
        self = None
        return

    def _nend(self):
        self._end('normal', 'end from state machine')

# ***************************
# Livelist
# ***************************

    def _livelist_add_track(self, afile):
        (root, title) = os.path.split(afile)
        (root, ext) = os.path.splitext(afile)
        if ext.lower() in LiveShow.IMAGE_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['image'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() in LiveShow.VIDEO_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['video'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })
        if ext.lower() in LiveShow.AUDIO_FILES:
            self._livelist_new_track(LiveShow.NEW_TRACKS['video'], {
                'title': title,
                'track-ref': '',
                'location': afile
            })

    def _livelist_new_track(self, fields, values):
        new_track = fields
        self._new_livelist.append(copy.deepcopy(new_track))
        last = len(self._new_livelist) - 1
        self._new_livelist[last].update(values)

    def _new_livelist_create(self):

        self._new_livelist = []
        if os.path.exists(self._pp_live_dir1):
            for file in os.listdir(self._pp_live_dir1):
                file = self._pp_live_dir1 + os.sep + file
                (root_file, ext_file) = os.path.splitext(file)
                if ext_file.lower(
                ) in LiveShow.IMAGE_FILES + LiveShow.VIDEO_FILES + LiveShow.AUDIO_FILES:
                    self._livelist_add_track(file)

        if os.path.exists(self._pp_live_dir2):
            for file in os.listdir(self._pp_live_dir2):
                file = self._pp_live_dir2 + os.sep + file
                (root_file, ext_file) = os.path.splitext(file)
                if ext_file.lower(
                ) in LiveShow.IMAGE_FILES + LiveShow.VIDEO_FILES + LiveShow.AUDIO_FILES:
                    self._livelist_add_track(file)

        self._new_livelist = sorted(
            self._new_livelist,
            key=lambda track: os.path.basename(track['location']).lower())
#       for it in self._new_livelist:
#          print it['location']
#      print ''

    def _livelist_replace_if_changed(self):
        self._new_livelist_create()
        if self._new_livelist <> self._livelist:
            self._livelist = copy.deepcopy(self._new_livelist)
            self._livelist_index = 0

    def _livelist_next(self):
        if self._livelist_index == len(self._livelist) - 1:
            self._livelist_index = 0
        else:
            self._livelist_index += 1

# ***************************
# Play Loop
# ***************************

    def _play_first_track(self):
        self._new_livelist_create()
        self._livelist = copy.deepcopy(self._new_livelist)
        self._livelist_index = 0
        self._play_track()

    def _play_track(self):
        self._livelist_replace_if_changed()
        if len(self._livelist) > 0:
            self._play_selected_track(self._livelist[self._livelist_index])
        else:
            self.display_message(self.canvas, None,
                                 self.resource('liveshow', 'm01'), 5,
                                 self._what_next)

    def _what_next(self):
        # user wants to end
        if self._end_liveshow_signal == True:
            self._end_liveshow_signal = False
            self._end('normal', "show ended by user")

        # play child?
        elif self._play_child_signal == True:
            self._play_child_signal = False
            index = self.medialist.index_of_track('pp-child-show')
            if index >= 0:
                #don't select the track as need to preserve mediashow sequence.
                child_track = self.medialist.track(index)
                self._display_eggtimer(self.resource('liveshow', 'm02'))
                self._play_selected_track(child_track)
            else:
                self.mon.err(
                    self, "Child show not found in medialist: " +
                    self.show['pp-child-show'])
                self._end('error', "child show not found in medialist")

        # otherwise loop to next track
        else:
            self._livelist_next()
            self._play_track()


# ***************************
# Dispatching to Players/Shows
# ***************************

# used to display internal messages in situations where a medialist entry could not be used.

    def display_message(self, canvas, source, content, duration,
                        _display_message_callback):
        self._display_message_callback = _display_message_callback
        tp = {
            'duration': duration,
            'message-colour': 'white',
            'message-font': 'Helvetica 20 bold'
        }
        self.player = MessagePlayer(canvas, tp, tp)
        self.player.play(content, self._display_message_end, None)

    def _display_message_end(self, reason, message):
        self.player = None
        if reason in ("killed", 'error'):
            self._end(reason, message)
        else:
            self._display_message_callback()

    def complete_path(self, selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        self.mon.log(self, "Track to play is: " + track_file)
        return track_file

    def _play_selected_track(self, selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected_track is a dictionary for the track/show
        """
        # self.canvas.delete(ALL)

        # is menu required
        if self.show['has-child'] == "yes":
            enable_child = True
        else:
            enable_child = False

        #dispatch track by type
        self.player = None
        self.shower = None
        track_type = selected_track['type']
        self.mon.log(self, "Track type is: " + track_type)

        if track_type == "image":
            track_file = self.complete_path(selected_track)
            # images played from menus don't have children
            self.player = ImagePlayer(self.canvas, self.show, selected_track)
            self.player.play(track_file,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)
        elif track_type == "video":
            # create a videoplayer
            track_file = self.complete_path(selected_track)
            self.player = VideoPlayer(self.canvas, self.show, selected_track)
            self.player.play(track_file,
                             self.end_player,
                             self.ready_callback,
                             enable_menu=enable_child)

        elif track_type == "show":
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >= 0:
                self.showlist.select(index)
                selected_show = self.showlist.selected_show()
            else:
                self.mon.err(
                    self, "Show not found in showlist: " +
                    selected_track['sub-show'])
                self._stop("Unknown show")

            if selected_show['type'] == "mediashow":
                self.shower = MediaShow(selected_show, self.canvas,
                                        self.showlist, self.pp_home,
                                        self.pp_profile)
                self.shower.play(self.end_shower, top=False, command='nil')

            elif selected_show['type'] == "menu":
                self.shower = MenuShow(selected_show, self.canvas,
                                       self.showlist, self.pp_home,
                                       self.pp_profile)
                self.shower.play(self.end_shower, top=False, command='nil')

            else:
                self.mon.err(self,
                             "Unknown Show Type: " + selected_show['type'])
                self._stop("Unknown show type")

        else:
            self.mon.err(self, "Unknown Track Type: " + track_type)
            self._stop("Unknown track type")

    def ready_callback(self):
        self._delete_eggtimer()

    def end_player(self, reason, message):
        self.mon.log(self, "Returned from player with message: " + message)
        self.player = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        else:
            self._what_next()

    def end_shower(self, reason, message):
        self.mon.log(self, "Returned from shower with message: " + message)
        self.shower = None
        if reason in ("killed", "error"):
            self._end(reason, message)
        else:
            self._what_next()

    def _display_eggtimer(self, text):
        self.canvas.create_text(int(self.canvas['width']) / 2,
                                int(self.canvas['height']) / 2,
                                text=text,
                                fill='white',
                                font="Helvetica 20 bold")
        self.canvas.update_idletasks()

    def _delete_eggtimer(self):
        self.canvas.delete(ALL)
class HyperlinkShow:
    """
        Aimed at touchscreens but can be used for any purpose where the user is required to follow hyperlinks between tracks
        Profiles for media tracks (message, image, video, audio ) specify links to other tracks
        in a link a symbolic name of an input is associated with a track-reference
        The show begins at first-track and then uses events (GPIO, keypresses etc.) to link to other tracks via their symbolic names
        If using 'call' keeps a record of the tracks it has visited so the 'return' command can go back.
        Executes timeout-track if no user input is received.

        links are of the form:
           symbolic-name command [track-ref]
        
        link commands:
          call <track-ref> play track-ref and add it to the path
          return - return 1 back up the path removing the track from the path, stops at home-track.
          return n - return n tracks back up the path removing the track from the path, stops at home-track.
          return <track-ref> return to <track-ref> removing tracks from the path
          home  - return to home-track removing tracks from the path
          jump <track-ref-> - play trck-ref forgetting the path back to home-track
          goto <track-ref> - play track-ref, forget the path 
          exit - end the hyperlink show
          null - inhibits the link defined in the show with the same symbolic name.

          reserved symbolic names
          pp-onend command  - pseudo symbolic name for end of a track

        interface:
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
    """

# *********************
# external interface
# ********************

    def __init__(self,
                            show_params,
                            root,
                            canvas,
                            showlist,
                             pp_dir,
                            pp_home,
                            pp_profile):
        """ canvas - the canvas that the tracks of the event show are to be written on
            show_params - the name of the configuration dictionary section for the hyperlinkshow
            showlist  - the showlist, to enable runningnof show type tracks.
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """
        
        self.mon=Monitor()
        self.mon.on()
        
        #instantiate arguments
        self.show_params=show_params
        self.root=root
        self.showlist=showlist
        self.canvas=canvas
        self.pp_dir=pp_dir
        self.pp_home=pp_home
        self.pp_profile=pp_profile

        # open resources
        self.rr=ResourceReader()

        #create a path stack
        self.path = PathManager()
        
        # init variables
        self.drawn  = None
        self.player=None
        self.shower=None
        self.timeout_running=None
        self.error=False



    def play(self,show_id,end_callback,ready_callback,top=False,command='nil'):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              ready_callback - callback when event-show is ready to display its forst track (not used?)
              top is True when the show is top level (run from [start] or from show control)
              command is not used
        """
        
        #instantiate arguments
        self.show_id=show_id
        self.end_callback=end_callback
        self.ready_callback=ready_callback
        self.top=top
        self.command=command

        # check data files are available.
        self.medialist_file = self.pp_profile + "/" + self.show_params['medialist']
        if not os.path.exists(self.medialist_file):
            self.mon.err(self,"Medialist file not found: "+ self.medialist_file)
            self.end('error',"Medialist file not found")
        
        #create a medialist object for the hyperlinkshow and read the file into it.
        self.medialist=MediaList()
        if self.medialist.open_list(self.medialist_file,self.showlist.sissue()) == False:
            self.mon.err(self,"Version of medialist different to Pi Presents")
            self.end('error',"Version of medialist different to Pi Presents")

        #get controls for this show if top level
        controlsmanager=ControlsManager()
        if self.top==True:
            self.controls_list=controlsmanager.default_controls()
            # and merge in controls from profile
            self.controls_list=controlsmanager.merge_show_controls(self.controls_list,self.show_params['controls'])

        
        # read show  links and destinations
        self.first_track_ref=self.show_params['first-track-ref']
        self.home_track_ref=self.show_params['home-track-ref']
        self.timeout_track_ref=self.show_params['timeout-track-ref']
        

    # state variables and signals   
        self.end_hyperlinkshow_signal= False
        self.egg_timer=None
        self.next_track_signal=False
        self.next_track_ref=''
        self.current_track_ref=''
        self.current_track_type=''

        # ready callback for show
        if self.ready_callback<>None:
            self.ready_callback()
                    
        self.canvas.delete('pp-content')
        self.canvas.config(bg='black')
        
        self.do_first_track()

        
#stop received from another concurrent show via ShowManager

    def managed_stop(self):
            # set signal to stop the hyperlinkshow when all  sub-shows and players have ended
            self.end_hyperlinkshow_signal=True
            # then stop and shows or tracks.
            if self.shower<>None:
                self.shower.managed_stop()
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.end('normal','stopped by ShowManager')
                
  
       
    # kill or error
    def terminate(self,reason):
        self.end_hyperlinkshow_signal=True
        if self.shower<>None:
            self.shower.terminate(reason)
        elif self.player<>None:
            self.player.terminate(reason)
        else:
            self.end(reason,'terminated without terminating shower or player')

          

   # respond to inputs
    def input_pressed(self,symbol,edge,source):

        self.mon.log(self,"received symbol: " + symbol)
        
        #does the symbol match a link, if so execute it
        if self.is_link(symbol,edge,source)==True:
            return
        
        # controls are disabled so ignore anything else
        if self.show_params['disable-controls']=='yes':
            return

        # does it match a control       
        # if at top convert symbolic name to operation otherwise lower down we have received an operatio    
        # look through list of controls to find match
        if self.top==True:
            operation=self.lookup_control(symbol,self.controls_list)
        else:
            operation=symbol
        # print 'operation',operation 
        if operation<>'':
            self.do_operation(operation,edge,source)



    def do_operation(self,operation,edge,source):
        if self.shower<>None:
            # if next lower show is running pass down to stop the show and lower level
            self.shower.input_pressed(operation,edge,source) 
        else:        
            # control this show and its tracks
            if operation=='stop':
                if self.player<>None:
                    if self.current_track_ref==self.first_track_ref and self.top==False:
                        self.end_radiobuttonshow_signal=True
                    self.player.input_pressed('stop')

            elif operation == 'pause':
                if self.player<>None:
                    self.player.input_pressed(operation)

            elif operation[0:4]=='omx-' or operation[0:6]=='mplay-'or operation[0:5]=='uzbl-':
                if self.player<>None:
                    self.player.input_pressed(operation)

   
    def is_link(self,symbol,edge,source):
        # find the first entry in links that matches the symbol and execute its operation
        # print 'hyperlinkshow ',symbol
        found=False
        for link in self.links:
            #print link
            if symbol==link[0]:
                found=True
                if link[1]<>'null':
                    # print 'match',link[0]
                    link_operation=link[1]
                    if link_operation=='home':
                        self.do_home(edge,source)
                    elif link_operation =='return':
                        self.do_return(link[2],edge,source)
                    elif link_operation =='call':
                        self.do_call(link[2],edge,source)
                    elif link_operation =='goto':
                        self.do_goto(link[2],edge,source)
                    elif link_operation =='jump':
                        self.do_jump(link[2],edge,source)
                    elif link_operation=='exit':
                        self.end('normal','executed exit command')
        return found


    def lookup_control(self,symbol,controls_list):
        for control in controls_list:
            if symbol == control[0]:
                return control[1]
        return ''

# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************


    def timeout_callback(self):
        self.do_call(self.timeout_track_ref,'front','timeout')

    def do_call(self,track_ref,edge,source):
        if track_ref<>self.current_track_ref:
            self.mon.log(self, 'call: '+track_ref)
            self.next_track_signal=True
            self.next_track_op='call'
            self.next_track_arg=track_ref
            if self.shower<>None:
                self.shower.input_pressed('stop',edge,source)
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.what_next()


    def do_goto(self,to,edge,source):
        if to<>self.current_track_ref:
            self.mon.log(self,'goto: '+to)
            self.next_track_signal=True
            self.next_track_op='goto'
            self.next_track_arg=to
            if self.shower<>None:
                self.shower.input_pressed('stop',edge,source)
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.what_next()

    def do_jump(self,to,edge,source):
        if to<>self.current_track_ref:
            self.mon.log(self,'jump to: '+to)
            self.next_track_signal=True
            self.next_track_op='jump'
            self.next_track_arg=to
            if self.shower<>None:
                self.shower.input_pressed('stop',edge,source)
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.what_next()
        
    def do_return(self,to,edge,source):
        self.next_track_signal=True
        if to.isdigit() or to=='':
            self.mon.log(self,'hyperlink command - return by: '+to)
            self.next_track_op='return-by'
            if to == '':
                self.next_track_arg='1'
            else:    
                self.next_track_arg=to
        else:
            self.mon.log(self,'hyperlink command - return to: '+to)
            self.next_track_op='return-to'
            self.next_track_arg=to        
        if self.shower<>None:
            self.shower.input_pressed('stop',edge,source)
        elif self.player<>None:
            self.player.input_pressed('stop')
        else:
            self.what_next()
        
    def do_home(self,edge,source):
        if self.current_track_ref<>self.home_track_ref:
            self.mon.log(self,'hyperlink command - home')
            self.next_track_signal=True
            self.next_track_op='home'
            if self.shower<>None:
                self.shower.input_pressed('stop',edge,source)
            elif self.player<>None:
                self.player.input_pressed('stop')
            else:
                self.what_next()


    def do_first_track(self):
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >=0:
            #don't use select the track as not using selected_track in hyperlinkshow
            first_track=self.medialist.track(index)
            self.current_track_ref=first_track['track-ref']
            self.path.append(first_track['track-ref'])
            self.play_selected_track(first_track)
        else:
            self.mon.err(self,"first-track not found in medialist: "+ self.show_params['first-track-ref'])
            self.end('error',"first track not found in medialist")

            

    
    def what_next(self):
        # user wants to end the show 
        if self.end_hyperlinkshow_signal==True:
            self.end_hyperlinkshow_signal=False
            self.end('normal',"show ended by user")

        # user has selected another track
        elif self.next_track_signal==True:
                self.next_track_signal=False

                # home
                if self.next_track_op in ('home'):
                    # back to 1 before home
                    back_ref=self.path.back_to(self.home_track_ref)
                    if back_ref=='':
                        self.mon.err(self,"home - home track not in path: "+self.home_track_ref)
                        self.end('error',"home - home track not in path")
                    # play home
                    self.next_track_ref=self.home_track_ref
                    self.path.append(self.next_track_ref)

                # return-by
                elif self.next_track_op in ('return-by'):
                    if self.current_track_ref<>self.home_track_ref:
                        # back n stopping at home
                        # back one more and return it
                        back_ref=self.path.back_by(self.home_track_ref,self.next_track_arg)
                        # use returned track
                        self.next_track_ref=back_ref
                        self.path.append(self.next_track_ref)

                # return-to
                elif self.next_track_op in ('return-to'):
                    #back to one before return-to track
                    back_ref=self.path.back_to(self.next_track_arg)
                    if back_ref=='':
                        self.mon.err(self,"return-to - track not in path: "+self.next_track_arg)
                        self.end('error',"return-to - track not in path")
                    # and append the return to track
                    self.next_track_ref=self.next_track_arg
                    self.path.append(self.next_track_ref)
                    
                # call
                elif self.next_track_op in ('call'):
                    # append the required track
                    self.path.append(self.next_track_arg)
                    self.next_track_ref=self.next_track_arg

                # goto
                elif self.next_track_op in ('goto'):
                    self.path.pop_for_sibling()
##                    #back to first track and remove it
##                    back_ref=self.path.back_to(self.first_track_ref)
##                    if back_ref=='':
##                        self.mon.err(self,"goto - first track not in path: "+self.first_track_ref)
##                        self.end('error',"goto - first track not in path")
                   #add the goto track
                    self.next_track_ref=self.next_track_arg
                    self.path.append(self.next_track_arg)

                # jump
                elif self.next_track_op in ('jump'):
                    # back to home and remove it
                    back_ref=self.path.back_to(self.home_track_ref)
                    if back_ref=='':
                        self.mon.err(self,"jump - home track not in path: "+self.home_track_ref)
                        self.end('error',"jump - track not in path")
                    # add back the home track without playing it
                    self.path.append(self.home_track_ref)
                    # append the jumped to track
                    self.next_track_ref=self.next_track_arg
                    self.path.append(self.next_track_ref)

                else:
                    self.mon.err(self,"unaddressed what next: "+ self.next_track_op+ ' '+self.next_track_arg)
                    self.end('error',"unaddressed what next")
                
                self.current_track_ref=self.next_track_ref                    
                index = self.medialist.index_of_track(self.next_track_ref)
                if index >=0:
                    #don't use select the track as not using selected_track in hyperlinkshow
                    next_track=self.medialist.track(index)
                    self.play_selected_track(next_track)
                else:
                    self.mon.err(self,"next-track not found in medialist: "+ self.next_track_ref)
                    self.end('error',"next track not found in medialist")
                    
        else:
            #track ends naturally
            #then input pp-onend symbolic name
            self.input_pressed('pp-onend','front','key')
        




# *********************
# Dispatching to Players
# *********************


    def page_callback(self):
        # called from a Player when ready to play, merge the links from the track with those from the show
        # and then enable the click areas
        self.delete_eggtimer()
        links_text=self.player.get_links()
        reason,message,track_links=self.path.parse_links(links_text)
        if reason=='error':
            self.mon.err(self,message + " in page")
            self.end('error',message)
        self.path.merge_links(self.links,track_links)

        # enable the click-area that are in the list of links
        self.enable_click_areas()


    
    def enable_click_areas(self):
        for link in self.links:
            #print 'trying link ',link[0] 
            if not('key-' in link[0]) and link[1]<>'null' and link[0]<>'pp-auto':
                # print 'enabling link ',link[0]
                self.canvas.itemconfig(link[0],state='normal')

                
    def play_selected_track(self,selected_track):
        """ selects the appropriate player from type field of the medialist and computes
              the parameters for that type
              selected track is a dictionary for the track/show
        """     

        if self.timeout_running<>None:
            self.canvas.after_cancel(self.timeout_running)
            self.timeout_running=None
            
        # self.canvas.delete(ALL)
        self.display_eggtimer(self.resource('hyperlinkshow','m01'))

        self.current_track_type = selected_track['type']
        
        #read the show links. Track links will be added by ready_callback
        links_text=self.show_params['links']
        reason,message,self.links=self.path.parse_links(links_text)
        if reason=='error':
            self.mon.err(self,message + " in show")
            self.end('error',message)

        #start timeout for the track if required           
             
        if self.current_track_ref<>self.first_track_ref and int(self.show_params['timeout'])<>0:
            self.timeout_running=self.canvas.after(int(self.show_params['timeout'])*1000,self.timeout_callback)
        

        # dispatch track by type
        self.player=None
        self.shower=None
        track_type = selected_track['type']
        self.mon.log(self,"Track type is: "+ track_type)
        
        if track_type=="video":
            # create a videoplayer
            track_file=self.complete_path(selected_track)
            self.player=VideoPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                        
        elif track_type=="audio":
            # create a audioplayer
            track_file=self.complete_path(selected_track)
            self.player=AudioPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                        
        elif track_type=="image":
            track_file=self.complete_path(selected_track)
            self.player=ImagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                    self.showlist,
                                    self.end_player,
                                    self.page_callback,
                                    enable_menu=False,
                                    )

        elif track_type=="web":
            # create a browser
            track_file=self.complete_path(selected_track)
            self.player=BrowserPlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(track_file,
                                        self.showlist,
                                        self.end_player,
                                        self.page_callback,
                                        enable_menu=False)
                                    
                         
        elif track_type=="message":
            # bit odd because MessagePlayer is used internally to display text. 
            text=selected_track['text']
            self.player=MessagePlayer(self.show_id,self.root,self.canvas,self.show_params,selected_track,self.pp_dir,self.pp_home,self.pp_profile)
            self.player.play(text,
                                    self.showlist,
                                    self.end_player,
                                    self.page_callback,
                                    enable_menu=False
                                    )
 
        elif track_type=="show":
            self.enable_click_areas()
            # get the show from the showlist
            index = self.showlist.index_of_show(selected_track['sub-show'])
            if index >=0:
                self.showlist.select(index)
                selected_show=self.showlist.selected_show()
            else:
                self.mon.err(self,"Show not found in showlist: "+ selected_track['sub-show'])
                self.end("Unknown show")
            
            if selected_show['type']=="mediashow":    
                self.shower= MediaShow(selected_show,
                                                               self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="liveshow":    
                self.shower= LiveShow(selected_show,
                                                                self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                              self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="radiobuttonshow":    
                self.shower= RadioButtonShow(selected_show,
                                                                 self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                                 self.pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')

            elif selected_show['type']=="hyperlinkshow":    
                self.shower= HyperlinkShow(selected_show,
                                                               self.root,
                                                                self.canvas,
                                                                self.showlist,
                                                               self,pp_dir,
                                                                self.pp_home,
                                                                self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')
                
            elif selected_show['type']=="menu": 
                self.shower= MenuShow(selected_show,
                                                        self.root,
                                                        self.canvas,
                                                        self.showlist,
                                                        self.pp_dir,
                                                        self.pp_home,
                                                        self.pp_profile)
                self.shower.play(self.show_id,self.end_shower,self.ready_callback,top=False,command='nil')                    
            else:
                self.mon.err(self,"Unknown Show Type: "+ selected_show['type'])
                self.end("Unknown show type")  
                
        else:
            self.mon.err(self,"Unknown Track Type: "+ track_type)
            self.end("Unknown track type")

    
    # callback from when player ends
    def end_player(self,reason,message):
        self.mon.log(self,"Returned from player with message: "+ message)
        self.player=None
        # this does not seem to change the colour of the polygon
        self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks( )
        if reason in("killed","error"):
            self.end(reason,message)
        else:
            self.display_eggtimer(self.resource('hyperlinkshow','m02'))
            self.what_next()

    # callback from when shower ends
    def end_shower(self,show_id,reason,message):
        self.mon.log(self,"Returned from shower with message: "+ message)
        self.shower=None
        self.canvas.itemconfig('pp-click-area',state='hidden')
        self.canvas.update_idletasks( )
        if reason in ("killed","error"):
            self.end(reason,message)
        else:
            self.display_eggtimer(self.resource('hyperlinkshow','m03'))
            self.what_next()  


# *********************
# End the show
# *********************
    # finish the player for killing, error or normally
    # this may be called directly sub/child shows or players are not running
    # if they might be running then need to call terminate.

    def end(self,reason,message):
        self.mon.log(self,"Ending hyperlinkshow: "+ self.show_params['show-ref'])  
        self.end_callback(self.show_id,reason,message)
        self=None
        return




# *********************
# displaying things
# *********************

    def display_eggtimer(self,text):
        #self.egg_timer=self.canvas.create_text(int(self.canvas['width'])/2,
                                              #int(self.canvas['height'])/2,
                                                  #text= text,
                                                # fill='white',
                                               # font="Helvetica 20 bold")
        #self.canvas.update_idletasks( )
        pass


    def delete_eggtimer(self):
        if self.egg_timer!=None:
            self.canvas.delete(self.egg_timer)

# *********************
# utilities
# *********************

    def complete_path(self,selected_track):
        #  complete path of the filename of the selected entry
        track_file = selected_track['location']
        if track_file<>'' and track_file[0]=="+":
                track_file=self.pp_home+track_file[1:]
        self.mon.log(self,"Track to play is: "+ track_file)
        return track_file     

    def resource(self,section,item):
        value=self.rr.get(section,item)
        if value==False:
            self.mon.err(self, "resource: "+section +': '+ item + " not found" )
            # players or showers may be running so need terminate
            self.terminate("error")
        else:
            return value