LINT fixes for mpv plugin

This commit is contained in:
Fabio Manganiello 2022-09-30 10:41:56 +02:00
parent 82ab7face2
commit fef7aff245
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -3,8 +3,15 @@ import threading
from platypush.context import get_bus
from platypush.plugins.media import PlayerState, MediaPlugin
from platypush.message.event.media import MediaPlayEvent, MediaPlayRequestEvent, \
MediaPauseEvent, MediaStopEvent, NewPlayingMediaEvent, MediaSeekEvent, MediaResumeEvent
from platypush.message.event.media import (
MediaPlayEvent,
MediaPlayRequestEvent,
MediaPauseEvent,
MediaStopEvent,
NewPlayingMediaEvent,
MediaSeekEvent,
MediaResumeEvent,
)
from platypush.plugins import action
@ -66,29 +73,58 @@ class MediaMpvPlugin(MediaPlugin):
def _event_callback(self):
def callback(event):
from mpv import MpvEventID as Event
from mpv import MpvEventEndFile as EndFile
from mpv import (
MpvEvent,
MpvEventID as Event,
MpvEventEndFile as EndFile,
)
self.logger.info('Received mpv event: {}'.format(event))
if isinstance(event, MpvEvent):
event = event.as_dict()
evt = event.get('event_id')
if not evt:
return
if (evt == Event.FILE_LOADED or evt == Event.START_FILE) and self._get_current_resource():
if (
evt == Event.FILE_LOADED or evt == Event.START_FILE
) and self._get_current_resource():
self._playback_rebounce_event.set()
self._post_event(NewPlayingMediaEvent, resource=self._get_current_resource(),
title=self._player.filename)
self._post_event(
NewPlayingMediaEvent,
resource=self._get_current_resource(),
title=self._player.filename,
)
elif evt == Event.PLAYBACK_RESTART:
self._playback_rebounce_event.set()
self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename)
self._post_event(
MediaPlayEvent,
resource=self._get_current_resource(),
title=self._player.filename,
)
elif evt == Event.PAUSE:
self._post_event(MediaPauseEvent, resource=self._get_current_resource(), title=self._player.filename)
self._post_event(
MediaPauseEvent,
resource=self._get_current_resource(),
title=self._player.filename,
)
elif evt == Event.UNPAUSE:
self._post_event(MediaResumeEvent, resource=self._get_current_resource(), title=self._player.filename)
elif evt == Event.SHUTDOWN or evt == Event.IDLE or (
evt == Event.END_FILE and event.get('event', {}).get('reason') in
[EndFile.EOF, EndFile.ABORTED, EndFile.QUIT]):
self._post_event(
MediaResumeEvent,
resource=self._get_current_resource(),
title=self._player.filename,
)
elif (
evt == Event.SHUTDOWN
or evt == Event.IDLE
or (
evt == Event.END_FILE
and event.get('event', {}).get('reason')
in [EndFile.EOF, EndFile.ABORTED, EndFile.QUIT]
)
):
playback_rebounced = self._playback_rebounce_event.wait(timeout=0.5)
if playback_rebounced:
self._playback_rebounce_event.clear()
@ -211,8 +247,7 @@ class MediaMpvPlugin(MediaPlugin):
return None, 'No mpv instance is running'
if not self._player.seekable:
return None, 'The resource is not seekable'
pos = min(self._player.time_pos + self._player.time_remaining,
max(0, position))
pos = min(self._player.time_pos + self._player.time_remaining, max(0, position))
self._player.time_pos = pos
return self.status()
@ -233,8 +268,10 @@ class MediaMpvPlugin(MediaPlugin):
return None, 'No mpv instance is running'
if not self._player.seekable:
return None, 'The resource is not seekable'
pos = min(self._player.time_pos + self._player.time_remaining,
self._player.time_pos + offset)
pos = min(
self._player.time_pos + self._player.time_remaining,
self._player.time_pos + offset,
)
return self.seek(pos)
@action
@ -261,11 +298,6 @@ class MediaMpvPlugin(MediaPlugin):
"""Add a subtitles file"""
return self._player.sub_add(filename)
@action
def remove_subtitles(self, sub_id):
""" Remove a subtitles track by id """
return self._player.sub_remove(sub_id)
@action
def toggle_fullscreen(self):
"""Toggle the fullscreen mode"""
@ -324,10 +356,12 @@ class MediaMpvPlugin(MediaPlugin):
return self.set_property(subfile=filename, sub_visibility=True)
@action
def remove_subtitles(self):
def remove_subtitles(self, sub_id=None):
"""Removes (hides) the subtitles"""
if not self._player:
return None, 'No mpv instance is running'
if sub_id:
return self._player.sub_remove(sub_id)
self._player.sub_visibility = False
@action
@ -382,28 +416,35 @@ class MediaMpvPlugin(MediaPlugin):
return {'state': PlayerState.STOP.value}
return {
'audio_channels': getattr(self._player, 'audio_channels'),
'audio_codec': getattr(self._player, 'audio_codec_name'),
'delay': getattr(self._player, 'delay'),
'duration': getattr(self._player, 'playback_time', 0) + getattr(self._player, 'playtime_remaining', 0)
if getattr(self._player, 'playtime_remaining') else None,
'filename': getattr(self._player, 'filename'),
'file_size': getattr(self._player, 'file_size'),
'fullscreen': getattr(self._player, 'fs'),
'mute': getattr(self._player, 'mute'),
'name': getattr(self._player, 'name'),
'pause': getattr(self._player, 'pause'),
'percent_pos': getattr(self._player, 'percent_pos'),
'position': getattr(self._player, 'playback_time'),
'seekable': getattr(self._player, 'seekable'),
'state': (PlayerState.PAUSE.value if self._player.pause else PlayerState.PLAY.value),
'title': getattr(self._player, 'media_title') or getattr(self._player, 'filename'),
'audio_channels': getattr(self._player, 'audio_channels', None),
'audio_codec': getattr(self._player, 'audio_codec_name', None),
'delay': getattr(self._player, 'delay', None),
'duration': getattr(self._player, 'playback_time', 0)
+ getattr(self._player, 'playtime_remaining', 0)
if getattr(self._player, 'playtime_remaining', None)
else None,
'filename': getattr(self._player, 'filename', None),
'file_size': getattr(self._player, 'file_size', None),
'fullscreen': getattr(self._player, 'fs', None),
'mute': getattr(self._player, 'mute', None),
'name': getattr(self._player, 'name', None),
'pause': getattr(self._player, 'pause', None),
'percent_pos': getattr(self._player, 'percent_pos', None),
'position': getattr(self._player, 'playback_time', None),
'seekable': getattr(self._player, 'seekable', None),
'state': (
PlayerState.PAUSE.value
if self._player.pause
else PlayerState.PLAY.value
),
'title': getattr(self._player, 'media_title', None)
or getattr(self._player, 'filename', None),
'url': self._get_current_resource(),
'video_codec': getattr(self._player, 'video_codec'),
'video_format': getattr(self._player, 'video_format'),
'volume': getattr(self._player, 'volume'),
'volume_max': getattr(self._player, 'volume_max'),
'width': getattr(self._player, 'width'),
'video_codec': getattr(self._player, 'video_codec', None),
'video_format': getattr(self._player, 'video_format', None),
'volume': getattr(self._player, 'volume', None),
'volume_max': getattr(self._player, 'volume_max', None),
'width': getattr(self._player, 'width', None),
}
def on_stop(self, callback):
@ -413,8 +454,9 @@ class MediaMpvPlugin(MediaPlugin):
if not self._player or not self._player.stream_path:
return
return ('file://' if os.path.isfile(self._player.stream_path)
else '') + self._player.stream_path
return (
'file://' if os.path.isfile(self._player.stream_path) else ''
) + self._player.stream_path
def _get_resource(self, resource):
if self._is_youtube_resource(resource):