From 706d52ad5de06517a9f0445537c805312a4c1cf4 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Thu, 12 Jan 2017 22:04:53 +0100 Subject: [PATCH] Improve the songbeamer encoding detection. --- openlp/plugins/songs/lib/importers/songbeamer.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/openlp/plugins/songs/lib/importers/songbeamer.py b/openlp/plugins/songs/lib/importers/songbeamer.py index f85f5d361..bec2c3811 100644 --- a/openlp/plugins/songs/lib/importers/songbeamer.py +++ b/openlp/plugins/songs/lib/importers/songbeamer.py @@ -28,6 +28,7 @@ import logging import os import re +from openlp.core.common import get_file_encoding from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib.importers.songimport import SongImport @@ -113,13 +114,15 @@ class SongBeamerImport(SongImport): read_verses = False file_name = os.path.split(import_file)[1] if os.path.isfile(import_file): - # First open in binary mode to detect the encoding - detect_file = open(import_file, 'rb') - details = chardet.detect(detect_file.read()) - detect_file.close() - infile = codecs.open(import_file, 'r', details['encoding']) + # Detect the encoding + self.input_file_encoding = get_file_encoding(import_file)['encoding'] + # The encoding should only be ANSI (cp1252), UTF-8, Unicode, Big-Endian-Unicode. + # So if it doesn't start with 'u' we default to cp1252. See: + # 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() - infile.close() else: continue self.title = file_name.split('.sng')[0]