fixed bug #804747; clean ups

Fixes: https://launchpad.net/bugs/804747
This commit is contained in:
Andreas Preikschat 2011-08-12 15:11:30 +02:00
parent ccaa1bdf92
commit 0924bfc246
6 changed files with 79 additions and 112 deletions

View File

@ -247,7 +247,7 @@ class EventReceiver(QtCore.QObject):
``msg`` ``msg``
Defaults to *None*. The message to send with the event. Defaults to *None*. The message to send with the event.
""" """
log.debug(u'Event %s passed with payload %s' % (event, msg)) #log.debug(u'Event %s passed with payload %s' % (event, msg))
self.emit(QtCore.SIGNAL(event), msg) self.emit(QtCore.SIGNAL(event), msg)

View File

@ -28,6 +28,7 @@
import logging import logging
import os import os
import sys import sys
import shutil
from tempfile import gettempdir from tempfile import gettempdir
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
@ -721,11 +722,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
plugin.firstTime() plugin.firstTime()
Receiver.send_message(u'openlp_process_events') Receiver.send_message(u'openlp_process_events')
temp_dir = os.path.join(unicode(gettempdir()), u'openlp') temp_dir = os.path.join(unicode(gettempdir()), u'openlp')
if not os.path.exists(temp_dir): shutil.rmtree(temp_dir, True)
return
for filename in os.listdir(temp_dir):
delete_file(os.path.join(temp_dir, filename))
os.removedirs(temp_dir)
def onFirstTimeWizardClicked(self): def onFirstTimeWizardClicked(self):
""" """

View File

