annotate abcfirstline.py @ 727:772402f5f8ea

Provide full titles for next and previous tunes in web pages.
author Jim Hague <jim.hague@acm.org>
date Wed, 11 Oct 2017 17:45:27 +0100
parents 1b79867b4f35
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
1 #!/usr/bin/env python3
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
2 #
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
3 # Write out a modified version of a .abc file with just the data
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
4 # to print the first line of the music only.
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
5 #
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
6
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
7 import sys
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
8
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
9 def process(inf):
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
10 continued = False
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
11 print("X:1")
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
12 for line in inf:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
13 line = line.strip()
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
14 # If it is empty or starts "%", ignore it.
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
15 if len(line) == 0 or line[0] == "%":
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
16 continue
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
17
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
18 # Is it a header line? I.e. does it start LETTER (or +) COLON?
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
19 # If so, output only ones we need.
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
20 start = line[:2]
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
21 if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'):
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
22 if start[0] in ["M", "K", "L"]:
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
23 print(line)
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
24 # Output line. If it is a continuation, output at most one
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
25 # continuation.
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
26 else:
587
1b79867b4f35 Convert remaining Python to Python3 and add +: support.
Jim Hague <jim.hague@acm.org>
parents: 64
diff changeset
27 print(line)
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
28 if continued or line[-1] != "\\":
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
29 break
64
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
30 else:
811151d3ae73 Improve first line extractor. Only output at most two lines.
Jim Hague <jim.hague@acm.org>
parents: 54
diff changeset
31 continued = True
50
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
32
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
33 if len(sys.argv) > 1:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
34 for arg in sys.argv[1:]:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
35 try:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
36 inf = open(arg, "r")
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
37 process(inf)
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
38 finally:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
39 inf.close()
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
40 else:
e666306c5ab1 Add list of tune first lines.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
41 process(sys.stdin)