You are not logged in.

#1 2010-07-31 18:48:27

guisacouto
Member
From: Portugal
Registered: 2009-06-27
Posts: 107

lircrcMaker - a GUI for lirc managing

I know this isnt very arch way, but  I just wanted to learn python and developing GUI's so I just thought about something simple to start. I normally only use C and Java, without any GUI toolkit, so this is kind of a test.

-> .lircrc editor (add/remove) so you  cand bind the remote keys to functions
-> start and stop Lirc with module start

import wx
import os
import fileinput
import re
import types


bindslist = []
module = ""

configfile = os.getenv("HOME")
configfile = configfile + "/.lircrc"

class Binds:
    def __init__(self, remote, button, config): 
        self._remote = remote
        self._button = button
        self._config = config

class MyFrame(wx.Frame):
    
    def __init__(self,parent, id, title):
        wx.Frame.__init__(self,parent,id,title,size=(370, 350), style=wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.RESIZE_BOX | wx.MAXIMIZE_BOX))
        
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        helpMenu = wx.Menu()

        menu.Append(101, "Start Lirc Daemon")
        menu.Append(102, "Stop Lirc Daemon")
        menu.Append(103, "Exit")
        
        helpMenu.Append(104, "About")
        menuBar.Append(menu, "Options")
        menuBar.Append(helpMenu, "Help")
        self.SetMenuBar(menuBar)


#listctrl


        self.lstctrl = wx.ListCtrl(self, 150, pos=(10, 140), style=wx.LC_REPORT, size=(360, 135))
        self.lstctrl.InsertColumn(0, 'Remote')
        self.lstctrl.InsertColumn(1, 'Button')
        self.lstctrl.InsertColumn(2, 'Execute')
        self.lstctrl.SetColumnWidth(0, 125)
        self.lstctrl.SetColumnWidth(1, 125)
        self.lstctrl.SetColumnWidth(2, 125)


#Buttons


        okButton = wx.Button(self, id=131, pos=(270, 90), name="Add Bind", label="Add Bind")
        self.Bind(wx.EVT_BUTTON, self.WriteNewBind, id=131)
        removeButton = wx.Button(self, id=132, pos=(270, 290), name="Remove", label="Remove")
        self.Bind(wx.EVT_BUTTON, self.RemoveBind, id=132)

        
#Static text labels

        
        stRemote = wx.StaticText(self, 111, "Remote Module", pos=(10, 10))
        stButton = wx.StaticText(self, 112, "Button", pos=(10, 40))
        stConfig = wx.StaticText(self, 113, "Execute This", pos=(10, 70))


#ComboBox's for getting the input from user


        self.remote = wx.ComboBox(self, 121, pos=(100, 10), size=(150, 20))
        self.remote.Append("lirc-mceusb2")

        self.button = wx.ComboBox(self, id=122, pos=(100, 40), size=(150, 20))
        self.button.Append("Play")
        self.button.Append("Stop")
        self.button.Append("Skip")
        self.button.Append("Replay")
        self.config = wx.TextCtrl(self, 123, pos=(100, 70), size=(150, 20))


#Menu Binds


        self.Bind(wx.EVT_MENU, self.StartLirc, id=101)
        self.Bind(wx.EVT_MENU, self.StopLirc, id=102)
        self.Bind(wx.EVT_MENU, self.QuitApp, id=103)
        self.Bind(wx.EVT_MENU, self.About, id=104)


