diff --git a/src/calibre/srv/library_broker.py b/src/calibre/srv/library_broker.py index fe5c129045..def734a068 100644 --- a/src/calibre/srv/library_broker.py +++ b/src/calibre/srv/library_broker.py @@ -32,7 +32,12 @@ def samefile(a, b): def basename(path): while path and path[-1] in ('/' + os.sep): path = path[:-1] - return os.path.basename(path) + ans = os.path.basename(path) + if not ans: + # Can happen for a path like D:\ on windows + if len(path) == 2 and path[1] == ':': + ans = path[0] + return ans or 'Library' def init_library(library_path, is_default_library): @@ -52,7 +57,7 @@ def make_library_id_unique(library_id, existing): return library_id -def library_id_from_path(path, existing): +def library_id_from_path(path, existing=frozenset()): library_id = basename(path).replace(' ', '_') return make_library_id_unique(library_id, existing) diff --git a/src/calibre/srv/tests/routes.py b/src/calibre/srv/tests/routes.py index 9b7bdf9210..36070bd353 100644 --- a/src/calibre/srv/tests/routes.py +++ b/src/calibre/srv/tests/routes.py @@ -6,11 +6,22 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' +import os from calibre.srv.tests.base import BaseTest class TestRouter(BaseTest): + def test_library_id_construction(self): + from calibre.srv.library_broker import library_id_from_path + self.ae(library_id_from_path('as'), 'as') + self.ae(library_id_from_path('as/'), 'as') + self.ae(library_id_from_path('as////'), 'as') + self.ae(library_id_from_path('/as/'), 'as') + if os.sep == '\\': + self.ae(library_id_from_path('as/' + os.sep), 'as') + self.ae(library_id_from_path('X:' + os.sep), 'X') + def test_route_construction(self): ' Test route construction ' from calibre.srv.routes import Route, endpoint, RouteError