refactor media extensions

bzr-revno: 2874
This commit is contained in:
Phill 2019-06-01 11:12:12 +01:00
commit 306ae3b5bf
12 changed files with 46 additions and 131 deletions

View File

@ -23,7 +23,6 @@ from contextlib import suppress
from json import JSONDecoder, JSONEncoder
from pathlib import Path
_registered_classes = {}

View File

@ -589,7 +589,7 @@ class MediaManagerItem(QtWidgets.QWidget, RegistryProperties):
"""
Add this item to the current service.
:param item: Item to be processed
:param QtWidgets.QListWidgetItem | QtWidgets.QTreeWidgetItem | None item: Item to be processed
:param replace: Replace the existing item
:param remote: Triggered from remote
:param position: Position to place item
@ -627,7 +627,7 @@ class MediaManagerItem(QtWidgets.QWidget, RegistryProperties):
def build_service_item(self, item=None, remote=False, context=ServiceItemContext.Live):
"""
Common method for generating a service item
:param item: Service Item to be built.
:param QtWidgets.QListWidgetItem | QtWidgets.QTreeWidgetItem | None item: Service Item to be built.
:param remote: Remote triggered (False)
:param context: The context on which this is called
"""

View File

@ -593,9 +593,11 @@ class ServiceItem(RegistryProperties):
"""
return not bool(self.slides)
def validate_item(self, suffix_list=None):
def validate_item(self, suffixes=None):
"""
Validates a service item to make sure it is valid
:param set[str] suffixes: A set of vaild suffixes
"""
self.is_valid = True
for slide in self.slides:
@ -612,8 +614,8 @@ class ServiceItem(RegistryProperties):
if not os.path.exists(file_name):
self.is_valid = False
break
if suffix_list and not self.is_text():
if suffixes and not self.is_text():
file_suffix = slide['title'].split('.')[-1]
if file_suffix.lower() not in suffix_list:
if file_suffix.lower() not in suffixes:
self.is_valid = False
break

View File

@ -44,7 +44,7 @@ from openlp.core.lib.ui import critical_error_message_box
from openlp.core.ui import DisplayControllerType
from openlp.core.ui.media import MediaState, ItemMediaInfo, MediaType, parse_optical_path
from openlp.core.ui.media.endpoint import media_endpoint
from openlp.core.ui.media.vlcplayer import VlcPlayer, get_vlc
from openlp.core.ui.media.vlcplayer import AUDIO_EXT, VIDEO_EXT, VlcPlayer, get_vlc
log = logging.getLogger(__name__)
@ -65,11 +65,6 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties):
current_media_players is an array of player instances keyed on ControllerType.
"""
def __init__(self, parent=None):
"""
Constructor
"""
super(MediaController, self).__init__(parent)
def setup(self):
self.vlc_player = None
@ -95,28 +90,8 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties):
Registry().register_function('songs_hide', self.media_hide)
Registry().register_function('songs_blank', self.media_blank)
Registry().register_function('songs_unblank', self.media_unblank)
Registry().register_function('mediaitem_suffixes', self._generate_extensions_lists)
register_endpoint(media_endpoint)
def _generate_extensions_lists(self):
"""
Set the active players and available media files
"""
suffix_list = []
self.audio_extensions_list = []
if self.vlc_player.is_active:
for item in self.vlc_player.audio_extensions_list:
if item not in self.audio_extensions_list:
self.audio_extensions_list.append(item)
suffix_list.append(item[2:])
self.video_extensions_list = []
if self.vlc_player.is_active:
for item in self.vlc_player.video_extensions_list:
if item not in self.video_extensions_list:
self.video_extensions_list.append(item)
suffix_list.append(item[2:])
self.service_manager.supported_suffixes(suffix_list)
def bootstrap_initialise(self):
"""
Check to see if we have any media Player's available.
@ -131,7 +106,6 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties):
else:
State().missing_text('media_live', translate('OpenLP.SlideController',
'VLC or pymediainfo are missing, so you are unable to play any media'))
self._generate_extensions_lists()
return True
def bootstrap_post_set_up(self):
@ -381,7 +355,7 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties):
if file.is_file:
suffix = '*%s' % file.suffix.lower()
file = str(file)
if suffix in self.vlc_player.video_extensions_list:
if suffix in VIDEO_EXT:
if not controller.media_info.is_background or controller.media_info.is_background and \
self.vlc_player.can_background:
self.resize(display, self.vlc_player)
@ -389,7 +363,7 @@ class MediaController(RegistryBase, LogMixin, RegistryProperties):
self.current_media_players[controller.controller_type] = self.vlc_player
controller.media_info.media_type = MediaType.Video
return True
if suffix in self.vlc_player.audio_extensions_list:
if suffix in AUDIO_EXT:
if self.vlc_player.load(display, file):
self.current_media_players[controller.controller_type] = self.vlc_player
controller.media_info.media_type = MediaType.Audio

