2424from beets .plugins import BeetsPlugin
2525
2626
27- class MusicPlayerDaemonClient () :
27+ class MusicPlayerDaemonClient :
2828 """Simple socket client to provide connectivity to a Music Player Daemon
2929 server for updates and queue management.
3030 """
3131
32- def __init__ (self , host = ' localhost' , port = 6600 , password = None ):
32+ def __init__ (self , host = " localhost" , port = 6600 , password = None ):
3333 self .sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
3434 self .sock .connect ((host , port ))
3535 self .sock .settimeout (0.25 )
3636 acknowledgement = self ._read ()[0 ]
37- if not acknowledgement .startswith (' OK MPD' ):
37+ if not acknowledgement .startswith (" OK MPD" ):
3838 self .close ()
3939 if password :
4040 self ._send ('password "{}"' .format (password ))
4141 response = self ._read ()[0 ]
42- if not response .startswith ('OK' ):
42+ if not response .startswith ("OK" ):
4343 self .close ()
4444
4545 def _read (self ):
@@ -48,7 +48,7 @@ def _read(self):
4848 This operation may return an empty list if the server does not send
4949 anything during the socket timeout period.
5050 """
51- data = b''
51+ data = b""
5252 while True :
5353 try :
5454 data_buffer = self .sock .recv (1024 )
@@ -57,28 +57,27 @@ def _read(self):
5757 if not data_buffer :
5858 break
5959 data += data_buffer
60- return data .decode ().strip ().split (' \n ' )
60+ return data .decode ().strip ().split (" \n " )
6161
6262 def _send (self , data ):
6363 """Sends a string to the server."""
64- if data [- 1 ] != ' \n ' :
65- data = '' .join ([data , ' \n ' ])
64+ if data [- 1 ] != " \n " :
65+ data = "" .join ([data , " \n " ])
6666 self .sock .send (data .encode ())
6767
6868 def add (self , path ):
6969 """Adds given path to MPD queue."""
7070 self ._send ('add "{}"' .format (path ))
71- return self ._read ()[0 ] == 'OK'
71+ return self ._read ()[0 ] == "OK"
7272
7373 def close (self ):
7474 """Closes the connection to the server."""
75- self ._send (' close' )
75+ self ._send (" close" )
7676 self .sock .close ()
7777
7878 def status (self ):
79- """Fetches status from Music Player Daemon. Returns a tuple of strings.
80- """
81- self ._send ('status' )
79+ """Fetches status from Music Player Daemon. Returns a tuple of strings."""
80+ self ._send ("status" )
8281 return self ._read ()
8382
8483 def update (self , directory ):
@@ -88,12 +87,12 @@ def update(self, directory):
8887 """
8988 self ._send ('update "{}"' .format (directory ))
9089 response = self ._read ()
91- if response [- 1 ] != 'OK' :
90+ if response [- 1 ] != "OK" :
9291 return
9392 while True :
9493 updating = False
9594 for line in self .status ():
96- if line .startswith (' updating_db' ):
95+ if line .startswith (" updating_db" ):
9796 updating = True
9897 if not updating :
9998 break
@@ -108,17 +107,19 @@ class MPDQueuePlugin(BeetsPlugin):
108107
109108 def __init__ (self ):
110109 super (MPDQueuePlugin , self ).__init__ ()
111- config ['mpd' ].add ({
112- 'host' : os .environ .get ('MPD_HOST' , 'localhost' ),
113- 'port' : 6600 ,
114- 'password' : '' ,
115- })
116- config ['mpd' ]['password' ].redact = True
110+ config ["mpd" ].add (
111+ {
112+ "host" : os .environ .get ("MPD_HOST" , "localhost" ),
113+ "port" : 6600 ,
114+ "password" : "" ,
115+ }
116+ )
117+ config ["mpd" ]["password" ].redact = True
117118
118119 self .files = []
119120
120- self .register_listener (' import_task_files' , self .import_task_files )
121- self .register_listener (' cli_exit' , self .update_queue )
121+ self .register_listener (" import_task_files" , self .import_task_files )
122+ self .register_listener (" cli_exit" , self .update_queue )
122123
123124 def import_task_files (self , task , session ):
124125 """Track all the files added during an import operation so they can be
@@ -128,14 +129,14 @@ def import_task_files(self, task, session):
128129 property as it indicates a reimport of existing library files.
129130 """
130131 if not task .toppath :
131- self ._log .debug (u' Skipping library re-import' )
132+ self ._log .debug (" Skipping library re-import" )
132133 return
133134
134135 tracks = []
135136 items = sorted (task .imported_items (), key = lambda x : x .track )
136137 for item in items :
137138 destination = item .destination (relative_to_libdir = True ).decode ()
138- self ._log .debug (u' {0} will be added to queue' , destination )
139+ self ._log .debug (" {0} will be added to queue" , destination )
139140 tracks .append (destination )
140141 self .files += tracks
141142
@@ -149,27 +150,27 @@ def update_queue(self, lib):
149150 files are in the database when they are added.
150151 """
151152 if not self .files :
152- self ._log .debug (u' No files to add to queue' )
153+ self ._log .debug (" No files to add to queue" )
153154 return
154155
155- host = config [' mpd' ][ ' host' ].as_str ()
156- port = config [' mpd' ][ ' port' ].get (int )
157- password = config [' mpd' ][ ' password' ].as_str ()
156+ host = config [" mpd" ][ " host" ].as_str ()
157+ port = config [" mpd" ][ " port" ].get (int )
158+ password = config [" mpd" ][ " password" ].as_str ()
158159 client = MusicPlayerDaemonClient (host , port , password )
159160
160161 directories = set ()
161162 for file_ in self .files :
162163 directories .add (os .path .dirname (file_ ))
163164 for directory in directories :
164- self ._log .debug (u' Updating directory {0}' , directory )
165+ self ._log .debug (" Updating directory {0}" , directory )
165166 client .update (directory )
166- self ._log .debug (u' Finished updating {0}' , directory )
167+ self ._log .debug (" Finished updating {0}" , directory )
167168
168169 for file_ in self .files :
169- self ._log .debug (u' Adding {0} to queue' , file_ )
170+ self ._log .debug (" Adding {0} to queue" , file_ )
170171 success = client .add (file_ )
171172 if success :
172- self ._log .debug (u' Added {0} to queue' , file_ )
173+ self ._log .debug (" Added {0} to queue" , file_ )
173174 else :
174- self ._log .warning (u' Failed to add {0} to queue' , file_ )
175+ self ._log .warning (" Failed to add {0} to queue" , file_ )
175176 client .close ()
0 commit comments