def check(filePath, interactive=False): songDir, fileName = os.path.split(filePath) status = 0 # validate file try: validateXML(filePath, getSchema()) except: # validation NOT OK status += 1 if interactive: exctype, value = sys.exc_info()[:2] excMsg = u'%s: %s' % (unicode(exctype), value) # display info, offer chance to correct the song corrInvalid = subprocess.call([u'yad', u'--title=Invalid XML fájl', u'--center', u'--window-icon=gtk-dialog-warning', u'--image=gtk-dialog-warning', u'--no-markup', u'--text=Invalid XML fájl!\n\n%s\n\nJavítod?' % excMsg]) if corrInvalid == 0: # OK, edit the song song = ol.Song(filePath) song, fileName, songDir = editSong(song, fileName, songDir) else: # do not correct it pass # check verse order song = ol.Song(filePath) if not isVerseOrderOK(song): # verse order is NOT OK status += 2 if interactive: # offer chance to correct it corrVerseOrder = subprocess.call([u'yad', u'--title=Hibás versszaksorrend', u'--center', u'--window-icon=gtk-dialog-warning', u'--image=gtk-dialog-warning', u'--text=Hibás a versszaksorrend! Javítod?']) if corrVerseOrder == 0: # OK, edit the song song = ol.Song(filePath) song, fileName, songDir = editSong(song, fileName, songDir) else: # do not correct it pass return status
def updateSongs(args): inDirPath = unicode(args.inDir, encoding=sys.getfilesystemencoding()) outDirPath = unicode(args.outDir, encoding=sys.getfilesystemencoding()) newSongDataTablePath = unicode(args.songTable, encoding=sys.getfilesystemencoding()) if not os.path.isdir(outDirPath): os.mkdir(outDirPath) inSongInfoDF = getSongInfoDF(inDirPath) newSongDataDF = getSongDataDF(newSongDataTablePath) for newSongDataIndex, newSongDataS in newSongDataDF.iterrows(): songbookEntry = newSongDataS[u'#'] outFileName = getOutFileName(newSongDataS) outFilePath = os.path.join(outDirPath, outFileName) if songbookEntry: # skip songs without songbookEntry print songbookEntry # process indicator if len(inSongInfoDF[inSongInfoDF[u'Sorszám']==songbookEntry].index) > 0: # input file exists inFilePath = inSongInfoDF[inSongInfoDF[u'Sorszám']==songbookEntry].iloc[0][u'Fájlnév'] shutil.copy2(inFilePath, outFilePath) song = ol.Song(outFilePath) else: # input file doesn't exist, create new file song = ol.Song() updateSong(song, newSongDataS) song = setMetaData(song) song.write(outFilePath)
def getSongInfo(songInfo): #~ #if isinstance(item, str) or isinstance(item, unicode): #~ if isinstance(item, unicode): #~ song = ol.Song(item) #~ else: #~ song = item #~ #~ if songInfo is None: #~ songInfo = SongInfo() song = ol.Song(songInfo.filePath) # first title if len(song.props.titles) > 0: songInfo.titleText = unicode(song.props.titles[0].text) else: songInfo.titleText = u'' # first author if len(song.props.authors) > 0: songInfo.authorName = unicode(song.props.authors[0].name) else: songInfo.authorName = u'' # first songbook entry if len(song.props.songbooks) > 0: songInfo.songbookEntry = unicode(song.props.songbooks[0].entry) else: songInfo.songbookEntry = u'' # status songInfo.status = unicode(check(songInfo.filePath)) return songInfo
def createNewSong(): song = ol.Song() song.props.titles.append(ol.Title()) song.props.authors.append(ol.Author()) song.verses.append(ol.Verse()) song.verses[0].lines.append(ol.Lines()) song.verses[0].lines[0].lines.append(ol.Line(u'')) song.props.songbooks.append(ol.Songbook()) return song
def renumberSongs(zerosOn, songDir): songInfoDF = getSongInfoDF(songDir) for songIndex, songInfoS in songInfoDF.iterrows(): filePath = songInfoS[u'Fájlnév'] song = ol.Song(filePath) for songbook in song.props.songbooks: if songbook.name == u'Énekfüzet': entryInt = int(songbook.entry) if zerosOn: songbook.entry = '%03d' % entryInt else: songbook.entry = '%d' % entryInt song = setMetaData(song) song.write(filePath)
def main(): filePath = sys.argv[1] outDir = sys.argv[2] song = ol.Song(filePath) authorsUpdated = [] for author in song.props.authors: if not author.type: author.type = 'words' authorsUpdated.append(author) author2 = copy.deepcopy(author) author2.type = 'music' authorsUpdated.append(author2) else: authorsUpdated.append(author) song.props.authors = authorsUpdated song.modifiedIn = u'openlyrics.py %s' % (ol.__version__) songDir, fileName = os.path.split(filePath) outPath = os.path.join(outDir, fileName) song.write(outPath) return 0
def main(): parser = argparse.ArgumentParser(description=u'Edit OpenLyrics file(s).') parser.add_argument(u'target', help=u'OpenLyrics XML file or a directory') parser.add_argument(u'-c', u'--check', action=u'store_true', help=u'Check file instead of editing it') #~ parser.add_argument(u'-s', u'--schema', help=u'RelaxNG XML schema') args = parser.parse_args() if os.path.isfile(args.target): filePath = unicode(args.target, encoding=sys.getfilesystemencoding()) songDir, fileName = os.path.split(filePath) if args.check: check(filePath, interactive=True) else: song = ol.Song(filePath) song, fileName, songDir = editSong(song, fileName, songDir) elif os.path.isdir(args.target): songDir = unicode(args.target, encoding=sys.getfilesystemencoding()) returncode, results = editDir(songDir) else: raise Exception(u'Error! %s is not a file or a directory' % args.target) return 0
def editDir(songDir): while True: songInfoDF = getSongInfoDF(songDir) writeCache(songInfoDF, songDir) dialogFieldContents = getDialogFieldContents(songInfoDF) xmlCount = len(songInfoDF.index) returncode, results = editDirByYad(dialogFieldContents, songDir, xmlCount) #~ print returncode fileName = getSongFileName(results) songPath = os.path.join(songDir, fileName) if fileName: # a song has been selected if returncode == 2: # Edit song song = ol.Song(songPath) song, fileName, songDir = editSong(song, fileName, songDir) elif returncode == 8: # Delete song os.remove(songPath) else: # no song has been selected if returncode in [0, 252]: # Close OR Esc/Windows closed pass elif returncode == 2: # Edit song pass # can not edit, if no song has been selected elif returncode == 4: # New song song, fileName, songDir = editSong(songDir=songDir) elif returncode == 8: # Delete song pass # can not delete, if no song has been selected elif returncode == 16: # Refresh song list pass # song list will be refreshed in the new iteration else: raise Exception(u'Error! Unknown exit code: %d. See YAD documentation (man yad) for details!' % returncode) # finish editing on Close or Esc/Windows closed if returncode not in [2, 4, 8, 16]: break return returncode, results