Handle SHGetFolder failing to return home directory on windows

This commit is contained in:
Kovid Goyal 2015-11-04 21:38:00 +05:30
parent 7459e67a2a
commit 2d71675fe1
2 changed files with 10 additions and 5 deletions

View File

@ -127,7 +127,10 @@ def get_library_path(gui_runner):
if library_path is None: # Need to migrate to new database layout if library_path is None: # Need to migrate to new database layout
base = os.path.expanduser('~') base = os.path.expanduser('~')
if iswindows: if iswindows:
base = winutil.special_folder_path(winutil.CSIDL_PERSONAL) try:
base = winutil.special_folder_path(winutil.CSIDL_PERSONAL)
except ValueError:
base = None
if not base or not os.path.exists(base): if not base or not os.path.exists(base):
from PyQt5.Qt import QDir from PyQt5.Qt import QDir
base = unicode(QDir.homePath()).replace('/', os.sep) base = unicode(QDir.homePath()).replace('/', os.sep)

View File

@ -12,7 +12,7 @@ from contextlib import closing
from PyQt5.Qt import (QWizard, QWizardPage, QPixmap, Qt, QAbstractListModel, from PyQt5.Qt import (QWizard, QWizardPage, QPixmap, Qt, QAbstractListModel,
QItemSelectionModel, QObject, QTimer, pyqtSignal, QItemSelection) QItemSelectionModel, QObject, QTimer, pyqtSignal, QItemSelection, QDir)
from calibre import __appname__, patheq from calibre import __appname__, patheq
from calibre.library.move import MoveLibrary from calibre.library.move import MoveLibrary
from calibre.constants import (filesystem_encoding, iswindows, plugins, from calibre.constants import (filesystem_encoding, iswindows, plugins,
@ -805,7 +805,10 @@ class LibraryPage(QWizardPage, LibraryUI):
fname = _('Calibre Library') fname = _('Calibre Library')
base = os.path.expanduser(u'~') base = os.path.expanduser(u'~')
if iswindows: if iswindows:
x = winutil.special_folder_path(winutil.CSIDL_PERSONAL) try:
x = winutil.special_folder_path(winutil.CSIDL_PERSONAL)
except ValueError:
x = QDir.homePath().replace('/', os.sep)
if x and os.access(x, os.W_OK): if x and os.access(x, os.W_OK):
base = x base = x
@ -837,8 +840,7 @@ class LibraryPage(QWizardPage, LibraryUI):
newloc = unicode(self.location.text()) newloc = unicode(self.location.text())
try: try:
dln = self.default_library_name dln = self.default_library_name
if (dln and os.path.exists(dln) and not os.listdir(dln) and newloc if (dln and os.path.exists(dln) and not os.listdir(dln) and newloc != dln):
!= dln):
os.rmdir(dln) os.rmdir(dln)
except: except:
pass pass