def __init__(self, cfg):
     #Service that handles HTTP requests
     dl = DataListener()
 
     #Start the HTTP server
     cp_cfg = cfg.get('cherrypy',{})
     cp_cfg['log.screen'] = cp_cfg.get('log.screen', False)
     self.ls = ListeningService(cp_cfg, dl)
     self.ls.server_started.connect(self.on_server_started)
     self.ls.server_stopped.connect(self.on_server_stopped)
     self.ls.start()
     
     self.cfg = cfg
     self.session_manager = SessionManager(cfg, dl)
     self.session_manager.session_new.connect(self.on_session_new)
     self.session_manager.switch_new_session()
     
     self.command_console = CommandConsole()
     self.command_console.quit_command.connect(self.on_quit_command)
     self.command_console.save_command.connect(self.on_save_command)
    def __init__(self, app, cfg):
        self.frame = MainFrame(app)
        self.cfg = cfg
        self.session_id2page = {}
        self.page2session_id = {}
        
        #Event Bindings
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_save_session, 
                  id=self.frame.save_session_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_new_session, 
                  id=self.frame.new_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_switch_session, 
                  id=self.frame.switch_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_clone_session,
                  id=self.frame.clone_session_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_clear_session, 
                  id=self.frame.clear_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_start_server, 
                  id=self.frame.start_server_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_stop_server, 
                  id=self.frame.stop_server_btn.GetId())
        
        self.frame.Bind(wx.EVT_CLOSE, self.on_quit)
        
        #Service that handles HTTP requests
        dl = DataListener()
    
        self.session_manager = SessionManager(cfg, dl)
        self.session_manager.session_new.connect(self.on_session_new)
        self.session_manager.session_switch.connect(self.on_session_switch)
        self.session_manager.switch_new_session()
        
        #Start the HTTP server
        self.ls = ListeningService(cfg.get('cherrypy',{}), dl)
        self.ls.server_started.connect(self.on_server_started)
        self.ls.server_stopped.connect(self.on_server_stopped)
        self.ls.start()
        

        self.frame.Show()
        self.frame.Maximize()
class ConsoleController(object):
    def __init__(self, cfg):
        #Service that handles HTTP requests
        dl = DataListener()
    
        #Start the HTTP server
        cp_cfg = cfg.get('cherrypy',{})
        cp_cfg['log.screen'] = cp_cfg.get('log.screen', False)
        self.ls = ListeningService(cp_cfg, dl)
        self.ls.server_started.connect(self.on_server_started)
        self.ls.server_stopped.connect(self.on_server_stopped)
        self.ls.start()
        
        self.cfg = cfg
        self.session_manager = SessionManager(cfg, dl)
        self.session_manager.session_new.connect(self.on_session_new)
        self.session_manager.switch_new_session()
        
        self.command_console = CommandConsole()
        self.command_console.quit_command.connect(self.on_quit_command)
        self.command_console.save_command.connect(self.on_save_command)
    
    def cmdloop(self):
        self.command_console.cmdloop()
    
    def on_server_started(self, **kwargs):
        print "HTTP Server Started"
    
    def on_server_stopped(self, **kwargs):
        print "HTTP Server Stopped"    
    
    def on_quit_command(self, **kwargs):
        self.ls.exit()
    
    def on_save_command(self, output_dir, **kwargs):
        self.session_manager.save(output_dir)
        
    def start_server(self):
        self.ls.start()
        
    def on_session_new(self, session_id, session_monitors, **kwargs):
        #for monitor_name, action_cfg in self.cfg.iteritems():
        for monitor_name, monitor in session_monitors.iteritems():
            monitor_cfg = self.cfg['elements'][monitor_name]
            panel_factory = FigureMemoryPanelFactory()
            p = panel_factory.build(monitor.get_figure(), monitor_cfg)
            #we throw away p?
