-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
343 lines (242 loc) · 10 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import sys
from command import *
from components import *
from wrapper import *
ResourceTuple = namedtuple("ResourceTuple", "mpd status ui browser")
class ResCommand(Command):
def __init__(self, res, name="unknown", description=""):
super(ResCommand, self).__init__(name, description)
self.mpd = res.mpd
self.status = res.status
self.ui = res.ui
self.browser = res.browser
#### Playback control commands ####
class NextCommand(ResCommand):
def __init__(self, res):
super(NextCommand, self).__init__(res, "next",
"Play next song in playlist")
def execute(self, *unused_args):
try:
self.mpd.player("next")
except CommandError:
raise CommandExecutionError("Couldn't execute 'next' command")
class PrevCommand(ResCommand):
def __init__(self, res):
super(PrevCommand, self).__init__(res, "prev",
"Play previous song in playlist")
def execute(self, *unused_args):
try:
self.mpd.player("previous")
except CommandError:
raise CommandExecutionError("Couldn't execute 'prev' command")
class StopCommand(ResCommand):
def __init__(self, res):
super(StopCommand, self).__init__(res, "stop", "Stop playing")
def execute(self, *unused_args):
try:
self.mpd.player("stop")
except CommandError:
raise CommandExecutionError("Couldn't execute 'stop' command")
class PlayCommand(ResCommand):
def __init__(self, res):
super(PlayCommand, self).__init__(res, "play", "Play song")
def execute(self, *args):
try:
selected = self.status.playlist.selected()
if selected:
self.mpd.player("play", selected.pos)
except CommandError:
raise CommandExecutionError("Couldn't execute 'play' command")
class ToggleCommand(ResCommand):
def __init__(self, res):
super(ToggleCommand, self).__init__(res, "toggle", "Play/pause")
def execute(self, *unused_args):
s = "play" if self.status.state != "play" else "pause"
try:
self.mpd.player(s)
except CommandError:
raise CommandExecutionError("Couldn't execute '%s' command" % s)
class SeekCurCommand(ResCommand):
def __init__(self, res):
super(SeekCurCommand, self).__init__(res, "seekcur", "")
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
try:
int(args[0])
if self.status.current:
self.mpd.seekcur(args[0])
except ValueError:
raise WrongArgException(args[0], "expected number")
except CommandError:
raise CommandExecutionError("Couldn't execute 'seekcur' command")
class SeekCurPercentageCommand(ResCommand):
def __init__(self, res):
super(SeekCurPercentageCommand, self).__init__(res, "seekcur_per", "")
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
try:
f = min(max(0.0, float(args[0])), 1.0)
if self.status.current:
self.mpd.seekcur(str(int(f * self.status.current.time)))
except ValueError:
raise WrongArgException(args[0], "expected number")
except CommandError:
raise CommandExecutionError("Couldn't execute 'seekcur_per " +
"command")
#### Playback options commands ####
class BooleanOptionCommand(ResCommand):
def __init__(self, res, cmd, name, desc):
super(BooleanOptionCommand, self).__init__(res, name, desc)
self.cmd = cmd
def autocomplete(self, n, arg):
return [MatchTuple(s, None) for s in ["false", "true"]
if (n == 0 and s.startswith(arg))]
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
elif args[0] not in ("true", "false"):
raise WrongArgException(args[0], "expected true/false")
try:
self.mpd.option(self.cmd, 1 if args[0] == "true" else 0)
except CommandError:
raise CommandExecutionError("Couldn't execute '%s' command" %
self.cmd)
class IntegerOptionCommand(ResCommand):
def __init__(self, res, cmd, name, desc):
super(IntegerOptionCommand, self).__init__(res, name, desc)
self.cmd = cmd
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
elif not args[0].isdigit():
raise WrongArgException(args[0], "expected an integer")
try:
self.mpd.option(self.cmd, int(args[0]))
except CommandError:
raise CommandExecutionError("Couldn't execute '%s' command" %
self.cmd)
class CrossfadeOptionCommand(IntegerOptionCommand):
def __init__(self, res):
super(CrossfadeOptionCommand, self).__init__(res, "crossfade",
"crossfade", "Set crossfade")
def autocomplete(self, n, arg):
matches = []
if n == 0:
current = self.status.options["xfade"]
if "0".startswith(arg):
matches.append(MatchTuple("0", "Disable crossfade"))
if str(current).startswith(arg):
matches.append(MatchTuple(str(current), "Current value"))
return matches
def boolean_option_command(res, name):
d = {"consume": ["consume", "consume", "Set consume mode"],
"single": ["single", "single", "Set single mode"],
"random": ["random", "random", "Set random mode"],
"repeat": ["repeat", "repeat", "Set random mode"]}
return BooleanOptionCommand(res, *d[name])
#### Playlist commands ####
class PlaylistClearCommand(ResCommand):
def __init__(self, res):
super(PlaylistClearCommand, self).__init__(res, "clear",
"Clear the playlist")
def execute(self, *args):
try:
self.mpd.clear()
except CommandError:
raise CommandExecutionError("Couldn't execute 'clear' command")
class PlaylistDeleteCommand(ResCommand):
def __init__(self, res):
super(PlaylistDeleteCommand, self).__init__(res, "delete",
"Delete song from playlist")
def execute(self, *args):
try:
selected = self.status.playlist.selected()
if selected:
self.mpd.delete(selected.pos)
except CommandError:
traceback.print_exc()
raise CommandExecutionError("Couldn't execute 'delete' command")
class PlaylistGoToCurrentCommand(ResCommand):
def __init__(self, res):
super(PlaylistGoToCurrentCommand, self).__init__(res, "current",
"Go to current")
def execute(self, *args):
try:
if self.status.current != None:
self.status.playlist.select(self.status.current.pos)
except CommandError:
raise CommandExecutionError("Couldn't execute 'clear' command")
#### Browser commands ####
class BrowserAddCommand(ResCommand):
def __init__(self, res):
super(BrowserAddCommand, self).__init__(res, "add",
"Add browser items to playlist")
def execute(self, *args):
if isinstance(self.ui.main, BrowserUI):
selected = self.browser.selected()
if selected != None and selected.ntype in ("directory", "song"):
self.mpd.add(unicode(selected.path))
self.browser.select(1, True)
class BrowserUpdateCommand(ResCommand):
def __init__(self, res):
super(BrowserUpdateCommand, self).__init__(res, "update",
"Update the currently selected directory")
def execute(self, *args):
if len(args) == 0:
self.mpd.update(self.browser.path_str("/"))
else:
self.mpd.update()
class BrowserEnterCommand(ResCommand):
def __init__(self, res):
super(BrowserEnterCommand, self).__init__(res, "etner",
"Enter directory/load file")
def execute(self, *args):
self.browser.enter()
class BrowserGoUpCommand(ResCommand):
def __init__(self, res):
super(BrowserGoUpCommand, self).__init__(res, "go_up",
"Go to the parent directory")
def execute(self, *unused_args):
self.browser.go_up()
#### Application-specific commands ####
class MainSearchCommand(ResCommand):
def __init__(self, res):
super(MainSearchCommand, self).__init__(res, "search",
"Search (filter) items")
def execute(self, *args):
s = None
if len(args) > 0:
s = " ".join(args)
if self.ui.main and self.ui.main.is_list():
self.ui.main.search(s)
class MainSelectCommand(ResCommand):
def __init__(self, res):
super(MainSelectCommand, self).__init__(res, "", "")
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
elif not args[0].isdigit():
raise WrongArgException(args[0], "expected an integer")
if self.ui.main and self.ui.main.is_list():
self.ui.main.select(int(args[0]) - 1)
class MainRelativeSelectCommand(ResCommand):
def __init__(self, res):
super(MainRelativeSelectCommand, self).__init__(res, "", "")
def execute(self, *args):
if len(args) == 0:
raise MissingArgException("requires one argument")
if self.ui.main and self.ui.main.is_list():
self.ui.main.select(int(args[0]), True)
class MainTextSetStartCommand(ResCommand):
def __init__(self, res):
super(MainTextSetStartCommand, self).__init__(res, "", "")
def execute(self, pos, rel=False):
if self.ui.main:
self.ui.main.set_start(pos, rel)
class QuitCommand(ResCommand):
def __init__(self, res):
super(QuitCommand, self).__init__(res, "quit", "Close the program")
def execute(self, *unused_args):
sys.exit()