diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index b8a1a4d2e..f3076a86f 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -24,6 +24,7 @@ The :mod:`common` module contains most of the components and libraries that make OpenLP work. """ import hashlib + import logging import os import re @@ -31,6 +32,7 @@ import sys import traceback from ipaddress import IPv4Address, IPv6Address, AddressValueError from shutil import which +from subprocess import check_output, CalledProcessError, STDOUT from PyQt5 import QtCore, QtGui from PyQt5.QtCore import QCryptographicHash as QHash @@ -247,6 +249,9 @@ from .applocation import AppLocation from .actions import ActionList from .languagemanager import LanguageManager +if is_win(): + from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW + def add_actions(target, actions): """ @@ -371,3 +376,28 @@ def clean_filename(filename): if not isinstance(filename, str): filename = str(filename, 'utf-8') return INVALID_FILE_CHARS.sub('_', CONTROL_CHARS.sub('', filename)) + + +def check_binary_exists(program_path): + """ + Function that checks whether a binary exists. + + :param program_path:The full path to the binary to check. + :return: program output to be parsed + """ + log.debug('testing program_path: %s', program_path) + try: + # Setup startupinfo options for check_output to avoid console popping up on windows + if is_win(): + startupinfo = STARTUPINFO() + startupinfo.dwFlags |= STARTF_USESHOWWINDOW + else: + startupinfo = None + runlog = check_output([program_path, '--help'], stderr=STDOUT, startupinfo=startupinfo) + except CalledProcessError as e: + runlog = e.output + except Exception: + trace_error_handler(log) + runlog = '' + log.debug('check_output returned: %s' % runlog) + return runlog diff --git a/openlp/core/ui/media/mediacontroller.py b/openlp/core/ui/media/mediacontroller.py index cf116e861..71e0f1903 100644 --- a/openlp/core/ui/media/mediacontroller.py +++ b/openlp/core/ui/media/mediacontroller.py @@ -296,7 +296,7 @@ class MediaController(RegistryMixin, OpenLPMixin, RegistryProperties): tooltip=translate('OpenLP.SlideController', 'Stop playing media.'), triggers=controller.send_to_plugins) controller.mediabar.add_toolbar_action('playbackLoop', text='media_playback_loop', - icon=':/slides/media_playback_stop.png', checked=False, + icon=':/media/media_repeat.png', checked=False, tooltip=translate('OpenLP.SlideController', 'Loop playing media.'), triggers=controller.send_to_plugins) controller.position_label = QtWidgets.QLabel() diff --git a/openlp/plugins/media/mediaplugin.py b/openlp/plugins/media/mediaplugin.py index daeb4dc2c..1d5529084 100644 --- a/openlp/plugins/media/mediaplugin.py +++ b/openlp/plugins/media/mediaplugin.py @@ -24,10 +24,13 @@ The Media plugin """ import logging +import os +import re +from shutil import which from PyQt5 import QtCore -from openlp.core.common import Settings, translate +from openlp.core.common import AppLocation, Settings, translate, check_binary_exists, is_win from openlp.core.lib import Plugin, StringContent, build_icon from openlp.plugins.media.lib import MediaMediaItem, MediaTab @@ -62,6 +65,15 @@ class MediaPlugin(Plugin): """ super().initialise() + def check_pre_conditions(self): + """ + Check it we have a valid environment. + :return: true or false + """ + log.debug('check_installed Mediainfo') + # Use the user defined program if given + return process_check_binary('mediainfo') + def app_startup(self): """ Override app_startup() in order to do nothing @@ -137,3 +149,21 @@ class MediaPlugin(Plugin): Add html code to htmlbuilder. """ return self.media_controller.get_media_display_html() + + +def process_check_binary(program_path): + """ + Function that checks whether a binary MediaInfo is present + + :param program_path:The full path to the binary to check. + :return: If exists or not + """ + program_type = None + runlog = check_binary_exists(program_path) + print(runlog, type(runlog)) + # Analyse the output to see it the program is mediainfo + for line in runlog.splitlines(): + decoded_line = line.decode() + if re.search('MediaInfo Command line', decoded_line, re.IGNORECASE): + return True + return False diff --git a/openlp/plugins/presentations/lib/pdfcontroller.py b/openlp/plugins/presentations/lib/pdfcontroller.py index dbea84327..48150a9f2 100644 --- a/openlp/plugins/presentations/lib/pdfcontroller.py +++ b/openlp/plugins/presentations/lib/pdfcontroller.py @@ -22,13 +22,12 @@ import os import logging -from tempfile import NamedTemporaryFile import re from shutil import which -from subprocess import check_output, CalledProcessError, STDOUT +from subprocess import check_output, CalledProcessError -from openlp.core.common import AppLocation -from openlp.core.common import Settings, is_win, trace_error_handler +from openlp.core.common import AppLocation, check_binary_exists +from openlp.core.common import Settings, is_win from openlp.core.lib import ScreenList from .presentationcontroller import PresentationController, PresentationDocument @@ -61,7 +60,7 @@ class PdfController(PresentationController): self.check_installed() @staticmethod - def check_binary(program_path): + def process_check_binary(program_path): """ Function that checks whether a binary is either ghostscript or mudraw or neither. Is also used from presentationtab.py @@ -70,22 +69,7 @@ class PdfController(PresentationController): :return: Type of the binary, 'gs' if ghostscript, 'mudraw' if mudraw, None if invalid. """ program_type = None - runlog = '' - log.debug('testing program_path: %s', program_path) - try: - # Setup startupinfo options for check_output to avoid console popping up on windows - if is_win(): - startupinfo = STARTUPINFO() - startupinfo.dwFlags |= STARTF_USESHOWWINDOW - else: - startupinfo = None - runlog = check_output([program_path, '--help'], stderr=STDOUT, startupinfo=startupinfo) - except CalledProcessError as e: - runlog = e.output - except Exception: - trace_error_handler(log) - runlog = '' - log.debug('check_output returned: %s' % runlog) + runlog = check_binary_exists(program_path) # Analyse the output to see it the program is mudraw, ghostscript or neither for line in runlog.splitlines(): decoded_line = line.decode() @@ -122,7 +106,7 @@ class PdfController(PresentationController): # Use the user defined program if given if Settings().value('presentations/enable_pdf_program'): pdf_program = Settings().value('presentations/pdf_program') - program_type = self.check_binary(pdf_program) + program_type = self.process_check_binary(pdf_program) if program_type == 'gs': self.gsbin = pdf_program elif program_type == 'mudraw': diff --git a/tests/functional/openlp_plugins/media/test_mediaplugin.py b/tests/functional/openlp_plugins/media/test_mediaplugin.py index 1e11de4fa..c49cdbaa4 100644 --- a/tests/functional/openlp_plugins/media/test_mediaplugin.py +++ b/tests/functional/openlp_plugins/media/test_mediaplugin.py @@ -25,7 +25,7 @@ Test the media plugin from unittest import TestCase from openlp.core import Registry -from openlp.plugins.media.mediaplugin import MediaPlugin +from openlp.plugins.media.mediaplugin import MediaPlugin, process_check_binary from tests.functional import MagicMock, patch from tests.helpers.testmixin import TestMixin @@ -63,3 +63,29 @@ class MediaPluginTest(TestCase, TestMixin): self.assertIsInstance(MediaPlugin.about(), str) # THEN: about() should return a non-empty string self.assertNotEquals(len(MediaPlugin.about()), 0) + + @patch('openlp.plugins.media.mediaplugin.check_binary_exists') + def process_check_binary_pass_test(self, mocked_checked_binary_exists): + """ + Test that the Process check returns true if found + """ + # GIVEN: A media plugin instance + # WHEN: function is called with the correct name + mocked_checked_binary_exists.return_value = str.encode('MediaInfo Command line') + result = process_check_binary('MediaInfo') + + # THEN: The the result should be True + self.assertTrue(result, 'Mediainfo should have been found') + + @patch('openlp.plugins.media.mediaplugin.check_binary_exists') + def process_check_binary_fail_test(self, mocked_checked_binary_exists): + """ + Test that the Process check returns false if not found + """ + # GIVEN: A media plugin instance + # WHEN: function is called with the wrong name + mocked_checked_binary_exists.return_value = str.encode('MediaInfo1 Command line') + result = process_check_binary("MediaInfo1") + + # THEN: The the result should be True + self.assertFalse(result, "Mediainfo should not have been found")