# Buggy code
def run(self):
    """Delete a root (parent) directory from Medusa"""
    if app.ROOT_DIRS == "":
        return _responds(
            RESULT_FAILURE, _get_root_dirs(), msg="No root directories detected"
        )

    new_index = 0
    root_dirs_new = []
    root_dirs = app.ROOT_DIRS
    index = int(root_dirs[0])
    root_dirs.pop(0)
    # clean up the list - replace %xx escapes by their single-character equivalent
    root_dirs = [unquote_plus(x) for x in root_dirs]
    old_root_dir = root_dirs[index]
    for curRootDir in root_dirs:
        if not curRootDir == self.location:
            root_dirs_new.append(curRootDir)
        else:
            new_index = 0

    for curIndex, curNewRootDir in enumerate(root_dirs_new):
        if curNewRootDir is old_root_dir:
            new_index = curIndex
            break

    root_dirs_new = [unquote_plus(x) for x in root_dirs_new]
    if root_dirs_new:
        root_dirs_new.insert(0, new_index)
    root_dirs_new = "|".join(text_type(x) for x in root_dirs_new)

    app.ROOT_DIRS = root_dirs_new
    # what if the root dir was not found?
    return _responds(RESULT_SUCCESS, _get_root_dirs(), msg="Root directory deleted")


# Traceback (ValueError)
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'cmd': 'sb.getrootdirs'}
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: sb.getrootdirs: {}
2017-11-12 22:05:04 ERROR    TORNADO :: [0f24093] API :: "invalid literal for int() with base 10: '/media/SAMSUNG/media/series'"
Traceback (most recent call last):
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 126, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 196, in call_dispatcher
    cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 1530, in run
    return _responds(RESULT_SUCCESS, _get_root_dirs())
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 555, in _get_root_dirs
    default_index = int(app.ROOT_DIRS[0])
ValueError: invalid literal for int() with base 10: '/media/SAMSUNG/media/series'
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'sort': 'name', 'cmd': 'shows'}
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: shows: {'sort': 'name'}
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'cmd': 'sb.getrootdirs'}
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: sb.getrootdirs: {}
2017-11-12 22:05:05 ERROR    TORNADO :: [0f24093] API :: "invalid literal for int() with base 10: '/media/SAMSUNG/media/series'"
Traceback (most recent call last):
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 126, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 196, in call_dispatcher
    cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 1530, in run
    return _responds(RESULT_SUCCESS, _get_root_dirs())
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 555, in _get_root_dirs
    default_index = int(app.ROOT_DIRS[0])
ValueError: invalid literal for int() with base 10: '/media/SAMSUNG/media/series'

# Fixed code
def run(self):
    """Delete a root (parent) directory from Medusa"""
    if not app.ROOT_DIRS:
        return _responds(
            RESULT_FAILURE, _get_root_dirs(), msg="No root directories detected"
        )

    index = int(app.ROOT_DIRS[0])
    # clean up the list: replace %xx escapes with single-character equivalent
    # and remove default_index value from list (this fixes the offset)
    root_dirs = [unquote_plus(directory) for directory in app.ROOT_DIRS[1:]]
    default_dir = root_dirs[index]
    location = unquote_plus(self.location)
    try:
        root_dirs.remove(location)
    except ValueError:
        result = RESULT_FAILURE
        msg = "Location not in root directories"
        return _responds(result, _get_root_dirs(), msg=msg)

    try:
        index = root_dirs.index(default_dir)
    except ValueError:
        if default_dir == location:
            result = RESULT_DENIED
            msg = "Default directory cannot be deleted; Please set a new default directory."
        else:
            result = RESULT_ERROR
            msg = "Default directory not found"
    else:
        root_dirs.insert(0, index)
        app.ROOT_DIRS = root_dirs
        result = RESULT_SUCCESS
        msg = "Root directory {0} deleted".format(location)

    return _responds(result, _get_root_dirs(), msg=msg)