exc yaml

Process yaml file generated by exc utility.

https://github.com/a-services/jbang-catalog/blob/main/exc.java

Extract line numbers for exceptions and print then in chunks of chunk_length

import yaml
import argparse

parser = argparse.ArgumentParser(description="exc yaml")

parser.add_argument("log_path", help="Log path")

args = parser.parse_args()

log_path = args.log_path

chunk_length = 5

def read_yaml(file_path):
    with open(file_path, 'r') as file:
        # Load all documents from the YAML file
        yaml_content = yaml.safe_load(file)
    return [item['lno'] for item in yaml_content]

def extract_lines(log_path, line_numbers):
    lines_to_extract = set()
    # Include the three following lines for each line number
    for line in line_numbers:
        lines_to_extract.update(range(line, line + chunk_length))

    extracted_lines = {}
    with open(log_path, 'r') as file:
        for i, line in enumerate(file, 1):
            if i in lines_to_extract:
                extracted_lines[i] = line.rstrip()
    return extracted_lines

def write_output(extracted_lines, output_path):
    k1 = 0
    with open(output_path, 'w') as file:
        for k2 in sorted(extracted_lines.keys()):
            if k2 > k1 + 1:
                file.write("\n\n")
            k1 = k2

            file.write(f"{k2}: {extracted_lines[k2]}\n")


yaml_path = log_path + '.yml'

output_path = log_path + '.txt'

line_numbers = read_yaml(yaml_path)
extracted_lines = extract_lines(log_path, line_numbers)
write_output(extracted_lines, output_path)
print(f"Extraction complete. Check '{output_path}'.")