Add Icon and defense tests for missing mediainfo

bzr-revno: 2648
This commit is contained in:
Tim Bentley 2016-04-23 14:45:55 +01:00
commit fed2466ad6
5 changed files with 95 additions and 25 deletions

View File

@ -24,6 +24,7 @@ The :mod:`common` module contains most of the components and libraries that make
OpenLP work. OpenLP work.
""" """
import hashlib import hashlib
import logging import logging
import os import os
import re import re
@ -31,6 +32,7 @@ import sys
import traceback import traceback
from ipaddress import IPv4Address, IPv6Address, AddressValueError from ipaddress import IPv4Address, IPv6Address, AddressValueError
from shutil import which from shutil import which
from subprocess import check_output, CalledProcessError, STDOUT
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QCryptographicHash as QHash from PyQt5.QtCore import QCryptographicHash as QHash
@ -247,6 +249,9 @@ from .applocation import AppLocation
from .actions import ActionList from .actions import ActionList
from .languagemanager import LanguageManager from .languagemanager import LanguageManager
if is_win():
from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW
def add_actions(target, actions): def add_actions(target, actions):
""" """
@ -371,3 +376,28 @@ def clean_filename(filename):
if not isinstance(filename, str): if not isinstance(filename, str):
filename = str(filename, 'utf-8') filename = str(filename, 'utf-8')
return INVALID_FILE_CHARS.sub('_', CONTROL_CHARS.sub('', filename)) 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

View File

@ -296,7 +296,7 @@ class MediaController(RegistryMixin, OpenLPMixin, RegistryProperties):
tooltip=translate('OpenLP.SlideController', 'Stop playing media.'), tooltip=translate('OpenLP.SlideController', 'Stop playing media.'),
triggers=controller.send_to_plugins) triggers=controller.send_to_plugins)
controller.mediabar.add_toolbar_action('playbackLoop', text='media_playback_loop', 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.'), tooltip=translate('OpenLP.SlideController', 'Loop playing media.'),
triggers=controller.send_to_plugins) triggers=controller.send_to_plugins)
controller.position_label = QtWidgets.QLabel() controller.position_label = QtWidgets.QLabel()

View File

@ -24,10 +24,13 @@ The Media plugin
""" """
import logging import logging
import os
import re
from shutil import which
from PyQt5 import QtCore 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.core.lib import Plugin, StringContent, build_icon
from openlp.plugins.media.lib import MediaMediaItem, MediaTab from openlp.plugins.media.lib import MediaMediaItem, MediaTab
@ -62,6 +65,15 @@ class MediaPlugin(Plugin):
""" """
super().initialise() 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): def app_startup(self):
""" """
Override app_startup() in order to do nothing Override app_startup() in order to do nothing
@ -137,3 +149,21 @@ class MediaPlugin(Plugin):
Add html code to htmlbuilder. Add html code to htmlbuilder.
""" """
return self.media_controller.get_media_display_html() 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

View File

@ -22,13 +22,12 @@
import os import os
import logging import logging
from tempfile import NamedTemporaryFile
import re import re
from shutil import which 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 AppLocation, check_binary_exists
from openlp.core.common import Settings, is_win, trace_error_handler from openlp.core.common import Settings, is_win
from openlp.core.lib import ScreenList from openlp.core.lib import ScreenList
from .presentationcontroller import PresentationController, PresentationDocument from .presentationcontroller import PresentationController, PresentationDocument
@ -61,7 +60,7 @@ class PdfController(PresentationController):
self.check_installed() self.check_installed()
@staticmethod @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. Function that checks whether a binary is either ghostscript or mudraw or neither.
Is also used from presentationtab.py 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. :return: Type of the binary, 'gs' if ghostscript, 'mudraw' if mudraw, None if invalid.
""" """
program_type = None program_type = None
runlog = '' runlog = check_binary_exists(program_path)
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)
# Analyse the output to see it the program is mudraw, ghostscript or neither # Analyse the output to see it the program is mudraw, ghostscript or neither
for line in runlog.splitlines(): for line in runlog.splitlines():
decoded_line = line.decode() decoded_line = line.decode()
@ -122,7 +106,7 @@ class PdfController(PresentationController):
# Use the user defined program if given # Use the user defined program if given
if Settings().value('presentations/enable_pdf_program'): if Settings().value('presentations/enable_pdf_program'):
pdf_program = Settings().value('presentations/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': if program_type == 'gs':
self.gsbin = pdf_program self.gsbin = pdf_program
elif program_type == 'mudraw': elif program_type == 'mudraw':

View File

@ -25,7 +25,7 @@ Test the media plugin
from unittest import TestCase from unittest import TestCase
from openlp.core import Registry 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.functional import MagicMock, patch
from tests.helpers.testmixin import TestMixin from tests.helpers.testmixin import TestMixin
@ -63,3 +63,29 @@ class MediaPluginTest(TestCase, TestMixin):
self.assertIsInstance(MediaPlugin.about(), str) self.assertIsInstance(MediaPlugin.about(), str)
# THEN: about() should return a non-empty string # THEN: about() should return a non-empty string
self.assertNotEquals(len(MediaPlugin.about()), 0) 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")