from calibre.gui2.main_window import option_parser as _option_parser
from calibre.utils.config import prefs, dynamic
from calibre.library.database2 import LibraryDatabase2
from calibre.library.sqlite import sqlite, DatabaseException
def option_parser():
parser = _option_parser('%prog [opts] [path_to_ebook]\n\nLaunch the main calibre Graphical User Interface and optionally add the ebook at\npath_to_ebook to the database.\n')
parser.add_option('--with-library', default = None, action = 'store', help = _('Use the library located at the specified path.'))
parser.add_option('--start-in-tray', default = False, action = 'store_true', help = _('Start minimized to system tray.'))
parser.add_option('-v', '--verbose', default = 0, action = 'count', help = _('Log debugging information to console'))
parser.add_option('--no-update-check', default = False, action = 'store_true', help = _('Do not check for updates'))
base = plugins['winutil'][0].special_folder_path(plugins['winutil'][0].CSIDL_PERSONAL)
if not base or not os.path.exists(base):
QDir = QDir
import PyQt4.Qt
base = unicode(QDir.homePath()).replace('/', os.sep)
candidate = choose_dir(None, 'choose calibre library', _('Choose a location for your calibre e-book library'), default_dir = base)
if not candidate:
candidate = os.path.join(base, 'Calibre Library')
library_path = os.path.abspath(candidate)
if not os.path.exists(library_path):
try:
os.makedirs(library_path)
error_dialog(parent, _('Failed to create library'), _('Failed to create calibre library at: %r.') % library_path, det_msg = traceback.format_exc(), show = True)
library_path = choose_dir(parent, 'choose calibre library', _('Choose a location for your new calibre e-book library'), default_dir = get_default_library_path())
return library_path
class DBRepair(QThread):
repair_done = pyqtSignal(object, object)
progress = pyqtSignal(object, object)
def __init__(self, library_path, parent, pd):
QThread.__init__(self, parent)
self.library_path = library_path
self.pd = pd
self.progress.connect(self._callback, type = Qt.QueuedConnection)
error_dialog(self.splash_screen, _('Repairing failed'), _('The database repair failed. Starting with a new empty library.'), det_msg = tb, show = True)
if db is None:
candidate = choose_dir(self.splash_screen, 'choose calibre library', _('Choose a location for your new calibre e-book library'), default_dir = get_default_library_path())
if not candidate:
self.initialization_failed()
try:
self.library_path = candidate
db = LibraryDatabase2(candidate)
error_dialog(self.splash_screen, _('Bad database location'), _('Bad database location %r. calibre will now quit.') % self.library_path, det_msg = traceback.format_exc(), show = True)
self.initialization_failed()
self.db = db
self.start_gui()
def initialize_db(self):
db = None
try:
db = LibraryDatabase2(self.library_path)
except (sqlite.Error, DatabaseException):
repair = question_dialog(self.splash_screen, _('Corrupted database'), _('Your calibre database appears to be corrupted. Do you want calibre to try and repair it automatically? If you say No, a new empty calibre library will be created.'), det_msg = traceback.format_exc())
if repair:
self.repair_pd = QProgressDialog(_('Repairing database. This can take a very long time for a large collection'), QString(), 0, 0)