Commits

Olav Andreas Lindekleiv committed f41e386

Handle line breaks in row data appropriately.

  • Participants
  • Parent commits 1c18adf

Comments (0)

Files changed (1)

File rptparser.py

 import re
 
 column_pattern = re.compile('([A-Za-z]+)')
+affected_pattern = re.compile('\(([0-9]+) row\(s\) affected\)', re.MULTILINE)
 
 
 class RPTParser(object):
             # Get column lengths based on '---- ---':
             lengths = f.readline().split(' ')
             for i in range(column_count):
-                length = len(lengths[i]) + 1
-                column_lengths.append(length)
+                length = len(lengths[i])
+                column_lengths.append(length + 1)
+
+            columns_sum = sum(column_lengths)
 
             # Loop over rows:
+            row_start = f.tell()
             while True:
-                line = f.readline()
+                # Seek back to where we want the row data starts, as line breaks
+                # is not a good indicator (they can occur in text)
+                f.seek(row_start)
+                line = f.read(columns_sum + 1)
                 if not line:
                     break
 
                 if len(line.strip()) == 0:
                     break
 
+                # Skip "(X row(s) affected)":
+                if affected_pattern.search(line):
+                    break
+
+                try:
+                    row_end = line.rindex('\n')  # Last line break should work
+                    line = line[0:row_end]
+                except ValueError:
+                    break
+                row_start += len(line)
+
                 # Loop over columns and get slice of row equal to column value's length:
                 char = 0
                 row = []