|
5 | 5 | # (2) If direct comparison of actual output with output stored in Java file fails: |
6 | 6 | # (3) Use chain of responsibility to successively try filters until one matches or all fail |
7 | 7 | from pathlib import Path |
| 8 | +import textwrap |
8 | 9 | import re |
9 | 10 |
|
| 11 | +def fill_to_width(text): |
| 12 | + result = "" |
| 13 | + for line in text.splitlines(): |
| 14 | + result += textwrap.fill(line, width=59) + "\n" |
| 15 | + return result.strip() |
| 16 | + |
| 17 | +def adjust_lines(text): |
| 18 | + text = text.replace("\0", "NUL") |
| 19 | + lines = text.splitlines() |
| 20 | + slug = lines[0] |
| 21 | + if "(First and Last " in slug: |
| 22 | + num_of_lines = int(slug.split()[5]) |
| 23 | + adjusted = lines[:num_of_lines + 1] +\ |
| 24 | + ["...________...________...________...________..."] +\ |
| 25 | + lines[-num_of_lines:] |
| 26 | + return "\n".join(adjusted) |
| 27 | + elif "(First " in slug: |
| 28 | + num_of_lines = int(slug.split()[3]) |
| 29 | + adjusted = lines[:num_of_lines + 1] +\ |
| 30 | + [" ..."] |
| 31 | + return "\n".join(adjusted) |
| 32 | + else: |
| 33 | + return text |
| 34 | + |
| 35 | +def phase1(): |
| 36 | + """ |
| 37 | + (0) Do first/last lines before formatting to width |
| 38 | + (1) Combine output and error (if present) files |
| 39 | + (2) Format all output to width limit |
| 40 | + (3) Add closing '*/' |
| 41 | + """ |
| 42 | + for outfile in Path(".").rglob("*.out"): |
| 43 | + out_text = adjust_lines(outfile.read_text()) |
| 44 | + phase_1 = outfile.with_suffix(".p1") |
| 45 | + with phase_1.open('w') as phs1: |
| 46 | + phs1.write(fill_to_width(out_text) + "\n") |
| 47 | + errfile = outfile.with_suffix(".err") |
| 48 | + if errfile.exists(): |
| 49 | + phs1.write("___[ Error Output ]___\n") |
| 50 | + phs1.write(fill_to_width(errfile.read_text()) + "\n") |
| 51 | + phs1.write("*/\n") |
10 | 52 |
|
11 | 53 | if __name__ == '__main__': |
| 54 | + phase1() |
12 | 55 | find_output = re.compile(r"/\* (Output:.*)\*/", re.DOTALL) # should space be \s+ ?? |
13 | 56 | for outfile in Path(".").rglob("*.p1"): |
14 | 57 | javafile = outfile.with_suffix(".java") |
|
0 commit comments