def delete_link(index, path):
    path = os.path.join(path, LINK_FILE)
    new_lines = []
    with open(path) as f:
        for i, line in enumerate(f):
            if i == int(index):
                continue
            new_lines.append(line)

    write_links(path, new_lines)
    kodi.refresh_container()
def create_dir(path, dir_name=None):
    if dir_name is None:
        dir_name = kodi.get_keyboard('Enter Directory Name')
        if dir_name is None:
            return

    try:
        os.mkdir(os.path.join(path, dir_name))
    except OSError as e:
        kodi.notify(msg=e.strerror)
    kodi.refresh_container()
def delete_dir(path, dir_name):
    path = os.path.join(path, dir_name)
    try:
        os.rmdir(path)
    except OSError:
        delete = xbmcgui.Dialog().yesno('Directory Not Empty',
                                        'Delete it?',
                                        nolabel='No',
                                        yeslabel='Yes')
        if delete:
            shutil.rmtree(path)
            kodi.refresh_container()
def rename_dir(path, dir_name, new_name=None):
    if new_name is None:
        new_name = kodi.get_keyboard('Enter Directory Name', dir_name)
        if new_name is None:
            return

    old_path = os.path.join(path, dir_name)
    new_path = os.path.join(path, new_name)
    try:
        os.rename(old_path, new_path)
    except OSError as e:
        kodi.notify(msg=e.strerror)
    kodi.refresh_container()
def edit_link(index, path):
    path = os.path.join(path, LINK_FILE)
    new_lines = []
    with open(path) as f:
        for i, line in enumerate(f):
            if i == int(index):
                item = line.split('|')
                result = prompt_for_link(*item)
                if result:
                    line = '|'.join(result)

            new_lines.append(line)

    write_links(path, new_lines)
    kodi.refresh_container()
def add_link(link=None, name=None, refresh=True, path=None):
    if path is None:
        path = LINK_PATH
    if link is None:
        result = prompt_for_link()
    else:
        if name is None:
            result = (link, )
        else:
            result = (link, name)

    if result:
        if not os.path.exists(os.path.dirname(path)):
            os.mkdir(os.path.dirname(path))

        path = os.path.join(path, LINK_FILE)
        with open(path, 'a') as f:
            line = '|'.join(result)
            if not line.endswith('\n'):
                line += '\n'
            f.write(line)

        if refresh:
            kodi.refresh_container()