2016-10-31 23:55:28 +00:00
|
|
|
#!/usr/bin/env python3
|
2012-03-04 20:16:48 +00:00
|
|
|
#
|
|
|
|
# Write out a modified version of a .abc file with just the data
|
|
|
|
# to print the first line of the music only.
|
|
|
|
#
|
|
|
|
|
2023-03-03 11:12:00 +00:00
|
|
|
import argparse
|
|
|
|
import pathlib
|
2012-03-04 20:16:48 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
def process(inf):
|
2012-03-05 11:05:57 +00:00
|
|
|
continued = False
|
2016-10-31 23:55:28 +00:00
|
|
|
print("X:1")
|
2012-03-04 20:16:48 +00:00
|
|
|
for line in inf:
|
|
|
|
line = line.strip()
|
2012-03-05 11:05:57 +00:00
|
|
|
# If it is empty or starts "%", ignore it.
|
|
|
|
if len(line) == 0 or line[0] == "%":
|
|
|
|
continue
|
|
|
|
|
2016-10-31 23:55:28 +00:00
|
|
|
# Is it a header line? I.e. does it start LETTER (or +) COLON?
|
2012-03-05 11:05:57 +00:00
|
|
|
# If so, output only ones we need.
|
2012-03-04 20:16:48 +00:00
|
|
|
start = line[:2]
|
2016-10-31 23:55:28 +00:00
|
|
|
if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'):
|
2012-03-04 20:16:48 +00:00
|
|
|
if start[0] in ["M", "K", "L"]:
|
2016-10-31 23:55:28 +00:00
|
|
|
print(line)
|
2023-03-03 11:12:00 +00:00
|
|
|
# Output line. If the line ends with a continuation, carry
|
|
|
|
# on outputting lines, otherwise stop.
|
2012-03-05 11:05:57 +00:00
|
|
|
else:
|
2016-10-31 23:55:28 +00:00
|
|
|
print(line)
|
2023-03-03 11:12:00 +00:00
|
|
|
if line[-1] != "\\":
|
2012-03-04 20:16:48 +00:00
|
|
|
break
|
|
|
|
|
2023-03-03 11:12:00 +00:00
|
|
|
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)
|