23
23
# https://github.com/Mic92/python-mpd2/issues
24
24
25
25
from __future__ import absolute_import
26
+ from __future__ import unicode_literals
26
27
from mpd .base import CommandError
27
28
from mpd .base import CommandListError
28
29
from mpd .base import ERROR_PREFIX
36
37
from mpd .base import mpd_commands
37
38
from twisted .internet import defer
38
39
from twisted .protocols import basic
39
- from types import GeneratorType
40
+ import types
40
41
41
42
42
43
def _create_command (wrapper , name , callback ):
@@ -49,7 +50,7 @@ def bound_callback(lines):
49
50
50
51
@mpd_command_provider
51
52
class MPDProtocol (basic .LineReceiver , MPDClientBase ):
52
- delimiter = " \n "
53
+ delimiter = b' \n '
53
54
54
55
def __init__ (self , default_idle = True , idle_result = None ):
55
56
super (MPDProtocol , self ).__init__ (use_unicode = True )
@@ -74,7 +75,7 @@ def add_command(cls, name, callback):
74
75
return
75
76
# create command and hook it on class
76
77
func = _create_command (cls ._execute , name , callback )
77
- escaped_name = name .replace (" " , "_" )
78
+ escaped_name = name .replace (' ' , '_' )
78
79
setattr (cls , escaped_name , func )
79
80
80
81
def lineReceived (self , line ):
@@ -92,7 +93,7 @@ def lineReceived(self, line):
92
93
state_list [0 ].errback (CommandError (error ))
93
94
for state in state_list [1 :- 1 ]:
94
95
state .errback (
95
- CommandListError (" An earlier command failed." ))
96
+ CommandListError (' An earlier command failed.' ))
96
97
state_list [- 1 ].errback (CommandListError (error ))
97
98
del self ._state [0 ]
98
99
del self ._command_list_results [0 ]
@@ -112,10 +113,10 @@ def lineReceived(self, line):
112
113
def _execute (self , command , args , parser ):
113
114
# close or kill command in command list not allowed
114
115
if self ._command_list and not callable (parser ):
115
- msg = " {} not allowed in command list" .format (command )
116
+ msg = ' {} not allowed in command list' .format (command )
116
117
raise CommandListError (msg )
117
118
# default state idle and currently in idle state, trigger noidle
118
- if self ._default_idle and self ._idle and command != " idle" :
119
+ if self ._default_idle and self ._idle and command != ' idle' :
119
120
self .noidle ().addCallback (self ._dispatch_noidle_result )
120
121
# write command to MPD
121
122
self ._write_command (command , args )
@@ -134,15 +135,11 @@ def _execute(self, command, args, parser):
134
135
return deferred
135
136
136
137
def _write_command (self , command , args = []):
137
- parts = [command ]
138
- parts += ['"{}"' .format (escape (arg .encode ('utf-8' ))
139
- if isinstance (arg , unicode ) else str (arg )) for arg in args ]
140
- cmd = " " .join (parts )
141
- self .sendLine (cmd )
138
+ parts = [command ] + ['"{}"' .format (escape (arg )) for arg in args ]
139
+ self .sendLine (' ' .join (parts ).encode ('utf-8' ))
142
140
143
141
def _parse_command_list_item (self , result ):
144
- # TODO: find a better way to do this
145
- if type (result ) == GeneratorType :
142
+ if isinstance (result , types .GeneratorType ):
146
143
result = list (result )
147
144
self ._command_list_results [0 ].append (result )
148
145
return result
@@ -177,13 +174,13 @@ def _dispatch_idle_result(self, result):
177
174
178
175
def idle (self ):
179
176
if self ._idle :
180
- raise CommandError (" Already in idle state" )
177
+ raise CommandError (' Already in idle state' )
181
178
self ._idle = True
182
179
return self ._execute ('idle' , [], self ._parse_list )
183
180
184
181
def noidle (self ):
185
182
if not self ._idle :
186
- raise CommandError (" Not in idle state" )
183
+ raise CommandError (' Not in idle state' )
187
184
# delete first pending deferred, idle returns nothing when
188
185
# noidle gets called
189
186
self ._state .pop (0 )
@@ -192,18 +189,18 @@ def noidle(self):
192
189
193
190
def command_list_ok_begin (self ):
194
191
if self ._command_list :
195
- raise CommandListError (" Already in command list" )
192
+ raise CommandListError (' Already in command list' )
196
193
if self ._default_idle and self ._idle :
197
194
self .noidle ().addCallback (self ._dispatch_noidle_result )
198
- self ._write_command (" command_list_ok_begin" )
195
+ self ._write_command (' command_list_ok_begin' )
199
196
self ._command_list = True
200
197
self ._command_list_results .append ([])
201
198
self ._state .append ([])
202
199
203
200
def command_list_end (self ):
204
201
if not self ._command_list :
205
- raise CommandListError (" Not in command list" )
206
- self ._write_command (" command_list_end" )
202
+ raise CommandListError (' Not in command list' )
203
+ self ._write_command (' command_list_end' )
207
204
deferred = defer .Deferred ()
208
205
deferred .addCallback (self ._parse_command_list_end )
209
206
self ._state [- 1 ].append (deferred )
0 commit comments