From 05f4f2d72e2fad9e63926d59762b97e9cc3f152e Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 30 Jun 2014 21:54:11 +0100 Subject: [PATCH 1/6] fixed tests --- tests/functional/openlp_core_lib/test_file_dialog.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py index ab7663a83..875e422da 100644 --- a/tests/functional/openlp_core_lib/test_file_dialog.py +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -65,11 +65,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 correlate with the input. - 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_os.path.exists.assert_any_call('/Valid File') + self.mocked_os.path.exists.assert_any_call('/url%20encoded%20file%20%231') + self.mocked_os.path.exists.assert_any_call('/url encoded file #1') + self.mocked_os.path.exists.assert_any_call('/non-existing') + self.mocked_os.path.exists.assert_any_call('/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') From 9b6979b840f393a3f31f30213260147a68397c60 Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 30 Jun 2014 21:59:22 +0100 Subject: [PATCH 2/6] Changed log.warn to log.warning --- openlp/core/lib/toolbar.py | 2 +- openlp/core/lib/ui.py | 6 +++--- openlp/core/ui/media/mediacontroller.py | 2 +- openlp/core/utils/actions.py | 4 ++-- .../plugins/bibles/forms/bibleupgradeform.py | 12 ++++++------ openlp/plugins/bibles/lib/http.py | 4 ++-- openlp/plugins/bibles/lib/opensong.py | 2 +- .../presentations/lib/impresscontroller.py | 18 +++++++++--------- .../presentations/lib/messagelistener.py | 4 ++-- .../presentations/presentationplugin.py | 4 ++-- openlp/plugins/songs/lib/songshowplusimport.py | 2 +- .../songs/lib/worshipcenterproimport.py | 2 +- 12 files changed, 31 insertions(+), 31 deletions(-) diff --git a/openlp/core/lib/toolbar.py b/openlp/core/lib/toolbar.py index b1cc7b249..b24be89a8 100644 --- a/openlp/core/lib/toolbar.py +++ b/openlp/core/lib/toolbar.py @@ -81,4 +81,4 @@ class OpenLPToolbar(QtGui.QToolBar): if handle in self.actions: self.actions[handle].setVisible(visible) else: - log.warn('No handle "%s" in actions list.', str(handle)) + log.warning('No handle "%s" in actions list.', str(handle)) diff --git a/openlp/core/lib/ui.py b/openlp/core/lib/ui.py index a1e37abcf..cbc35e28d 100644 --- a/openlp/core/lib/ui.py +++ b/openlp/core/lib/ui.py @@ -172,7 +172,7 @@ def create_button(parent, name, **kwargs): kwargs.setdefault('icon', ':/services/service_down.png') kwargs.setdefault('tooltip', translate('OpenLP.Ui', 'Move selection down one position.')) else: - log.warn('The role "%s" is not defined in create_push_button().', role) + log.warning('The role "%s" is not defined in create_push_button().', role) if kwargs.pop('btn_class', '') == 'toolbutton': button = QtGui.QToolButton(parent) else: @@ -190,7 +190,7 @@ def create_button(parent, name, **kwargs): button.clicked.connect(kwargs.pop('click')) for key in list(kwargs.keys()): if key not in ['text', 'icon', 'tooltip', 'click']: - log.warn('Parameter %s was not consumed in create_button().', key) + log.warning('Parameter %s was not consumed in create_button().', key) return button @@ -275,7 +275,7 @@ def create_action(parent, name, **kwargs): action.triggered.connect(kwargs.pop('triggers')) for key in list(kwargs.keys()): if key not in ['text', 'icon', 'tooltip', 'statustip', 'checked', 'can_shortcuts', 'category', 'triggers']: - log.warn('Parameter %s was not consumed in create_action().' % key) + log.warning('Parameter %s was not consumed in create_action().' % key) return action diff --git a/openlp/core/ui/media/mediacontroller.py b/openlp/core/ui/media/mediacontroller.py index 596b618cb..f086b80b2 100644 --- a/openlp/core/ui/media/mediacontroller.py +++ b/openlp/core/ui/media/mediacontroller.py @@ -175,7 +175,7 @@ class MediaController(RegistryMixin, OpenLPMixin, RegistryProperties): # On some platforms importing vlc.py might cause # also OSError exceptions. (e.g. Mac OS X) except (ImportError, OSError): - log.warn('Failed to import %s on path %s', module_name, path) + log.warning('Failed to import %s on path %s', module_name, path) player_classes = MediaPlayer.__subclasses__() for player_class in player_classes: player = player_class(self) diff --git a/openlp/core/utils/actions.py b/openlp/core/utils/actions.py index d81e16b2e..fb794866b 100644 --- a/openlp/core/utils/actions.py +++ b/openlp/core/utils/actions.py @@ -279,7 +279,7 @@ class ActionList(object): actions.append(action) ActionList.shortcut_map[shortcuts[1]] = actions else: - log.warn('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % + log.warning('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % (shortcuts[1], action.objectName())) shortcuts.remove(shortcuts[1]) # Check the primary shortcut. @@ -290,7 +290,7 @@ class ActionList(object): actions.append(action) ActionList.shortcut_map[shortcuts[0]] = actions else: - log.warn('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % + log.warning('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % (shortcuts[0], action.objectName())) shortcuts.remove(shortcuts[0]) action.setShortcuts([QtGui.QKeySequence(shortcut) for shortcut in shortcuts]) diff --git a/openlp/plugins/bibles/forms/bibleupgradeform.py b/openlp/plugins/bibles/forms/bibleupgradeform.py index 09c0942b7..2b0b57695 100644 --- a/openlp/plugins/bibles/forms/bibleupgradeform.py +++ b/openlp/plugins/bibles/forms/bibleupgradeform.py @@ -423,7 +423,7 @@ class BibleUpgradeForm(OpenLPWizard): else: language_id = self.new_bibles[number].get_language(name) if not language_id: - log.warn('Upgrading from "%s" failed' % filename[0]) + log.warning('Upgrading from "%s" failed' % filename[0]) self.new_bibles[number].session.close() del self.new_bibles[number] self.increment_progress_bar( @@ -444,7 +444,7 @@ class BibleUpgradeForm(OpenLPWizard): book_ref_id = self.new_bibles[number].\ get_book_ref_id_by_name(book, len(books), language_id) if not book_ref_id: - log.warn('Upgrading books from %s - download name: "%s" aborted by user' % ( + log.warning('Upgrading books from %s - download name: "%s" aborted by user' % ( meta_data['download_source'], meta_data['download_name'])) self.new_bibles[number].session.close() del self.new_bibles[number] @@ -457,7 +457,7 @@ class BibleUpgradeForm(OpenLPWizard): if oldbook: verses = old_bible.get_verses(oldbook['id']) if not verses: - log.warn('No verses found to import for book "%s"', book) + log.warning('No verses found to import for book "%s"', book) continue for verse in verses: if self.stop_import_flag: @@ -472,7 +472,7 @@ class BibleUpgradeForm(OpenLPWizard): if not language_id: language_id = self.new_bibles[number].get_language(name) if not language_id: - log.warn('Upgrading books from "%s" failed' % name) + log.warning('Upgrading books from "%s" failed' % name) self.new_bibles[number].session.close() del self.new_bibles[number] self.increment_progress_bar( @@ -493,7 +493,7 @@ class BibleUpgradeForm(OpenLPWizard): (number + 1, max_bibles, name, book['name'])) book_ref_id = self.new_bibles[number].get_book_ref_id_by_name(book['name'], len(books), language_id) if not book_ref_id: - log.warn('Upgrading books from %s " failed - aborted by user' % name) + log.warning('Upgrading books from %s " failed - aborted by user' % name) self.new_bibles[number].session.close() del self.new_bibles[number] self.success[number] = False @@ -503,7 +503,7 @@ class BibleUpgradeForm(OpenLPWizard): book_details['testament_id']) verses = old_bible.get_verses(book['id']) if not verses: - log.warn('No verses found to import for book "%s"', book['name']) + log.warning('No verses found to import for book "%s"', book['name']) self.new_bibles[number].delete_book(db_book) continue for verse in verses: diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index 64b024639..8be0dbb5f 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -165,7 +165,7 @@ class BGExtract(RegistryProperties): if len(verse_parts) > 1: verse = int(verse_parts[0]) except TypeError: - log.warn('Illegal verse number: %s', str(verse)) + log.warning('Illegal verse number: %s', str(verse)) verses.append((verse, text)) verse_list = {} for verse, text in verses[::-1]: @@ -198,7 +198,7 @@ class BGExtract(RegistryProperties): if len(verse_parts) > 1: clean_verse_num = int(verse_parts[0]) except TypeError: - log.warn('Illegal verse number: %s', str(raw_verse_num)) + log.warning('Illegal verse number: %s', str(raw_verse_num)) if clean_verse_num: verse_text = raw_verse_num.next_element part = raw_verse_num.next_element.next_element diff --git a/openlp/plugins/bibles/lib/opensong.py b/openlp/plugins/bibles/lib/opensong.py index fa8323d7f..ecd1f718e 100644 --- a/openlp/plugins/bibles/lib/opensong.py +++ b/openlp/plugins/bibles/lib/opensong.py @@ -123,7 +123,7 @@ class OpenSongBible(BibleDB): if len(verse_parts) > 1: number = int(verse_parts[0]) except TypeError: - log.warn('Illegal verse number: %s', str(verse.attrib['n'])) + log.warning('Illegal verse number: %s', str(verse.attrib['n'])) verse_number = number else: verse_number += 1 diff --git a/openlp/plugins/presentations/lib/impresscontroller.py b/openlp/plugins/presentations/lib/impresscontroller.py index 584c1401f..1d5e111c9 100644 --- a/openlp/plugins/presentations/lib/impresscontroller.py +++ b/openlp/plugins/presentations/lib/impresscontroller.py @@ -129,7 +129,7 @@ class ImpressController(PresentationController): try: uno_instance = get_uno_instance(resolver) except: - log.warn('Unable to find running instance ') + log.warning('Unable to find running instance ') self.start_process() loop += 1 try: @@ -138,7 +138,7 @@ class ImpressController(PresentationController): desktop = self.manager.createInstanceWithContext("com.sun.star.frame.Desktop", uno_instance) return desktop except: - log.warn('Failed to get UNO desktop') + log.warning('Failed to get UNO desktop') return None def get_com_desktop(self): @@ -152,7 +152,7 @@ class ImpressController(PresentationController): try: desktop = self.manager.createInstance('com.sun.star.frame.Desktop') except (AttributeError, pywintypes.com_error): - log.warn('Failure to find desktop - Impress may have closed') + log.warning('Failure to find desktop - Impress may have closed') return desktop if desktop else None def get_com_servicemanager(self): @@ -163,7 +163,7 @@ class ImpressController(PresentationController): try: return Dispatch('com.sun.star.ServiceManager') except pywintypes.com_error: - log.warn('Failed to get COM service manager. Impress Controller has been disabled') + log.warning('Failed to get COM service manager. Impress Controller has been disabled') return None def kill(self): @@ -180,7 +180,7 @@ class ImpressController(PresentationController): else: desktop = self.get_com_desktop() except: - log.warn('Failed to find an OpenOffice desktop to terminate') + log.warning('Failed to find an OpenOffice desktop to terminate') if not desktop: return docs = desktop.getComponents() @@ -198,7 +198,7 @@ class ImpressController(PresentationController): desktop.terminate() log.debug('OpenOffice killed') except: - log.warn('Failed to terminate OpenOffice') + log.warning('Failed to terminate OpenOffice') class ImpressDocument(PresentationDocument): @@ -244,7 +244,7 @@ class ImpressDocument(PresentationDocument): try: self.document = desktop.loadComponentFromURL(url, '_blank', 0, properties) except: - log.warn('Failed to load presentation %s' % url) + log.warning('Failed to load presentation %s' % url) return False if os.name == 'nt': # As we can't start minimized the Impress window gets in the way. @@ -318,7 +318,7 @@ class ImpressDocument(PresentationDocument): self.presentation = None self.document.dispose() except: - log.warn("Closing presentation failed") + log.warning("Closing presentation failed") self.document = None self.controller.remove_doc(self) @@ -335,7 +335,7 @@ class ImpressDocument(PresentationDocument): log.debug("getPresentation failed to find a presentation") return False except: - log.warn("getPresentation failed to find a presentation") + log.warning("getPresentation failed to find a presentation") return False return True diff --git a/openlp/plugins/presentations/lib/messagelistener.py b/openlp/plugins/presentations/lib/messagelistener.py index 724282eb4..ac115228a 100644 --- a/openlp/plugins/presentations/lib/messagelistener.py +++ b/openlp/plugins/presentations/lib/messagelistener.py @@ -98,7 +98,7 @@ class Controller(object): return True if not self.doc.is_loaded(): if not self.doc.load_presentation(): - log.warn('Failed to activate %s' % self.doc.filepath) + log.warning('Failed to activate %s' % self.doc.filepath) return False if self.is_live: self.doc.start_presentation() @@ -109,7 +109,7 @@ class Controller(object): if self.doc.is_active(): return True else: - log.warn('Failed to activate %s' % self.doc.filepath) + log.warning('Failed to activate %s' % self.doc.filepath) return False def slide(self, slide): diff --git a/openlp/plugins/presentations/presentationplugin.py b/openlp/plugins/presentations/presentationplugin.py index 5e0d7395d..7f080df22 100644 --- a/openlp/plugins/presentations/presentationplugin.py +++ b/openlp/plugins/presentations/presentationplugin.py @@ -90,7 +90,7 @@ class PresentationPlugin(Plugin): try: self.controllers[controller].start_process() except Exception: - log.warn('Failed to start controller process') + log.warning('Failed to start controller process') self.controllers[controller].available = False self.media_item.build_file_mask_string() @@ -134,7 +134,7 @@ class PresentationPlugin(Plugin): try: __import__(module_name, globals(), locals(), []) except ImportError: - log.warn('Failed to import %s on path %s', module_name, path) + log.warning('Failed to import %s on path %s', module_name, path) controller_classes = PresentationController.__subclasses__() for controller_class in controller_classes: controller = controller_class(self) diff --git a/openlp/plugins/songs/lib/songshowplusimport.py b/openlp/plugins/songs/lib/songshowplusimport.py index aebded029..b57a9bac1 100644 --- a/openlp/plugins/songs/lib/songshowplusimport.py +++ b/openlp/plugins/songs/lib/songshowplusimport.py @@ -152,7 +152,7 @@ class SongShowPlusImport(SongImport): if match: self.ccli_number = int(match.group()) else: - log.warn("Can't parse CCLI Number from string: %s" % self.decode(data)) + log.warning("Can't parse CCLI Number from string: %s" % self.decode(data)) elif block_key == VERSE: self.add_verse(self.decode(data), "%s%s" % (VerseType.tags[VerseType.Verse], verse_no)) elif block_key == CHORUS: diff --git a/openlp/plugins/songs/lib/worshipcenterproimport.py b/openlp/plugins/songs/lib/worshipcenterproimport.py index b24d2ae83..8c32870a1 100644 --- a/openlp/plugins/songs/lib/worshipcenterproimport.py +++ b/openlp/plugins/songs/lib/worshipcenterproimport.py @@ -58,7 +58,7 @@ class WorshipCenterProImport(SongImport): try: conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s' % self.import_source) except (pyodbc.DatabaseError, pyodbc.IntegrityError, pyodbc.InternalError, pyodbc.OperationalError) as e: - log.warn('Unable to connect the WorshipCenter Pro database %s. %s', self.import_source, str(e)) + log.warning('Unable to connect the WorshipCenter Pro database %s. %s', self.import_source, str(e)) # Unfortunately no specific exception type self.log_error(self.import_source, translate('SongsPlugin.WorshipCenterProImport', 'Unable to connect the WorshipCenter Pro database.')) From 67ab5082862cda6f248f0c0f7cf3078e760bd679 Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Sun, 20 Jul 2014 21:00:06 +0100 Subject: [PATCH 3/6] Tweeked test to use a call list --- tests/functional/openlp_core_lib/test_file_dialog.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_file_dialog.py b/tests/functional/openlp_core_lib/test_file_dialog.py index 875e422da..1190810da 100644 --- a/tests/functional/openlp_core_lib/test_file_dialog.py +++ b/tests/functional/openlp_core_lib/test_file_dialog.py @@ -5,7 +5,7 @@ from unittest import TestCase from openlp.core.common import UiStrings from openlp.core.lib.filedialog import FileDialog -from tests.functional import MagicMock, patch +from tests.functional import MagicMock, call, patch class TestFileDialog(TestCase): @@ -65,11 +65,9 @@ class TestFileDialog(TestCase): # THEN: os.path.exists should have been called with known args. QmessageBox.information should have been # called. The returned result should correlate with the input. - self.mocked_os.path.exists.assert_any_call('/Valid File') - self.mocked_os.path.exists.assert_any_call('/url%20encoded%20file%20%231') - self.mocked_os.path.exists.assert_any_call('/url encoded file #1') - self.mocked_os.path.exists.assert_any_call('/non-existing') - self.mocked_os.path.exists.assert_any_call('/non-existing') + call_list = [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_has_calls(call_list) 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') From 0b93faf7b9c877fef5714b8c9b228065278b906f Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Tue, 26 Aug 2014 18:38:07 +0100 Subject: [PATCH 4/6] PEP fixes --- openlp/core/utils/actions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlp/core/utils/actions.py b/openlp/core/utils/actions.py index fb794866b..9b5117233 100644 --- a/openlp/core/utils/actions.py +++ b/openlp/core/utils/actions.py @@ -280,7 +280,7 @@ class ActionList(object): ActionList.shortcut_map[shortcuts[1]] = actions else: log.warning('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % - (shortcuts[1], action.objectName())) + (shortcuts[1], action.objectName())) shortcuts.remove(shortcuts[1]) # Check the primary shortcut. existing_actions = ActionList.shortcut_map.get(shortcuts[0], []) @@ -291,7 +291,7 @@ class ActionList(object): ActionList.shortcut_map[shortcuts[0]] = actions else: log.warning('Shortcut "%s" is removed from "%s" because another action already uses this shortcut.' % - (shortcuts[0], action.objectName())) + (shortcuts[0], action.objectName())) shortcuts.remove(shortcuts[0]) action.setShortcuts([QtGui.QKeySequence(shortcut) for shortcut in shortcuts]) From 62811d660fbbc005da735eb1649c2437c9719e92 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Wed, 27 Aug 2014 10:43:18 +0200 Subject: [PATCH 5/6] Implemented a workaround for bug1251437 Fixes: https://launchpad.net/bugs/1251437 --- openlp/core/utils/__init__.py | 19 +++++++++++++++++++ openlp/plugins/bibles/lib/http.py | 1 - .../openlp_plugins/bibles/test_lib_http.py | 13 +++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 9b024eb84..255384b2d 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -109,6 +109,22 @@ class VersionThread(QtCore.QThread): Registry().execute('openlp_version_check', '%s' % version) +class HTTPRedirectHandlerFixed(urllib.request.HTTPRedirectHandler): + """ + Special HTTPRedirectHandler used to work around http://bugs.python.org/issue22248 + (Redirecting to urls with special chars) + """ + def redirect_request(self, req, fp, code, msg, headers, newurl): + # Test if the newurl can be decoded to ascii + try: + test_url = newurl.encode('latin1').decode('ascii') + fixed_url = newurl + except Exception: + # The url could not be decoded to ascii, so we do some url encoding + fixed_url = urllib.parse.quote(newurl.encode('latin1').decode('utf-8', 'replace'), safe='/:') + return super(HTTPRedirectHandlerFixed, self).redirect_request(req, fp, code, msg, headers, fixed_url) + + def get_application_version(): """ Returns the application version of the running instance of OpenLP:: @@ -341,6 +357,9 @@ def get_web_page(url, header=None, update_openlp=False): # http://docs.python.org/library/urllib2.html if not url: return None + # This is needed to work around http://bugs.python.org/issue22248 and https://bugs.launchpad.net/openlp/+bug/1251437 + opener = urllib.request.build_opener(HTTPRedirectHandlerFixed()) + urllib.request.install_opener(opener) req = urllib.request.Request(url) if not header or header[0].lower() != 'user-agent': user_agent = _get_user_agent() diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index 6b26dfabe..9f60ecc33 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -32,7 +32,6 @@ The :mod:`http` module enables OpenLP to retrieve scripture from bible websites. import logging import re import socket -import urllib.request import urllib.parse import urllib.error from html.parser import HTMLParseError diff --git a/tests/interfaces/openlp_plugins/bibles/test_lib_http.py b/tests/interfaces/openlp_plugins/bibles/test_lib_http.py index 517732e4d..9d72677ce 100644 --- a/tests/interfaces/openlp_plugins/bibles/test_lib_http.py +++ b/tests/interfaces/openlp_plugins/bibles/test_lib_http.py @@ -59,6 +59,19 @@ class TestBibleHTTP(TestCase): # THEN: We should get back a valid service item assert len(books) == 66, 'The bible should not have had any books added or removed' + def bible_gateway_extract_books_support_redirect_test(self): + """ + Test the Bible Gateway retrieval of book list for DN1933 bible with redirect (bug 1251437) + """ + # GIVEN: A new Bible Gateway extraction class + handler = BGExtract() + + # WHEN: The Books list is called + books = handler.get_books_from_http('DN1933') + + # THEN: We should get back a valid service item + assert len(books) == 66, 'This bible should have 66 books' + def bible_gateway_extract_verse_test(self): """ Test the Bible Gateway retrieval of verse list for NIV bible John 3 From f3207dac8e2760c1f9fffaa0b836a35b42624997 Mon Sep 17 00:00:00 2001 From: Jonathan Springer Date: Wed, 27 Aug 2014 19:18:06 -0400 Subject: [PATCH 6/6] Consolidate platform specific checks into the common module --- openlp/core/__init__.py | 8 +-- openlp/core/common/__init__.py | 27 ++++++++++ openlp/core/common/applocation.py | 8 +-- openlp/core/common/registryproperties.py | 6 +-- openlp/core/common/settings.py | 6 +-- openlp/core/ui/exceptionform.py | 4 +- openlp/core/ui/maindisplay.py | 6 +-- openlp/core/ui/mainwindow.py | 13 ++--- openlp/core/ui/media/vlcplayer.py | 8 +-- openlp/core/utils/__init__.py | 4 +- openlp/core/utils/languagemanager.py | 4 +- .../presentations/lib/impresscontroller.py | 20 ++++---- .../presentations/lib/pdfcontroller.py | 4 +- .../presentations/lib/powerpointcontroller.py | 10 ++-- .../presentations/lib/pptviewcontroller.py | 8 +-- openlp/plugins/songs/forms/songselectform.py | 4 +- openlp/plugins/songs/lib/importer.py | 6 +-- .../plugins/songs/lib/importers/openoffice.py | 9 ++-- .../songs/lib/importers/songsoffellowship.py | 3 +- .../openlp_core_common/test_common.py | 51 ++++++++++++++++++- 20 files changed, 146 insertions(+), 63 deletions(-) diff --git a/openlp/core/__init__.py b/openlp/core/__init__.py index 4ab94a250..32ef5c5a3 100644 --- a/openlp/core/__init__.py +++ b/openlp/core/__init__.py @@ -36,14 +36,14 @@ logging and a plugin framework are contained within the openlp.core module. import os import sys -import platform import logging from optparse import OptionParser from traceback import format_exception from PyQt4 import QtCore, QtGui -from openlp.core.common import Registry, OpenLPMixin, AppLocation, Settings, UiStrings, check_directory_exists +from openlp.core.common import Registry, OpenLPMixin, AppLocation, Settings, UiStrings, check_directory_exists, \ + is_macosx, is_win from openlp.core.lib import ScreenList from openlp.core.resources import qInitResources from openlp.core.ui.mainwindow import MainWindow @@ -126,7 +126,7 @@ class OpenLP(OpenLPMixin, QtGui.QApplication): alternate_rows_repair_stylesheet = \ 'QTableWidget, QListWidget, QTreeWidget {alternate-background-color: ' + base_color.name() + ';}\n' application_stylesheet += alternate_rows_repair_stylesheet - if os.name == 'nt': + if is_win(): application_stylesheet += NT_REPAIR_STYLESHEET if application_stylesheet: self.setStyleSheet(application_stylesheet) @@ -275,7 +275,7 @@ def main(args=None): # Throw the rest of the arguments at Qt, just in case. qt_args.extend(args) # Bug #1018855: Set the WM_CLASS property in X11 - if platform.system() not in ['Windows', 'Darwin']: + if not is_win() and not is_macosx(): qt_args.append('OpenLP') # Initialise the resources qInitResources() diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index 22207dec4..0776547ae 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -127,6 +127,33 @@ def de_hump(name): sub_name = FIRST_CAMEL_REGEX.sub(r'\1_\2', name) return SECOND_CAMEL_REGEX.sub(r'\1_\2', sub_name).lower() + +def is_win(): + """ + Returns true if running on a system with a nt kernel e.g. Windows, Wine + + :return: True if system is running a nt kernel false otherwise + """ + return os.name.startswith('nt') + + +def is_macosx(): + """ + Returns true if running on a system with a darwin kernel e.g. Mac OS X + + :return: True if system is running a darwin kernel false otherwise + """ + return sys.platform.startswith('darwin') + + +def is_linux(): + """ + Returns true if running on a system with a linux kernel e.g. Ubuntu, Debian, etc + + :return: True if system is running a linux kernel false otherwise + """ + return sys.platform.startswith('linux') + from .openlpmixin import OpenLPMixin from .registry import Registry from .registrymixin import RegistryMixin diff --git a/openlp/core/common/applocation.py b/openlp/core/common/applocation.py index 073d3c7f7..89f637e69 100644 --- a/openlp/core/common/applocation.py +++ b/openlp/core/common/applocation.py @@ -33,10 +33,10 @@ import logging import os import sys -from openlp.core.common import Settings +from openlp.core.common import Settings, is_win, is_macosx -if sys.platform != 'win32' and sys.platform != 'darwin': +if not is_win() and not is_macosx(): try: from xdg import BaseDirectory XDG_BASE_AVAILABLE = True @@ -145,13 +145,13 @@ def _get_os_dir_path(dir_type): directory = os.path.abspath(os.path.join(os.path.dirname(openlp.__file__), '..', 'resources')) if os.path.exists(directory): return directory - if sys.platform == 'win32': + if is_win(): if dir_type == AppLocation.DataDir: return os.path.join(str(os.getenv('APPDATA')), 'openlp', 'data') elif dir_type == AppLocation.LanguageDir: return os.path.dirname(openlp.__file__) return os.path.join(str(os.getenv('APPDATA')), 'openlp') - elif sys.platform == 'darwin': + elif is_macosx(): if dir_type == AppLocation.DataDir: return os.path.join(str(os.getenv('HOME')), 'Library', 'Application Support', 'openlp', 'Data') elif dir_type == AppLocation.LanguageDir: diff --git a/openlp/core/common/registryproperties.py b/openlp/core/common/registryproperties.py index 791fc33f7..e2cfffa09 100644 --- a/openlp/core/common/registryproperties.py +++ b/openlp/core/common/registryproperties.py @@ -29,9 +29,7 @@ """ Provide Registry values for adding to classes """ -import os - -from openlp.core.common import Registry +from openlp.core.common import Registry, is_win class RegistryProperties(object): @@ -45,7 +43,7 @@ class RegistryProperties(object): Adds the openlp to the class dynamically. Windows needs to access the application in a dynamic manner. """ - if os.name == 'nt': + if is_win(): return Registry().get('application') else: if not hasattr(self, '_application') or not self._application: diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 634bc5ced..f7202b590 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -36,7 +36,7 @@ import sys from PyQt4 import QtCore, QtGui -from openlp.core.common import ThemeLevel, SlideLimits, UiStrings +from openlp.core.common import ThemeLevel, SlideLimits, UiStrings, is_win, is_linux log = logging.getLogger(__name__) @@ -44,7 +44,7 @@ log = logging.getLogger(__name__) # Fix for bug #1014422. X11_BYPASS_DEFAULT = True -if sys.platform.startswith('linux'): +if is_linux(): # Default to False on Gnome. X11_BYPASS_DEFAULT = bool(not os.environ.get('GNOME_DESKTOP_SESSION_ID')) # Default to False on Xfce. @@ -86,7 +86,7 @@ class Settings(QtCore.QSettings): """ __default_settings__ = { 'advanced/add page break': False, - 'advanced/alternate rows': not sys.platform.startswith('win'), + 'advanced/alternate rows': not is_win(), 'advanced/current media plugin': -1, 'advanced/data path': '', 'advanced/default color': '#ffffff', diff --git a/openlp/core/ui/exceptionform.py b/openlp/core/ui/exceptionform.py index e0228a43b..65b858b75 100644 --- a/openlp/core/ui/exceptionform.py +++ b/openlp/core/ui/exceptionform.py @@ -38,7 +38,7 @@ import bs4 import sqlalchemy from lxml import etree -from openlp.core.common import RegistryProperties +from openlp.core.common import RegistryProperties, is_linux from PyQt4 import Qt, QtCore, QtGui, QtWebKit @@ -137,7 +137,7 @@ class ExceptionForm(QtGui.QDialog, Ui_ExceptionDialog, RegistryProperties): 'pyICU: %s\n' % ICU_VERSION + \ 'pyUNO bridge: %s\n' % self._pyuno_import() + \ 'VLC: %s\n' % VLC_VERSION - if platform.system() == 'Linux': + if is_linux(): if os.environ.get('KDE_FULL_SESSION') == 'true': system += 'Desktop: KDE SC\n' elif os.environ.get('GNOME_DESKTOP_SESSION_ID'): diff --git a/openlp/core/ui/maindisplay.py b/openlp/core/ui/maindisplay.py index 5c905c972..f9f00a235 100644 --- a/openlp/core/ui/maindisplay.py +++ b/openlp/core/ui/maindisplay.py @@ -43,7 +43,7 @@ import sys from PyQt4 import QtCore, QtGui, QtWebKit, QtOpenGL from PyQt4.phonon import Phonon -from openlp.core.common import Registry, RegistryProperties, OpenLPMixin, Settings, translate +from openlp.core.common import Registry, RegistryProperties, OpenLPMixin, Settings, translate, is_macosx from openlp.core.lib import ServiceItem, ImageSource, build_html, expand_tags, image_to_byte from openlp.core.lib.theme import BackgroundType @@ -74,7 +74,7 @@ class Display(QtGui.QGraphicsView): # OpenGL. Only white blank screen is shown on the 2nd monitor all the # time. We need to investigate more how to use OpenGL properly on Mac OS # X. - if sys.platform != 'darwin': + if not is_macosx(): self.setViewport(QtOpenGL.QGLWidget()) def setup(self): @@ -143,7 +143,7 @@ class MainDisplay(OpenLPMixin, Display, RegistryProperties): # on Mac OS X. For next OpenLP version we should test it on other # platforms. For OpenLP 2.0 keep it only for OS X to not cause any # regressions on other platforms. - if sys.platform == 'darwin': + if is_macosx(): window_flags = QtCore.Qt.FramelessWindowHint | QtCore.Qt.Window # For primary screen ensure it stays above the OS X dock # and menu bar diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 6894293ce..1a6b688c7 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -41,7 +41,8 @@ from datetime import datetime from PyQt4 import QtCore, QtGui -from openlp.core.common import Registry, RegistryProperties, AppLocation, Settings, check_directory_exists, translate +from openlp.core.common import Registry, RegistryProperties, AppLocation, Settings, check_directory_exists, translate, \ + is_win, is_macosx from openlp.core.lib import Renderer, OpenLPDockWidget, PluginManager, ImageManager, PluginStatus, ScreenList, \ build_icon from openlp.core.lib.ui import UiStrings, create_action @@ -289,7 +290,7 @@ class Ui_MainWindow(object): triggers=self.on_about_item_clicked) # Give QT Extra Hint that this is an About Menu Item self.about_item.setMenuRole(QtGui.QAction.AboutRole) - if os.name == 'nt': + if is_win(): self.local_help_file = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'OpenLP.chm') self.offline_help_item = create_action(main_window, 'offlineHelpItem', icon=':/system/system_help_contents.png', @@ -323,7 +324,7 @@ class Ui_MainWindow(object): # Qt on OS X looks for keywords in the menu items title to determine which menu items get added to the main # menu. If we are running on Mac OS X the menu items whose title contains those keywords but don't belong in the # main menu need to be marked as such with QAction.NoRole. - if sys.platform == 'darwin': + if is_macosx(): self.settings_shortcuts_item.setMenuRole(QtGui.QAction.NoRole) self.formatting_tag_item.setMenuRole(QtGui.QAction.NoRole) add_actions(self.settings_menu, (self.settings_plugin_list_item, self.settings_language_menu.menuAction(), @@ -332,7 +333,7 @@ class Ui_MainWindow(object): add_actions(self.tools_menu, (self.tools_open_data_folder, None)) add_actions(self.tools_menu, (self.tools_first_time_wizard, None)) add_actions(self.tools_menu, [self.update_theme_images]) - if os.name == 'nt': + if is_win(): add_actions(self.help_menu, (self.offline_help_item, self.on_line_help_item, None, self.web_site_item, self.about_item)) else: @@ -426,7 +427,7 @@ class Ui_MainWindow(object): self.settings_plugin_list_item.setStatusTip(translate('OpenLP.MainWindow', 'List the Plugins')) self.about_item.setText(translate('OpenLP.MainWindow', '&About')) self.about_item.setStatusTip(translate('OpenLP.MainWindow', 'More information about OpenLP')) - if os.name == 'nt': + if is_win(): self.offline_help_item.setText(translate('OpenLP.MainWindow', '&User Guide')) self.on_line_help_item.setText(translate('OpenLP.MainWindow', '&Online Help')) self.search_shortcut_action.setText(UiStrings().Search) @@ -1073,7 +1074,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow, RegistryProperties): if self.live_controller.display: self.live_controller.display.close() self.live_controller.display = None - if os.name == 'nt': + if is_win(): # Needed for Windows to stop crashes on exit Registry().remove('application') diff --git a/openlp/core/ui/media/vlcplayer.py b/openlp/core/ui/media/vlcplayer.py index d02526b0e..4394126c0 100644 --- a/openlp/core/ui/media/vlcplayer.py +++ b/openlp/core/ui/media/vlcplayer.py @@ -38,7 +38,7 @@ import threading from PyQt4 import QtGui -from openlp.core.common import Settings +from openlp.core.common import Settings, is_win, is_macosx from openlp.core.lib import translate from openlp.core.ui.media import MediaState from openlp.core.ui.media.mediaplayer import MediaPlayer @@ -52,7 +52,7 @@ try: except (ImportError, NameError, NotImplementedError): pass except OSError as e: - if sys.platform.startswith('win'): + if is_win(): if not isinstance(e, WindowsError) and e.winerror != 126: raise else: @@ -139,9 +139,9 @@ class VlcPlayer(MediaPlayer): # You have to give the id of the QFrame (or similar object) # to vlc, different platforms have different functions for this. win_id = int(display.vlc_widget.winId()) - if sys.platform == "win32": + if is_win(): display.vlc_media_player.set_hwnd(win_id) - elif sys.platform == "darwin": + elif is_macosx(): # We have to use 'set_nsobject' since Qt4 on OSX uses Cocoa # framework and not the old Carbon. display.vlc_media_player.set_nsobject(win_id) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 9b024eb84..bd72db1ac 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -44,10 +44,10 @@ from random import randint from PyQt4 import QtGui, QtCore -from openlp.core.common import Registry, AppLocation, Settings +from openlp.core.common import Registry, AppLocation, Settings, is_win, is_macosx -if sys.platform != 'win32' and sys.platform != 'darwin': +if not is_win() and not is_macosx(): try: from xdg import BaseDirectory XDG_BASE_AVAILABLE = True diff --git a/openlp/core/utils/languagemanager.py b/openlp/core/utils/languagemanager.py index dd048e04c..3c741e58f 100644 --- a/openlp/core/utils/languagemanager.py +++ b/openlp/core/utils/languagemanager.py @@ -35,7 +35,7 @@ import sys from PyQt4 import QtCore, QtGui -from openlp.core.common import AppLocation, Settings, translate +from openlp.core.common import AppLocation, Settings, translate, is_win, is_macosx log = logging.getLogger(__name__) @@ -60,7 +60,7 @@ class LanguageManager(object): app_translator = QtCore.QTranslator() app_translator.load(language, lang_path) # A translator for buttons and other default strings provided by Qt. - if sys.platform != 'win32' and sys.platform != 'darwin': + if not is_win() and not is_macosx(): lang_path = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath) default_translator = QtCore.QTranslator() default_translator.load('qt_%s' % language, lang_path) diff --git a/openlp/plugins/presentations/lib/impresscontroller.py b/openlp/plugins/presentations/lib/impresscontroller.py index 1d5e111c9..d032b9161 100644 --- a/openlp/plugins/presentations/lib/impresscontroller.py +++ b/openlp/plugins/presentations/lib/impresscontroller.py @@ -42,7 +42,9 @@ import logging import os import time -if os.name == 'nt': +from openlp.core.common import is_win + +if is_win(): from win32com.client import Dispatch import pywintypes # Declare an empty exception to match the exception imported from UNO @@ -93,7 +95,7 @@ class ImpressController(PresentationController): Impress is able to run on this machine. """ log.debug('check_available') - if os.name == 'nt': + if is_win(): return self.get_com_servicemanager() is not None else: return uno_available @@ -104,7 +106,7 @@ class ImpressController(PresentationController): UNO interface when required. """ log.debug('start process Openoffice') - if os.name == 'nt': + if is_win(): self.manager = self.get_com_servicemanager() self.manager._FlagAsMethod('Bridge_GetStruct') self.manager._FlagAsMethod('Bridge_GetValueObject') @@ -175,7 +177,7 @@ class ImpressController(PresentationController): self.docs[0].close_presentation() desktop = None try: - if os.name != 'nt': + if not is_win(): desktop = self.get_uno_desktop() else: desktop = self.get_com_desktop() @@ -223,7 +225,7 @@ class ImpressDocument(PresentationDocument): is available the presentation is loaded and started. """ log.debug('Load Presentation OpenOffice') - if os.name == 'nt': + if is_win(): desktop = self.controller.get_com_desktop() if desktop is None: self.controller.start_process() @@ -236,7 +238,7 @@ class ImpressDocument(PresentationDocument): return False self.desktop = desktop properties = [] - if os.name != 'nt': + if not is_win(): # Recent versions of Impress on Windows won't start the presentation if it starts as minimized. It seems OK # on Linux though. properties.append(self.create_property('Minimized', True)) @@ -246,7 +248,7 @@ class ImpressDocument(PresentationDocument): except: log.warning('Failed to load presentation %s' % url) return False - if os.name == 'nt': + if is_win(): # As we can't start minimized the Impress window gets in the way. # Either window.setPosSize(0, 0, 200, 400, 12) or .setVisible(False) window = self.document.getCurrentController().getFrame().getContainerWindow() @@ -264,7 +266,7 @@ class ImpressDocument(PresentationDocument): log.debug('create thumbnails OpenOffice') if self.check_thumbnails(): return - if os.name == 'nt': + if is_win(): thumb_dir_url = 'file:///' + self.get_temp_folder().replace('\\', '/') \ .replace(':', '|').replace(' ', '%20') else: @@ -297,7 +299,7 @@ class ImpressDocument(PresentationDocument): Create an OOo style property object which are passed into some Uno methods. """ log.debug('create property OpenOffice') - if os.name == 'nt': + if is_win(): property_object = self.controller.manager.Bridge_GetStruct('com.sun.star.beans.PropertyValue') else: property_object = PropertyValue() diff --git a/openlp/plugins/presentations/lib/pdfcontroller.py b/openlp/plugins/presentations/lib/pdfcontroller.py index b98ae131a..0283fefd4 100644 --- a/openlp/plugins/presentations/lib/pdfcontroller.py +++ b/openlp/plugins/presentations/lib/pdfcontroller.py @@ -34,7 +34,7 @@ import re from subprocess import check_output, CalledProcessError, STDOUT from openlp.core.utils import AppLocation -from openlp.core.common import Settings +from openlp.core.common import Settings, is_win from openlp.core.lib import ScreenList from .presentationcontroller import PresentationController, PresentationDocument @@ -123,7 +123,7 @@ class PdfController(PresentationController): else: # Fallback to autodetection application_path = AppLocation.get_directory(AppLocation.AppDir) - if os.name == 'nt': + if is_win(): # for windows we only accept mudraw.exe in the base folder application_path = AppLocation.get_directory(AppLocation.AppDir) if os.path.isfile(os.path.join(application_path, 'mudraw.exe')): diff --git a/openlp/plugins/presentations/lib/powerpointcontroller.py b/openlp/plugins/presentations/lib/powerpointcontroller.py index 0f9c2ff35..f42e4f814 100644 --- a/openlp/plugins/presentations/lib/powerpointcontroller.py +++ b/openlp/plugins/presentations/lib/powerpointcontroller.py @@ -33,7 +33,9 @@ This modul is for controlling powerpiont. PPT API documentation: import os import logging -if os.name == 'nt': +from openlp.core.common import is_win + +if is_win(): from win32com.client import Dispatch import winreg import win32ui @@ -69,7 +71,7 @@ class PowerpointController(PresentationController): PowerPoint is able to run on this machine. """ log.debug('check_available') - if os.name == 'nt': + if is_win(): try: winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, 'PowerPoint.Application').Close() return True @@ -77,7 +79,7 @@ class PowerpointController(PresentationController): pass return False - if os.name == 'nt': + if is_win(): def start_process(self): """ Loads PowerPoint process. @@ -271,7 +273,7 @@ class PowerpointDocument(PresentationDocument): trace_error_handler(log) self.show_error_msg() - if os.name == 'nt': + if is_win(): def start_presentation(self): """ Starts a presentation from the beginning. diff --git a/openlp/plugins/presentations/lib/pptviewcontroller.py b/openlp/plugins/presentations/lib/pptviewcontroller.py index a9090dd1e..7e03e322f 100644 --- a/openlp/plugins/presentations/lib/pptviewcontroller.py +++ b/openlp/plugins/presentations/lib/pptviewcontroller.py @@ -30,7 +30,9 @@ import logging import os -if os.name == 'nt': +from openlp.core.common import is_win + +if is_win(): from ctypes import cdll from ctypes.wintypes import RECT @@ -63,11 +65,11 @@ class PptviewController(PresentationController): PPT Viewer is able to run on this machine. """ log.debug('check_available') - if os.name != 'nt': + if not is_win(): return False return self.check_installed() - if os.name == 'nt': + if is_win(): def check_installed(self): """ Check the viewer is installed. diff --git a/openlp/plugins/songs/forms/songselectform.py b/openlp/plugins/songs/forms/songselectform.py index f9f658c5b..aecdf9682 100755 --- a/openlp/plugins/songs/forms/songselectform.py +++ b/openlp/plugins/songs/forms/songselectform.py @@ -37,7 +37,7 @@ from time import sleep from PyQt4 import QtCore, QtGui from openlp.core import Settings -from openlp.core.common import Registry +from openlp.core.common import Registry, is_win from openlp.core.lib import translate from openlp.plugins.songs.forms.songselectdialog import Ui_SongSelectDialog from openlp.plugins.songs.lib.songselect import SongSelectImport @@ -377,7 +377,7 @@ class SongSelectForm(QtGui.QDialog, Ui_SongSelectDialog): Adds the openlp to the class dynamically. Windows needs to access the application in a dynamic manner. """ - if os.name == 'nt': + if is_win(): return Registry().get('application') else: if not hasattr(self, '_application'): diff --git a/openlp/plugins/songs/lib/importer.py b/openlp/plugins/songs/lib/importer.py index e9ee2c2f3..12c8887f7 100644 --- a/openlp/plugins/songs/lib/importer.py +++ b/openlp/plugins/songs/lib/importer.py @@ -32,7 +32,7 @@ The :mod:`importer` modules provides the general song import functionality. import os import logging -from openlp.core.common import translate, UiStrings +from openlp.core.common import translate, UiStrings, is_win from openlp.core.ui.wizard import WizardStrings from .importers.opensong import OpenSongImport from .importers.easyslides import EasySlidesImport @@ -70,14 +70,14 @@ except ImportError: log.exception('Error importing %s', 'OooImport') HAS_OOO = False HAS_MEDIASHOUT = False -if os.name == 'nt': +if is_win(): try: from .importers.mediashout import MediaShoutImport HAS_MEDIASHOUT = True except ImportError: log.exception('Error importing %s', 'MediaShoutImport') HAS_WORSHIPCENTERPRO = False -if os.name == 'nt': +if is_win(): try: from .importers.worshipcenterpro import WorshipCenterProImport HAS_WORSHIPCENTERPRO = True diff --git a/openlp/plugins/songs/lib/importers/openoffice.py b/openlp/plugins/songs/lib/importers/openoffice.py index 0e499f7ae..a0bb1df88 100644 --- a/openlp/plugins/songs/lib/importers/openoffice.py +++ b/openlp/plugins/songs/lib/importers/openoffice.py @@ -32,13 +32,14 @@ import time from PyQt4 import QtCore +from openlp.core.common import is_win from openlp.core.utils import get_uno_command, get_uno_instance from openlp.core.lib import translate from .songimport import SongImport log = logging.getLogger(__name__) -if os.name == 'nt': +if is_win(): from win32com.client import Dispatch NoConnectException = Exception else: @@ -106,7 +107,7 @@ class OpenOfficeImport(SongImport): Start OpenOffice.org process TODO: The presentation/Impress plugin may already have it running """ - if os.name == 'nt': + if is_win(): self.start_ooo_process() self.desktop = self.ooo_manager.createInstance('com.sun.star.frame.Desktop') else: @@ -133,7 +134,7 @@ class OpenOfficeImport(SongImport): Start the OO Process """ try: - if os.name == 'nt': + if is_win(): self.ooo_manager = Dispatch('com.sun.star.ServiceManager') self.ooo_manager._FlagAsMethod('Bridge_GetStruct') self.ooo_manager._FlagAsMethod('Bridge_GetValueObject') @@ -150,7 +151,7 @@ class OpenOfficeImport(SongImport): Open the passed file in OpenOffice.org Impress """ self.file_path = file_path - if os.name == 'nt': + if is_win(): url = file_path.replace('\\', '/') url = url.replace(':', '|').replace(' ', '%20') url = 'file:///' + url diff --git a/openlp/plugins/songs/lib/importers/songsoffellowship.py b/openlp/plugins/songs/lib/importers/songsoffellowship.py index c1ef8666f..2cc49caef 100644 --- a/openlp/plugins/songs/lib/importers/songsoffellowship.py +++ b/openlp/plugins/songs/lib/importers/songsoffellowship.py @@ -37,12 +37,13 @@ import logging import os import re +from openlp.core.common import is_win from .openoffice import OpenOfficeImport log = logging.getLogger(__name__) -if os.name == 'nt': +if is_win(): from .openoffice import PAGE_BEFORE, PAGE_AFTER, PAGE_BOTH RuntimeException = Exception else: diff --git a/tests/functional/openlp_core_common/test_common.py b/tests/functional/openlp_core_common/test_common.py index f52256c5c..0474bd404 100644 --- a/tests/functional/openlp_core_common/test_common.py +++ b/tests/functional/openlp_core_common/test_common.py @@ -32,7 +32,8 @@ Functional tests to test the AppLocation class and related methods. from unittest import TestCase -from openlp.core.common import check_directory_exists, de_hump, trace_error_handler, translate +from openlp.core.common import check_directory_exists, de_hump, trace_error_handler, translate, is_win, is_macosx, \ + is_linux from tests.functional import MagicMock, patch @@ -139,3 +140,51 @@ class TestCommonFunctions(TestCase): # THEN: the translated string should be returned, and the mocked function should have been called mocked_translate.assert_called_with(context, text, comment, encoding, n) self.assertEqual('Translated string', result, 'The translated string should have been returned') + + def is_win_test(self): + """ + Test the is_win() function + """ + # GIVEN: Mocked out objects + with patch('openlp.core.common.os') as mocked_os, patch('openlp.core.common.sys') as mocked_sys: + + # WHEN: The mocked os.name and sys.platform are set to 'nt' and 'win32' repectivly + mocked_os.name = 'nt' + mocked_sys.platform = 'win32' + + # THEN: The three platform functions should perform properly + self.assertTrue(is_win(), 'is_win() should return True') + self.assertFalse(is_macosx(), 'is_macosx() should return False') + self.assertFalse(is_linux(), 'is_linux() should return False') + + def is_macosx_test(self): + """ + Test the is_macosx() function + """ + # GIVEN: Mocked out objects + with patch('openlp.core.common.os') as mocked_os, patch('openlp.core.common.sys') as mocked_sys: + + # WHEN: The mocked os.name and sys.platform are set to 'posix' and 'darwin' repectivly + mocked_os.name = 'posix' + mocked_sys.platform = 'darwin' + + # THEN: The three platform functions should perform properly + self.assertTrue(is_macosx(), 'is_macosx() should return True') + self.assertFalse(is_win(), 'is_win() should return False') + self.assertFalse(is_linux(), 'is_linux() should return False') + + def is_linux_test(self): + """ + Test the is_linux() function + """ + # GIVEN: Mocked out objects + with patch('openlp.core.common.os') as mocked_os, patch('openlp.core.common.sys') as mocked_sys: + + # WHEN: The mocked os.name and sys.platform are set to 'posix' and 'linux3' repectivly + mocked_os.name = 'posix' + mocked_sys.platform = 'linux3' + + # THEN: The three platform functions should perform properly + self.assertTrue(is_linux(), 'is_linux() should return True') + self.assertFalse(is_win(), 'is_win() should return False') + self.assertFalse(is_macosx(), 'is_macosx() should return False')