class GUIController(object):
    def __init__(self, app, cfg):
        self.frame = MainFrame(app)
        self.cfg = cfg
        self.session_id2page = {}
        self.page2session_id = {}
        
        #Event Bindings
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_save_session, 
                  id=self.frame.save_session_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_new_session, 
                  id=self.frame.new_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_switch_session, 
                  id=self.frame.switch_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_clone_session,
                  id=self.frame.clone_session_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_clear_session, 
                  id=self.frame.clear_session_btn.GetId()) 
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_start_server, 
                  id=self.frame.start_server_btn.GetId())
        self.frame.Bind(wx.EVT_TOOL, self.on_btn_stop_server, 
                  id=self.frame.stop_server_btn.GetId())
        
        self.frame.Bind(wx.EVT_CLOSE, self.on_quit)
        
        #Service that handles HTTP requests
        dl = DataListener()
    
        self.session_manager = SessionManager(cfg, dl)
        self.session_manager.session_new.connect(self.on_session_new)
        self.session_manager.session_switch.connect(self.on_session_switch)
        self.session_manager.switch_new_session()
        
        #Start the HTTP server
        self.ls = ListeningService(cfg.get('cherrypy',{}), dl)
        self.ls.server_started.connect(self.on_server_started)
        self.ls.server_stopped.connect(self.on_server_stopped)
        self.ls.start()
        

        self.frame.Show()
        self.frame.Maximize()
        
    def on_quit(self, event):
        self.ls.stop()
        self.ls.exit()
        event.Skip()
        
    def on_server_started(self, **kwargs):
        self.frame.toolbar.EnableTool(self.frame.start_server_btn.GetId(), False)
        self.frame.toolbar.EnableTool(self.frame.stop_server_btn.GetId(), True)
    
    def on_server_stopped(self, **kwargs):
        self.frame.toolbar.EnableTool(self.frame.start_server_btn.GetId(), True)
        self.frame.toolbar.EnableTool(self.frame.stop_server_btn.GetId(), False)
        
    def on_btn_clear_session(self, event):
        self.session_manager.clear()
        event.Skip()    
        
    def on_btn_save_session(self, event):
        dir_dialog = wx.DirDialog(self.frame, "Select the output directory")
        if dir_dialog.ShowModal() == wx.ID_OK:
            self.save(dir_dialog.GetPath())
        event.Skip()
    
    def on_btn_new_session(self, event):
        self.session_manager.new_session()
        event.Skip()
        
    def on_btn_switch_session(self, event):
        page_id = self.frame.get_selection()
        session_id = self.page2session_id[page_id]
        self.session_manager.switch_session(session_id)
        event.Skip()
        
    def on_btn_clone_session(self, event):
        page_id = self.frame.get_selection()
        session_id = self.page2session_id[page_id]
        self.session_manager.clone_session(session_id)
        event.Skip()
            
    def on_btn_start_server(self, event):
        self.ls.start()
        event.Skip()
        
    def on_btn_stop_server(self, event):
        self.ls.stop()
        event.Skip()
        
    def save(self, outdir):
        self.session_manager.save(outdir)
    
    def on_session_switch(self, session_id, **kwargs):
        page_id = self.session_id2page[session_id]
        self.frame.select_active_session(page_id)
    
        
    def on_session_new(self, session_id, session_monitors, **kwargs):
        session_panel, page_id = self.frame.new_session_panel(session_id)
        self.session_id2page[session_id] = page_id
        self.page2session_id[page_id] = session_id
        for tab_name, tab_monitors in sorted(session_monitors.iteritems()):
            tab_panel = session_panel.new_tab(tab_name)
            for monitor_name, monitor in sorted(tab_monitors.iteritems()):
                try:
                    monitor_cfg = self.cfg['elements']['tabs'][tab_name][monitor_name]
                except KeyError:
                    monitor_cfg = self.cfg['elements'][monitor_name]
                monitor_figure = monitor.get_figure()
                tab_panel.add_monitor(monitor_cfg, 
                                   monitor_figure, monitor_name)