Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: ValueError: invalid literal for int() with base 10: '/media/SAMSUNG/media/series'
### INFO
**Python Version**: `2.7.9 (default, Aug 13 2016, 17:56:53) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.29-10-osmc-armv7l-with-debian-8.9`
**Locale**: `UTF-8`
**Branch**: [optimized](../tree/optimized)
**Database**: `44.8`
**Commit**: pymedusa/Medusa@0f24093e8000bd10f2c1ce99327570aaf3046fc9
**Link to Log**: https://gist.github.com/4d50ef1a0248108156abd54bc0febbd8
### ERROR
<pre>
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/<a href="../blob/0f24093e8000bd10f2c1ce99327570aaf3046fc9/medusa/server/api/v1/core.py#L126">medusa/server/api/v1/core.py</a>", line 126, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "/home/osmc/Medusa/<a href="../blob/0f24093e8000bd10f2c1ce99327570aaf3046fc9/medusa/server/api/v1/core.py#L196">medusa/server/api/v1/core.py</a>", line 196, in call_dispatcher
    cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
  File "/home/osmc/Medusa/<a href="../blob/0f24093e8000bd10f2c1ce99327570aaf3046fc9/medusa/server/api/v1/core.py#L1530">medusa/server/api/v1/core.py</a>", line 1530, in run
    return _responds(RESULT_SUCCESS, _get_root_dirs())
  File "/home/osmc/Medusa/<a href="../blob/0f24093e8000bd10f2c1ce99327570aaf3046fc9/medusa/server/api/v1/core.py#L555">medusa/server/api/v1/core.py</a>", 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'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Discussion:
@p0psicles 
This is weird as my config is like this:
root_dirs = 0, /media/SAMSUNG/media/series
0, is translated to a list: [0]
Seems the API changed my root dirs:

2017-11-13 19:54:23 INFO     MAIN :: [4c3cff8] Starting Medusa [optimized] using '/home/osmc/Medusa/config.ini'
```
Traceback (most recent call last):
  File "start.py", line 7, in <module>
    main()
  File "/home/osmc/Medusa/medusa/__main__.py", line 2109, in main
    application.start(sys.argv[1:])
  File "/home/osmc/Medusa/medusa/__main__.py", line 347, in start
    self.web_server = AppWebServer(self.web_options)
  File "/home/osmc/Medusa/medusa/server/core.py", line 95, in __init__
    self.video_root = root_dirs[int(root_dirs[0]) + 1]
```
I had to change my config.ini back to : `root_dirs = 0,/media/SAMSUNG/media/series,`

API (using third party app) changed to `root_dirs = /media/SAMSUNG/media/series,` causing medusa to not boot
without the 0 that indicates the default root dir
The getrootdirs API call works and doesn't change the rootdir.  The addrootdir call fails though, but it does not change the config.ini.

Resolving patch:
diff --git a/medusa/server/api/v1/core.py b/medusa/server/api/v1/core.py
--- a/medusa/server/api/v1/core.py
+++ b/medusa/server/api/v1/core.py
@@ -547,31 +547,30 @@ def _map_quality(show_obj):
 
 
 def _get_root_dirs():
-    if app.ROOT_DIRS == '':
+    if not app.ROOT_DIRS:
         return {}
 
     root_dir = {}
-    root_dirs = app.ROOT_DIRS
     default_index = int(app.ROOT_DIRS[0])
+    root_dir['default_index'] = default_index
+    # 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(x)
+        for x in app.ROOT_DIRS[1:]
+    ]
 
-    root_dir['default_index'] = int(app.ROOT_DIRS[0])
-    # remove default_index value from list (this fixes the offset)
-    root_dirs.pop(0)
-
-    if len(root_dirs) < default_index:
+    try:
+        default_dir = root_dirs[default_index]
+    except IndexError:
         return {}
 
-    # clean up the list - replace %xx escapes by their single-character equivalent
-    root_dirs = [unquote_plus(x) for x in root_dirs]
-
-    default_dir = root_dirs[default_index]
-
     dir_list = []
     for root_dir in root_dirs:
         valid = 1
         try:
             os.listdir(root_dir)
-        except Exception:
+        except OSError:
             valid = 0
         default = 0
         if root_dir is default_dir:
@@ -1335,7 +1334,7 @@ def run(self):
         """ Add a new root (parent) directory to Medusa """
 
         self.location = unquote_plus(self.location)
-        location_matched = 0
+        location_matched = False
         index = 0
 
         # disallow adding/setting an invalid dir
@@ -1344,31 +1343,35 @@ def run(self):
 
         root_dirs = []
 
-        if app.ROOT_DIRS == '':
-            self.default = 1
+        if not app.ROOT_DIRS:
+            self.default = True
         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:
+            # 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 location_matched == 0:
-            if self.default == 1:
+        if not location_matched:
+            if self.default:
                 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 = [
+            unquote_plus(directory)
+            for directory in root_dirs
+        ]
+        # reinsert index value in the list
         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')
 
@@ -1445,36 +1448,41 @@ def __init__(self, args, kwargs):
 
     def run(self):
         """ Delete a root (parent) directory from Medusa """
-        if app.ROOT_DIRS == '':
+        if not 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
+        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)
 
-        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)
+        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)
 
-        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')
+        return _responds(result, _get_root_dirs(), msg=msg)
 
 
 class CMD_GetDefaults(ApiCall):


Test patch: