# Path: pyDEA/main.py
# def main(filename, output_format='xlsx', output_dir='', sheet_name_usr=''):
#     ''' Main function to run DEA models from terminal.
# 
#         Args:
#             filename (str): path to file with parameters.
#             output_format (str, optional): file format of solution file.
#                 This value is used
#                 only if OUTPUT_FILE in parameters is empty or set to auto.
#                 Defaults to xlsx.
#             output_dir (str, optional): directory where solution must
#                 be written.
#                 If it is not given, solution will be written to current folder.
#                 This value is used
#                 only if OUTPUT_FILE in parameters is empty or set to auto.
#             sheet_name_usr (str, optional): name of the sheet in xlsx-file with
#                 input data from which data will be read. If input data file is
#                 in csv format,
#                 this value is ignored.
# 
#     '''
#     print('Params file', filename, 'output_format', output_format,
#           'output_dir', output_dir, 'sheet_name_usr', sheet_name_usr)
#     
#     logger = get_logger()
#     logger.info('Params file "%s", output format "%s", output directory "%s", sheet name "%s".',
#                 filename, output_format, output_dir, sheet_name_usr)
# 
#     params = parse_parameters_from_file(filename)
#     params.print_all_parameters()
#     run_method = RunMethodTerminal(params, sheet_name_usr, output_format,
#                                    output_dir)
#     run_method.run(params)
#     clean_up_pickled_files()
#     logger.info('pyDEA exited.')
# 
# Path: pyDEA/core/data_processing/parameters.py
# def parse_parameters_from_file(filename):
#     ''' Reads parameters from a given file. If file does not exist, the
#         exception will be thrown. Returns Parameters object with
#         all parameters. Does not validate parameters.
#         Throws an exception if there are repeating
#         parameters (all parameters must be unique).
#         All parameters must follow convention: <param_name> {param_value}.
#         Supports one line comments:
#         <param_name> {param_value} # this parameter is important.
#         If some parameters are missing, their value will be set to empty
#         string.
# 
#         Args:
#             filename (str): path to file with parameters. Only text files
#                 are accepted, but actual file extension does not matter.
# 
#         Returns:
#             Parameters: parameter object with parsed parameters.
# 
#         Raises:
#             FileNotFound: if given file does not exist.
#             ValueError: if file does not contain any valid parameters.
#     '''
#     pattern = ''.join(['<(', VALID_PARAM_NAME, r')>\s*{(',
#                        VALID_PARAM_VALUE, ')}'])
#     params = Parameters()
#     # will raise IOError exception if fileName
#     # does not exists, 'rU' - r - read mode,
#     # U - Universal for encoding
#     nb_parsed_params = 0
#     with open(filename, 'rU') as file_with_params:
#         for line in file_with_params:
#             line = extract_comment(line)
#             matched_params = re.findall(pattern, line)
#             for match in matched_params:
#                 if match[0] in VALID_PARAM_NAMES:
#                     params.update_parameter(match[0], match[1])
#                     nb_parsed_params += 1
#     if nb_parsed_params == 0:
#         raise ValueError('File {filename} does not contain any valid'
#                          ' parameter values'.format(filename=filename))
#     return params
# 
# Path: pyDEA/core/utils/dea_utils.py
# def auto_name_if_needed(params, output_format, new_output_dir=''):
#     ''' Creates an automatic name for solution file based on current
#         parameter values, if OUTPUT_FILE is empty or set to auto.
# 
#         Args:
#             params (Parameters): parameters
#             output_format (str): output format of solution file that
#                 should be used. Allowed values: xlsx, csv
#             new_output_dir (str, optional): directory where solution must be
#                 stored. It should be specified if it is different from current
#                 folder. Defaults to empty string.
# 
#         Returns:
#             str: automatic name if OUTPUT_FILE is empty or set to auto in
#                 parameters.
# 
#         Raises:
#             ValueError: if output_format is not 'xlsx' or 'csv'.
#     '''
#     output_name = params.get_parameter_value('OUTPUT_FILE')
#     if output_name.lower() == 'auto' or output_name.strip() == '':
#         input_file_name = params.get_parameter_value('DATA_FILE')
#         input_base_name = os.path.basename(input_file_name)
#         input_base_name, ext_tmp = os.path.splitext(input_base_name)
#         ext = output_format
#         if ext not in ['xlsx', 'csv']:
#             raise ValueError('{0} is not supported output format'.format(ext))
#         output_name = os.path.join(new_output_dir,
#                                    input_base_name + '_result.' + ext)
#         output_name = change_to_unique_name_if_needed(output_name)
#     return output_name


import os
import shutil
from pyDEA.main import main
from pyDEA.core.data_processing.parameters import parse_parameters_from_file
from pyDEA.core.utils.dea_utils import auto_name_if_needed



def test_main_correct_params():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)

