
from replay_processing.parseargs import parse_params
from replay_processing.sc2files import parse_replays


def main():
    # Get params and environment variables
    params = parse_params()
    logger = params["logger"]
    # Parse replays using params
    logger.info("Parsing Replay Files")

    output_path = params.get("output_path")
    output_file = None
    try:
        if output_path is not None:
            output_file = open(output_path, "w+")
        ret = parse_replays(params["replay_dir"],
                            params["threads"],
                            max_replays=params.get("max_replays"),
                            logger=logger,
                            outfile=output_file)
    finally:
        if output_file is not None:
            output_file.close

    match_data, count, errors, error_replays, match_stats, korean_replays = ret

    logger.info("Match Stats: {0}".format({k: v for k, v in match_stats.items()}))
    logger.info("Total Replays: {0}".format(count))
    logger.info("Error Replays: {0}".format(errors))
    logger.info("Error Percent: {0}".format(str(errors / float(count))))
    logger.info("Error Replays: \n  {0}".format(error_replays))
    logger.info("Korean replays: \n  {0}".format(korean_replays))


if __name__ == "__main__":
    main()
