From b831ebd48ed05ccd4977d71e149b0e97600d5958 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 10 Mar 2014 20:53:11 +0100 Subject: [PATCH 1/3] Use ICU for locale sort key --- openlp/core/utils/__init__.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 426606987..1e358454f 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -426,17 +426,14 @@ def get_locale_key(string): The corresponding string. """ string = string.lower() - # For Python 3 on platforms other than Windows ICU is not necessary. In those cases locale.strxfrm(str) can be used. - if os.name == 'nt': - global ICU_COLLATOR - if ICU_COLLATOR is None: - import icu - from .languagemanager import LanguageManager - language = LanguageManager.get_language() - icu_locale = icu.Locale(language) - ICU_COLLATOR = icu.Collator.createInstance(icu_locale) - return ICU_COLLATOR.getSortKey(string) - return locale.strxfrm(string).encode() + global ICU_COLLATOR + if ICU_COLLATOR is None: + import icu + from .languagemanager import LanguageManager + language = LanguageManager.get_language() + icu_locale = icu.Locale(language) + ICU_COLLATOR = icu.Collator.createInstance(icu_locale) + return ICU_COLLATOR.getSortKey(string) def get_natural_key(string): From 2b55da02c11214b9c9c8a5f61936c4495bbf084c Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 10 Mar 2014 20:56:36 +0100 Subject: [PATCH 2/3] Reuse QApplication instance to avoid segfaults. --- tests/interfaces/openlp_core_lib/test_pluginmanager.py | 9 ++++++--- tests/interfaces/openlp_core_ui/test_filerenamedialog.py | 9 ++++++--- .../interfaces/openlp_core_ui/test_listpreviewwidget.py | 9 ++++++--- tests/interfaces/openlp_core_ui/test_mainwindow.py | 9 ++++++--- tests/interfaces/openlp_core_ui/test_servicemanager.py | 7 +++++-- .../interfaces/openlp_core_ui/test_servicenotedialog.py | 7 +++++-- tests/interfaces/openlp_core_ui/test_settings_form.py | 7 +++++-- tests/interfaces/openlp_core_ui/test_starttimedialog.py | 7 +++++-- tests/interfaces/openlp_core_ui/test_thememanager.py | 9 ++++++--- .../openlp_plugins/custom/forms/test_customform.py | 7 +++++-- .../openlp_plugins/custom/forms/test_customslideform.py | 9 ++++++--- .../openlp_plugins/songs/forms/test_authorsform.py | 9 ++++++--- .../openlp_plugins/songs/forms/test_editsongform.py | 9 ++++++--- .../openlp_plugins/songs/forms/test_editverseform.py | 7 +++++-- .../openlp_plugins/songs/forms/test_topicsform.py | 9 ++++++--- 15 files changed, 84 insertions(+), 39 deletions(-) diff --git a/tests/interfaces/openlp_core_lib/test_pluginmanager.py b/tests/interfaces/openlp_core_lib/test_pluginmanager.py index 9e8dc534b..0306a33ff 100644 --- a/tests/interfaces/openlp_core_lib/test_pluginmanager.py +++ b/tests/interfaces/openlp_core_lib/test_pluginmanager.py @@ -7,7 +7,7 @@ import shutil from tempfile import mkstemp, mkdtemp from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry, Settings from openlp.core.lib.pluginmanager import PluginManager @@ -29,12 +29,15 @@ class TestPluginManager(TestCase): Settings().setValue('advanced/data path', self.temp_dir) Registry.create() Registry().register('service_list', MagicMock()) - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) def tearDown(self): - del self.app del self.main_window Settings().remove('advanced/data path') shutil.rmtree(self.temp_dir) diff --git a/tests/interfaces/openlp_core_ui/test_filerenamedialog.py b/tests/interfaces/openlp_core_ui/test_filerenamedialog.py index fabefc427..8fac36213 100644 --- a/tests/interfaces/openlp_core_ui/test_filerenamedialog.py +++ b/tests/interfaces/openlp_core_ui/test_filerenamedialog.py @@ -3,7 +3,7 @@ """ from unittest import TestCase -from PyQt4 import QtGui, QtTest +from PyQt4 import QtCore, QtGui, QtTest from openlp.core.common import Registry from openlp.core.ui import filerenameform @@ -17,7 +17,11 @@ class TestStartFileRenameForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = filerenameform.FileRenameForm() @@ -28,7 +32,6 @@ class TestStartFileRenameForm(TestCase): """ del self.form del self.main_window - del self.app def window_title_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_listpreviewwidget.py b/tests/interfaces/openlp_core_ui/test_listpreviewwidget.py index 6d638cdd1..7b91ce152 100644 --- a/tests/interfaces/openlp_core_ui/test_listpreviewwidget.py +++ b/tests/interfaces/openlp_core_ui/test_listpreviewwidget.py @@ -4,7 +4,7 @@ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.core.lib import ServiceItem @@ -20,7 +20,11 @@ class TestListPreviewWidget(TestCase): Create the UI. """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() self.image = QtGui.QImage(1, 1, QtGui.QImage.Format_RGB32) self.image_manager = MagicMock() @@ -34,7 +38,6 @@ class TestListPreviewWidget(TestCase): """ del self.preview_widget del self.main_window - del self.app def initial_slide_count_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_mainwindow.py b/tests/interfaces/openlp_core_ui/test_mainwindow.py index 279fe15db..db55f53ec 100644 --- a/tests/interfaces/openlp_core_ui/test_mainwindow.py +++ b/tests/interfaces/openlp_core_ui/test_mainwindow.py @@ -3,7 +3,7 @@ Package to test the openlp.core.ui.mainwindow package. """ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.core.ui.mainwindow import MainWindow @@ -18,7 +18,11 @@ class TestMainWindow(TestCase): """ Registry.create() self.registry = Registry() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance # Mock cursor busy/normal methods. self.app.set_busy_cursor = MagicMock() self.app.set_normal_cursor = MagicMock() @@ -42,7 +46,6 @@ class TestMainWindow(TestCase): Delete all the C++ objects at the end so that we don't have a segfault """ del self.main_window - del self.app def restore_current_media_manager_item_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_servicemanager.py b/tests/interfaces/openlp_core_ui/test_servicemanager.py index 5e4d5dead..7487b22e2 100644 --- a/tests/interfaces/openlp_core_ui/test_servicemanager.py +++ b/tests/interfaces/openlp_core_ui/test_servicemanager.py @@ -19,7 +19,11 @@ class TestServiceManager(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance ScreenList.create(self.app.desktop()) Registry().register('application', MagicMock()) with patch('openlp.core.lib.PluginManager'): @@ -31,7 +35,6 @@ class TestServiceManager(TestCase): Delete all the C++ objects at the end so that we don't have a segfault """ del self.main_window - del self.app def basic_service_manager_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_servicenotedialog.py b/tests/interfaces/openlp_core_ui/test_servicenotedialog.py index e8aa70ede..4a0f0acff 100644 --- a/tests/interfaces/openlp_core_ui/test_servicenotedialog.py +++ b/tests/interfaces/openlp_core_ui/test_servicenotedialog.py @@ -17,7 +17,11 @@ class TestStartNoteDialog(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = servicenoteform.ServiceNoteForm() @@ -28,7 +32,6 @@ class TestStartNoteDialog(TestCase): """ del self.form del self.main_window - del self.app def basic_display_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_settings_form.py b/tests/interfaces/openlp_core_ui/test_settings_form.py index c475df994..904f7d5dd 100644 --- a/tests/interfaces/openlp_core_ui/test_settings_form.py +++ b/tests/interfaces/openlp_core_ui/test_settings_form.py @@ -31,7 +31,11 @@ class TestSettingsForm(TestCase): self.dummy2 = MagicMock() self.dummy3 = MagicMock() self.desktop = MagicMock() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.desktop.primaryScreen.return_value = SCREEN['primary'] self.desktop.screenCount.return_value = SCREEN['number'] self.desktop.screenGeometry.return_value = SCREEN['size'] @@ -44,7 +48,6 @@ class TestSettingsForm(TestCase): Delete all the C++ objects at the end so that we don't have a segfault """ del self.form - del self.app def basic_cancel_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_starttimedialog.py b/tests/interfaces/openlp_core_ui/test_starttimedialog.py index 709a5d68f..b9d16852a 100644 --- a/tests/interfaces/openlp_core_ui/test_starttimedialog.py +++ b/tests/interfaces/openlp_core_ui/test_starttimedialog.py @@ -17,7 +17,11 @@ class TestStartTimeDialog(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = starttimeform.StartTimeForm() @@ -28,7 +32,6 @@ class TestStartTimeDialog(TestCase): """ del self.form del self.main_window - del self.app def ui_defaults_test(self): """ diff --git a/tests/interfaces/openlp_core_ui/test_thememanager.py b/tests/interfaces/openlp_core_ui/test_thememanager.py index 17a22f215..c2b3f0764 100644 --- a/tests/interfaces/openlp_core_ui/test_thememanager.py +++ b/tests/interfaces/openlp_core_ui/test_thememanager.py @@ -33,7 +33,7 @@ import os from unittest import TestCase from tempfile import mkstemp -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry, Settings from openlp.core.ui import ThemeManager @@ -50,7 +50,11 @@ class TestThemeManager(TestCase): """ fd, self.ini_file = mkstemp('.ini') Settings().set_filename(self.ini_file) - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance Registry.create() self.theme_manager = ThemeManager() @@ -60,7 +64,6 @@ class TestThemeManager(TestCase): """ os.unlink(self.ini_file) os.unlink(Settings().fileName()) - del self.app def initialise_test(self): """ diff --git a/tests/interfaces/openlp_plugins/custom/forms/test_customform.py b/tests/interfaces/openlp_plugins/custom/forms/test_customform.py index e933bb17d..e98778926 100644 --- a/tests/interfaces/openlp_plugins/custom/forms/test_customform.py +++ b/tests/interfaces/openlp_plugins/custom/forms/test_customform.py @@ -21,7 +21,11 @@ class TestEditCustomForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) media_item = MagicMock() @@ -34,7 +38,6 @@ class TestEditCustomForm(TestCase): """ del self.form del self.main_window - del self.app def load_themes_test(self): """ diff --git a/tests/interfaces/openlp_plugins/custom/forms/test_customslideform.py b/tests/interfaces/openlp_plugins/custom/forms/test_customslideform.py index f98ae376e..ece444b07 100644 --- a/tests/interfaces/openlp_plugins/custom/forms/test_customslideform.py +++ b/tests/interfaces/openlp_plugins/custom/forms/test_customslideform.py @@ -3,7 +3,7 @@ Module to test the EditCustomSlideForm. """ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.plugins.custom.forms.editcustomslideform import EditCustomSlideForm @@ -19,7 +19,11 @@ class TestEditCustomSlideForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = EditCustomSlideForm() @@ -30,7 +34,6 @@ class TestEditCustomSlideForm(TestCase): """ del self.form del self.main_window - del self.app def basic_test(self): """ diff --git a/tests/interfaces/openlp_plugins/songs/forms/test_authorsform.py b/tests/interfaces/openlp_plugins/songs/forms/test_authorsform.py index f4dc5c228..0aea0a626 100644 --- a/tests/interfaces/openlp_plugins/songs/forms/test_authorsform.py +++ b/tests/interfaces/openlp_plugins/songs/forms/test_authorsform.py @@ -3,7 +3,7 @@ Package to test the openlp.plugins.songs.forms.authorsform package. """ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.plugins.songs.forms.authorsform import AuthorsForm @@ -19,7 +19,11 @@ class TestAuthorsForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = AuthorsForm() @@ -30,7 +34,6 @@ class TestAuthorsForm(TestCase): """ del self.form del self.main_window - del self.app def ui_defaults_test(self): """ diff --git a/tests/interfaces/openlp_plugins/songs/forms/test_editsongform.py b/tests/interfaces/openlp_plugins/songs/forms/test_editsongform.py index 6804858a4..010d9d0f0 100644 --- a/tests/interfaces/openlp_plugins/songs/forms/test_editsongform.py +++ b/tests/interfaces/openlp_plugins/songs/forms/test_editsongform.py @@ -3,7 +3,7 @@ Package to test the openlp.plugins.songs.forms.editsongform package. """ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.plugins.songs.forms.editsongform import EditSongForm @@ -20,7 +20,11 @@ class TestEditSongForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) Registry().register('theme_manager', MagicMock()) @@ -32,7 +36,6 @@ class TestEditSongForm(TestCase): """ del self.form del self.main_window - del self.app def ui_defaults_test(self): """ diff --git a/tests/interfaces/openlp_plugins/songs/forms/test_editverseform.py b/tests/interfaces/openlp_plugins/songs/forms/test_editverseform.py index f047903bc..592827546 100644 --- a/tests/interfaces/openlp_plugins/songs/forms/test_editverseform.py +++ b/tests/interfaces/openlp_plugins/songs/forms/test_editverseform.py @@ -19,7 +19,11 @@ class TestEditVerseForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = EditVerseForm() @@ -30,7 +34,6 @@ class TestEditVerseForm(TestCase): """ del self.form del self.main_window - del self.app def ui_defaults_test(self): """ diff --git a/tests/interfaces/openlp_plugins/songs/forms/test_topicsform.py b/tests/interfaces/openlp_plugins/songs/forms/test_topicsform.py index c8057c4ea..7fde007f0 100644 --- a/tests/interfaces/openlp_plugins/songs/forms/test_topicsform.py +++ b/tests/interfaces/openlp_plugins/songs/forms/test_topicsform.py @@ -3,7 +3,7 @@ Package to test the openlp.plugins.songs.forms.topicsform package. """ from unittest import TestCase -from PyQt4 import QtGui +from PyQt4 import QtGui, QtCore from openlp.core.common import Registry from openlp.plugins.songs.forms.topicsform import TopicsForm @@ -19,7 +19,11 @@ class TestTopicsForm(TestCase): Create the UI """ Registry.create() - self.app = QtGui.QApplication([]) + old_app_instance = QtCore.QCoreApplication.instance() + if old_app_instance is None: + self.app = QtGui.QApplication([]) + else: + self.app = old_app_instance self.main_window = QtGui.QMainWindow() Registry().register('main_window', self.main_window) self.form = TopicsForm() @@ -30,7 +34,6 @@ class TestTopicsForm(TestCase): """ del self.form del self.main_window - del self.app def ui_defaults_test(self): """ From 5ac6f853d0b345f8f3fc59d34b57d4adca0c44bd Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 11 Mar 2014 20:38:47 +0100 Subject: [PATCH 3/3] Changed get_locale_key to only use ICU if available. --- openlp/core/utils/__init__.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 1e358454f..510528bcb 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -426,15 +426,18 @@ def get_locale_key(string): The corresponding string. """ string = string.lower() + # ICU is the prefered way to handle locale sort key, we fallback to locale.strxfrm which will work in most cases. global ICU_COLLATOR - if ICU_COLLATOR is None: - import icu - from .languagemanager import LanguageManager - language = LanguageManager.get_language() - icu_locale = icu.Locale(language) - ICU_COLLATOR = icu.Collator.createInstance(icu_locale) - return ICU_COLLATOR.getSortKey(string) - + try: + if ICU_COLLATOR is None: + import icu + from .languagemanager import LanguageManager + language = LanguageManager.get_language() + icu_locale = icu.Locale(language) + ICU_COLLATOR = icu.Collator.createInstance(icu_locale) + return ICU_COLLATOR.getSortKey(string) + except: + return locale.strxfrm(string).encode() def get_natural_key(string): """