Improve the songbeamer encoding detection.

This commit is contained in:
Tomas Groth 2017-01-12 22:04:53 +01:00
parent 32d3c0038e
commit 706d52ad5d

View File

@ -28,6 +28,7 @@ import logging
import os import os
import re import re
from openlp.core.common import get_file_encoding
from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib import VerseType
from openlp.plugins.songs.lib.importers.songimport import SongImport from openlp.plugins.songs.lib.importers.songimport import SongImport
@ -113,13 +114,15 @@ class SongBeamerImport(SongImport):
read_verses = False read_verses = False
file_name = os.path.split(import_file)[1] file_name = os.path.split(import_file)[1]
if os.path.isfile(import_file): if os.path.isfile(import_file):
# First open in binary mode to detect the encoding # Detect the encoding
detect_file = open(import_file, 'rb') self.input_file_encoding = get_file_encoding(import_file)['encoding']
details = chardet.detect(detect_file.read()) # The encoding should only be ANSI (cp1252), UTF-8, Unicode, Big-Endian-Unicode.
detect_file.close() # So if it doesn't start with 'u' we default to cp1252. See:
infile = codecs.open(import_file, 'r', details['encoding']) # https://forum.songbeamer.com/viewtopic.php?p=419&sid=ca4814924e37c11e4438b7272a98b6f2
if self.input_file_encoding.lower().startswith('u'):
self.input_file_encoding = 'cp1252'
infile = open(import_file, 'rt', encoding=self.input_file_encoding)
song_data = infile.readlines() song_data = infile.readlines()
infile.close()
else: else:
continue continue
self.title = file_name.split('.sng')[0] self.title = file_name.split('.sng')[0]