From ed358ef024a469d3884d87f41789a15fbc60f375 Mon Sep 17 00:00:00 2001 From: Daniel Voogsgerd Date: Wed, 27 Mar 2019 16:33:15 +0100 Subject: [PATCH 1/3] feat(Spotify): Add argument parser --- resources/custom_modules/mediaplayer.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/resources/custom_modules/mediaplayer.py b/resources/custom_modules/mediaplayer.py index 64809a68..e19ce5b8 100755 --- a/resources/custom_modules/mediaplayer.py +++ b/resources/custom_modules/mediaplayer.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import argparse import sys import signal import gi @@ -52,7 +53,14 @@ def signal_handler(sig, frame): sys.exit(0) +def parse_arguments(): + parser = argparse.ArgumentParser() + + return parser.parse_args() + + def main(): + arguments = parse_arguments() manager = Playerctl.PlayerManager() loop = GLib.MainLoop() From db69b449bad14e781d227d4f28af4c21dc8df6e4 Mon Sep 17 00:00:00 2001 From: Daniel Voogsgerd Date: Wed, 27 Mar 2019 16:33:47 +0100 Subject: [PATCH 2/3] feat(Spotify): Add logger --- resources/custom_modules/mediaplayer.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/resources/custom_modules/mediaplayer.py b/resources/custom_modules/mediaplayer.py index e19ce5b8..3c688659 100755 --- a/resources/custom_modules/mediaplayer.py +++ b/resources/custom_modules/mediaplayer.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 import argparse +import logging import sys import signal import gi gi.require_version('Playerctl', '2.0') from gi.repository import Playerctl, GLib +logger = logging.getLogger(__name__) + def on_play(player, status, manager): on_metadata(player, player.props.metadata, manager) @@ -56,11 +59,26 @@ def signal_handler(sig, frame): def parse_arguments(): parser = argparse.ArgumentParser() + # Increase verbosity with every occurance of -v + parser.add_argument('-v', '--verbose', action="count", default=0) + return parser.parse_args() def main(): arguments = parse_arguments() + + # Initialize logging + logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, + format='%(name)s %(levelname)s %(message)s') + + # Logging is set by default to WARN and higher. + # With every occurance of -v it's lowered by one + logger.setLevel(max((3 - arguments.verbose) * 10, 0)) + + # Log the sent command line arguments + logger.debug('Arguments received {}'.format(vars(arguments))) + manager = Playerctl.PlayerManager() loop = GLib.MainLoop() From 3bcf3befeca697069ecf8d178c5fa5abb3983052 Mon Sep 17 00:00:00 2001 From: Daniel Voogsgerd Date: Wed, 27 Mar 2019 17:00:12 +0100 Subject: [PATCH 3/3] chore(Spotify): Add logging for events --- resources/custom_modules/mediaplayer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resources/custom_modules/mediaplayer.py b/resources/custom_modules/mediaplayer.py index 3c688659..4d5f5146 100755 --- a/resources/custom_modules/mediaplayer.py +++ b/resources/custom_modules/mediaplayer.py @@ -11,10 +11,12 @@ logger = logging.getLogger(__name__) def on_play(player, status, manager): + logger.info('Received new playback status') on_metadata(player, player.props.metadata, manager) def on_metadata(player, metadata, manager): + logger.info("Received new metadata") track_info = '' if player.props.player_name == 'spotify' and \ @@ -37,11 +39,13 @@ def on_metadata(player, metadata, manager): def on_player_vanished(manager, player): + logger.info("Player has vanished") sys.stdout.write("\n") sys.stdout.flush() def init_player(manager, name): + logger.debug("Initialize player: {player}".format(player=name.name)) player = Playerctl.Player.new_from_name(name) player.connect('playback-status', on_play, manager) player.connect('metadata', on_metadata, manager) @@ -50,6 +54,7 @@ def init_player(manager, name): def signal_handler(sig, frame): + logger.debug('Received signal to stop, exiting') sys.stdout.write("\n") sys.stdout.flush() # loop.quit()