View File

@ -38,13 +38,10 @@ class MediaPlayer(RegistryProperties):
self.parent = parent
self.name = name
self.available = self.check_available()
self.is_active = False
self.can_background = False
self.can_folder = False
self.state = {0: MediaState.Off, 1: MediaState.Off}
self.has_own_widget = False
self.audio_extensions_list = []
self.video_extensions_list = []
def check_available(self):
"""
@ -166,12 +163,6 @@ class MediaPlayer(RegistryProperties):
"""
return ''
def get_info(self):
"""
Returns Information about the player
"""
return ''
def get_live_state(self):
"""
Get the state of the live player

View File

@ -33,7 +33,6 @@ import vlc
from PyQt5 import QtWidgets
from openlp.core.common import is_linux, is_macosx, is_win
from openlp.core.common.i18n import translate
from openlp.core.common.settings import Settings
from openlp.core.ui.media import MediaState, MediaType
from openlp.core.ui.media.mediaplayer import MediaPlayer
@ -42,20 +41,18 @@ from openlp.core.ui.media.mediaplayer import MediaPlayer
log = logging.getLogger(__name__)
# Audio and video extensions copied from 'include/vlc_interface.h' from vlc 2.2.0 source
AUDIO_EXT = ['*.3ga', '*.669', '*.a52', '*.aac', '*.ac3', '*.adt', '*.adts', '*.aif', '*.aifc', '*.aiff', '*.amr',
'*.aob', '*.ape', '*.awb', '*.caf', '*.dts', '*.flac', '*.it', '*.kar', '*.m4a', '*.m4b', '*.m4p', '*.m5p',
'*.mid', '*.mka', '*.mlp', '*.mod', '*.mpa', '*.mp1', '*.mp2', '*.mp3', '*.mpc', '*.mpga', '*.mus',
'*.oga', '*.ogg', '*.oma', '*.opus', '*.qcp', '*.ra', '*.rmi', '*.s3m', '*.sid', '*.spx', '*.thd', '*.tta',
'*.voc', '*.vqf', '*.w64', '*.wav', '*.wma', '*.wv', '*.xa', '*.xm']
AUDIO_EXT = ('3ga', '669', 'a52', 'aac', 'ac3', 'adt', 'adts', 'aif', 'aifc', 'aiff', 'amr', 'aob', 'ape', 'awb', 'caf',
'dts', 'flac', 'it', 'kar', 'm4a', 'm4b', 'm4p', 'm5p', 'mid', 'mka', 'mlp', 'mod', 'mpa', 'mp1', 'mp2',
'mp3', 'mpc', 'mpga', 'mus', 'oga', 'ogg', 'oma', 'opus', 'qcp', 'ra', 'rmi', 's3m', 'sid', 'spx', 'thd',
'tta', 'voc', 'vqf', 'w64', 'wav', 'wma', 'wv', 'xa', 'xm')
VIDEO_EXT = ['*.3g2', '*.3gp', '*.3gp2', '*.3gpp', '*.amv', '*.asf', '*.avi', '*.bik', '*.divx', '*.drc', '*.dv',
'*.f4v', '*.flv', '*.gvi', '*.gxf', '*.iso', '*.m1v', '*.m2v', '*.m2t', '*.m2ts', '*.m4v', '*.mkv',
'*.mov', '*.mp2', '*.mp2v', '*.mp4', '*.mp4v', '*.mpe', '*.mpeg', '*.mpeg1', '*.mpeg2', '*.mpeg4', '*.mpg',
'*.mpv2', '*.mts', '*.mtv', '*.mxf', '*.mxg', '*.nsv', '*.nuv', '*.ogg', '*.ogm', '*.ogv', '*.ogx', '*.ps',
'*.rec', '*.rm', '*.rmvb', '*.rpl', '*.thp', '*.tod', '*.ts', '*.tts', '*.txd', '*.vob', '*.vro', '*.webm',
'*.wm', '*.wmv', '*.wtv', '*.xesc',
VIDEO_EXT = ('3g2', '3gp', '3gp2', '3gpp', 'amv', 'asf', 'avi', 'bik', 'divx', 'drc', 'dv', 'f4v', 'flv', 'gvi', 'gxf',
'iso', 'm1v', 'm2v', 'm2t', 'm2ts', 'm4v', 'mkv', 'mov', 'mp2', 'mp2v', 'mp4', 'mp4v', 'mpe', 'mpeg',
'mpeg1', 'mpeg2', 'mpeg4', 'mpg', 'mpv2', 'mts', 'mtv', 'mxf', 'mxg', 'nsv', 'nuv', 'ogg', 'ogm', 'ogv',
'ogx', 'ps', 'rec', 'rm', 'rmvb', 'rpl', 'thp', 'tod', 'ts', 'tts', 'txd', 'vob', 'vro', 'webm', 'wm',
'wmv', 'wtv', 'xesc',
# These extensions was not in the official list, added manually.
'*.nut', '*.rv', '*.xvid']
'nut', 'rv', 'xvid')
def get_vlc():
@ -109,8 +106,6 @@ class VlcPlayer(MediaPlayer):
self.display_name = '&VLC'
self.parent = parent
self.can_folder = True
self.audio_extensions_list = AUDIO_EXT
self.video_extensions_list = VIDEO_EXT
def setup(self, output_display, live_display):
"""
@ -374,14 +369,3 @@ class VlcPlayer(MediaPlayer):
else:
controller.seek_slider.setSliderPosition(output_display.vlc_media_player.get_time())
controller.seek_slider.blockSignals(False)
def get_info(self):
"""
Return some information about this player
"""
return(translate('Media.player', 'VLC is an external player which '
'supports a number of different formats.') +
'<br/> <strong>' + translate('Media.player', 'Audio') +
'</strong><br/>' + str(AUDIO_EXT) + '<br/><strong>' +
translate('Media.player', 'Video') + '</strong><br/>' +
str(VIDEO_EXT) + '<br/>')

