#! py -3 # Requires Python 3.5 # Phase1: # (0) Do first/last lines before formatting to width # (1) Combine output and error (if present) files # (2) Format all output to width limit # (3) Add closing '*/' from pathlib import Path import textwrap import pprint def fill_to_width(text): result = "" for line in text.splitlines(): result += textwrap.fill(line, width=59) + "\n" return result.strip() def adjust_lines(text): text = text.replace("\0", "NUL") lines = text.splitlines() slug = lines[0] if "(First and Last " in slug: num_of_lines = int(slug.split()[5]) adjusted = lines[:num_of_lines + 1] +\ ["...________...________...________...________..."] +\ lines[-num_of_lines:] return "\n".join(adjusted) elif "(First " in slug: num_of_lines = int(slug.split()[3]) adjusted = lines[:num_of_lines + 1] +\ [" ..."] return "\n".join(adjusted) else: return text if __name__ == '__main__': for outfile in Path(".").rglob("*.out"): out_text = adjust_lines(outfile.read_text()) phase_1 = outfile.with_suffix(".p1") with phase_1.open('w') as phs1: phs1.write(fill_to_width(out_text) + "\n") errfile = outfile.with_suffix(".err") if errfile.exists(): phs1.write("___[ Error Output ]___\n") phs1.write(fill_to_width(errfile.read_text()) + "\n") phs1.write("*/\n")