From 90d2282e08ca0adc0f44964ed4f57165ab8d5857 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 15 Mar 2010 20:47:07 +0200 Subject: [PATCH 1/3] First attempt at fixing the Bible file name issue. --- openlp/plugins/bibles/lib/db.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index be4112a54..c3f22883f 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -26,6 +26,7 @@ import os import logging import chardet +import re from sqlalchemy import or_ from PyQt4 import QtCore @@ -69,10 +70,11 @@ class BibleDB(QtCore.QObject): raise KeyError(u'Missing keyword argument "config".') self.stop_import_flag = False self.name = kwargs[u'name'] + self.filename = self.clean_filename(kwargs[u'name']) self.config = kwargs[u'config'] self.db_file = os.path.join(kwargs[u'path'], - u'%s.sqlite' % kwargs[u'name']) - log.debug(u'Load bible %s on path %s', kwargs[u'name'], self.db_file) + u'%s.sqlite' % self.filename) + log.debug(u'Load bible %s on path %s', self.filename, self.db_file) db_type = self.config.get_config(u'db type', u'sqlite') db_url = u'' if db_type == u'sqlite': @@ -86,6 +88,12 @@ class BibleDB(QtCore.QObject): self.metadata, self.session = init_models(db_url) self.metadata.create_all(checkfirst=True) + def clean_filename(self, old_filename): + for char in [u'\\', u'/', u':', u'*', u'?', u'"', u'<', u'>', u'|', u' ']: + old_filename = old_filename.replace(char, u'_') + old_filename = re.sub(r'[_]+', u'_', old_filename).strip(u'_') + return old_filename + def register(self, wizard): """ This method basically just initialialises the database. It is called From 9044ed3a4a18d436f44bfa472459a311f5e2b9f9 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Tue, 16 Mar 2010 21:46:19 +0200 Subject: [PATCH 2/3] Fixed a bug where Bible names were being used for file names, and causing issues on various operating systems. --- openlp/plugins/bibles/lib/db.py | 35 +++++++++++++++++++++------- openlp/plugins/bibles/lib/manager.py | 16 ++++++++----- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index a09223a20..08c6cbe38 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -66,15 +66,20 @@ class BibleDB(QtCore.QObject): raise KeyError(u'Missing keyword argument "path".') if u'config' not in kwargs: raise KeyError(u'Missing keyword argument "config".') - if u'name' not in kwargs: - raise KeyError(u'Missing keyword argument "name".') + if u'name' not in kwargs and u'file' not in kwargs: + raise KeyError(u'Missing keyword argument "name" or "file".') self.stop_import_flag = False self.config = kwargs[u'config'] - self.name = kwargs[u'name'] - #self.filename = self.clean_filename(kwargs[u'name']) - self.db_file = os.path.join(kwargs[u'path'], - u'%s.sqlite' % self.name) - log.debug(u'Load bible %s on path %s', self.name, self.db_file) + if u'name' in kwargs: + self.name = kwargs[u'name'] + if not isinstance(self.name, unicode): + self.name = unicode(self.name, u'utf-8') + self.file = self.clean_filename(self.name) + if u'file' in kwargs: + self.file = kwargs[u'file'] + + self.db_file = os.path.join(kwargs[u'path'], self.file) + log.debug(u'Load bible %s on path %s', self.file, self.db_file) db_type = self.config.get_config(u'db type', u'sqlite') db_url = u'' if db_type == u'sqlite': @@ -87,18 +92,30 @@ class BibleDB(QtCore.QObject): self.config.get_config(u'db database')) self.metadata, self.session = init_models(db_url) self.metadata.create_all(checkfirst=True) + if u'file' in kwargs: + self.get_name() + + def get_name(self): + version_name = self.get_meta(u'Version') + if version_name: + self.name = version_name.value + else: + self.name = None + return self.name def clean_filename(self, old_filename): + if not isinstance(old_filename, unicode): + old_filename = unicode(old_filename, u'utf-8') for char in [u'\\', u'/', u':', u'*', u'?', u'"', u'<', u'>', u'|', u' ']: old_filename = old_filename.replace(char, u'_') old_filename = re.sub(r'[_]+', u'_', old_filename).strip(u'_') - return old_filename + return old_filename + u'.sqlite' def register(self, wizard): """ This method basically just initialialises the database. It is called from the Bible Manager when a Bible is imported. Descendant classes - may want to override this method to supply their own custom + may want to override this method to suVersionpply their own custom initialisation as well. """ self.wizard = wizard diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index 5fc871abc..12b4eeec8 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -123,17 +123,21 @@ class BibleManager(object): log.debug(u'Bible Files %s', files) self.db_cache = {} for filename in files: - name, extension = os.path.splitext(filename) - self.db_cache[name] = BibleDB(self.parent, path=self.path, - name=name, config=self.config) + bible = BibleDB(self.parent, path=self.path, file=filename, + config=self.config) + #self.db_cache[name] = BibleDB(self.parent, path=self.path, + # name=name, config=self.config) + name = bible.get_name() + log.debug(u'Bible Name: "%s"', name) + self.db_cache[name] = bible # look to see if lazy load bible exists and get create getter. source = self.db_cache[name].get_meta(u'download source') if source: download_name = self.db_cache[name].get_meta(u'download name').value meta_proxy = self.db_cache[name].get_meta(u'proxy url') - web_bible = HTTPBible(self.parent, path=self.path, name=name, - config=self.config, download_source=source.value, - download_name=download_name) + web_bible = HTTPBible(self.parent, path=self.path, + file=filename, config=self.config, + download_source=source.value, download_name=download_name) if meta_proxy: web_bible.set_proxy_server(meta_proxy.value) #del self.db_cache[name] From dc6c6bec31938fe9f65c90a3419eb0d7ac5916ac Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Tue, 16 Mar 2010 22:43:41 +0200 Subject: [PATCH 3/3] - Removed commented code (just for you, TRB143!) - Removed any non-alphanumeric character for Bible names, just to be safe, since we don't actually use the filenames anymore. --- openlp/plugins/bibles/lib/db.py | 9 +-------- openlp/plugins/bibles/lib/manager.py | 3 --- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index 08c6cbe38..8f55fb5fc 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -106,9 +106,7 @@ class BibleDB(QtCore.QObject): def clean_filename(self, old_filename): if not isinstance(old_filename, unicode): old_filename = unicode(old_filename, u'utf-8') - for char in [u'\\', u'/', u':', u'*', u'?', u'"', u'<', u'>', u'|', u' ']: - old_filename = old_filename.replace(char, u'_') - old_filename = re.sub(r'[_]+', u'_', old_filename).strip(u'_') + old_filename = re.sub(r'[^\w]+', u'_', old_filename).strip(u'_') return old_filename + u'.sqlite' def register(self, wizard): @@ -266,8 +264,6 @@ class BibleDB(QtCore.QObject): count = self.session.query(Verse.chapter).join(Book)\ .filter(Book.name==book)\ .distinct().count() - #verse = self.session.query(Verse).join(Book).filter( - # Book.name == bookname).order_by(Verse.chapter.desc()).first() if not count: return 0 else: @@ -279,9 +275,6 @@ class BibleDB(QtCore.QObject): .filter(Book.name==book)\ .filter(Verse.chapter==chapter)\ .count() - #verse = self.session.query(Verse).join(Book).filter( - # Book.name == bookname).filter( - # Verse.chapter == chapter).order_by(Verse.verse.desc()).first() if not count: return 0 else: diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index 12b4eeec8..90401a3e1 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -125,8 +125,6 @@ class BibleManager(object): for filename in files: bible = BibleDB(self.parent, path=self.path, file=filename, config=self.config) - #self.db_cache[name] = BibleDB(self.parent, path=self.path, - # name=name, config=self.config) name = bible.get_name() log.debug(u'Bible Name: "%s"', name) self.db_cache[name] = bible @@ -140,7 +138,6 @@ class BibleManager(object): download_source=source.value, download_name=download_name) if meta_proxy: web_bible.set_proxy_server(meta_proxy.value) - #del self.db_cache[name] self.db_cache[name] = web_bible log.debug(u'Bibles reloaded')