From 22541e729785ed93d15115404ae6b0c194d5edcf Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Wed, 24 Apr 2013 21:05:34 +0200 Subject: [PATCH 1/6] use true division (py3) --- openlp/core/lib/__init__.py | 9 +++++---- openlp/core/lib/dockwidget.py | 3 ++- openlp/core/lib/htmlbuilder.py | 4 ++-- openlp/core/lib/renderer.py | 12 ++++++------ openlp/core/lib/screen.py | 5 +++-- openlp/core/lib/searchedit.py | 6 +++--- openlp/core/ui/maindisplay.py | 7 ++++--- openlp/core/ui/themestab.py | 4 +++- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 155c10141..14d67e914 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -30,6 +30,7 @@ The :mod:`lib` module contains most of the components and libraries that make OpenLP work. """ +from __future__ import division from distutils.version import LooseVersion import logging import os @@ -207,7 +208,7 @@ def create_thumb(image_path, thumb_path, return_icon=True, size=None): ext = os.path.splitext(thumb_path)[1].lower() reader = QtGui.QImageReader(image_path) if size is None: - ratio = float(reader.size().width()) / float(reader.size().height()) + ratio = reader.size().width() / reader.size().height() reader.setScaledSize(QtCore.QSize(int(ratio * 88), 88)) else: reader.setScaledSize(size) @@ -260,8 +261,8 @@ def resize_image(image_path, width, height, background=u'#000000'): log.debug(u'resize_image - start') reader = QtGui.QImageReader(image_path) # The image's ratio. - image_ratio = float(reader.size().width()) / float(reader.size().height()) - resize_ratio = float(width) / float(height) + image_ratio = reader.size().width() / reader.size().height() + resize_ratio = width / height # Figure out the size we want to resize the image to (keep aspect ratio). if image_ratio == resize_ratio: size = QtCore.QSize(width, height) @@ -282,7 +283,7 @@ def resize_image(image_path, width, height, background=u'#000000'): new_image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied) painter = QtGui.QPainter(new_image) painter.fillRect(new_image.rect(), QtGui.QColor(background)) - painter.drawImage((width - real_width) / 2, (height - real_height) / 2, preview) + painter.drawImage((width - real_width) // 2, (height - real_height) // 2, preview) return new_image diff --git a/openlp/core/lib/dockwidget.py b/openlp/core/lib/dockwidget.py index 30182c901..15c116e0f 100644 --- a/openlp/core/lib/dockwidget.py +++ b/openlp/core/lib/dockwidget.py @@ -30,6 +30,7 @@ """ Provide additional functionality required by OpenLP from the inherited QDockWidget. """ +from __future__ import division import logging from PyQt4 import QtGui @@ -55,7 +56,7 @@ class OpenLPDockWidget(QtGui.QDockWidget): self.setWindowIcon(build_icon(icon)) # Sort out the minimum width. screens = ScreenList() - main_window_docbars = screens.current[u'size'].width() / 5 + main_window_docbars = screens.current[u'size'].width() // 5 if main_window_docbars > 300: self.setMinimumWidth(300) else: diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index 9c8b04076..d4e22b0dd 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -26,7 +26,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### - +from __future__ import division import logging from PyQt4 import QtWebKit @@ -276,7 +276,7 @@ def build_background_css(item, width): ``item`` Service Item containing theme and location information """ - width = int(width) / 2 + width = int(width) // 2 theme = item.themedata background = u'background-color: black' if theme: diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index c426e8871..5161246c0 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -26,7 +26,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### - +from __future__ import division import logging from PyQt4 import QtGui, QtCore, QtWebKit @@ -327,7 +327,7 @@ class Renderer(object): screen_size = self.screens.current[u'size'] self.width = screen_size.width() self.height = screen_size.height() - self.screen_ratio = float(self.height) / float(self.width) + self.screen_ratio = self.height / self.width log.debug(u'_calculate default %s, %f' % (screen_size, self.screen_ratio)) # 90% is start of footer self.footer_start = int(self.height * 0.90) @@ -546,15 +546,15 @@ class Renderer(object): """ smallest_index = 0 highest_index = len(html_list) - 1 - index = int(highest_index / 2) + index = highest_index // 2 while True: if not self._text_fits_on_slide(previous_html + separator.join(html_list[:index + 1]).strip()): # We know that it does not fit, so change/calculate the new index and highest_index accordingly. highest_index = index - index = int(index - (index - smallest_index) / 2) + index = index - (index - smallest_index) // 2 else: smallest_index = index - index = int(index + (highest_index - index) / 2) + index = index + (highest_index - index) // 2 # We found the number of words which will fit. if smallest_index == index or highest_index == index: index = smallest_index @@ -582,7 +582,7 @@ class Renderer(object): html_list[0] = html_tags + html_list[0] smallest_index = 0 highest_index = len(html_list) - 1 - index = int(highest_index / 2) + index = highest_index // 2 return previous_html, previous_raw def _text_fits_on_slide(self, text): diff --git a/openlp/core/lib/screen.py b/openlp/core/lib/screen.py index 84e7e4258..146b492db 100644 --- a/openlp/core/lib/screen.py +++ b/openlp/core/lib/screen.py @@ -30,6 +30,7 @@ The :mod:`screen` module provides management functionality for a machines' displays. """ +from __future__ import division import logging import copy @@ -232,8 +233,8 @@ class ScreenList(object): ``window`` A QWidget we are finding the location of. """ - x = window.x() + (window.width() / 2) - y = window.y() + (window.height() / 2) + x = window.x() + (window.width() // 2) + y = window.y() + (window.height() // 2) for screen in self.screen_list: size = screen[u'size'] if x >= size.x() and x <= (size.x() + size.width()) and y >= size.y() and y <= (size.y() + size.height()): diff --git a/openlp/core/lib/searchedit.py b/openlp/core/lib/searchedit.py index 9623b0f53..a0c51cb74 100644 --- a/openlp/core/lib/searchedit.py +++ b/openlp/core/lib/searchedit.py @@ -26,7 +26,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### - +from __future__ import division import logging from PyQt4 import QtCore, QtGui @@ -85,10 +85,10 @@ class SearchEdit(QtGui.QLineEdit): size = self.clear_button.size() frame_width = self.style().pixelMetric(QtGui.QStyle.PM_DefaultFrameWidth) self.clear_button.move(self.rect().right() - frame_width - size.width(), - (self.rect().bottom() + 1 - size.height()) / 2) + (self.rect().bottom() + 1 - size.height()) // 2) if hasattr(self, u'menu_button'): size = self.menu_button.size() - self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) / 2) + self.menu_button.move(self.rect().left() + frame_width + 2, (self.rect().bottom() + 1 - size.height()) // 2) def current_search_type(self): """ diff --git a/openlp/core/ui/maindisplay.py b/openlp/core/ui/maindisplay.py index 2504520c0..02ae469d6 100644 --- a/openlp/core/ui/maindisplay.py +++ b/openlp/core/ui/maindisplay.py @@ -35,6 +35,7 @@ Some of the code for this form is based on the examples at: * `http://html5demos.com/two-videos`_ """ +from __future__ import division import cgi import logging import sys @@ -207,8 +208,8 @@ class MainDisplay(Display): painter_image.begin(self.initial_fame) painter_image.fillRect(self.initial_fame.rect(), background_color) painter_image.drawImage( - (self.screen[u'size'].width() - splash_image.width()) / 2, - (self.screen[u'size'].height() - splash_image.height()) / 2, + (self.screen[u'size'].width() - splash_image.width()) // 2, + (self.screen[u'size'].height() - splash_image.height()) // 2, splash_image) service_item = ServiceItem() service_item.bg_image_bytes = image_to_byte(self.initial_fame) @@ -268,7 +269,7 @@ class MainDisplay(Display): self.resize(self.width(), alert_height) self.setVisible(True) if location == AlertLocation.Middle: - self.move(self.screen[u'size'].left(), (self.screen[u'size'].height() - alert_height) / 2) + self.move(self.screen[u'size'].left(), (self.screen[u'size'].height() - alert_height) // 2) elif location == AlertLocation.Bottom: self.move(self.screen[u'size'].left(), self.screen[u'size'].height() - alert_height) else: diff --git a/openlp/core/ui/themestab.py b/openlp/core/ui/themestab.py index f0f821494..d6dba2880 100644 --- a/openlp/core/ui/themestab.py +++ b/openlp/core/ui/themestab.py @@ -29,6 +29,8 @@ """ The Themes configuration tab """ +from __future__ import division + from PyQt4 import QtCore, QtGui from openlp.core.lib import Registry, Settings, SettingsTab, UiStrings, translate @@ -90,7 +92,7 @@ class ThemesTab(SettingsTab): self.global_level_label.setObjectName(u'global_level_label') self.level_layout.addRow(self.global_level_radio_button, self.global_level_label) label_top_margin = (self.song_level_radio_button.sizeHint().height() - - self.song_level_label.sizeHint().height()) / 2 + self.song_level_label.sizeHint().height()) // 2 for label in [self.song_level_label, self.service_level_label, self.global_level_label]: rect = label.rect() rect.setTop(rect.top() + label_top_margin) From 9d54944ad43643e6826e2712ead113a2fccef864 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Wed, 24 Apr 2013 22:47:30 +0200 Subject: [PATCH 2/6] use true division (py3) --- openlp/core/lib/settingstab.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openlp/core/lib/settingstab.py b/openlp/core/lib/settingstab.py index d63f9c678..ab775599a 100644 --- a/openlp/core/lib/settingstab.py +++ b/openlp/core/lib/settingstab.py @@ -30,6 +30,7 @@ The :mod:`~openlp.core.lib.settingstab` module contains the base SettingsTab class which plugins use for adding their own tab to the settings dialog. """ +from __future__ import division from PyQt4 import QtGui @@ -90,7 +91,7 @@ class SettingsTab(QtGui.QWidget): QtGui.QWidget.resizeEvent(self, event) width = self.width() - self.tab_layout.spacing() - \ self.tab_layout.contentsMargins().left() - self.tab_layout.contentsMargins().right() - left_width = min(width - self.right_column.minimumSizeHint().width(), width / 2) + left_width = min(width - self.right_column.minimumSizeHint().width(), width // 2) left_width = max(left_width, self.left_column.minimumSizeHint().width()) self.left_column.setFixedWidth(left_width) From 75c8382188a0ff39d1ac516d4d14b0df256ec3c6 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sun, 12 May 2013 14:02:48 +0200 Subject: [PATCH 3/6] added test --- tests/functional/openlp_core_lib/test_settings.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/functional/openlp_core_lib/test_settings.py b/tests/functional/openlp_core_lib/test_settings.py index 786a884a0..754d82256 100644 --- a/tests/functional/openlp_core_lib/test_settings.py +++ b/tests/functional/openlp_core_lib/test_settings.py @@ -30,6 +30,19 @@ class TestSettings(TestCase): os.unlink(self.ini_file) os.unlink(Settings().fileName()) + def extend_default_settings_test(self): + """ + Test the static extend_default_settings() method. + """ + with self.assertRaises(KeyError) as context: + Settings().value(u'core/does not exist') + self.assertEqual(context.exception[0], u'core/does not exist') + + Settings.extend_default_settings({u'core/does exist': True}) + value = Settings().value(u'core/does exist') + assert value and isinstance(value, bool) + + def settings_basic_test(self): """ Test the Settings creation and its default usage From d03a9a8eeca21eae468a177b6c5339bf9f780c56 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sun, 12 May 2013 14:04:15 +0200 Subject: [PATCH 4/6] added comments --- tests/functional/openlp_core_lib/test_settings.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/functional/openlp_core_lib/test_settings.py b/tests/functional/openlp_core_lib/test_settings.py index 754d82256..714ee6fb5 100644 --- a/tests/functional/openlp_core_lib/test_settings.py +++ b/tests/functional/openlp_core_lib/test_settings.py @@ -32,14 +32,24 @@ class TestSettings(TestCase): def extend_default_settings_test(self): """ - Test the static extend_default_settings() method. + Test the static extend_default_settings() method """ + # GIVEN: + + # WHEN: Try to access not existing setting. with self.assertRaises(KeyError) as context: Settings().value(u'core/does not exist') + + # THEN: An exception should be raised. self.assertEqual(context.exception[0], u'core/does not exist') + # GIVEN: Extended setting. Settings.extend_default_settings({u'core/does exist': True}) + + # WHEN: Try to access it. value = Settings().value(u'core/does exist') + + # THEN: The correct value should be returned. assert value and isinstance(value, bool) From 7a42cd3769ac06eb55cf956ccb34cb795f84b703 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 18 May 2013 10:44:03 +0200 Subject: [PATCH 5/6] added test --- openlp/core/lib/__init__.py | 3 +- tests/functional/openlp_core_lib/test_lib.py | 61 ++++++++++++++++---- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 911ba4703..d6c338271 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -203,7 +203,8 @@ def create_thumb(image_path, thumb_path, return_icon=True, size=None): States if an icon should be build and returned from the thumb. Defaults to ``True``. ``size`` - Allows to state a own size to use. Defaults to ``None``, which means that a default height of 88 is used. + Allows to state a own size (QtCore.QSize) to use. Defaults to ``None``, which means that a default height of 88 + is used. """ ext = os.path.splitext(thumb_path)[1].lower() reader = QtGui.QImageReader(image_path) diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index c03a11265..b0d1a4bed 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -1,13 +1,20 @@ """ Package to test the openlp.core.lib package. """ +import os + from unittest import TestCase from datetime import datetime, timedelta from mock import MagicMock, patch +from PyQt4 import QtCore, QtGui + +from openlp.core.lib import str_to_bool, create_thumb, translate, check_directory_exists, get_text_file_string, \ + build_icon, image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags + + +TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources')) -from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon, \ - image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags class TestLib(TestCase): @@ -125,7 +132,7 @@ class TestLib(TestCase): Test the check_directory_exists() function """ with patch(u'openlp.core.lib.os.path.exists') as mocked_exists, \ - patch(u'openlp.core.lib.os.makedirs') as mocked_makedirs: + patch(u'openlp.core.lib.os.makedirs') as mocked_makedirs: # GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists directory_to_check = u'existing/directory' @@ -219,7 +226,7 @@ class TestLib(TestCase): Test the build_icon() function with a resource URI """ with patch(u'openlp.core.lib.QtGui') as MockedQtGui, \ - patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap: + patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap: # GIVEN: A mocked QIcon and a mocked QPixmap MockedQtGui.QIcon = MagicMock MockedQtGui.QIcon.Normal = 1 @@ -261,9 +268,43 @@ class TestLib(TestCase): mocked_byte_array.toBase64.assert_called_with() assert result == u'base64mock', u'The result should be the return value of the mocked out base64 method' + def create_thumb_with_size_test(self): + """ + Test the create_thumb() function + """ + # GIVEN: An image to create a thumb of. + image_path = os.path.join(TEST_PATH, u'church.jpg') + thumb_path = os.path.join(TEST_PATH, u'church_thumb.jpg') + thumb_size = QtCore.QSize(10, 20) + + # Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the + # last test. + try: + os.remove(thumb_path) + except: + pass + + # Only continue when the thumb does not exist. + assert not os.path.exists(thumb_path), u'Test was not ran, because the thumb already exists.' + + # WHEN: Create the thumb. + icon = create_thumb(image_path, thumb_path, size=thumb_size) + + # THEN: Check if the thumb was created. + assert os.path.exists(thumb_path), u'Test was not ran, because the thumb already exists.' + assert isinstance(icon, QtGui.QIcon), u'The icon should be a QIcon.' + assert not icon.isNull(), u'The icon should not be null.' + assert QtGui.QImageReader(thumb_path).size() == thumb_size, u'The thumb should have the given size.' + + # Remove the thumb so that the test actually tests if the thumb will be created. + try: + os.remove(thumb_path) + except: + pass + def check_item_selected_true_test(self): """ - Test that the check_item_selected() function returns True when there are selected indexes. + Test that the check_item_selected() function returns True when there are selected indexes """ # GIVEN: A mocked out QtGui module and a list widget with selected indexes MockedQtGui = patch(u'openlp.core.lib.QtGui') @@ -423,7 +464,7 @@ class TestLib(TestCase): def create_separated_list_qlocate_test(self): """ - Test the create_separated_list function using the Qt provided method. + Test the create_separated_list function using the Qt provided method """ with patch(u'openlp.core.lib.Qt') as mocked_qt, \ patch(u'openlp.core.lib.QtCore.QLocale.createSeparatedList') as mocked_createSeparatedList: @@ -442,7 +483,7 @@ class TestLib(TestCase): def create_separated_list_empty_list_test(self): """ - Test the create_separated_list function with an empty list. + Test the create_separated_list function with an empty list """ with patch(u'openlp.core.lib.Qt') as mocked_qt: # GIVEN: An empty list and the mocked Qt module. @@ -458,7 +499,7 @@ class TestLib(TestCase): def create_separated_list_with_one_item_test(self): """ - Test the create_separated_list function with a list consisting of only one entry. + Test the create_separated_list function with a list consisting of only one entry """ with patch(u'openlp.core.lib.Qt') as mocked_qt: # GIVEN: A list with a string and the mocked Qt module. @@ -474,7 +515,7 @@ class TestLib(TestCase): def create_separated_list_with_two_items_test(self): """ - Test the create_separated_list function with a list of two entries. + Test the create_separated_list function with a list of two entries """ with patch(u'openlp.core.lib.Qt') as mocked_qt, patch(u'openlp.core.lib.translate') as mocked_translate: # GIVEN: A list of strings and the mocked Qt module. @@ -491,7 +532,7 @@ class TestLib(TestCase): def create_separated_list_with_three_items_test(self): """ - Test the create_separated_list function with a list of three items. + Test the create_separated_list function with a list of three items """ with patch(u'openlp.core.lib.Qt') as mocked_qt, patch(u'openlp.core.lib.translate') as mocked_translate: # GIVEN: A list with a string and the mocked Qt module. From 6e66edce03060b6c2f5823c7fdea3577b5c78229 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Sat, 18 May 2013 10:49:21 +0200 Subject: [PATCH 6/6] removed test --- .../openlp_core_lib/test_settings.py | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_settings.py b/tests/functional/openlp_core_lib/test_settings.py index 714ee6fb5..786a884a0 100644 --- a/tests/functional/openlp_core_lib/test_settings.py +++ b/tests/functional/openlp_core_lib/test_settings.py @@ -30,29 +30,6 @@ class TestSettings(TestCase): os.unlink(self.ini_file) os.unlink(Settings().fileName()) - def extend_default_settings_test(self): - """ - Test the static extend_default_settings() method - """ - # GIVEN: - - # WHEN: Try to access not existing setting. - with self.assertRaises(KeyError) as context: - Settings().value(u'core/does not exist') - - # THEN: An exception should be raised. - self.assertEqual(context.exception[0], u'core/does not exist') - - # GIVEN: Extended setting. - Settings.extend_default_settings({u'core/does exist': True}) - - # WHEN: Try to access it. - value = Settings().value(u'core/does exist') - - # THEN: The correct value should be returned. - assert value and isinstance(value, bool) - - def settings_basic_test(self): """ Test the Settings creation and its default usage