#!/usr/bin/env python2

import argparse
import os
import logging
from slurmy import JobHandler, test_mode, Slurm
from slurmy.tools.utils import _get_prompt, list_sessions, load, load_path, load_latest
from slurmy.tools.defs import Status, Type, Theme
import slurmy.tools.options as _ops

_log = logging.getLogger('slurmy')


if __name__ == '__main__':
    _parser = argparse.ArgumentParser(description = 'Slurmy interactive')
    _parser.add_argument('-p', '--path', dest = 'path', type = str, default = None, help = 'Path to the base folder of an existing JobHandler session. Directly loads the JobHandler as "jh".')
    _parser.add_argument('-c', '--config', dest = 'config', type = str, default = None, help = 'Path to a job configuration file.')
    _parser.add_argument('-t', dest = 'test_mode', action = 'store_true', default = False, help = 'Switch to start in test/local mode.')
    _parser.add_argument('--debug', dest = 'debug', action = 'store_true', default = False, help = 'Run in debugging mode.')
    _args = _parser.parse_args()

    ## Switching to interactive mode
    _ops.Main.interactive_mode = True
    ## Modes to start slurmy in
    if _args.test_mode: test_mode()
    if _args.debug: _log.setLevel(level = logging.DEBUG)
    ## Load config file if one was provided
    if _args.config:
        with open(_args.config, 'r') as in_file:
            exec(in_file.read())
    ## Load jobhandler according to provided path
    elif _args.path:
        jh = load_path(_args.path)
    ## Check if we have sessions in bookkeeping and load the latest one
    else:
        jh = load_latest()
    ## Load interactive session
    _get_prompt()()