@ -29,6 +29,7 @@ The bible import functions for OpenLP
import logging import logging
import os import os
import shutil import shutil
from tempfile import gettempdir
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
@ -124,7 +125,7 @@ class BibleUpgradeForm(OpenLPWizard):
self.preWizard() self.preWizard()
self.performWizard() self.performWizard()
self.postWizard() self.postWizard()
elif self.page(pageId) == self.selectPage and self.maxBibles == 0: elif self.page(pageId) == self.selectPage and not self.files:
self.next() self.next()
def onBackupBrowseButtonClicked(self): def onBackupBrowseButtonClicked(self):
@ -425,9 +426,13 @@ class BibleUpgradeForm(OpenLPWizard):
return False return False
return True return True
elif self.currentPage() == self.selectPage: elif self.currentPage() == self.selectPage:
temp_dir = os.path.join(gettempdir(), u'openlp')
check_directory_exists(temp_dir)
for number, filename in enumerate(self.files): for number, filename in enumerate(self.files):
if not self.checkBox[number].checkState() == QtCore.Qt.Checked: if not self.checkBox[number].checkState() == QtCore.Qt.Checked:
continue continue
# Move bibles to temp dir.
shutil.move(os.path.join(self.path, filename[0]), temp_dir)
version_name = unicode(self.versionNameEdit[number].text()) version_name = unicode(self.versionNameEdit[number].text())
if not version_name: if not version_name:
critical_error_message_box(UiStrings().EmptyField, critical_error_message_box(UiStrings().EmptyField,
@ -435,37 +440,6 @@ class BibleUpgradeForm(OpenLPWizard):
'You need to specify a version name for your Bible.')) 'You need to specify a version name for your Bible.'))
self.versionNameEdit[number].setFocus() self.versionNameEdit[number].setFocus()
return False return False
elif self.manager.exists(version_name):
critical_error_message_box(
translate('BiblesPlugin.UpgradeWizardForm',
'Bible Exists'),
translate('BiblesPlugin.UpgradeWizardForm',
'This Bible already exists. Please upgrade '
'a different Bible, delete the existing one or '
'uncheck.'))
self.versionNameEdit[number].setFocus()
return False
elif os.path.exists(os.path.join(self.path, clean_filename(
version_name))) and version_name == filename[1]:
newfilename = u'old_database_%s' % filename[0]
if not os.path.exists(os.path.join(self.path,
newfilename)):
os.rename(os.path.join(self.path, filename[0]),
os.path.join(self.path, newfilename))
self.files[number] = [newfilename, filename[1]]
continue
else:
critical_error_message_box(
translate('BiblesPlugin.UpgradeWizardForm',
'Bible Exists'),
translate('BiblesPlugin.UpgradeWizardForm',
'This Bible already exists. Please upgrade '
'a different Bible, delete the existing one or '
'uncheck.'))
self.verticalWidget[number].show()
self.formWidget[number].show()
self.versionNameEdit[number].setFocus()
return False
elif os.path.exists(os.path.join(self.path, elif os.path.exists(os.path.join(self.path,
clean_filename(version_name))): clean_filename(version_name))):
critical_error_message_box( critical_error_message_box(
@ -495,7 +469,6 @@ class BibleUpgradeForm(OpenLPWizard):
self.files = self.manager.old_bible_databases self.files = self.manager.old_bible_databases
self.addScrollArea() self.addScrollArea()
self.retranslateUi() self.retranslateUi()
self.maxBibles = len(self.files)
for number, filename in enumerate(self.files): for number, filename in enumerate(self.files):
self.checkBox[number].setCheckState(QtCore.Qt.Checked) self.checkBox[number].setCheckState(QtCore.Qt.Checked)
oldname = filename[1] oldname = filename[1]
@ -516,9 +489,8 @@ class BibleUpgradeForm(OpenLPWizard):
Prepare the UI for the upgrade. Prepare the UI for the upgrade.
""" """
OpenLPWizard.preWizard(self) OpenLPWizard.preWizard(self)
self.progressLabel.setText(translate( self.progressLabel.setText(
'BiblesPlugin.UpgradeWizardForm', translate('BiblesPlugin.UpgradeWizardForm', 'Starting upgrade...'))
'Starting upgrade...'))
Receiver.send_message(u'openlp_process_events') Receiver.send_message(u'openlp_process_events')
def performWizard(self): def performWizard(self):
@ -527,46 +499,47 @@ class BibleUpgradeForm(OpenLPWizard):
""" """
self.include_webbible = False self.include_webbible = False
proxy_server = None proxy_server = None
if self.maxBibles == 0: if not self.files:
self.progressLabel.setText( self.progressLabel.setText(
translate('BiblesPlugin.UpgradeWizardForm', 'There are no ' translate('BiblesPlugin.UpgradeWizardForm', 'There are no '
'Bibles that need to be upgraded.')) 'Bibles that need to be upgraded.'))
self.progressBar.hide() self.progressBar.hide()
return return
self.maxBibles = 0 max_bibles = 0
for number, file in enumerate(self.files): for number, file in enumerate(self.files):
if self.checkBox[number].checkState() == QtCore.Qt.Checked: if self.checkBox[number].checkState() == QtCore.Qt.Checked:
self.maxBibles += 1 max_bibles += 1
number = 0 temp_dir = os.path.join(gettempdir(), u'openlp')
for biblenumber, filename in enumerate(self.files): for number, filename in enumerate(self.files):
if self.stop_import_flag: if self.stop_import_flag:
bible_failed = True bible_failed = True
break break
bible_failed = False bible_failed = False
self.success[biblenumber] = False self.success[number] = False
if not self.checkBox[biblenumber].checkState() == QtCore.Qt.Checked: if not self.checkBox[number].checkState() == QtCore.Qt.Checked:
continue continue
self.progressBar.reset() self.progressBar.reset()
oldbible = OldBibleDB(self.mediaItem, path=self.path, oldbible = OldBibleDB(self.mediaItem, path=temp_dir,
file=filename[0]) file=filename[0])
name = filename[1] name = filename[1]
if name is None: if name is None:
print u'no Name!!!'
# FIXME: ?
delete_file(os.path.join(self.path, filename[0])) delete_file(os.path.join(self.path, filename[0]))
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nFailed')) % 'Upgrading Bible %s of %s: "%s"\nFailed')) %
(number + 1, self.maxBibles, name), (number + 1, max_bibles, name),
self.progressBar.maximum() - self.progressBar.value()) self.progressBar.maximum() - self.progressBar.value())
number += 1
continue continue
self.progressLabel.setText(unicode(translate( self.progressLabel.setText(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nUpgrading ...')) % 'Upgrading Bible %s of %s: "%s"\nUpgrading ...')) %
(number + 1, self.maxBibles, name)) (number + 1, max_bibles, name))
if os.path.exists(os.path.join(self.path, filename[0])): if os.path.exists(os.path.join(self.path, filename[0])):
name = unicode(self.versionNameEdit[biblenumber].text()) name = unicode(self.versionNameEdit[number].text())
self.newbibles[number] = BibleDB(self.mediaItem, path=self.path, self.newbibles[number] = BibleDB(self.mediaItem, path=self.path,
name=name) name=name, file=filename[0])
self.newbibles[number].register(self.plugin.upgrade_wizard) self.newbibles[number].register(self.plugin.upgrade_wizard)
metadata = oldbible.get_metadata() metadata = oldbible.get_metadata()
webbible = False webbible = False
@ -595,7 +568,7 @@ class BibleUpgradeForm(OpenLPWizard):
u'name: "%s" failed' % ( u'name: "%s" failed' % (
meta_data[u'download source'], meta_data[u'download source'],
meta_data[u'download name'])) meta_data[u'download name']))
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
del self.newbibles[number] del self.newbibles[number]
critical_error_message_box( critical_error_message_box(
translate('BiblesPlugin.UpgradeWizardForm', translate('BiblesPlugin.UpgradeWizardForm',
@ -606,9 +579,8 @@ class BibleUpgradeForm(OpenLPWizard):
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nFailed')) % 'Upgrading Bible %s of %s: "%s"\nFailed')) %
(number + 1, self.maxBibles, name), (number + 1, max_bibles, name),
self.progressBar.maximum() - self.progressBar.value()) self.progressBar.maximum() - self.progressBar.value())
number += 1
continue continue
bible = BiblesResourcesDB.get_webbible( bible = BiblesResourcesDB.get_webbible(
meta_data[u'download name'], meta_data[u'download name'],
@ -621,14 +593,13 @@ class BibleUpgradeForm(OpenLPWizard):
language_id = self.newbibles[number].get_language(name) language_id = self.newbibles[number].get_language(name)
if not language_id: if not language_id:
log.warn(u'Upgrading from "%s" failed' % filename[0]) log.warn(u'Upgrading from "%s" failed' % filename[0])
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
del self.newbibles[number] del self.newbibles[number]
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nFailed')) % 'Upgrading Bible %s of %s: "%s"\nFailed')) %
(number + 1, self.maxBibles, name), (number + 1, max_bibles, name),
self.progressBar.maximum() - self.progressBar.value()) self.progressBar.maximum() - self.progressBar.value())
number += 1
continue continue
self.progressBar.setMaximum(len(books)) self.progressBar.setMaximum(len(books))
for book in books: for book in books:
@ -639,7 +610,7 @@ class BibleUpgradeForm(OpenLPWizard):
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\n' 'Upgrading Bible %s of %s: "%s"\n'
'Upgrading %s ...')) % 'Upgrading %s ...')) %
(number + 1, self.maxBibles, name, book)) (number + 1, max_bibles, name, book))
book_ref_id = self.newbibles[number].\ book_ref_id = self.newbibles[number].\
get_book_ref_id_by_name(book, len(books), language_id) get_book_ref_id_by_name(book, len(books), language_id)
if not book_ref_id: if not book_ref_id:
@ -647,7 +618,7 @@ class BibleUpgradeForm(OpenLPWizard):
u'name: "%s" aborted by user' % ( u'name: "%s" aborted by user' % (
meta_data[u'download source'], meta_data[u'download source'],
meta_data[u'download name'])) meta_data[u'download name']))
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
del self.newbibles[number] del self.newbibles[number]
bible_failed = True bible_failed = True
break break
@ -678,14 +649,13 @@ class BibleUpgradeForm(OpenLPWizard):
language_id = self.newbibles[number].get_language(name) language_id = self.newbibles[number].get_language(name)
if not language_id: if not language_id:
log.warn(u'Upgrading books from "%s" failed' % name) log.warn(u'Upgrading books from "%s" failed' % name)
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
del self.newbibles[number] del self.newbibles[number]
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nFailed')) % 'Upgrading Bible %s of %s: "%s"\nFailed')) %
(number + 1, self.maxBibles, name), (number + 1, max_bibles, name),
self.progressBar.maximum() - self.progressBar.value()) self.progressBar.maximum() - self.progressBar.value())
number += 1
continue continue
books = oldbible.get_books() books = oldbible.get_books()
self.progressBar.setMaximum(len(books)) self.progressBar.setMaximum(len(books))
@ -697,14 +667,14 @@ class BibleUpgradeForm(OpenLPWizard):
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\n' 'Upgrading Bible %s of %s: "%s"\n'
'Upgrading %s ...')) % 'Upgrading %s ...')) %
(number + 1, self.maxBibles, name, book[u'name'])) (number + 1, max_bibles, name, book[u'name']))
book_ref_id = self.newbibles[number].\ book_ref_id = self.newbibles[number].\
get_book_ref_id_by_name(book[u'name'], len(books), get_book_ref_id_by_name(book[u'name'], len(books),
language_id) language_id)
if not book_ref_id: if not book_ref_id:
log.warn(u'Upgrading books from %s " '\ log.warn(u'Upgrading books from %s " '\
'failed - aborted by user' % name) 'failed - aborted by user' % name)
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
del self.newbibles[number] del self.newbibles[number]
bible_failed = True bible_failed = True
break break
@ -729,21 +699,21 @@ class BibleUpgradeForm(OpenLPWizard):
if not bible_failed: if not bible_failed:
self.newbibles[number].create_meta(u'Version', name) self.newbibles[number].create_meta(u'Version', name)
oldbible.close_connection() oldbible.close_connection()
delete_file(os.path.join(self.path, filename[0])) delete_file(os.path.join(temp_dir, filename[0]))
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\n' 'Upgrading Bible %s of %s: "%s"\n'
'Complete')) % 'Complete')) %
(number + 1, self.maxBibles, name)) (number + 1, max_bibles, name))
self.success[biblenumber] = True self.success[number] = True
else: else:
self.incrementProgressBar(unicode(translate( self.incrementProgressBar(unicode(translate(
'BiblesPlugin.UpgradeWizardForm', 'BiblesPlugin.UpgradeWizardForm',
'Upgrading Bible %s of %s: "%s"\nFailed')) % 'Upgrading Bible %s of %s: "%s"\nFailed')) %
(number + 1, self.maxBibles, name), (number + 1, max_bibles, name),
self.progressBar.maximum() - self.progressBar.value()) self.progressBar.maximum() - self.progressBar.value())
delete_database(self.path, clean_filename(name)) delete_database(temp_dir, clean_filename(name))
number += 1 shutil.rmtree(temp_dir, True)
def postWizard(self): def postWizard(self):
""" """
@ -752,7 +722,7 @@ class BibleUpgradeForm(OpenLPWizard):
successful_import = 0 successful_import = 0
failed_import = 0 failed_import = 0
for number, filename in enumerate(self.files): for number, filename in enumerate(self.files):
if number in self.success and self.success[number] == True: if self.success.has_key(number) and self.success[number]:
successful_import += 1 successful_import += 1
elif self.checkBox[number].checkState() == QtCore.Qt.Checked: elif self.checkBox[number].checkState() == QtCore.Qt.Checked:
failed_import += 1 failed_import += 1
@ -776,7 +746,6 @@ class BibleUpgradeForm(OpenLPWizard):
'Bible(s): %s successful%s')) % (successful_import, 'Bible(s): %s successful%s')) % (successful_import,
failed_import_text)) failed_import_text))
else: else:
self.progressLabel.setText( self.progressLabel.setText(translate(
translate('BiblesPlugin.UpgradeWizardForm', 'Upgrade ' 'BiblesPlugin.UpgradeWizardForm', 'Upgrade failed.'))
'failed.'))
OpenLPWizard.postWizard(self) OpenLPWizard.postWizard(self)

View File

@ -169,6 +169,7 @@ class BibleDB(QtCore.QObject, Manager):
self.wizard = None self.wizard = None
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import) QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import)
print kwargs, (self.file, self.path)
def stop_import(self): def stop_import(self):
""" """

View File

@ -153,7 +153,7 @@ class BibleManager(object):
if name is None: if name is None:
delete_file(os.path.join(self.path, filename)) delete_file(os.path.join(self.path, filename))
continue continue
# Find old database versions # Find old database versions.
if bible.is_old_database(): if bible.is_old_database():
self.old_bible_databases.append([filename, name]) self.old_bible_databases.append([filename, name])
bible.session.close() bible.session.close()

View File

@ -612,7 +612,7 @@ class BibleMediaItem(MediaManagerItem):
if restore: if restore:
old_text = unicode(combo.currentText()) old_text = unicode(combo.currentText())
combo.clear() combo.clear()
combo.addItems([unicode(i) for i in range(range_from, range_to + 1)]) combo.addItems(map(unicode, range(range_from, range_to + 1)))
if restore and combo.findText(old_text) != -1: if restore and combo.findText(old_text) != -1:
combo.setCurrentIndex(combo.findText(old_text)) combo.setCurrentIndex(combo.findText(old_text))