# Little script to change the working directory if the module is executed from this file.

if __name__ == "__main__" :
    from os import chdir
    from os import getcwd
    from os.path import abspath
    from os.path import dirname
    from sys import path
    chdir(dirname(abspath(__file__)))
    path.append(getcwd())

# This file is for the Command-Line Interface (CLI).

import utils.functions as functions

from argparse import ArgumentParser
from LazyFrame import LazyFrame
from utils.functions import *
from utils.utils import list_functions

print("----- Command-line interface for AIS-Operations -----\n")
parser = ArgumentParser()

print("** Loading functions list")
functions_list = list_functions(functions)
print("** Done\n")

parser.add_argument("--target",
    help = "string(s) of path(s), file(s) or regexp(s) where to execute the function",
    nargs = '+', type = str, required = True)
parser.add_argument("--function", help = ", ".join(functions_list), type = str, required = True)

# Get the args from the parser.
args = parser.parse_args()

# Create a new LazyFrame object from target.
target = LazyFrame(args.target)

# If the given function doesn't contains a '(', that means we don't have parameters. Just add the
# target. Else if we have a '(' that means we have a function with parameters. Add them after the
# target then.

if '(' not in args.function:
    target = eval(args.function + "(target)")
else:
    function = (args.function).split('(', 1)[0]
    parameters = (args.function).split('(', 1)[1]
    target = eval(function + "(target, " + parameters)

target.save()
print("Saved in resources/csv")