some pylint lead improvements

This commit is contained in:
simon 2021-01-29 10:21:27 +07:00
parent f8707672a0
commit bc933a56f4
3 changed files with 36 additions and 31 deletions

View File

@ -1,6 +1,12 @@
#!/usr/bin/env python3
# https://python-mpd2.readthedocs.io/en/latest
# https://dunst-project.org/documentation/
"""
Main file to handle mpd controll.
Will read argument passed to script to figure
out what to do with it.
docs:
- https://python-mpd2.readthedocs.io/en/latest
- https://dunst-project.org/documentation/
"""
import sys
import subprocess
@ -9,22 +15,25 @@ from mpd import MPDClient
import mpd_playback
import mpd_state
signal_id = 11
cover_art_temp = '/tmp/mpd_cover_art_temp.jpg'
arguments = sys.argv
if len(arguments) == 1:
mpc_command = 'state'
elif len(arguments) == 2:
mpc_command = arguments[1]
def get_conf():
""" setting up variables """
arguments = sys.argv
signal_id = 11
cover_art_path = '/tmp/mpd_cover_art_temp.jpg'
if len(arguments) == 1:
mpc_command = 'state'
elif len(arguments) == 2:
mpc_command = arguments[1]
return mpc_command, cover_art_path, signal_id
def main():
"""
establish connection to daemon
then process the command and close at end
"""
establish connection to daemon
then process the command and disconnect at end
"""
mpc_command, cover_art_path, signal_id = get_conf()
client = MPDClient()
# try to connect, if error try to start daemon
try:
@ -32,7 +41,7 @@ def main():
except ConnectionRefusedError:
# mpd is not connecting, try to start
if mpc_command == 'toggle':
output = subprocess.run(["mpd"], capture_output=True)
output = subprocess.run(["mpd"], capture_output=True, check=False)
if output.returncode == 0:
# connect
client.connect("localhost", 6600)
@ -47,9 +56,8 @@ def main():
elif mpc_command == 'prev':
mpd_playback.play_prev(client)
elif mpc_command == 'state':
mpd_state.get_state(client, cover_art_temp)
# close connection
client.close()
mpd_state.get_state(client, cover_art_path)
# disconnect connection
client.disconnect()

View File

@ -1,3 +1,4 @@
""" handles the play interactions with mpd """
from time import sleep
import subprocess
@ -9,7 +10,7 @@ def fade(way, current_vol, client):
elif way == 'in':
steps = 50
amount = +2
for i in range(steps):
for _ in range(steps):
client.volume(amount)
sleep(0.03)
@ -29,7 +30,7 @@ def toggle(client, signal_id):
fade('in', current_vol, client)
# call pkill to refresh status bar
subprocess.call(["pkill", "-RTMIN+" + str(signal_id), "i3blocks"])
def play_next(client):
""" skip to next in playlist """
@ -49,4 +50,3 @@ def play_prev(client):
client.pause()
client.setvol(current_vol)
client.previous()

View File

@ -1,4 +1,4 @@
""" prints the state of mpd to use as a notification """
import subprocess
@ -17,10 +17,9 @@ def print_state(artist, album, song_title, mpd_status):
print(print_main)
print(print_small)
print(print_color)
return
def get_state(client, cover_art_temp):
def get_state(client, cover_art_path):
""" sends a notification of the current status """
# read out status from client
now_playing = client.currentsong()
@ -31,12 +30,12 @@ def get_state(client, cover_art_temp):
album = now_playing['album']
song_title = now_playing['title']
# try to write cover art to temp file
cover_art_blob = client.readpicture(music_file)
try:
cover_art = client.readpicture(music_file)
with open(cover_art_temp, 'w+b') as f:
f.write(cover_art['binary'])
except:
cover_art = False
with open(cover_art_path, 'w+b') as cover_file:
cover_file.write(cover_art_blob['binary'])
except KeyError:
cover_art_blob = False
# set player status icon
mpd_status = client_status['state']
if mpd_status == 'play':
@ -48,6 +47,4 @@ def get_state(client, cover_art_temp):
# send message
title = icon + '\t' + artist
message = album + " - " + song_title
subprocess.call(['notify-send', title, message, '-i', cover_art_temp])
return
subprocess.call(['notify-send', title, message, '-i', cover_art_path])