#!/usr/bin/env python
# Copyright (C) 2012-2013 Educational Testing Service

# This file is part of SciKit-Learn Laboratory.

# SciKit-Learn Laboratory is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.

# SciKit-Learn Laboratory is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with
# SciKit-Learn Laboratory.  If not, see <http://www.gnu.org/licenses/>.

'''
Little helper script to create a summary file out of a list of JSON results
files.

:author: Dan Blanchard (dblanchard@ets.org)
:date: September 2013
'''

from __future__ import print_function, unicode_literals

import argparse
import sys
from io import open

from skll.experiments import _write_summary_file
from skll.version import __version__


def main():
    '''
    Handles command line arguments and gets things started.
    '''
    # Get command line arguments
    parser = argparse.ArgumentParser(
        description="Creates an experiment summary TSV file from a list of JSON\
                     files generated by run_experiment.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        conflict_handler='resolve')
    parser.add_argument('summary_file',
                        help='TSV file to store summary of results.')
    parser.add_argument('json_file',
                        help='JSON results file generated by run_experiment.',
                        nargs='+')
    parser.add_argument('-a', '--ablation', action='store_true', default=False,
                        help='The results files are from an ablation run.')
    parser.add_argument('--version', action='version',
                        version='%(prog)s {0}'.format(__version__))
    args = parser.parse_args()

    file_mode = 'w' if sys.version_info >= (3, 0) else 'wb'
    with open(args.summary_file, file_mode) as output_file:
        _write_summary_file(args.json_file, output_file, ablation=args.ablation)


if __name__ == '__main__':
    main()
