1
0
Fork 0
pytexbook/generate_ly.py

68 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
import os
def generate_ly(title=None, scores=None, copy=False, root=None):
"""Generate a new lilypond file containing all entries from scores, if copy is False, they will be 'linked'"""
# Find title
if title is not None:
title = "\\header {\n\ttitle = \""+ title + "\"\n}\n"
else:
title = ""
# Content of scores
scorestext = ""
for item in scores:
if item.startswith('std'):
continue
# item is always relative
if root is not None:
item = os.path.join(root, item)
if not os.path.exists(item):
continue
if copy is True:
with open(item, 'r') as f:
for line in f:
if line.startswith('\\include'):
incline = line.replace('\\include', '').strip('"\' ')
if os.path.isabs(incline): #already absolute
incline = os.path.join(os.path.abspath(os.path.dirname(item)), incline)
line = "\\include \""+incline+"\""
scorestext += line.replace('\r', '')+'\n'
else:
scorestext += '\\include \"' + os.path.join( os.path.abspath( os.curdir ) ,item) + '\"\n'
## Output
if scorestext == "":
return False
return """\\version \"2.19.65\"
"""+ title + """
\\paper {
#(define page-breaking ly:minimal-breaking)
}
%% Scores
""" + scorestext
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser(__file__)
parser.add_argument('-c', dest='copy', action='store_true', default=False, help="Copy files instead of just linkingi")
parser.add_argument('-t', dest='title', default=None, help="Title for the set")
parser.add_argument('scores', nargs='+')
args = parser.parse_args()
print(generate_ly(args.title, args.scores, args.copy))