From 1b75cc9c6b37a6f762905b0af7851d8940d2dfff Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Thu, 29 Jan 2015 21:15:39 +0000 Subject: [PATCH] Fallback to manual encoding detection. --- .../plugins/songs/lib/importers/presentationmanager.py | 9 ++++++++- .../songs/test_presentationmanagerimport.py | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/songs/lib/importers/presentationmanager.py b/openlp/plugins/songs/lib/importers/presentationmanager.py index eb60f16e1..c913fd232 100644 --- a/openlp/plugins/songs/lib/importers/presentationmanager.py +++ b/openlp/plugins/songs/lib/importers/presentationmanager.py @@ -25,6 +25,7 @@ Presentationmanager song files into the current database. """ import os +import chardet from lxml import objectify, etree from openlp.core.ui.wizard import WizardStrings @@ -42,7 +43,13 @@ class PresentationManagerImport(SongImport): if self.stop_import_flag: return self.import_wizard.increment_progress_bar(WizardStrings.ImportingType % os.path.basename(file_path)) - tree = etree.parse(file_path, parser=etree.XMLParser(recover=True)) + try: + tree = etree.parse(file_path, parser=etree.XMLParser(recover=True)) + except etree.XMLSyntaxError: + # Try to detect encoding and use it + file = open(file_path, mode='rb') + encoding = chardet.detect(file)['encoding'] + tree = etree.parse(file_path, parser=etree.XMLParser(recover=True, encoding=encoding)) root = objectify.fromstring(etree.tostring(tree)) self.process_song(root) diff --git a/tests/functional/openlp_plugins/songs/test_presentationmanagerimport.py b/tests/functional/openlp_plugins/songs/test_presentationmanagerimport.py index c351f7ed3..585aa34c5 100644 --- a/tests/functional/openlp_plugins/songs/test_presentationmanagerimport.py +++ b/tests/functional/openlp_plugins/songs/test_presentationmanagerimport.py @@ -42,9 +42,9 @@ class TestPresentationManagerFileImport(SongImportTestHelper): """ Test that loading a PresentationManager file works correctly """ - #self.file_import([os.path.join(TEST_PATH, 'Great Is Thy Faithfulness.sng')], - # self.load_external_result_data(os.path.join(TEST_PATH, 'Great Is Thy Faithfulness.json'))) - #self.file_import([os.path.join(TEST_PATH, 'Agnus Dei.sng')], - # self.load_external_result_data(os.path.join(TEST_PATH, 'Agnus Dei.json'))) + self.file_import([os.path.join(TEST_PATH, 'Great Is Thy Faithfulness.sng')], + self.load_external_result_data(os.path.join(TEST_PATH, 'Great Is Thy Faithfulness.json'))) + self.file_import([os.path.join(TEST_PATH, 'Agnus Dei.sng')], + self.load_external_result_data(os.path.join(TEST_PATH, 'Agnus Dei.json'))) self.file_import([os.path.join(TEST_PATH, 'Amazing Grace.sng')], self.load_external_result_data(os.path.join(TEST_PATH, 'Amazing Grace.json')))