#Menu Actions


    def StartLirc(self, event): 
        global module
        dialog = MyDialog(self, 161, "Choose Module")
        dialog.ShowModal()
        dialog.Destroy()
        print "\n\n MODULE:" + module
        print "Starting Lirc"
        os.system("gksudo /etc/rc.d/lircd start")
        os.system("gksudo modprobe "+ module)
        os.system("irexec &")

    def StopLirc(self, event):
        os.system("gksudo killall lircd")

    def QuitApp(self, event):
        self.Close()


    def About(self, event):
        about = wx.AboutDialogInfo()
        
        about.AddDeveloper("Guilherme de Sousa")
        about.SetDescription("A simple Lircrc wizzard to add new Binds to your IR Remote")
        about.SetVersion("v 1.0")
        about.SetName("LircrcMaker")
        aboutBox = wx.AboutBox(about)
        
    def ChooseModule(self, event):
        print "\n\nCHOOSE MODULE\n\n"
        self.module = self.choosemodule.GetValue()
        self.dialog.Show(False)
        print self.module
        
        
#File Handling


    def WriteNewBind(self, event):
        global configfile
        fileHandle = open(configfile, "a")

        _remote = self.remote.GetValue()
        _button = self.button.GetValue()
        _config = self.config.GetLineText(0)
        
        fileHandle.write("\tbegin\n\t\tremote = " + _remote + "\n\t\tbutton = " + _button + "\n\t\tprog = irexec\n\t\trepeat = 0\n\t\tconfig = " + _config + "\n\tend\n")
        fileHandle.close()
        self.lstctrl.Append((_remote, _button, _config))

    def RemoveBind(self, event):
        global configfile
        item = self.lstctrl.GetNextItem(-1, wx.LIST_NEXT_ALL, wx.LIST_STATE_FOCUSED)
        
            
        self.lstctrl.DeleteItem(item)

        size = self.lstctrl.GetItemCount()
        
        os.remove(configfile)
            
        os.system("touch " + configfile)
        fileHandle = open(configfile , "a")
        for i in range(0, size):
            _remote = self.lstctrl.GetItem(i, 0).GetText()
            _button = self.lstctrl.GetItem(i, 1).GetText()
            _config = self.lstctrl.GetItem(i, 2).GetText()
            fileHandle.write("\tbegin\n\t\tremote = " + _remote + "\n\t\tbutton = " + _button + "\n\t\tprog = irexec\n\t\trepeat = 0\n\t\tconfig = " + _config +"\n\tend\n")
        fileHandle.close()

# Dialog for choosing remote module

class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(250,130))

        stChooseModule = wx.StaticText(self, 162, "Choose the module for your remote control:", pos=(10, 20))
        self.choosemodule = wx.ComboBox(self, 163, pos=(10, 50), size=(230, 20))
        self.choosemodule.Append("lirc-mceusb2")
        dialogOK = wx.Button(self, 164, pos=(150, 90), label="OK")
        self.Bind(wx.EVT_BUTTON, self.ChooseModule, id=164)
        
    def ChooseModule(self, event):
        global module
        module = self.choosemodule.GetValue()
        self.Show(False)
        print self.module

class MyApp(wx.App):
    
    global bindslist

    
    def MyParser(self):
    global configfile
    
    os.system("touch " + configfile)
        
    file = open(configfile, "r")
        
        for line in file:
            if "remote" in line:
                _split = line.split("=")
                _remote = _split[1]
            elif "button" in line:
                _split = line.split("=")
                _button = _split[1]
            elif "config" in line:
                _split = line.split("=")
                _config = _split[1]
            elif "end" in line:
                bindslist.append(Binds(_remote, _button, _config))

    def InsertListCtrl(self):
        for i in bindslist:
           self.frame.lstctrl.Append((i._remote, i._button, i._config))


    def PrintParser(self):
        for b in bindslist:
            print b._config
            print b._button
            print b._remote



    def OnInit(self):
        self.frame = MyFrame(None, -1, "LircrcMaker")
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        os.system("touch " + configfile)
        self.MyParser()
        self.InsertListCtrl()
        return True

app = MyApp(0)
app.MainLoop()

Dependencys:

wxpython
irexec
lirc
gksudo

if you find any bug feel free to post.
if you want to change/use the code feel free to do it smile

best regards!

pastebin code: http://pastebin.com/srrRAZj8

Last edited by guisacouto (2010-07-31 18:58:49)

Offline

Board footer

Powered by FluxBB