Example #1
0
class ClippyFrame(wx.Frame):
    def __init__(self, parent, title):
        self.port = 0
        self.init_clippy()

        ### Frame

        wx.Frame.__init__(self,
                          parent,
                          title=title,
                          size=(300, 200),
                          style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

        ### Menu

        fileMenu = wx.Menu()
        menuAbout = fileMenu.Append(
            wx.ID_ABOUT, "&About",
            "developed by Rahul Madhavan ([email protected])")
        menuExit = fileMenu.Append(wx.ID_EXIT, "&Exit", "Adios !!!")

        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "File")
        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        ### UI Elements

        self.current_port_no_label = wx.StaticText(self, wx.ID_ANY,
                                                   "Current Port No : ")
        self.port_text_box = IntCtrl(self,
                                     wx.ID_ANY,
                                     value=self.port,
                                     min=1000,
                                     max=100000)

        self.port_no_label = wx.StaticText(self, wx.ID_ANY, " Port No : ")
        self.current_port = wx.StaticText(self, wx.ID_ANY, str(self.port))

        self.restart_button = wx.Button(self, -1, "Restart Clippy")
        self.restart_button.Disable()

        ### Binders
        self.Bind(wx.EVT_BUTTON, self.OnRestart, self.restart_button)
        self.Bind(wx.EVT_TEXT, self.OnTextChange, self.port_text_box)

        ### Sizers

        flex_sizer = wx.FlexGridSizer(2, 2, 20, 35)

        flex_sizer.AddMany([(self.current_port_no_label),
                            (self.current_port, 1, wx.EXPAND),
                            (self.port_no_label),
                            (self.port_text_box, 1, wx.EXPAND)])

        v_sizer = wx.BoxSizer(wx.VERTICAL)

        v_sizer.Add(flex_sizer, 1, wx.EXPAND)
        v_sizer.Add(self.restart_button, 1, wx.EXPAND | wx.ALIGN_CENTER)

        f_sizer = wx.BoxSizer(wx.VERTICAL)
        f_sizer.Add(v_sizer, 1, wx.EXPAND | wx.ALL, 20)

        self.SetSizer(f_sizer)
        self.Show()

    def OnAbout(self, event):
        dlg = wx.MessageDialog(
            self,
            "developed by Rahul Madhavan ([email protected])",
            "Clippy",
            style=wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def OnExit(self, event):
        self.Close(True)

    def OnRestart(self, e):
        self.watcher.stop()
        self.receiver.stop()
        port = self.port_text_box.GetValue()
        self.persist_json_config({'port': port})
        self.init_clippy()
        self.current_port.SetLabelText(str(self.port))
        self.restart_button.Disable()

    def OnTextChange(self, e):
        temp_port = self.port_text_box.GetValue()
        if self.port != temp_port and self.port_text_box.IsInBounds():
            self.restart_button.Enable()
        else:
            self.restart_button.Disable()

    def init_clippy(self):
        self.msg = Msg("")
        config = self.fetch_clippy_configuration()
        port = config["port"]
        self.port = port
        print "USING PORT : " + str(port)
        self.watcher = ClippyWatcher(self.msg, port)
        self.receiver = ClippyReceiver(self.msg, port)
        self.watcher.daemon = True
        self.receiver.daemon = True
        self.watcher.start()
        self.receiver.start()

    def fetch_clippy_configuration(self):
        config = {}
        data_dir = wx.StandardPaths.Get().GetUserDataDir()
        filename = CONFIG_FILE
        file_abs_path = os.path.join(data_dir, filename)
        if os.path.isfile(file_abs_path):
            fp = open(file_abs_path, 'r')
            str = fp.read()
            config = json.loads(str)
        else:
            print "config file not found"
            config = self.create_json_config()
        return config

    def persist_json_config(self, config):
        data_dir = wx.StandardPaths.Get().GetUserDataDir()
        filename = CONFIG_FILE
        file_abs_path = os.path.join(data_dir, filename)
        with open(file_abs_path, 'w') as outfile:
            json.dump(config, outfile)
        return config

    def create_json_config(self):
        config = {"port": MCAST_PORT}
        data_dir = wx.StandardPaths.Get().GetUserDataDir()
        filename = CONFIG_FILE
        file_abs_path = os.path.join(data_dir, filename)
        if not os.path.exists(data_dir):
            os.makedirs(data_dir)
        with open(file_abs_path, 'w') as outfile:
            json.dump(config, outfile)
        return config