changeset 64:811151d3ae73

Improve first line extractor. Only output at most two lines. I've met .abc files where every line is a continuation.
author Jim Hague <jim.hague@acm.org>
date Mon, 05 Mar 2012 11:05:57 +0000
parents f73a0193beb8
children 6ced110a62aa
files abcfirstline.py
diffstat 1 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/abcfirstline.py	Mon Mar 05 11:03:15 2012 +0000
+++ b/abcfirstline.py	Mon Mar 05 11:05:57 2012 +0000
@@ -7,17 +7,28 @@
 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 COLON?
+        # If so, output only ones we need.
         start = line[:2]
-        if len(start) > 1 and start[0] != "|" and start[1] == ":":
+        if len(start) > 1 and start[1] == ":" and start[0].isalpha():
             if start[0] in ["M", "K", "L"]:
                 print line
-        elif start[0] != "%":
+        # Output line. If it is a continuation, output at most one
+        # continuation.
+        else:
             print line
-            if line[-1] != "\\":
+            if continued or line[-1] != "\\":
                 break
+            else:
+                continued = True
 
 if len(sys.argv) > 1:
     for arg in sys.argv[1:]: