Utility function to automatically set up brightway2 to work correctly with lcopt.

    It requires a valid username and password to login to the ecoinvent website.

    These can be entered directly into the function using the keyword arguments `ei_username` and `ei_password` or entered interactively by using no arguments.
    
    `ecoinvent_version` needs to be a string representation of a valid ecoinvent database, at time of writing these are "3.01", "3.1", "3.2", "3.3", "3.4"

    `ecoinvent_system_model` needs to be one of "cutoff", "apos", "consequential"

    To overwrite an existing version, set overwrite=True

def lcopt_bw2_autosetup(ei_username=None, ei_password=None, write_config=None, ecoinvent_version='3.3', ecoinvent_system_model = "cutoff", overwrite=False):  

    """
    Utility function to automatically set up brightway2 to work correctly with lcopt.

    It requires a valid username and password to login to the ecoinvent website.

    These can be entered directly into the function using the keyword arguments `ei_username` and `ei_password` or entered interactively by using no arguments.
    
    `ecoinvent_version` needs to be a string representation of a valid ecoinvent database, at time of writing these are "3.01", "3.1", "3.2", "3.3", "3.4"

    `ecoinvent_system_model` needs to be one of "cutoff", "apos", "consequential"

    To overwrite an existing version, set overwrite=True
    """

    ei_name = "Ecoinvent{}_{}_{}".format(*ecoinvent_version.split('.'), ecoinvent_system_model)

    config = check_for_config()
    # If, for some reason, there's no config file, write the defaults
    if config is None:
        config = DEFAULT_CONFIG
        with open(storage.config_file, "w") as cfg:
            yaml.dump(config, cfg, default_flow_style=False)

    store_option = storage.project_type

    # Check if there's already a project set up that matches the current configuration
    
    if store_option == 'single':

        project_name = storage.single_project_name

        if bw2_project_exists(project_name):
            bw2.projects.set_current(project_name)
            if ei_name in bw2.databases and overwrite == False:
                #print ('{} is already set up'.format(ei_name))
                return True

    else: # default to 'unique'
        project_name = DEFAULT_PROJECT_STEM + ei_name

        if bw2_project_exists(project_name):
            if overwrite:                                           
                bw2.projects.delete_project(name=project_name, delete_dir=True)

    auto_ecoinvent = partial(eidl.get_ecoinvent,db_name=ei_name, auto_write=True, version=ecoinvent_version, system_model=ecoinvent_system_model)

    # check for a config file (lcopt_config.yml)

    if config is not None:
        if "ecoinvent" in config:
            if ei_username is None:
                ei_username = config['ecoinvent'].get('username')
            if ei_password is None:
                ei_password = config['ecoinvent'].get('password')
            write_config = False

    if ei_username is None:
        ei_username = input('ecoinvent username: ')
    if ei_password is None:
        ei_password = getpass.getpass('ecoinvent password: ')
    if write_config is None:
        write_config = input('store username and password on this computer? y/[n]') in ['y', 'Y', 'yes', 'YES', 'Yes']

    if write_config:

        config['ecoinvent'] = {
            'username': ei_username,
            'password': ei_password
        }
        with open(storage.config_file, "w") as cfg:
            yaml.dump(config, cfg, default_flow_style=False)

    # no need to keep running bw2setup - we can just copy a blank project which has been set up before

    if store_option == 'single':
        if bw2_project_exists(project_name):
            bw2.projects.set_current(project_name)
        else:
            
            if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT):
                bw2.projects.set_current(project_name)
                bw2.bw2setup()
            else:
                bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)
                bw2.create_core_migrations()
                bw2.projects.copy_project(project_name, switch=True)

    else:    #if store_option == 'unique':

        if not bw2_project_exists(DEFAULT_BIOSPHERE_PROJECT):
            lcopt_biosphere_setup()
        
        bw2.projects.set_current(DEFAULT_BIOSPHERE_PROJECT)
        bw2.create_core_migrations()
        bw2.projects.copy_project(project_name, switch=True)
            
    if ei_username is not None and ei_password is not None:
        auto_ecoinvent(username=ei_username, password=ei_password)
    else:
        auto_ecoinvent()

    write_search_index(project_name, ei_name, overwrite=overwrite)

    return True