From 364fde73c54926ef67c02ee813c5c08ac854d402 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Tue, 13 Aug 2013 19:02:04 +0100 Subject: [PATCH 1/6] Fix #120515 by reimplementing QFileDialog.getOpenFileNames --- openlp/core/lib/__init__.py | 1 + openlp/core/lib/filedialog.py | 66 +++++++++++++++++ openlp/core/lib/mediamanageritem.py | 4 +- openlp/core/lib/uistrings.py | 2 + openlp/core/ui/thememanager.py | 7 +- openlp/plugins/songs/forms/editsongform.py | 6 +- openlp/plugins/songs/forms/songimportform.py | 4 +- .../openlp_core_lib/test_file_dialog.py | 71 +++++++++++++++++++ 8 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 openlp/core/lib/filedialog.py create mode 100644 tests/functional/openlp_core_lib/test_file_dialog.py diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index d6c338271..61891c1ef 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -377,6 +377,7 @@ def create_separated_list(stringlist): from registry import Registry from uistrings import UiStrings +from filedialog import FileDialog from screen import ScreenList from settings import Settings from listwidgetwithdnd import ListWidgetWithDnD diff --git a/openlp/core/lib/filedialog.py b/openlp/core/lib/filedialog.py new file mode 100644 index 000000000..b2159b511 --- /dev/null +++ b/openlp/core/lib/filedialog.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2013 Raoul Snyman # +# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan # +# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, # +# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. # +# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, # +# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, # +# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, # +# Frode Woldsund, Martin Zibricky # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program is distributed in the hope that it will be useful, but WITHOUT # +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along # +# with this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### + +""" +Provide a work around for a bug in QFileDialog (#1209515) +""" +import logging +import os +import urllib + +from PyQt4 import QtGui + +from openlp.core.lib import UiStrings + +log = logging.getLogger(__name__) + +class FileDialog(QtGui.QFileDialog): + """ + Inherit form QFileDialog + """ + @staticmethod + def getOpenFileNames(parent, *args, **kwargs): + """ + Reimplement getOpenFileNames to fix the way it returns some file names that url encoded when selecting multiple + files + """ + files = QtGui.QFileDialog.getOpenFileNames(parent, *args, **kwargs) + file_list = [] + for file in files: + file = unicode(file) + if not os.path.exists(file): + log.info(u'File %s not found. Attempting to unquote.') + file = urllib.unquote(unicode(file)) + if not os.path.exists(file): + log.error(u'File %s not found.' % file) + QtGui.QMessageBox.information(parent, UiStrings().FNFT, UiStrings().FNF % file) + continue + log.info(u'File %s found.') + file_list.append(file) + return file_list \ No newline at end of file diff --git a/openlp/core/lib/mediamanageritem.py b/openlp/core/lib/mediamanageritem.py index 973d457bb..1aa622b1e 100644 --- a/openlp/core/lib/mediamanageritem.py +++ b/openlp/core/lib/mediamanageritem.py @@ -35,7 +35,7 @@ import re from PyQt4 import QtCore, QtGui -from openlp.core.lib import OpenLPToolbar, ServiceItem, StringContent, ListWidgetWithDnD, \ +from openlp.core.lib import FileDialog, OpenLPToolbar, ServiceItem, StringContent, ListWidgetWithDnD, \ ServiceItemContext, Settings, Registry, UiStrings, translate from openlp.core.lib.searchedit import SearchEdit from openlp.core.lib.ui import create_widget_action, critical_error_message_box @@ -305,7 +305,7 @@ class MediaManagerItem(QtGui.QWidget): """ Add a file to the list widget to make it available for showing """ - files = QtGui.QFileDialog.getOpenFileNames(self, self.on_new_prompt, + files = FileDialog().getOpenFileNames(self, self.on_new_prompt, Settings().value(self.settings_section + u'/last directory'), self.on_new_file_masks) log.info(u'New files(s) %s', files) if files: diff --git a/openlp/core/lib/uistrings.py b/openlp/core/lib/uistrings.py index 6d4b4d250..26ad63b55 100644 --- a/openlp/core/lib/uistrings.py +++ b/openlp/core/lib/uistrings.py @@ -83,6 +83,8 @@ class UiStrings(object): self.Error = translate('OpenLP.Ui', 'Error') self.Export = translate('OpenLP.Ui', 'Export') self.File = translate('OpenLP.Ui', 'File') + self.FNFT = unicode(translate('OpenLP.Ui', 'File Not Found')) + self.FNF = unicode(translate('OpenLP.Ui', 'File %s not found.\nPlease try selecting it individually.')) self.FontSizePtUnit = translate('OpenLP.Ui', 'pt', 'Abbreviated font pointsize unit') self.Help = translate('OpenLP.Ui', 'Help') self.Hours = translate('OpenLP.Ui', 'h', 'The abbreviated unit for hours') diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 58f29cab6..a1f761d8f 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -38,8 +38,9 @@ import re from xml.etree.ElementTree import ElementTree, XML from PyQt4 import QtCore, QtGui -from openlp.core.lib import ImageSource, OpenLPToolbar, Registry, Settings, UiStrings, get_text_file_string, \ - build_icon, translate, check_item_selected, check_directory_exists, create_thumb, validate_thumb +from openlp.core.lib import FileDialog, ImageSource, OpenLPToolbar, Registry, Settings, UiStrings, \ + get_text_file_string, build_icon, translate, check_item_selected, check_directory_exists, create_thumb, \ + validate_thumb from openlp.core.lib.theme import ThemeXML, BackgroundType, VerticalType, BackgroundGradientType from openlp.core.lib.ui import critical_error_message_box, create_widget_action from openlp.core.theme import Theme @@ -373,7 +374,7 @@ class ThemeManager(QtGui.QWidget): Opens a file dialog to select the theme file(s) to import before attempting to extract OpenLP themes from those files. This process will load both OpenLP version 1 and version 2 themes. """ - files = QtGui.QFileDialog.getOpenFileNames(self, + files = FileDialog().getOpenFileNames(self, translate('OpenLP.ThemeManager', 'Select Theme Import File'), Settings().value(self.settings_section + u'/last directory import'), translate('OpenLP.ThemeManager', 'OpenLP Themes (*.theme *.otz)')) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 24d0d3024..f73a4468c 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -38,8 +38,8 @@ import shutil from PyQt4 import QtCore, QtGui -from openlp.core.lib import Registry, PluginStatus, MediaType, UiStrings, translate, create_separated_list, \ - check_directory_exists +from openlp.core.lib import FileDialog, Registry, PluginStatus, MediaType, UiStrings, translate, \ + create_separated_list, check_directory_exists from openlp.core.lib.ui import set_case_insensitive_completer, critical_error_message_box, find_and_set_in_combo_box from openlp.core.utils import AppLocation from openlp.plugins.songs.lib import VerseType, clean_song @@ -753,7 +753,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): Loads file(s) from the filesystem. """ filters = u'%s (*)' % UiStrings().AllFiles - filenames = QtGui.QFileDialog.getOpenFileNames(self, + filenames = FileDialog().getOpenFileNames(self, translate('SongsPlugin.EditSongForm', 'Open File(s)'), u'', filters) for filename in filenames: item = QtGui.QListWidgetItem(os.path.split(unicode(filename))[1]) diff --git a/openlp/plugins/songs/forms/songimportform.py b/openlp/plugins/songs/forms/songimportform.py index c1aca92ad..bd9acc943 100644 --- a/openlp/plugins/songs/forms/songimportform.py +++ b/openlp/plugins/songs/forms/songimportform.py @@ -35,7 +35,7 @@ import os from PyQt4 import QtCore, QtGui -from openlp.core.lib import Registry, Settings, UiStrings, translate +from openlp.core.lib import FileDialog, Registry, Settings, UiStrings, translate from openlp.core.lib.ui import critical_error_message_box from openlp.core.ui.wizard import OpenLPWizard, WizardStrings from openlp.plugins.songs.lib.importer import SongFormat, SongFormatSelect @@ -244,7 +244,7 @@ class SongImportForm(OpenLPWizard): if filters: filters += u';;' filters += u'%s (*)' % UiStrings().AllFiles - filenames = QtGui.QFileDialog.getOpenFileNames(self, title, + filenames = FileDialog().getOpenFileNames(self, title, Settings().value(self.plugin.settings_section + u'/last directory import'), filters) if filenames: listbox.addItems(filenames) diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py new file mode 100644 index 000000000..dc704eaef --- /dev/null +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -0,0 +1,71 @@ +""" +Package to test the openlp.core.lib.formattingtags package. +""" +import copy +from unittest import TestCase +from mock import MagicMock, call, patch + +from openlp.core.lib.filedialog import FileDialog +from openlp.core.lib.uistrings import UiStrings + +class TestFileDialog(TestCase): + """ + Test the functions in the :mod:`filedialog` module. + """ + def setUp(self): + self.os_patcher = patch(u'openlp.core.lib.filedialog.os') + self.qt_gui_patcher = patch(u'openlp.core.lib.filedialog.QtGui') + self.ui_strings_patcher = patch(u'openlp.core.lib.filedialog.UiStrings') + self.mocked_os = self.os_patcher.start() + self.mocked_qt_gui = self.qt_gui_patcher.start() + self.mocked_ui_strings = self.ui_strings_patcher.start() + self.mocked_parent = MagicMock() + + def tearDown(self): + self.os_patcher.stop() + self.qt_gui_patcher.stop() + self.ui_strings_patcher.stop() + + def get_open_file_names_canceled_test(self): + """ + Test that FileDialog.getOpenFileNames() returns and empty QStringList when QFileDialog is canceled + (returns an empty QStringList) + """ + self.mocked_os.reset() + + # GIVEN: An empty QStringList as a return value from QFileDialog.getOpenFileNames + self.mocked_qt_gui.QFileDialog.getOpenFileNames.return_value = [] + + # WHEN: FileDialog.getOpenFileNames is called + result = FileDialog.getOpenFileNames(self.mocked_parent) + + # THEN: The returned value should be an empty QStiingList and os.path.exists should not have been called + assert not self.mocked_os.path.exists.called + self.assertEqual(result, [], + u'FileDialog.getOpenFileNames should return and empty list when QFileDialog.getOpenFileNames is canceled') + + def returned_file_list_test(self): + """ + Test that FileDialog.getOpenFileNames handles a list of files properly when QFileList.getOpenFileNames + returns a good file name, a urlencoded file name and a non-existing file + """ + self.mocked_os.rest() + self.mocked_qt_gui.reset() + + # GIVEN: A List of known values as a return value from QFileDialog.getOpenFileNamesand a list of valid + # file names. + self.mocked_qt_gui.QFileDialog.getOpenFileNames.return_value = [ + u'/Valid File', u'/url%20encoded%20file%20%231', u'/non-existing'] + self.mocked_os.path.exists.side_effect = lambda file_name: file_name in [ + u'/Valid File', u'/url encoded file #1'] + + # WHEN: FileDialog.getOpenFileNames is called + result = FileDialog.getOpenFileNames(self.mocked_parent) + + # THEN: os.path.exists should have been called with known args. QmessageBox.information should have been + # called. The returned result should corrilate with the input. + self.mocked_os.path.exists.assert_has_calls([call(u'/Valid File'), call(u'/url%20encoded%20file%20%231'), + call(u'/url encoded file #1'), call(u'/non-existing'), call(u'/non-existing')]) + self.mocked_qt_gui.QmessageBox.information.called_with(self.mocked_parent, UiStrings().FNFT, + UiStrings().FNF % u'/non-existing') + self.assertEqual(result, [u'/Valid File', u'/url encoded file #1'], u'The returned file list is incorrect') \ No newline at end of file From 2ceb165901ff9c7e6cc50eced7fa76f56ff8f215 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Thu, 22 Aug 2013 05:33:19 +0000 Subject: [PATCH 2/6] change string names --- openlp/core/lib/filedialog.py | 7 ++++--- openlp/core/lib/uistrings.py | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/openlp/core/lib/filedialog.py b/openlp/core/lib/filedialog.py index b2159b511..ea2eba045 100644 --- a/openlp/core/lib/filedialog.py +++ b/openlp/core/lib/filedialog.py @@ -28,7 +28,7 @@ ############################################################################### """ -Provide a work around for a bug in QFileDialog (#1209515) +Provide a work around for a bug in QFileDialog """ import logging import os @@ -42,7 +42,7 @@ log = logging.getLogger(__name__) class FileDialog(QtGui.QFileDialog): """ - Inherit form QFileDialog + Subclass QFileDialog to work round a bug """ @staticmethod def getOpenFileNames(parent, *args, **kwargs): @@ -59,7 +59,8 @@ class FileDialog(QtGui.QFileDialog): file = urllib.unquote(unicode(file)) if not os.path.exists(file): log.error(u'File %s not found.' % file) - QtGui.QMessageBox.information(parent, UiStrings().FNFT, UiStrings().FNF % file) + QtGui.QMessageBox.information(parent, UiStrings().FileNotFound, + UiStrings().FileNotFoundMessage % file) continue log.info(u'File %s found.') file_list.append(file) diff --git a/openlp/core/lib/uistrings.py b/openlp/core/lib/uistrings.py index 26ad63b55..faab77a49 100644 --- a/openlp/core/lib/uistrings.py +++ b/openlp/core/lib/uistrings.py @@ -83,8 +83,8 @@ class UiStrings(object): self.Error = translate('OpenLP.Ui', 'Error') self.Export = translate('OpenLP.Ui', 'Export') self.File = translate('OpenLP.Ui', 'File') - self.FNFT = unicode(translate('OpenLP.Ui', 'File Not Found')) - self.FNF = unicode(translate('OpenLP.Ui', 'File %s not found.\nPlease try selecting it individually.')) + self.FileNotFound = unicode(translate('OpenLP.Ui', 'File Not Found')) + self.FileNotFoundMessage = unicode(translate('OpenLP.Ui', 'File %s not found.\nPlease try selecting it individually.')) self.FontSizePtUnit = translate('OpenLP.Ui', 'pt', 'Abbreviated font pointsize unit') self.Help = translate('OpenLP.Ui', 'Help') self.Hours = translate('OpenLP.Ui', 'h', 'The abbreviated unit for hours') From 49c9663dcfb0b49c744da3141916ca6568fbd4b0 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Thu, 22 Aug 2013 20:20:02 +0000 Subject: [PATCH 3/6] renamed var, removed some uneeded ()'s --- openlp/core/lib/mediamanageritem.py | 2 +- openlp/core/ui/thememanager.py | 2 +- openlp/plugins/songs/forms/editsongform.py | 2 +- openlp/plugins/songs/forms/songimportform.py | 2 +- tests/functional/openlp_core_lib/test_file_dialog.py | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openlp/core/lib/mediamanageritem.py b/openlp/core/lib/mediamanageritem.py index 1aa622b1e..018c6801f 100644 --- a/openlp/core/lib/mediamanageritem.py +++ b/openlp/core/lib/mediamanageritem.py @@ -305,7 +305,7 @@ class MediaManagerItem(QtGui.QWidget): """ Add a file to the list widget to make it available for showing """ - files = FileDialog().getOpenFileNames(self, self.on_new_prompt, + files = FileDialog.getOpenFileNames(self, self.on_new_prompt, Settings().value(self.settings_section + u'/last directory'), self.on_new_file_masks) log.info(u'New files(s) %s', files) if files: diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index a1f761d8f..1f64acbf7 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -374,7 +374,7 @@ class ThemeManager(QtGui.QWidget): Opens a file dialog to select the theme file(s) to import before attempting to extract OpenLP themes from those files. This process will load both OpenLP version 1 and version 2 themes. """ - files = FileDialog().getOpenFileNames(self, + files = FileDialog.getOpenFileNames(self, translate('OpenLP.ThemeManager', 'Select Theme Import File'), Settings().value(self.settings_section + u'/last directory import'), translate('OpenLP.ThemeManager', 'OpenLP Themes (*.theme *.otz)')) diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index f73a4468c..153a96e14 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -753,7 +753,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): Loads file(s) from the filesystem. """ filters = u'%s (*)' % UiStrings().AllFiles - filenames = FileDialog().getOpenFileNames(self, + filenames = FileDialog.getOpenFileNames(self, translate('SongsPlugin.EditSongForm', 'Open File(s)'), u'', filters) for filename in filenames: item = QtGui.QListWidgetItem(os.path.split(unicode(filename))[1]) diff --git a/openlp/plugins/songs/forms/songimportform.py b/openlp/plugins/songs/forms/songimportform.py index bd9acc943..d0378cd45 100644 --- a/openlp/plugins/songs/forms/songimportform.py +++ b/openlp/plugins/songs/forms/songimportform.py @@ -244,7 +244,7 @@ class SongImportForm(OpenLPWizard): if filters: filters += u';;' filters += u'%s (*)' % UiStrings().AllFiles - filenames = FileDialog().getOpenFileNames(self, title, + filenames = FileDialog.getOpenFileNames(self, title, Settings().value(self.plugin.settings_section + u'/last directory import'), filters) if filenames: listbox.addItems(filenames) diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py index dc704eaef..b895cc67d 100644 --- a/tests/functional/openlp_core_lib/test_file_dialog.py +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -66,6 +66,6 @@ class TestFileDialog(TestCase): # called. The returned result should corrilate with the input. self.mocked_os.path.exists.assert_has_calls([call(u'/Valid File'), call(u'/url%20encoded%20file%20%231'), call(u'/url encoded file #1'), call(u'/non-existing'), call(u'/non-existing')]) - self.mocked_qt_gui.QmessageBox.information.called_with(self.mocked_parent, UiStrings().FNFT, - UiStrings().FNF % u'/non-existing') + self.mocked_qt_gui.QmessageBox.information.called_with(self.mocked_parent, UiStrings().FileNotFound, + UiStrings().FileNotFoundMessage % u'/non-existing') self.assertEqual(result, [u'/Valid File', u'/url encoded file #1'], u'The returned file list is incorrect') \ No newline at end of file From 9f5bd87a5052d5d2de29ae0dbe81c69dcc62ac6d Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Sun, 15 Sep 2013 20:27:40 +0100 Subject: [PATCH 4/6] Py3 Fixes --- openlp/core/lib/__init__.py | 38 +++++++++++++++++------------------ openlp/core/lib/filedialog.py | 11 +++++----- openlp/core/lib/uistrings.py | 4 ++-- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index b327b4a1c..b4f6b4223 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -374,22 +374,22 @@ def create_separated_list(stringlist): return translate('OpenLP.core.lib', '%s, %s', u'Locale list separator: start') % (stringlist[0], merged) -from registry import Registry -from uistrings import UiStrings -from filedialog import FileDialog -from screen import ScreenList -from settings import Settings -from listwidgetwithdnd import ListWidgetWithDnD -from treewidgetwithdnd import TreeWidgetWithDnD -from formattingtags import FormattingTags -from spelltextedit import SpellTextEdit -from plugin import PluginStatus, StringContent, Plugin -from pluginmanager import PluginManager -from settingstab import SettingsTab -from serviceitem import ServiceItem, ServiceItemType, ItemCapabilities -from htmlbuilder import build_html, build_lyrics_format_css, build_lyrics_outline_css -from toolbar import OpenLPToolbar -from dockwidget import OpenLPDockWidget -from imagemanager import ImageManager -from renderer import Renderer -from mediamanageritem import MediaManagerItem \ No newline at end of file +from .registry import Registry +from .uistrings import UiStrings +from .filedialog import FileDialog +from .screen import ScreenList +from .settings import Settings +from .listwidgetwithdnd import ListWidgetWithDnD +from .treewidgetwithdnd import TreeWidgetWithDnD +from .formattingtags import FormattingTags +from .spelltextedit import SpellTextEdit +from .plugin import PluginStatus, StringContent, Plugin +from .pluginmanager import PluginManager +from .settingstab import SettingsTab +from .serviceitem import ServiceItem, ServiceItemType, ItemCapabilities +from .htmlbuilder import build_html, build_lyrics_format_css, build_lyrics_outline_css +from .toolbar import OpenLPToolbar +from .dockwidget import OpenLPDockWidget +from .imagemanager import ImageManager +from .renderer import Renderer +from .mediamanageritem import MediaManagerItem \ No newline at end of file diff --git a/openlp/core/lib/filedialog.py b/openlp/core/lib/filedialog.py index ea2eba045..03a8b8c47 100644 --- a/openlp/core/lib/filedialog.py +++ b/openlp/core/lib/filedialog.py @@ -32,7 +32,7 @@ Provide a work around for a bug in QFileDialog Date: Sun, 15 Sep 2013 20:42:29 +0100 Subject: [PATCH 5/6] more Py3 fixes --- openlp/core/lib/__init__.py | 6 ++--- .../openlp_core_lib/test_file_dialog.py | 26 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index b4f6b4223..3700e2162 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -370,8 +370,8 @@ def create_separated_list(stringlist): 'Locale list separator: end') % (stringlist[-2], stringlist[-1]) for index in reversed(list(range(1, len(stringlist) - 2))): merged = translate('OpenLP.core.lib', '%s, %s', - u'Locale list separator: middle') % (stringlist[index], merged) - return translate('OpenLP.core.lib', '%s, %s', u'Locale list separator: start') % (stringlist[0], merged) + 'Locale list separator: middle') % (stringlist[index], merged) + return translate('OpenLP.core.lib', '%s, %s', 'Locale list separator: start') % (stringlist[0], merged) from .registry import Registry @@ -392,4 +392,4 @@ from .toolbar import OpenLPToolbar from .dockwidget import OpenLPDockWidget from .imagemanager import ImageManager from .renderer import Renderer -from .mediamanageritem import MediaManagerItem \ No newline at end of file +from .mediamanageritem import MediaManagerItem diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py index b895cc67d..0a5736ff0 100644 --- a/tests/functional/openlp_core_lib/test_file_dialog.py +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -1,5 +1,5 @@ """ -Package to test the openlp.core.lib.formattingtags package. +Package to test the openlp.core.lib.filedialog package. """ import copy from unittest import TestCase @@ -13,9 +13,9 @@ class TestFileDialog(TestCase): Test the functions in the :mod:`filedialog` module. """ def setUp(self): - self.os_patcher = patch(u'openlp.core.lib.filedialog.os') - self.qt_gui_patcher = patch(u'openlp.core.lib.filedialog.QtGui') - self.ui_strings_patcher = patch(u'openlp.core.lib.filedialog.UiStrings') + self.os_patcher = patch('openlp.core.lib.filedialog.os') + self.qt_gui_patcher = patch('openlp.core.lib.filedialog.QtGui') + self.ui_strings_patcher = patch('openlp.core.lib.filedialog.UiStrings') self.mocked_os = self.os_patcher.start() self.mocked_qt_gui = self.qt_gui_patcher.start() self.mocked_ui_strings = self.ui_strings_patcher.start() @@ -39,10 +39,10 @@ class TestFileDialog(TestCase): # WHEN: FileDialog.getOpenFileNames is called result = FileDialog.getOpenFileNames(self.mocked_parent) - # THEN: The returned value should be an empty QStiingList and os.path.exists should not have been called + # THEN: The returned value should be an empty QStringList and os.path.exists should not have been called assert not self.mocked_os.path.exists.called self.assertEqual(result, [], - u'FileDialog.getOpenFileNames should return and empty list when QFileDialog.getOpenFileNames is canceled') + 'FileDialog.getOpenFileNames should return and empty list when QFileDialog.getOpenFileNames is canceled') def returned_file_list_test(self): """ @@ -52,20 +52,20 @@ class TestFileDialog(TestCase): self.mocked_os.rest() self.mocked_qt_gui.reset() - # GIVEN: A List of known values as a return value from QFileDialog.getOpenFileNamesand a list of valid + # GIVEN: A List of known values as a return value from QFileDialog.getOpenFileNames and a list of valid # file names. self.mocked_qt_gui.QFileDialog.getOpenFileNames.return_value = [ - u'/Valid File', u'/url%20encoded%20file%20%231', u'/non-existing'] + '/Valid File', '/url%20encoded%20file%20%231', '/non-existing'] self.mocked_os.path.exists.side_effect = lambda file_name: file_name in [ - u'/Valid File', u'/url encoded file #1'] + '/Valid File', '/url encoded file #1'] # WHEN: FileDialog.getOpenFileNames is called result = FileDialog.getOpenFileNames(self.mocked_parent) # THEN: os.path.exists should have been called with known args. QmessageBox.information should have been # called. The returned result should corrilate with the input. - self.mocked_os.path.exists.assert_has_calls([call(u'/Valid File'), call(u'/url%20encoded%20file%20%231'), - call(u'/url encoded file #1'), call(u'/non-existing'), call(u'/non-existing')]) + self.mocked_os.path.exists.assert_has_calls([call('/Valid File'), call('/url%20encoded%20file%20%231'), + call('/url encoded file #1'), call('/non-existing'), call('/non-existing')]) self.mocked_qt_gui.QmessageBox.information.called_with(self.mocked_parent, UiStrings().FileNotFound, - UiStrings().FileNotFoundMessage % u'/non-existing') - self.assertEqual(result, [u'/Valid File', u'/url encoded file #1'], u'The returned file list is incorrect') \ No newline at end of file + UiStrings().FileNotFoundMessage % '/non-existing') + self.assertEqual(result, ['/Valid File', '/url encoded file #1'], 'The returned file list is incorrect') \ No newline at end of file From 81c2f1a4b3bd069ce69d0a1d1e98492259585807 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Sat, 16 Nov 2013 21:04:16 +0000 Subject: [PATCH 6/6] updated tests --- openlp/core/lib/filedialog.py | 2 +- tests/functional/openlp_core_lib/test_file_dialog.py | 12 +++++++----- tests/functional/openlp_core_lib/test_htmlbuilder.py | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/openlp/core/lib/filedialog.py b/openlp/core/lib/filedialog.py index 03a8b8c47..bac1b5ce2 100644 --- a/openlp/core/lib/filedialog.py +++ b/openlp/core/lib/filedialog.py @@ -36,7 +36,7 @@ from urllib import parse from PyQt4 import QtGui -from openlp.core.lib import UiStrings +from openlp.core.common import UiStrings log = logging.getLogger(__name__) diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py index 0a5736ff0..f42a865d7 100644 --- a/tests/functional/openlp_core_lib/test_file_dialog.py +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -1,12 +1,11 @@ """ Package to test the openlp.core.lib.filedialog package. """ -import copy from unittest import TestCase -from mock import MagicMock, call, patch +from openlp.core.common import UiStrings from openlp.core.lib.filedialog import FileDialog -from openlp.core.lib.uistrings import UiStrings +from tests.functional import MagicMock, patch class TestFileDialog(TestCase): """ @@ -64,8 +63,11 @@ class TestFileDialog(TestCase): # THEN: os.path.exists should have been called with known args. QmessageBox.information should have been # called. The returned result should corrilate with the input. - self.mocked_os.path.exists.assert_has_calls([call('/Valid File'), call('/url%20encoded%20file%20%231'), - call('/url encoded file #1'), call('/non-existing'), call('/non-existing')]) + self.mocked_os.path.exists.assert_callde_with('/Valid File') + self.mocked_os.path.exists.assert_callde_with('/url%20encoded%20file%20%231') + self.mocked_os.path.exists.assert_callde_with('/url encoded file #1') + self.mocked_os.path.exists.assert_callde_with('/non-existing') + self.mocked_os.path.exists.assert_callde_with('/non-existing') self.mocked_qt_gui.QmessageBox.information.called_with(self.mocked_parent, UiStrings().FileNotFound, UiStrings().FileNotFoundMessage % '/non-existing') self.assertEqual(result, ['/Valid File', '/url encoded file #1'], 'The returned file list is incorrect') \ No newline at end of file diff --git a/tests/functional/openlp_core_lib/test_htmlbuilder.py b/tests/functional/openlp_core_lib/test_htmlbuilder.py index 0ffab5458..fafa277d7 100644 --- a/tests/functional/openlp_core_lib/test_htmlbuilder.py +++ b/tests/functional/openlp_core_lib/test_htmlbuilder.py @@ -3,13 +3,13 @@ Package to test the openlp.core.lib.htmlbuilder module. """ from unittest import TestCase -from mock import MagicMock, patch from PyQt4 import QtCore from openlp.core.lib.htmlbuilder import build_html, build_background_css, build_lyrics_css, build_lyrics_outline_css, \ build_lyrics_format_css, build_footer_css from openlp.core.lib.theme import HorizontalType, VerticalType +from tests.functional import MagicMock, patch HTML = """