#!python

from emr_launcher.launcher import EmrLauncher, render_template, get_modules
from argparse import ArgumentParser
import logging
import json
import inspect
import re

def launch(args):
    """
        launches a new cluster
    """
    dry_run_prefix = "[DRY RUN] " if args.dry_run else ""
    logging.basicConfig(format=dry_run_prefix + '%(asctime)-15s %(message)s', level=logging.INFO)
    launcher = EmrLauncher()
    launcher.run(
        json.loads(render_template(open(args.config).read())),
        job_id=args.job_id,
        name=args.name,
        dry_run=args.dry_run,
        distinct=args.distinct,
        replace_existing_cluster=args.replace_existing_cluster
    )

def list_template_functions(args):
    """
        lists the template function available
    """
    modules = get_modules()
    for (module_name, module) in modules:
        print(module_name)
        print('=' * len(module_name))
        for (func_name, member) in inspect.getmembers(module):
            if inspect.isfunction(member) and member.__module__ == module.__name__ and not func_name.startswith('_'):
                print(re.sub('^', ' '*2, '.'.join([module_name,func_name])))
                doc = inspect.getdoc(member)
                if doc:
                    print(re.sub('^', ' '*4, inspect.getdoc(member), flags=re.MULTILINE))
                print("")

def main():
    """
        entry-point for the script
    """
    parser = ArgumentParser(version="3.0.0")

    subparsers = parser.add_subparsers(title='sub-commands')
    launch_parser = subparsers.add_parser('launch', help='Runs job flows on EMR')
    launch_parser.add_argument('--name', type=str, default=None, help='Name of the cluster')
    launch_parser.add_argument(
        '--job-id',
        type=str,
        dest='job_id',
        default=None,
        help='EMR Job ID to add steps to, using this only takes steps from the passed config'
    )

    launch_parser.add_argument(
        '--dry-run',
        action='store_true',
        dest='dry_run',
        help='Does a dry run. Builds the config an dumps it to stdout, without kicking off an EMR job'
    )

    launch_parser.add_argument(
        '--distinct',
        action='store_true',
        dest='distinct',
        help='Will only launch the job if another cluster of the same name is *not* running. Otherwise, will throw an error'
    )

    launch_parser.add_argument(
        '--replace-existing-cluster',
        action='store_true',
        dest='replace_existing_cluster',
        help='Shuts down an existing cluster with the same name, if more than one cluster exists it will fail.'
    )

    launch_parser.add_argument('config', type=str, help='path to the config to be used')
    launch_parser.set_defaults(func=launch)

    list_funcs_parser = subparsers.add_parser('list-template-functions', help='list the available functions for use in templates')
    list_funcs_parser.set_defaults(func=list_template_functions)

    args = parser.parse_args()
    args.func(args)


if __name__ == '__main__':
    main()
