move chardet function in to core

This commit is contained in:
Philip Ridout 2016-08-07 20:29:35 +01:00
parent 1abcff7519
commit 97dbc85918
6 changed files with 24 additions and 52 deletions

View File

@ -24,6 +24,7 @@ The :mod:`lib` module contains most of the components and libraries that make
OpenLP work. OpenLP work.
""" """
import chardet
import logging import logging
import os import os
from distutils.version import LooseVersion 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) 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 .exceptions import ValidationError
from .filedialog import FileDialog from .filedialog import FileDialog
from .screen import ScreenList from .screen import ScreenList

View File

@ -34,9 +34,11 @@ class BibleImport(BibleDB):
""" """
Helper class to import bibles from a third party source into OpenLP Helper class to import bibles from a third party source into OpenLP
""" """
#TODO: Test
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
log.debug(self.__class__.__name__) log.debug(self.__class__.__name__)
super().__init__(*args, **kwargs) 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): def get_language_id(self, file_language=None, bible_name=None):
""" """

View File

@ -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. All CSV files are expected to use a comma (',') as the delimiter and double quotes ('"') as the quote symbol.
""" """
import logging import logging
import chardet
import csv import csv
from openlp.core.common import translate 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.bibleimport import BibleImport
from openlp.plugins.bibles.lib.db import BiblesResourcesDB from openlp.plugins.bibles.lib.db import BiblesResourcesDB
@ -155,19 +155,3 @@ class CSVBible(BibleImport):
return False return False
else: else:
return success 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

View File

@ -34,17 +34,8 @@ log = logging.getLogger(__name__)
class OpenSongBible(BibleImport): 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): def get_text(self, element):
""" """
Recursively get all text in an objectify element and its child elements. Recursively get all text in an objectify element and its child elements.
@ -65,8 +56,6 @@ class OpenSongBible(BibleImport):
Loads a Bible from file. Loads a Bible from file.
""" """
log.debug('Starting OpenSong import from "{name}"'.format(name=self.filename)) 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 success = True
try: try:
bible = self.parse_xml(self.filename, use_objectify=True) bible = self.parse_xml(self.filename, use_objectify=True)

View File

@ -78,20 +78,11 @@ class OSISBible(BibleImport):
""" """
`OSIS <http://www.bibletechnologies.net/>`_ Bible format importer class. `OSIS <http://www.bibletechnologies.net/>`_ 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): def do_import(self, bible_name=None):
""" """
Loads a Bible from file. Loads a Bible from file.
""" """
log.debug('Starting OSIS import from "{name}"'.format(name=self.filename)) 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 success = True
try: try:
self.wizard.increment_progress_bar(translate('BiblesPlugin.OsisImport', self.wizard.increment_progress_bar(translate('BiblesPlugin.OsisImport',

View File

@ -21,12 +21,11 @@
############################################################################### ###############################################################################
import logging 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.core.lib.ui import critical_error_message_box
from openlp.plugins.bibles.lib.bibleimport import BibleImport 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__) log = logging.getLogger(__name__)
@ -39,24 +38,14 @@ REMOVABLE_TAGS = ('STYLE', 'GRAM', 'NOTE', 'SUP', 'XREF')
class ZefaniaBible(BibleImport): 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): def do_import(self, bible_name=None):
""" """
Loads a Bible from file. Loads a Bible from file.
""" """
log.debug('Starting Zefania import from "{name}"'.format(name=self.filename)) 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 success = True
try: try:
xmlbible = self.parse_xml(self.filename, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS) xmlbible = self.parse_xml(self.filename, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS)