"""generate_test_file
Part of tern-to-markdown project.
Author: Lukasz Kostrzewa
License: MIT

This script generates light report file.
"""

import yaml
import logging
import re
from typing import Set
import argparse
import pickle


def setup_logger():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    sh = logging.StreamHandler()
    format = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
    sh.setFormatter(format)
    logger.addHandler(sh)
    return logger


def generate_light_output(source_file: str):
    with open(source_file, 'r') as fstream:
        _LOG.debug('Opening {}. This may take a while.'.format(source_file))
        try:
            data = yaml.safe_load(fstream)
        except yaml.YAMLError as exc:
            _LOG.critical(exc)
            return

    for layer in data['image']['layers']:
        layer.pop('files', None)

    with open(source_file + '.light', 'w') as fstream:
        try:
            yaml.dump(data, fstream)
        except yaml.YAMLError as exc:
            _LOG.critical(exc)
            return

def setup_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-i',
        '--input',
        help='Input file in YAML format with report generated by the Tern tool.',
        required=True)
    return parser


_LOG = setup_logger()

if __name__ == "__main__":
    args = setup_parser().parse_args()
    generate_light_output(args.input)
