view abcfirstline.py @ 738:69a7499817c8 build-default-284

On Nook and A5 prints, start a new page with each tune and centre vertically.. A5 mostly did, but ran into trouble with longer comments on a tune. This may give blank space after tunes where a long comment overflows onto another page, but I think for now it's better to have each tune at the top of the page. Or rather, and this is the other change, in the middle of the page. Centre the page content for A5 and Nook.
author Jim Hague <jim.hague@acm.org>
date Thu, 12 Oct 2017 14:50:51 +0100
parents 1b79867b4f35
children
line wrap: on
line source

#!/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 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 it is a continuation, output at most one
        # continuation.
        else:
            print(line)
            if continued or line[-1] != "\\":
                break
            else:
                continued = True

if len(sys.argv) > 1:
    for arg in sys.argv[1:]:
        try:
            inf = open(arg, "r")
            process(inf)
        finally:
            inf.close()
else:
    process(sys.stdin)