Merge pull request #250 from DanielVoogsgerd/feature-arguments-logging

Feature: Command line arguments/Logging
This commit is contained in:
Alex 2019-03-28 09:38:33 +01:00 committed by GitHub
commit 7bd5454e43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,16 +1,22 @@
#!/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):
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 \
@ -33,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)
@ -46,13 +54,36 @@ 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()
sys.exit(0)
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()