mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Windows: Fix the case of library names in copied calibre:// links sometimes incorrect. Fixes #1907159 [Casing in calibre:\\ links](https://bugs.launchpad.net/calibre/+bug/1907159)
This commit is contained in:
parent
79fba89f32
commit
b3cd2a15a0
@ -62,6 +62,18 @@ def library_id_from_path(path, existing=frozenset()):
|
|||||||
return make_library_id_unique(library_id, existing)
|
return make_library_id_unique(library_id, existing)
|
||||||
|
|
||||||
|
|
||||||
|
def correct_case_of_last_path_component(original_path):
|
||||||
|
prefix, basename = os.path.split(original_path)
|
||||||
|
q = basename.lower()
|
||||||
|
equals = tuple(x for x in os.listdir(prefix) if x.lower() == q)
|
||||||
|
if len(equals) > 1:
|
||||||
|
if basename not in equals:
|
||||||
|
basename = equals[0]
|
||||||
|
else:
|
||||||
|
basename = equals[0]
|
||||||
|
return os.path.join(prefix, basename)
|
||||||
|
|
||||||
|
|
||||||
class LibraryBroker(object):
|
class LibraryBroker(object):
|
||||||
|
|
||||||
def __init__(self, libraries):
|
def __init__(self, libraries):
|
||||||
@ -82,9 +94,10 @@ class LibraryBroker(object):
|
|||||||
seen.add(path)
|
seen.add(path)
|
||||||
if is_samefile or not LibraryDatabase.exists_at(path):
|
if is_samefile or not LibraryDatabase.exists_at(path):
|
||||||
continue
|
continue
|
||||||
library_id = library_id_from_path(original_path, self.lmap)
|
corrected_path = correct_case_of_last_path_component(original_path)
|
||||||
|
library_id = library_id_from_path(corrected_path, self.lmap)
|
||||||
self.lmap[library_id] = path
|
self.lmap[library_id] = path
|
||||||
self.library_name_map[library_id] = basename(original_path)
|
self.library_name_map[library_id] = basename(corrected_path)
|
||||||
self.original_path_map[path] = original_path
|
self.original_path_map[path] = original_path
|
||||||
self.loaded_dbs = {}
|
self.loaded_dbs = {}
|
||||||
self.category_caches, self.search_caches, self.tag_browser_caches = (
|
self.category_caches, self.search_caches, self.tag_browser_caches = (
|
||||||
@ -195,11 +208,11 @@ class GuiLibraryBroker(LibraryBroker):
|
|||||||
if library_path not in self.original_path_map:
|
if library_path not in self.original_path_map:
|
||||||
self.original_path_map[library_path] = original_library_path
|
self.original_path_map[library_path] = original_library_path
|
||||||
db = self.init_library(library_path, False)
|
db = self.init_library(library_path, False)
|
||||||
library_id = library_id_from_path(library_path, self.lmap)
|
corrected_path = correct_case_of_last_path_component(original_library_path)
|
||||||
|
library_id = library_id_from_path(corrected_path, self.lmap)
|
||||||
db.new_api.server_library_id = library_id
|
db.new_api.server_library_id = library_id
|
||||||
self.lmap[library_id] = library_path
|
self.lmap[library_id] = library_path
|
||||||
self.library_name_map[library_id] = basename(
|
self.library_name_map[library_id] = basename(corrected_path)
|
||||||
original_library_path)
|
|
||||||
self.loaded_dbs[library_id] = db
|
self.loaded_dbs[library_id] = db
|
||||||
return db
|
return db
|
||||||
|
|
||||||
@ -226,10 +239,10 @@ class GuiLibraryBroker(LibraryBroker):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# A new library
|
# A new library
|
||||||
library_id = self.gui_library_id = library_id_from_path(
|
corrected_path = correct_case_of_last_path_component(original_path)
|
||||||
newloc, self.lmap)
|
library_id = self.gui_library_id = library_id_from_path(corrected_path, self.lmap)
|
||||||
self.lmap[library_id] = newloc
|
self.lmap[library_id] = newloc
|
||||||
self.library_name_map[library_id] = basename(original_path)
|
self.library_name_map[library_id] = basename(corrected_path)
|
||||||
self.original_path_map[newloc] = original_path
|
self.original_path_map[newloc] = original_path
|
||||||
self.loaded_dbs[library_id] = db
|
self.loaded_dbs[library_id] = db
|
||||||
db.new_api.server_library_id = library_id
|
db.new_api.server_library_id = library_id
|
||||||
|
@ -8,12 +8,13 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
import os
|
import os
|
||||||
from calibre.srv.tests.base import BaseTest
|
from calibre.srv.tests.base import BaseTest
|
||||||
from polyglot.builtins import itervalues, filter
|
from polyglot.builtins import itervalues, filter
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
|
||||||
class TestRouter(BaseTest):
|
class TestRouter(BaseTest):
|
||||||
|
|
||||||
def test_library_id_construction(self):
|
def test_library_id_construction(self):
|
||||||
from calibre.srv.library_broker import library_id_from_path
|
from calibre.srv.library_broker import library_id_from_path, correct_case_of_last_path_component
|
||||||
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')
|
||||||
self.ae(library_id_from_path('as////'), 'as')
|
self.ae(library_id_from_path('as////'), 'as')
|
||||||
@ -21,6 +22,11 @@ class TestRouter(BaseTest):
|
|||||||
if os.sep == '\\':
|
if os.sep == '\\':
|
||||||
self.ae(library_id_from_path('as/' + os.sep), 'as')
|
self.ae(library_id_from_path('as/' + os.sep), 'as')
|
||||||
self.ae(library_id_from_path('X:' + os.sep), 'X')
|
self.ae(library_id_from_path('X:' + os.sep), 'X')
|
||||||
|
with TemporaryDirectory() as tdir:
|
||||||
|
path = os.path.join(tdir, 'Test')
|
||||||
|
os.mkdir(path)
|
||||||
|
self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'test')), path)
|
||||||
|
self.ae(correct_case_of_last_path_component(os.path.join(tdir, 'Test')), path)
|
||||||
|
|
||||||
def test_route_construction(self):
|
def test_route_construction(self):
|
||||||
' Test route construction '
|
' Test route construction '
|
||||||
|
Loading…
x
Reference in New Issue
Block a user