#! /usr/bin/python
# $Header: https://oberon.oapd.inaf.it/shins/branches/gigan-virgin_system/templates/sequencer.py 1885 2019-06-10 08:08:14Z laud $

import argparse
import sys
#import tpl
import SHINS_SW_TEST

#from seq.templateLoader import TemplateLoader
from util.shinsLogger import logger
from util import variables as uvars
from util import functions as ufuns
from util import exception 

# # cp /home/shinsmgr/shins/src/scripts/templates/tester.py /opt/tan/lib/python/site-packages/shins/templates/
# cp /home/shinsmgr/shins/src/scripts/templates/tester.py /opt/tan/bin/shins
# scp /home/shinsmgr/shins/src/scripts/templates/tester.py shinsmgr@sharknirws.oapd.inaf.it:/opt/tan/bin/shins

def parser():
    """Parser for the sequencer arguments."""
    
    parser = argparse.ArgumentParser(description='''Tester for the SHARK-NIR instrument. It executes pure cummands
        in the form of python files''')

    obname = ['-t', '--test']
    kwobname = {'type':str, 'help':'python TEST script file name'}

    parser.add_argument(*obname, **kwobname)

    obname = ['-a', '--targs']
    kwobname = {'type':str, 'help':'argument passed to python script'}
    parser.add_argument(*obname, **kwobname)

    obname = ['-a1', '--targ1']
    kwobname = {'type':str, 'help':'argument1 passed to python script'}
    parser.add_argument(*obname, **kwobname)
    obname = ['-a2', '--targ2']
    kwobname = {'type':str, 'help':'argument2 passed to python script'}
    parser.add_argument(*obname, **kwobname)
    obname = ['-a3', '--targ3']
    kwobname = {'type':str, 'help':'argument3 passed to python script'}
    parser.add_argument(*obname, **kwobname)

    args = parser.parse_args()
    
    return(args)


def execute(test_file, targs, targ1, targ2, targ3):
    """Execute the python file."""
    logger.info(__name__)

    moduleToUse = getattr(SHINS_SW_TEST, test_file)
    methodToCall = getattr(moduleToUse, test_file)
    status = methodToCall(det_s=None, ins_s=None, tel_s=None, rtc_s=None, test_s=targs, test_s1=targ1, test_s2=targ2, test_s3=targ3)

    status = status[0] if type(status) == tuple else status
       
    if status == uvars.EXIT_FAILURE:
        errMsg = "Test {} failed execution".format(test_file)
        logger.error(errMsg)
 #           raise exception.TemplateException(errMsg)
    else:
        logger.info("Test {:s} execution success".format(test_file))

    return(status)


def main(args):
    logger.info('{}: begin'.format(__name__))

    status = execute(args.test, args.targs, args.targ1, args.targ2, args.targ3)

    logger.info('{}: end'.format(__name__))

    sys.exit(status)


if __name__ == "__main__":
    args = parser()
    main(args)
