dottes/abcfirstline.py

43 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Write out a modified version of a .abc file with just the data
# to print the first line of the music only.
#
import argparse
import pathlib
import sys
def process(inf):
continued = False
print("X:1")
for line in inf:
line = line.strip()
# If it is empty or starts "%", ignore it.
if len(line) == 0 or line[0] == "%":
continue
# Is it a header line? I.e. does it start LETTER (or +) COLON?
# If so, output only ones we need.
start = line[:2]
if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'):
if start[0] in ["M", "K", "L"]:
print(line)
# Output line. If the line ends with a continuation, carry
# on outputting lines, otherwise stop.
else:
print(line)
if line[-1] != "\\":
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Write minimal version of ABC input file with just the first line of music.")
parser.add_argument('input', type=argparse.FileType('r'),
help='input ABC file')
args = parser.parse_args()
path = pathlib.Path(args.input.name)
with path.open() as f:
process(f)
sys.exit(0)