# Buggy code
def run(self):
    """Add a new root (parent) directory to Medusa"""

    self.location = unquote_plus(self.location)
    location_matched = 0
    index = 0

    # disallow adding/setting an invalid dir
    if not os.path.isdir(self.location):
        return _responds(RESULT_FAILURE, msg="Location is invalid")

    root_dirs = []

    if app.ROOT_DIRS == "":
        self.default = 1
    else:
        root_dirs = app.ROOT_DIRS
        index = int(app.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]
        for x in root_dirs:
            if x == self.location:
                location_matched = 1
                if self.default == 1:
                    index = root_dirs.index(self.location)
                break

    if location_matched == 0:
        if self.default == 1:
            root_dirs.insert(0, self.location)
        else:
            root_dirs.append(self.location)

    root_dirs_new = [unquote_plus(x) for x in root_dirs]
    root_dirs_new.insert(0, index)
    root_dirs_new = "|".join(text_type(x) for x in root_dirs_new)

    app.ROOT_DIRS = root_dirs_new
    return _responds(RESULT_SUCCESS, _get_root_dirs(), msg="Root directories updated")


# 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):
    """Add a new root (parent) directory to Medusa"""

    self.location = unquote_plus(self.location)
    location_matched = False
    index = 0

    # disallow adding/setting an invalid dir
    if not os.path.isdir(self.location):
        return _responds(RESULT_FAILURE, msg="Location is invalid")

    root_dirs = []

    if not app.ROOT_DIRS:
        self.default = True
    else:
        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:]]
        for directory in root_dirs:
            if directory == self.location:
                location_matched = True
                if self.default:
                    index = root_dirs.index(self.location)
                break

    if not location_matched:
        if self.default:
            root_dirs.insert(0, self.location)
        else:
            root_dirs.append(self.location)

    root_dirs_new = [unquote_plus(directory) for directory in root_dirs]
    # reinsert index value in the list
    root_dirs_new.insert(0, index)
    app.ROOT_DIRS = root_dirs_new
    return _responds(RESULT_SUCCESS, _get_root_dirs(), msg="Root directories updated")