#!/usr/bin/env python3
"""
potluck_eval

Command-line potluck evaluation entry point; run this file in the shell
to evaluate a single submission or to generate a rubric file for a single
task. Run with '-h' or '--help' for a summary of command-line arguments.

Dependencies:

- `potluck` the main evaluation module.
- `jinja2` for report rendering via HTML templates.
- `pygments` for code highlighting in reports.

This script relies on a configuration file to find task meta-data, task
specifications, and the submission it will evaluate. By default it loads
`config.py` in the current directory. Command-line options can control
where the necessary information is looked up.

See `potluck.default_config` for a configuration file template; values
not specified in a custom config file will be pulled from that file.
"""

import sys


def create_argument_parser():
    """
    Creates an argument parser for handling command-line arguments.
    """
    import argparse

    parser = argparse.ArgumentParser(
        prog="potluck",
        description="Potluck: Evaluates code and behavior of Python programs."
    )

    parser.add_argument(
        '-t', "--task",
        required=True,
        help="ID string for task to evaluate"
    )
    parser.add_argument(
        "-u", "--user",
        help="who's submission are we evaluating?",
        default=None # okay if we're just generating rubrics
    )
    parser.add_argument(
        "--target",
        help="manually specify a file (or folder) to evaluate",
        default=None
    )
    parser.add_argument(
        "--log",
        help="where should we write our log file (default stdout)",
        default=None
    )
    parser.add_argument(
        "--rubric",
        help="generate blank rubric files and exit",
        action="store_true"
    )
    parser.add_argument(
        "--outfile",
        help="where should we write our report JSON file or rubric",
        default=None
    )
    parser.add_argument(
        "--conf",
        help="name of configuration module to load (without the .py)",
        default="potluck_config"
    )
    parser.add_argument(
        "--specs",
        help="custom specifications directory",
        default=None
    )
    parser.add_argument(
        "--clean",
        action='store_true',
        help="re-generate caches (e.g., generated images)"
    )
    parser.add_argument(
        "--import-from",
        help="use a specific directory for imports (including potluck)",
        default=None
    )

    return parser


def main(args):
    """
    Main entry point for running from the command line.
    """
    # Set up argument handling via argparse
    parser = create_argument_parser()
    opts = parser.parse_args()

    # Set up to import a specific potluck install...
    if opts.import_from:
        sys.path.insert(0, opts.import_from)

    # Actually import the potluck module after options are parsed to that
    # -h works even without it being on sys.path.
    import potluck

    # Load the specified config file
    config = potluck.load_configuration(opts.conf)

    # Common setup stuff
    potluck.setup(config, opts.specs)

    # Either generate a blank rubric or evaluate a submission
    if opts.rubric:
        potluck.launch_rubric_generation(
            config,
            opts.task,
            opts.log,
            opts.outfile
        )
    else:
        potluck.launch_evaluation(
            config,
            opts.task,
            opts.user,
            opts.log,
            opts.target,
            opts.outfile
        )


# If we're running this file, call main() using arguments from the
# command line
if __name__ == "__main__":
    main(sys.argv)