# Path: pyDEA/main.py
# def main(filename, output_format='xlsx', output_dir='', sheet_name_usr=''):
#     ''' Main function to run DEA models from terminal.
# 
#         Args:
#             filename (str): path to file with parameters.
#             output_format (str, optional): file format of solution file.
#                 This value is used
#                 only if OUTPUT_FILE in parameters is empty or set to auto.
#                 Defaults to xlsx.
#             output_dir (str, optional): directory where solution must
#                 be written.
#                 If it is not given, solution will be written to current folder.
#                 This value is used
#                 only if OUTPUT_FILE in parameters is empty or set to auto.
#             sheet_name_usr (str, optional): name of the sheet in xlsx-file with
#                 input data from which data will be read. If input data file is
#                 in csv format,
#                 this value is ignored.
# 
#     '''
#     print('Params file', filename, 'output_format', output_format,
#           'output_dir', output_dir, 'sheet_name_usr', sheet_name_usr)
#     
#     logger = get_logger()
#     logger.info('Params file "%s", output format "%s", output directory "%s", sheet name "%s".',
#                 filename, output_format, output_dir, sheet_name_usr)
# 
#     params = parse_parameters_from_file(filename)
#     params.print_all_parameters()
#     run_method = RunMethodTerminal(params, sheet_name_usr, output_format,
#                                    output_dir)
#     run_method.run(params)
#     clean_up_pickled_files()
#     logger.info('pyDEA exited.')
# 
# Path: pyDEA/core/data_processing/parameters.py
# def parse_parameters_from_file(filename):
#     ''' Reads parameters from a given file. If file does not exist, the
#         exception will be thrown. Returns Parameters object with
#         all parameters. Does not validate parameters.
#         Throws an exception if there are repeating
#         parameters (all parameters must be unique).
#         All parameters must follow convention: <param_name> {param_value}.
#         Supports one line comments:
#         <param_name> {param_value} # this parameter is important.
#         If some parameters are missing, their value will be set to empty
#         string.
# 
#         Args:
#             filename (str): path to file with parameters. Only text files
#                 are accepted, but actual file extension does not matter.
# 
#         Returns:
#             Parameters: parameter object with parsed parameters.
# 
#         Raises:
#             FileNotFound: if given file does not exist.
#             ValueError: if file does not contain any valid parameters.
#     '''
#     pattern = ''.join(['<(', VALID_PARAM_NAME, r')>\s*{(',
#                        VALID_PARAM_VALUE, ')}'])
#     params = Parameters()
#     # will raise IOError exception if fileName
#     # does not exists, 'rU' - r - read mode,
#     # U - Universal for encoding
#     nb_parsed_params = 0
#     with open(filename, 'rU') as file_with_params:
#         for line in file_with_params:
#             line = extract_comment(line)
#             matched_params = re.findall(pattern, line)
#             for match in matched_params:
#                 if match[0] in VALID_PARAM_NAMES:
#                     params.update_parameter(match[0], match[1])
#                     nb_parsed_params += 1
#     if nb_parsed_params == 0:
#         raise ValueError('File {filename} does not contain any valid'
#                          ' parameter values'.format(filename=filename))
#     return params
# 
# Path: pyDEA/core/utils/dea_utils.py
# def auto_name_if_needed(params, output_format, new_output_dir=''):
#     ''' Creates an automatic name for solution file based on current
#         parameter values, if OUTPUT_FILE is empty or set to auto.
# 
#         Args:
#             params (Parameters): parameters
#             output_format (str): output format of solution file that
#                 should be used. Allowed values: xlsx, csv
#             new_output_dir (str, optional): directory where solution must be
#                 stored. It should be specified if it is different from current
#                 folder. Defaults to empty string.
# 
#         Returns:
#             str: automatic name if OUTPUT_FILE is empty or set to auto in
#                 parameters.
# 
#         Raises:
#             ValueError: if output_format is not 'xlsx' or 'csv'.
#     '''
#     output_name = params.get_parameter_value('OUTPUT_FILE')
#     if output_name.lower() == 'auto' or output_name.strip() == '':
#         input_file_name = params.get_parameter_value('DATA_FILE')
#         input_base_name = os.path.basename(input_file_name)
#         input_base_name, ext_tmp = os.path.splitext(input_base_name)
#         ext = output_format
#         if ext not in ['xlsx', 'csv']:
#             raise ValueError('{0} is not supported output format'.format(ext))
#         output_name = os.path.join(new_output_dir,
#                                    input_base_name + '_result.' + ext)
#         output_name = change_to_unique_name_if_needed(output_name)
#     return output_name


# Path: tests/test_main.py
import os
import shutil
from pyDEA.main import main
from pyDEA.core.data_processing.parameters import parse_parameters_from_file
from pyDEA.core.utils.dea_utils import auto_name_if_needed




def test_main_correct_params():
    filename = 'tests/params_new_format.txt'
    params = parse_parameters_from_file(filename)

    auto_name = auto_name_if_needed(params, 'xlsx')