From 7f88043177999e3538e610515961568a8c29e80b Mon Sep 17 00:00:00 2001 From: Jon Tibble Date: Fri, 18 Jun 2010 02:26:01 +0100 Subject: [PATCH] RFC: Cleanups --- openlp/core/lib/db.py | 72 +++++++++---------- openlp/migration/migratebibles.py | 5 +- openlp/migration/migratesongs.py | 16 +++-- .../plugins/bibles/forms/importwizardform.py | 4 +- openlp/plugins/bibles/lib/db.py | 1 + openlp/plugins/custom/customplugin.py | 6 +- openlp/plugins/custom/forms/editcustomform.py | 3 +- openlp/plugins/songs/songsplugin.py | 4 +- openlp/plugins/songusage/lib/__init__.py | 6 +- 9 files changed, 63 insertions(+), 54 deletions(-) diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 87200faee..19cd3eadf 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -26,6 +26,7 @@ The :mod:`db` module provides the core database functionality for OpenLP """ import logging +import os from PyQt4 import QtCore from sqlalchemy import create_engine, MetaData @@ -55,11 +56,34 @@ def init_db(url, auto_flush=True, auto_commit=False): autocommit=auto_commit, bind=engine)) return session, metadata +def delete_database(plugin_name, db_file_name=None): + """ + Remove a database file from the system. + + ``plugin_name`` + The name of the plugin to remove the database for + + ``db_file_name`` + The database file name. Defaults to None resulting in the + plugin_name being used. + """ + db_file_path = None + if db_file_name: + db_file_path = os.path.join( + AppLocation.get_section_data_path(plugin_name), db_file_name) + else: + db_file_path = os.path.join( + AppLocation.get_section_data_path(plugin_name), plugin_name) + try: + os.remove(db_file_path) + return True + except OSError: + return False + class BaseModel(object): """ BaseModel provides a base object with a set of generic functions """ - @classmethod def populate(cls, **kwargs): """ @@ -111,30 +135,6 @@ class Manager(object): settings.endGroup() self.session = init_schema(self.db_url) - def delete_database(self, plugin_name, db_file_name=None): - """ - Remove a database file from the system. - - ``plugin_name`` - The name of the plugin to remove the database for - - ``db_file_name`` - The database file name. Defaults to None resulting in the - plugin_name being used. - """ - db_file_path = None - if db_file_name: - db_file_path = os.path.join( - AppLocation.get_section_data_path(plugin_name), db_file_name) - else: - db_file_path = os.path.join( - AppLocation.get_section_data_path(plugin_name), plugin_name) - try: - os.remove(db_file_path) - return True - except OSError: - return False - def insert_object(self, object_instance): """ Save an object to the database @@ -151,20 +151,20 @@ class Manager(object): log.exception(u'Object save failed') return False - def get_object(self, object_class, id=None): + def get_object(self, object_class, key=None): """ Return the details of an object ``object_class`` The type of object to return - ``id`` - The unique reference for the class instance to return + ``key`` + The unique reference or primary key for the instance to return """ - if not id: + if not key: return object_class() else: - return self.session.query(object_class).get(id) + return self.session.query(object_class).get(key) def get_object_filtered(self, object_class, filter_string): """ @@ -204,20 +204,20 @@ class Manager(object): """ return self.session.query(object_class).filter(filter_string).all() - def delete_object(self, object_class, id): + def delete_object(self, object_class, key): """ Delete an object from the database ``object_class`` The type of object to delete - ``id`` - The unique reference for the class instance to be deleted + ``key`` + The unique reference or primary key for the instance to be deleted """ - if id != 0: - object = self.get_object(object_class, id) + if key != 0: + object_instance = self.get_object(object_class, key) try: - self.session.delete(object) + self.session.delete(object_instance) self.session.commit() return True except InvalidRequestError: diff --git a/openlp/migration/migratebibles.py b/openlp/migration/migratebibles.py index 411da0ce6..37a0df933 100644 --- a/openlp/migration/migratebibles.py +++ b/openlp/migration/migratebibles.py @@ -27,6 +27,7 @@ import os import sys import sqlite3 +from sqlalchemy import Column, MetaData, Table, types from sqlalchemy.exceptions import InvalidRequestError from sqlalchemy.orm import mapper @@ -34,7 +35,9 @@ from openlp.core.lib import SettingsManager from openlp.core.lib.db import BaseModel from openlp.core.utils import AppLocation from openlp.plugins.bibles.lib.db import BibleMeta, Book, Testament, Verse - + +metadata = MetaData() + class TBibleMeta(BaseModel): """ Bible Meta Data diff --git a/openlp/migration/migratesongs.py b/openlp/migration/migratesongs.py index 0ede63387..0a6912c78 100644 --- a/openlp/migration/migratesongs.py +++ b/openlp/migration/migratesongs.py @@ -27,14 +27,16 @@ import os import sys import sqlite3 -from sqlalchemy import create_engine +from sqlalchemy import Column, create_engine, MetaData, Table, types from sqlalchemy.exceptions import InvalidRequestError from sqlalchemy.orm import scoped_session, sessionmaker, mapper, relation from openlp.core.lib import SettingsManager from openlp.core.lib.db import BaseModel from openlp.core.utils import AppLocation -from openlp.plugins.songs.lib.db import songs_table, Song, Author, Topic, Book +from openlp.plugins.songs.lib.db import Author, Book, Song, Topic + +metadata = MetaData() def init_models(url): engine = create_engine(url) @@ -57,14 +59,14 @@ def init_models(url): temp_authors_table = Table(u'authors_temp', metadata, Column(u'authorid', types.Integer, primary_key=True), - Column(u'authorname', String(40)) + Column(u'authorname', types.Unicode(40)) ) temp_songs_table = Table(u'songs_temp', metadata, Column(u'songid', types.Integer, primary_key=True), - Column(u'songtitle', String(60)), + Column(u'songtitle', types.Unicode(60)), Column(u'lyrics', types.UnicodeText), - Column(u'copyrightinfo', String(255)), + Column(u'copyrightinfo', types.Unicode(255)), Column(u'settingsid', types.Integer) ) @@ -101,8 +103,8 @@ class MigrateSongs(object): def process(self): self.display.output(u'Songs processing started') - for f in self.database_files: - self.v_1_9_0(f) + for data_file in self.database_files: + self.v_1_9_0(data_file) self.display.output(u'Songs processing finished') def v_1_9_0(self, database): diff --git a/openlp/plugins/bibles/forms/importwizardform.py b/openlp/plugins/bibles/forms/importwizardform.py index ac6c0995e..bb6398bb1 100644 --- a/openlp/plugins/bibles/forms/importwizardform.py +++ b/openlp/plugins/bibles/forms/importwizardform.py @@ -32,6 +32,7 @@ from PyQt4 import QtCore, QtGui from bibleimportwizard import Ui_BibleImportWizard from openlp.core.lib import Receiver, SettingsManager, translate +from openlp.core.lib.db import delete_database from openlp.core.utils import AppLocation from openlp.plugins.bibles.lib.manager import BibleFormat @@ -449,8 +450,7 @@ class ImportWizardForm(QtGui.QWizard, Ui_BibleImportWizard): self.ImportProgressLabel.setText( translate(u'BiblesPlugin.ImportWizardForm', u'Your Bible import failed.')) - importer.delete_database(self.bibleplugin.settingsSection, - importer.file) + delete_database(self.bibleplugin.settingsSection, importer.file) def postImport(self): self.ImportProgressBar.setValue(self.ImportProgressBar.maximum()) diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index e6178a098..16c3d918d 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -154,6 +154,7 @@ class BibleDB(QtCore.QObject, Manager): Manager.__init__(self, u'bibles', init_schema, self.file) if u'file' in kwargs: self.get_name() + self.wizard = None def stop_import(self): """ diff --git a/openlp/plugins/custom/customplugin.py b/openlp/plugins/custom/customplugin.py index a63bbdf47..9de676bf6 100644 --- a/openlp/plugins/custom/customplugin.py +++ b/openlp/plugins/custom/customplugin.py @@ -26,6 +26,7 @@ import logging from forms import EditCustomForm + from openlp.core.lib import Plugin, build_icon, PluginStatus, translate from openlp.core.lib.db import Manager from openlp.plugins.custom.lib import CustomMediaItem, CustomTab @@ -77,7 +78,8 @@ class CustomPlugin(Plugin): return about_text def can_delete_theme(self, theme): - filter = u'theme_name=%s' % theme - if not self.custommanager.get_all_objects_filtered(CustomSlide, filter): + filter_string = u'theme_name=%s' % theme + if not self.custommanager.get_all_objects_filtered(CustomSlide, + filter_string): return True return False diff --git a/openlp/plugins/custom/forms/editcustomform.py b/openlp/plugins/custom/forms/editcustomform.py index 2b8a5bc03..01296a329 100644 --- a/openlp/plugins/custom/forms/editcustomform.py +++ b/openlp/plugins/custom/forms/editcustomform.py @@ -167,8 +167,7 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog): u'utf-8') self.customSlide.theme_name = unicode(self.ThemeComboBox.currentText(), u'utf-8') - self.custommanager.insert_object(self.customSlide) - return True + return self.custommanager.insert_object(self.customSlide) def onUpButtonPressed(self): selectedRow = self.VerseListView.currentRow() diff --git a/openlp/plugins/songs/songsplugin.py b/openlp/plugins/songs/songsplugin.py index 2462502b9..3dd467ad6 100644 --- a/openlp/plugins/songs/songsplugin.py +++ b/openlp/plugins/songs/songsplugin.py @@ -197,7 +197,7 @@ class SongsPlugin(Plugin): return about_text def can_delete_theme(self, theme): - filter = u'theme_name=%s' % theme - if not self.manager.get_all_objects_filtered(Song, filter): + filter_string = u'theme_name=%s' % theme + if not self.manager.get_all_objects_filtered(Song, filter_string): return True return False diff --git a/openlp/plugins/songusage/lib/__init__.py b/openlp/plugins/songusage/lib/__init__.py index 50bba3ddc..ae8425317 100644 --- a/openlp/plugins/songusage/lib/__init__.py +++ b/openlp/plugins/songusage/lib/__init__.py @@ -22,5 +22,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### - -from manager import SongUsageManager +""" +The :mod:`lib` module contains the library functions for the songusage plugin. +""" +from openlp.plugins.songusage.lib.manager import SongUsageManager