From 97dbc8591890a32ea0be872aeec94902861848d3 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Sun, 7 Aug 2016 20:29:35 +0100 Subject: [PATCH] move chardet function in to core --- openlp/core/lib/__init__.py | 17 +++++++++++++++++ openlp/plugins/bibles/lib/bibleimport.py | 2 ++ openlp/plugins/bibles/lib/csvbible.py | 18 +----------------- openlp/plugins/bibles/lib/opensong.py | 13 +------------ openlp/plugins/bibles/lib/osis.py | 9 --------- openlp/plugins/bibles/lib/zefania.py | 17 +++-------------- 6 files changed, 24 insertions(+), 52 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index fed6df05c..7c00c0348 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -24,6 +24,7 @@ The :mod:`lib` module contains most of the components and libraries that make OpenLP work. """ +import chardet import logging import os from distutils.version import LooseVersion @@ -337,6 +338,22 @@ def create_separated_list(string_list): return translate('OpenLP.core.lib', '%s, %s', 'Locale list separator: start') % (string_list[0], merged) +def get_file_encoding(filename): + """ + Utility function to get the file encoding. + """ + detect_file = None + try: + detect_file = open(filename, 'rb') + details = chardet.detect(detect_file.read(1024)) + except IOError: + log.exception('Error detecting file encoding') + finally: + if detect_file: + detect_file.close() + return details + + from .exceptions import ValidationError from .filedialog import FileDialog from .screen import ScreenList diff --git a/openlp/plugins/bibles/lib/bibleimport.py b/openlp/plugins/bibles/lib/bibleimport.py index faf6f6587..41b72011e 100644 --- a/openlp/plugins/bibles/lib/bibleimport.py +++ b/openlp/plugins/bibles/lib/bibleimport.py @@ -34,9 +34,11 @@ class BibleImport(BibleDB): """ Helper class to import bibles from a third party source into OpenLP """ + #TODO: Test def __init__(self, *args, **kwargs): log.debug(self.__class__.__name__) super().__init__(*args, **kwargs) + self.filename = kwargs['filename'] if 'filename' in kwargs else None def get_language_id(self, file_language=None, bible_name=None): """ diff --git a/openlp/plugins/bibles/lib/csvbible.py b/openlp/plugins/bibles/lib/csvbible.py index 7e04d7535..33237c9c3 100644 --- a/openlp/plugins/bibles/lib/csvbible.py +++ b/openlp/plugins/bibles/lib/csvbible.py @@ -50,10 +50,10 @@ There are two acceptable formats of the verses file. They are: All CSV files are expected to use a comma (',') as the delimiter and double quotes ('"') as the quote symbol. """ import logging -import chardet import csv from openlp.core.common import translate +from openlp.core.lib import get_file_encoding from openlp.plugins.bibles.lib.bibleimport import BibleImport from openlp.plugins.bibles.lib.db import BiblesResourcesDB @@ -155,19 +155,3 @@ class CSVBible(BibleImport): return False else: return success - - -def get_file_encoding(filename): - """ - Utility function to get the file encoding. - """ - detect_file = None - try: - detect_file = open(filename, 'rb') - details = chardet.detect(detect_file.read(1024)) - except IOError: - log.exception('Error detecting file encoding') - finally: - if detect_file: - detect_file.close() - return details diff --git a/openlp/plugins/bibles/lib/opensong.py b/openlp/plugins/bibles/lib/opensong.py index b358d481a..43c1cf8ca 100644 --- a/openlp/plugins/bibles/lib/opensong.py +++ b/openlp/plugins/bibles/lib/opensong.py @@ -34,17 +34,8 @@ log = logging.getLogger(__name__) class OpenSongBible(BibleImport): """ - OpenSong Bible format importer class. + OpenSong Bible format importer class. This class is used to import Bibles from OpenSong's XML format. """ - def __init__(self, *args, **kwargs): - """ - Constructor to create and set up an instance of the OpenSongBible class. This class is used to import Bibles - from OpenSong's XML format. - """ - log.debug(self.__class__.__name__) - super().__init__(*args, **kwargs) - self.filename = kwargs['filename'] - def get_text(self, element): """ Recursively get all text in an objectify element and its child elements. @@ -65,8 +56,6 @@ class OpenSongBible(BibleImport): Loads a Bible from file. """ log.debug('Starting OpenSong import from "{name}"'.format(name=self.filename)) - if not isinstance(self.filename, str): - self.filename = str(self.filename, 'utf8') success = True try: bible = self.parse_xml(self.filename, use_objectify=True) diff --git a/openlp/plugins/bibles/lib/osis.py b/openlp/plugins/bibles/lib/osis.py index dfecfdeec..b5de169e7 100644 --- a/openlp/plugins/bibles/lib/osis.py +++ b/openlp/plugins/bibles/lib/osis.py @@ -78,20 +78,11 @@ class OSISBible(BibleImport): """ `OSIS `_ Bible format importer class. """ - log.info('BibleOSISImpl loaded') - - def __init__(self, *args, **kwargs): - log.debug(self.__class__.__name__) - super().__init__(*args, **kwargs) - self.filename = kwargs['filename'] - def do_import(self, bible_name=None): """ Loads a Bible from file. """ log.debug('Starting OSIS import from "{name}"'.format(name=self.filename)) - if not isinstance(self.filename, str): - self.filename = str(self.filename, 'utf8') success = True try: self.wizard.increment_progress_bar(translate('BiblesPlugin.OsisImport', diff --git a/openlp/plugins/bibles/lib/zefania.py b/openlp/plugins/bibles/lib/zefania.py index e71075e17..61ee41166 100644 --- a/openlp/plugins/bibles/lib/zefania.py +++ b/openlp/plugins/bibles/lib/zefania.py @@ -21,12 +21,11 @@ ############################################################################### import logging -from lxml import etree -from openlp.core.common import languages, translate +from openlp.core.common import translate from openlp.core.lib.ui import critical_error_message_box from openlp.plugins.bibles.lib.bibleimport import BibleImport -from openlp.plugins.bibles.lib.db import BibleDB, BiblesResourcesDB +from openlp.plugins.bibles.lib.db import BiblesResourcesDB log = logging.getLogger(__name__) @@ -39,24 +38,14 @@ REMOVABLE_TAGS = ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF') class ZefaniaBible(BibleImport): """ - Zefania Bible format importer class. + Zefania Bible format importer class. This class is used to import Bibles from ZefaniaBible's XML format. """ - def __init__(self, *args, **kwargs): - """ - Constructor to create and set up an instance of the ZefaniaBible class. This class is used to import Bibles - from ZefaniaBible's XML format. - """ - log.debug(self.__class__.__name__) - super().__init__(*args, **kwargs) - self.filename = kwargs['filename'] def do_import(self, bible_name=None): """ Loads a Bible from file. """ log.debug('Starting Zefania import from "{name}"'.format(name=self.filename)) - if not isinstance(self.filename, str): - self.filename = str(self.filename, 'utf8') success = True try: xmlbible = self.parse_xml(self.filename, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS)