import configparser
import os
import pkg_resources as pkgrs

from shutil import copyfile

# functions used by latools to read configurations
def read_configuration(config='DEFAULT'):
    """
    Read LAtools configuration file, and return parameters as dict.
    """
    # read configuration file
    conf = configparser.ConfigParser()
    conf.read(pkgrs.resource_filename('latools', 'latools.cfg'))
    # if 'DEFAULT', check which is the default configuration
    if config == 'DEFAULT':
        config = conf['DEFAULT']['config']

    # return the chosen configuration    
    return dict(conf[config])

# convenience functions for configuring LAtools
def locate():
    """
    Prints and returns the location of the latools.cfg file.
    """
    loc = pkgrs.resource_filename('latools', 'latools.cfg')
    print(loc)
    return loc

def print():
    """
    Prints all currently defined configurations.
    """
    # read configuration file
    conf = configparser.ConfigParser()
    conf.read(pkgrs.resource_filename('latools', 'latools.cfg'))

    default = conf['DEFAULT']['config']

    pstr = '\nCurrently defined LAtools configurations:\n\n'
    for s in conf.sections():
        if s == default:
            pstr += s + ' [DEFAULT]\n'
        elif s == 'REPRODUCE':
            pstr += s + ' [DO NOT ALTER]\n'
        else:
            pstr += s + '\n'

        for k, v in conf[s].items():
            if k != 'config':
                if v[:9] == 'resources':
                    v = pkgrs.resource_filename('latools', v)
                pstr += '   ' + k + ': ' + v + '\n'
        pstr += '\n'

    print(pstr)
    return

def copy_SRM_file(destination=None, config='DEFAULT'):
    """
    Creates a copy of the default SRM table at the specified location.

    Parameters
    ----------
    destination : str
        The save location for the SRM file. If no location specified, 
        saves it as 'LAtools_[config]_SRMTable.csv' in the current working 
        directory.
    config : str
        It's possible to set up different configurations with different
        SRM files. This specifies the name of the configuration that you 
        want to copy the SRM file from. If not specified, the 'DEFAULT'
        configuration is used.
    """
    # find SRM file from configuration    
    conf = read_configuration(config)
    src = pkgrs.resource_filename('latools', conf['srmfile'])

    # work out destination path (if not given)
    if destination is None:
        destination = './LAtools_' + conf['config'] + '_SRMTable.csv'

    copyfile(src, destination)

    print(src + ' copied to ' + destination)
    return

def new(config_name, srmfile=None, dataformat=None, base_on='DEFAULT', make_default=False):
    """
    Adds a new configuration to latools.cfg.

    Parameters
    ----------
    config_name : str
        The name of the new configuration. This should be descriptive
        (e.g. UC Davis Foram Group)
    srmfile : str (optional)
        The location of the srm file used for calibration.
    dataformat : str (optional)
        The location of the dataformat definition to use.
    base_on : str
        The name of the existing configuration to base the new one on.
        If either srm_file or dataformat are not specified, the new
        config will copy this information from the base_on config.
    make_default : bool
        Whether or not to make the new configuration the default
        for future analyses. Default = False.

    Returns
    -------
    None
    """

    base_config = read_configuration(base_on)

    # read config file
    config_file = pkgrs.resource_filename('latools', 'latools.cfg')
    cf = configparser.ConfigParser()
    cf.read(config_file)

    # if config doesn't already exist, create it.
    if config_name not in cf.sections():
        cf.add_section(config_name)
    # set parameter values
    if dataformat is None:
        dataformat = base_config['dataformat']
    cf.set(config_name, 'dataformat', dataformat)

    if srmfile is None:
        srmfile = base_config['srmfile']
    cf.set(config_name, 'srmfile', srmfile)

    # make the parameter set default, if requested
    if make_default:
        cf.set('DEFAULT', 'config', config_name)

    with open(config_file, 'w') as f:
        cf.write(f)

    return


@@ -839,16 +839,20 @@
 ef print
+_all
 ():%0A    
@@ -4295,20 +4295,1225 @@
 ite(f)%0A%0A    return%0A%0A
+def update(config, parameter, new_value):%0A    # read config file%0A    config_file = pkgrs.resource_filename('latools', 'latools.cfg')%0A    cf = configparser.ConfigParser()%0A    cf.read(config_file)%0A%0A    pstr = 'Are you sure you want to change the %7B:s%7D parameter of the %7B:s%7D configuration?%5Cn  It will be changed from:%5Cn    %7B:s%7D%5Cn  to:%5Cn    %7B:s%7D%5Cn%3E %5BN/y%5D: '%0A%0A    response = input(pstr.format(parameter, config, cf%5Bconfig%5D%5Bparameter%5D, new_value))%0A%0A    if response.lower() == 'y':%0A        cf.set(config, parameter, new_value)%0A        with open(config_file, 'w') as f:%0A            cf.write(f)%0A        print('Configuration updated!')%0A    else:%0A        print('Done nothing.')%0A%0A    return%0A%0Adef delete(config):%0A    # read config file%0A    config_file = pkgrs.resource_filename('latools', 'latools.cfg')%0A    cf = configparser.ConfigParser()%0A    cf.read(config_file)%0A%0A    pstr = 'Are you sure you want to delete the %7B:s%7D configuration?%5Cn%3E %5BN/y%5D: '%0A%0A    response = input(pstr.format(config))%0A%0A    if response.lower() == 'y':%0A        cf.remove_section(config)%0A        with open(config_file, 'w') as f:%0A            cf.write(f)%0A        print('Configuration deleted!')%0A    else:%0A        print('Done nothing.')%0A    %0A    return

update and delete functions