From 2670d4009416410466b2a62b1b368af5016e94a4 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 6 Dec 2023 01:31:05 +0100 Subject: [PATCH] Added `get_default_media_plugin` utility. --- platypush/plugins/media/__init__.py | 6 ++-- .../plugins/media/webtorrent/__init__.py | 12 +++---- platypush/utils/media.py | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 platypush/utils/media.py diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 4b0445c85..536e7658a 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -151,14 +151,14 @@ class MediaPlugin(Plugin, ABC): 'f4b', } - _supported_media_plugins = { + supported_media_plugins = [ 'media.mplayer', 'media.omxplayer', 'media.mpv', 'media.vlc', 'media.chromecast', 'media.gstreamer', - } + ] _supported_media_types = ['file', 'jellyfin', 'plex', 'torrent', 'youtube'] _default_search_timeout = 60 # 60 seconds @@ -217,7 +217,7 @@ class MediaPlugin(Plugin, ABC): if self.__class__.__name__ == 'MediaPlugin': # Abstract class, initialize with the default configured player for plugin_name in Config.get_plugins().keys(): - if plugin_name in self._supported_media_plugins: + if plugin_name in self.supported_media_plugins: player = get_plugin(plugin_name) if player and player.is_local(): # Local players have priority as default if configured diff --git a/platypush/plugins/media/webtorrent/__init__.py b/platypush/plugins/media/webtorrent/__init__.py index 4f4842805..2d22b74ae 100644 --- a/platypush/plugins/media/webtorrent/__init__.py +++ b/platypush/plugins/media/webtorrent/__init__.py @@ -37,7 +37,7 @@ class MediaWebtorrentPlugin(MediaPlugin): """ - _supported_media_plugins = { + supported_media_plugins = { 'media.mplayer', 'media.omxplayer', 'media.mpv', @@ -103,7 +103,7 @@ class MediaWebtorrentPlugin(MediaPlugin): def _init_media_player(self): self._media_plugin = None - for plugin_name in self._supported_media_plugins: + for plugin_name in self.supported_media_plugins: try: if Config.get(plugin_name): self._media_plugin = get_plugin(plugin_name) @@ -113,11 +113,9 @@ class MediaWebtorrentPlugin(MediaPlugin): if not self._media_plugin: raise RuntimeError( - ( - 'No media player specified and no ' - + 'compatible media plugin configured - ' - + 'supported media plugins: {}' - ).format(self._supported_media_plugins) + 'No media player specified and no ' + + 'compatible media plugin configured - ' + + f'supported media plugins: {self.supported_media_plugins}' ) def _read_process_line(self): diff --git a/platypush/utils/media.py b/platypush/utils/media.py new file mode 100644 index 000000000..2a6cde1e7 --- /dev/null +++ b/platypush/utils/media.py @@ -0,0 +1,33 @@ +from typing import List + +from platypush.config import Config +from platypush.context import get_plugin +from platypush.plugins.media import MediaPlugin + + +def get_default_media_plugin() -> MediaPlugin: + """ + Get the default media plugin based on the current configuration. + """ + + enabled_plugins: List[MediaPlugin] = [] + cfg = Config.get() or {} + + for plugin_name in MediaPlugin.supported_media_plugins: + try: + plugin = get_plugin(plugin_name) + if plugin and plugin_name in cfg and not cfg[plugin_name].get('disabled'): + enabled_plugins.append(plugin) + except Exception: + pass + + local_plugins = [plugin for plugin in enabled_plugins if plugin.is_local()] + + if local_plugins: + return local_plugins[0] + + assert ( + enabled_plugins + ), f'No media plugin is enabled. Supported plugins: {MediaPlugin.supported_media_plugins}' + + return enabled_plugins[0]