View File

@ -48,6 +48,7 @@ from openlp.core.lib.plugin import PluginStatus
from openlp.core.lib.serviceitem import ItemCapabilities, ServiceItem
from openlp.core.lib.ui import create_widget_action, critical_error_message_box, find_and_set_in_combo_box
from openlp.core.ui.icons import UiIcons
from openlp.core.ui.media.vlcplayer import AUDIO_EXT, VIDEO_EXT
from openlp.core.ui.serviceitemeditform import ServiceItemEditForm
from openlp.core.ui.servicenoteform import ServiceNoteForm
from openlp.core.ui.starttimeform import StartTimeForm
@ -320,7 +321,8 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
"""
super().__init__(parent)
self.service_items = []
self.suffixes = []
self.suffixes = set()
self.add_media_suffixes()
self.drop_position = -1
self.service_id = 0
# is a new service and has not been saved
@ -347,6 +349,13 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
self.service_item_edit_form = ServiceItemEditForm()
self.start_time_form = StartTimeForm()
def add_media_suffixes(self):
"""
Add the suffixes supported by :mod:`openlp.core.ui.media.vlcplayer`
"""
self.suffixes.update(AUDIO_EXT)
self.suffixes.update(VIDEO_EXT)
def set_modified(self, modified=True):
"""
Setter for property "modified". Sets whether or not the current service has been modified.
@ -401,22 +410,19 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
def reset_supported_suffixes(self):
"""
Resets the Suffixes list.
"""
self.suffixes = []
self.suffixes.clear()
def supported_suffixes(self, suffix_list):
"""
Adds Suffixes supported to the master list. Called from Plugins.
:param suffix_list: New Suffix's to be supported
:param list[str] | str suffix_list: New suffix(s) to be supported
"""
if isinstance(suffix_list, str):
self.suffixes.append(suffix_list)
self.suffixes.add(suffix_list)
else:
for suffix in suffix_list:
if suffix not in self.suffixes:
self.suffixes.append(suffix)
self.suffixes.update(suffix_list)
def on_new_service_clicked(self):
"""
@ -475,9 +481,11 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
QtWidgets.QMessageBox.Save | QtWidgets.QMessageBox.Discard |
QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.Save)
def on_recent_service_clicked(self):
def on_recent_service_clicked(self, checked):
"""
Load a recent file as the service triggered by mainwindow recent service list.
:param bool checked: Not used
"""
if self.is_modified():
result = self.save_modified_service()

View File

@ -32,7 +32,6 @@ from openlp.core.common.mixins import RegistryProperties
from openlp.core.common.registry import Registry
from openlp.core.lib.theme import BackgroundGradientType, BackgroundType
from openlp.core.lib.ui import critical_error_message_box
# TODO: Fix this. Use a "get_video_extensions" method which uses the current media player
from openlp.core.ui.media.vlcplayer import VIDEO_EXT
from openlp.core.ui.themelayoutform import ThemeLayoutForm
from openlp.core.ui.themewizard import Ui_ThemeWizard
@ -76,9 +75,8 @@ class ThemeForm(QtWidgets.QWizard, Ui_ThemeWizard, RegistryProperties):
self.image_path_edit.filters = \
'{name};;{text} (*)'.format(name=get_images_filter(), text=UiStrings().AllFiles)
self.image_path_edit.pathChanged.connect(self.on_image_path_edit_path_changed)
# TODO: Should work
visible_formats = '({name})'.format(name='; '.join(VIDEO_EXT))
actual_formats = '({name})'.format(name=' '.join(VIDEO_EXT))
visible_formats = '(*.{name})'.format(name='; *.'.join(VIDEO_EXT))
actual_formats = '(*.{name})'.format(name=' *.'.join(VIDEO_EXT))
video_filter = '{trans} {visible} {actual}'.format(trans=translate('OpenLP', 'Video Files'),
visible=visible_formats, actual=actual_formats)
self.video_path_edit.filters = '{video};;{ui} (*)'.format(video=video_filter, ui=UiStrings().AllFiles)

View File

@ -38,7 +38,7 @@ from openlp.core.lib.serviceitem import ItemCapabilities
from openlp.core.lib.ui import critical_error_message_box
from openlp.core.ui.icons import UiIcons
from openlp.core.ui.media import parse_optical_path, format_milliseconds
from openlp.core.ui.media.vlcplayer import get_vlc
from openlp.core.ui.media.vlcplayer import AUDIO_EXT, VIDEO_EXT, get_vlc
if get_vlc() is not None:
@ -232,9 +232,9 @@ class MediaMediaItem(MediaManagerItem, RegistryProperties):
"""
# self.populate_display_types()
self.on_new_file_masks = translate('MediaPlugin.MediaItem',
'Videos ({video});;Audio ({audio});;{files} '
'(*)').format(video=' '.join(self.media_controller.video_extensions_list),
audio=' '.join(self.media_controller.audio_extensions_list),
'Videos (*.{video});;Audio (*.{audio});;{files} '
'(*)').format(video=' *.'.join(VIDEO_EXT),
audio=' *.'.join(AUDIO_EXT),
files=UiStrings().AllFiles)
def on_delete_click(self):
@ -301,9 +301,9 @@ class MediaMediaItem(MediaManagerItem, RegistryProperties):
media_file_paths = Settings().value(self.settings_section + '/media files')
media_file_paths.sort(key=lambda file_path: get_natural_key(file_path.name))
if media_type == MediaType.Audio:
extension = self.media_controller.audio_extensions_list
extension = AUDIO_EXT
else:
extension = self.media_controller.video_extensions_list
extension = VIDEO_EXT
extension = [x[1:] for x in extension]
media = [x for x in media_file_paths if x.suffix in extension]
return media

