def enable_autocomplete_handler()

in modular_cli/modular_cli_autocomplete/complete_handler.py [0:0]


def enable_autocomplete_handler():
    interpreter, script = _get_interpreter_and_appropriate_script()
    from platform import python_version
    installed_python_link = 'python' + '.'.join(
        python_version().lower().split('.')[0:-1])
    try:
        import pathlib
        admin_home_path = pathlib.Path(__file__).parent.parent.resolve()
        if not os.path.exists(PROFILE_D_PATH):
            _LOG.info(f'Going to edit RC file')
            source_string = _add_str_to_rc_file(interpreter, script,
                                                admin_home_path,
                                                installed_python_link)
            return f'Autocomplete has been successfully installed and ' \
                   f'will start work after the current terminal session ' \
                   f'reload. If you want to manually activate ' \
                   f'autocomplete without reloading the terminal session, ' \
                   f'please run the following command \n {source_string}'
        # if admin instance installation
        _LOG.info(f'Going to copy autocomplete files to '
                  f'{PROFILE_D_PATH}')
        init_profile_script_path = os.path.join(admin_home_path,
                                                RELATIVE_PATH_TO_COMPLETE,
                                                script)
        python_script = os.path.join(admin_home_path,
                                     RELATIVE_PATH_TO_COMPLETE,
                                     COMPLETE_PROCESS_FILE)
        script = 'profile_' + script
        processed_profile_script_path = os.path.join(PROFILE_D_PATH, script)
        with open(init_profile_script_path, 'r+') as f:
            lines = f.readlines()
        script_was_found = False
        help_was_found = False
        with open(processed_profile_script_path, 'w') as f:
            for line in lines:
                if SCRIPT_PATH in line.strip(
                        "\n") and not script_was_found:
                    line = f'SCRIPT_PATH={python_script}\n'
                    script_was_found = True
                if HELP_FILE in line.strip(
                        "\n") and not help_was_found:
                    line = f'HELP_FILE=/home/$USER/modular_cli_help.txt'
                    help_was_found = True
                f.write(line)
        _LOG.info(f'Modular-CLI autocomplete has been '
                  f'successfully set up. Path to the "profile.d" file: '
                  f'{processed_profile_script_path}')
        return f'Modular-CLI autocomplete has been ' \
               f'successfully set up. Path to the "profile.d" file: ' \
               f'{processed_profile_script_path}'
    except AttributeError:
        _LOG.error(f'Autocomplete installation is not available')
        raise Exception(f'Autocomplete installation is not '
                        f'available')
    except Exception as e:
        _LOG.error(f'Something happen while setup autocomplete. Reason: {e}')
        raise Exception(f'Something happen while setup '
                        f'autocomplete. Reason: {e}')