import Song2HTML import os import glob import wx from string import lower class MyFrame(wx.Frame): def __init__(self, *args, **kwds): self.globals = globals self.NamesList, self.TitlesList = buildNamesAndTitlesList(self.globals.massOrderFile) self.songList = listSongs(self.globals.songdir) self.PathsList = [] # begin wxGlade: MyFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL wx.Frame.__init__(self, *args, **kwds) self.controls = [] #see if previous values stored - use those prevValues = True and os.path.exists(globals.chosenSongs) or False if prevValues: prevValueFile = open(globals.chosenSongs,"r") prevValueList = [line.strip() for line in prevValueFile.readlines()] prevValueFile.close() if len(prevValueList) != len(self.TitlesList): prevValues = False for name,useName in self.TitlesList: #previous values ready to use? if prevValues: comboBoxValue = prevValueList.pop(0) if not os.path.exists(os.path.join(globals.songdir,comboBoxValue + ".txt")): comboBoxValue = globals.noSong else: #no previous values comboBoxValue = self.songList[0] #Special changes for resp, acc and creed if name in globals.specialSongs.keys(): comboBoxValue = self.globals.specialSongs[name] self.controls.append([wx.StaticText(self,-1,str(name),wx.DefaultPosition,(120,-1),style=wx.ALIGN_CENTER|wx.ST_NO_AUTORESIZE), \ wx.ComboBox(self, -1, value=comboBoxValue, style=wx.ALIGN_CENTER|wx.CB_READONLY|wx.ALIGN_RIGHT, \ choices=self.songList)]) self.buttonExecute = wx.Button(self, -1, "Go", style=wx.BU_LEFT|wx.BU_RIGHT|wx.BU_TOP|wx.BU_BOTTOM) self.__do_layout() self.Bind(wx.EVT_BUTTON, self.execute, self.buttonExecute) # end wxGlade def __do_layout(self): # begin wxGlade: MyFrame.__do_layout self.hozSizers = [] sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_2 = wx.BoxSizer(wx.VERTICAL) for item in self.controls: self.hozSizers.append(wx.BoxSizer(wx.HORIZONTAL)) for control in item: self.hozSizers[-1].Add(control, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) sizer_2.Add(self.hozSizers[-1], 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0) sizer_2.Add(self.buttonExecute, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) # sizer_2.Add(wx.StaticText(self, -1,"Remember to change the Psalm and the Acclamation!",style=wx.ALIGN_CENTER), 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) self.SetAutoLayout(True) self.SetSizer(sizer_1) sizer_1.Fit(self) sizer_1.SetSizeHints(self) self.Layout() # end wxGlade def execute(self, event): # wxGlade: MyFrame. chosenSongs = open(self.globals.chosenSongs,"w") for i in range(len(self.TitlesList)): value = self.controls[i][1].GetValue() if (value != self.globals.noSong) and (value != ""): self.PathsList.append((self.NamesList[i], rebuildSongPath(self.globals.songdir,value))) # Write the song name to the chosen songs file chosenSongs.write(value + "\n") else: chosenSongs.write(globals.noSong + "\n") chosenSongs.close() Song2HTML.makeHTMLFiles(self.PathsList) #popup a message to inform that the html is ready. wx.MessageDialog(self,"The files have been created, and placed into %s" \ % (os.getcwd(),),"Files Created",style=wx.OK|wx.ICON_INFORMATION).ShowModal() # end of class MyFrame class wxSongPickerApp(wx.App): """The wx.App for the Song Picker application""" def OnInit(self): """Override OnInit to create our Frame""" frame = MyFrame(None, title="St Dom's Song Picker") frame.Show() self.SetTopWindow(frame) return True class globalvars: massOrderFile = "Service Order.txt" masterList = [] songdir = "songs" noSong = "No Song" chosenSongs = "Chosen Songs.txt" specialSongs = {"Responsorial Psalm" : "resp", "Gospel Acclamantion" : "gosp", "Creed" : "creed"} def listSongs(songLoc): songs = [line.strip(" \n")[len(globals.songdir)+1:-4] for line in glob.glob(os.path.join(songLoc,"*.txt"))] songs.sort(key=lower) return [globals.noSong] + songs def rebuildSongPath(songLoc,songName): return os.path.normpath(os.path.join(songLoc,songName + ".txt")) def buildNamesAndTitlesList(MassOrderFile): ''' Return a names list from the Service Order File, inserting None where the song name is to be used Also return a TitlesList, which lists all names regardless of whether they are to be used or not''' NamesList = [] TitlesList = [] fh = open(MassOrderFile,"r") lines = fh.readlines() fh.close() for line in lines: line = line.strip() if line and (line[0] != "#"): name,type = line.strip(" \n").split(" ~ ") TitlesList.append([name]) if type == "1": name = None TitlesList[-1].append(False) else: TitlesList[-1].append(True) NamesList.append(name) return NamesList, TitlesList # Put into execute funct if __name__ == "__main__": globals = globalvars() app = wxSongPickerApp(globals) app.MainLoop() # NamesList = buildNamesList(globals.massOrderFile)