View File

@ -22,7 +22,6 @@
"""
Package to test the openlp.core.common.path package.
"""
# TODO: fix patches
import os
from pathlib import Path
from unittest import TestCase

View File

@ -27,7 +27,6 @@ from unittest.mock import MagicMock, patch
from openlp.core.common.registry import Registry
from openlp.core.ui.media.mediacontroller import MediaController
from openlp.core.ui.media.vlcplayer import VlcPlayer
from tests.helpers.testmixin import TestMixin
from tests.utils.constants import RESOURCE_PATH
@ -43,26 +42,6 @@ class TestMediaController(TestCase, TestMixin):
Registry.create()
Registry().register('service_manager', MagicMock())
def test_generate_extensions_lists(self):
"""
Test that the extensions are create correctly
"""
# GIVEN: A MediaController and an active player with audio and video extensions
media_controller = MediaController()
media_controller.vlc_player = VlcPlayer(None)
media_controller.vlc_player.is_active = True
media_controller.vlc_player.audio_extensions_list = ['*.mp3', '*.wav', '*.wma', '*.ogg']
media_controller.vlc_player.video_extensions_list = ['*.mp4', '*.mov', '*.avi', '*.ogm']
# WHEN: calling _generate_extensions_lists
media_controller._generate_extensions_lists()
# THEN: extensions list should have been copied from the player to the mediacontroller
assert media_controller.video_extensions_list == media_controller.video_extensions_list, \
'Video extensions should be the same'
assert media_controller.audio_extensions_list == media_controller.audio_extensions_list, \
'Audio extensions should be the same'
def test_resize(self):
"""
Test that the resize method is called correctly

View File

@ -30,7 +30,7 @@ from unittest.mock import MagicMock, call, patch
from openlp.core.common.registry import Registry
from openlp.core.ui.media import MediaState, MediaType
from openlp.core.ui.media.vlcplayer import AUDIO_EXT, VIDEO_EXT, VlcPlayer, get_vlc
from openlp.core.ui.media.vlcplayer import VlcPlayer, get_vlc
from tests.helpers import MockDateTime
from tests.helpers.testmixin import TestMixin
@ -95,8 +95,6 @@ class TestVLCPlayer(TestCase, TestMixin):
assert '&VLC' == vlc_player.display_name
assert vlc_player.parent is None
assert vlc_player.can_folder is True
assert AUDIO_EXT == vlc_player.audio_extensions_list
assert VIDEO_EXT == vlc_player.video_extensions_list
@patch('openlp.core.ui.media.vlcplayer.is_win')
@patch('openlp.core.ui.media.vlcplayer.is_macosx')
@ -958,20 +956,3 @@ class TestVLCPlayer(TestCase, TestMixin):
mocked_controller.seek_slider.setSliderPosition.assert_called_with(300)
expected_calls = [call(True), call(False)]
assert expected_calls == mocked_controller.seek_slider.blockSignals.call_args_list
@patch('openlp.core.ui.media.vlcplayer.translate')
def test_get_info(self, mocked_translate):
"""
Test that get_info() returns some information about the VLC player
"""
# GIVEN: A VlcPlayer
mocked_translate.side_effect = lambda *x: x[1]
vlc_player = VlcPlayer(None)
# WHEN: get_info() is run
info = vlc_player.get_info()
# THEN: The information should be correct
assert 'VLC is an external player which supports a number of different formats.<br/> ' \
'<strong>Audio</strong><br/>' + str(AUDIO_EXT) + '<br/><strong>Video</strong><br/>' + \
str(VIDEO_EXT) + '<br/>' == info