def getVersion(): """ determine the LilyPond version of the current document """ match = re.search(r'\\version\s*"(\d+)(?:\.(\d+)(?:\.(\d+))?)?', editor.text()) if match: return tuple(int(s or "0") for s in match.groups()) else: return None
def convertLy(): """ Run convert-ly on the current document """ global version docVersion = getVersion() if not docVersion: sorry(_("Can't determine the LilyPond version of the current document." " Please add a \\version statement with the correct version.")) elif not version: sorry(_("Can't determine the version of LilyPond. " "Please check your LilyPond installation.")) elif docVersion >= version: sorry(_("This LilyPond document is already up-to-date.")) else: # Ok, let's run convert-ly. # We add the from-version. Only in that case convert-ly wants to # read from stdin. convert_ly = config("commands").get("convert-ly", "convert-ly") try: out, err = Popen((convert_ly, "-f", "%d.%d.%d" % docVersion, "-"), stdin=PIPE, stdout=PIPE, stderr=PIPE ).communicate(editor.text().encode('utf8')) if out: editor.setText(u"%s\n\n%%{\n%s\n%%}\n" % (out.decode('utf8'), err.decode('utf8'))) info(_( "The document has been processed with convert-ly. You'll find " "the messages of convert-ly in a comment block at the end. " "You still may have to edit some parts manually."), timeout=10) else: msg = err.decode('utf8').replace('\n', '<br>') info(_( "The document has been processed with convert-ly, but " "remained unchanged. This is the message given by " "convert-ly: %s") % "<br><br>%s" % msg, timeout=10) except OSError, e: error(_("Could not start convert-ly: %s") % e)