aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/moc.py
blob: d396b1e000100d5db5eb775dbe911ca8c5f9c33e (plain) (blame)
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
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016, 2017, 2018  Mattias Andrée (maandree@kth.se)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys
import subprocess

from util import *


class MOC: # TODO add support for waiting for events and reading settings
    '''
    Music On Console interface
    
    @variable  state  The state of moc, one of: MOC.NOT_RUNNING,
                      MOC.STOPPED, MOC.PAUSED, MOC.PLAYING
    '''
    
    
    NOT_RUNNING = None
    '''
    The MOC server is not running
    '''
    
    STOPPED = 'STOP'
    '''
    No file is played
    '''
    
    PAUSED = 'PAUSE'
    '''
    The current file is paused
    '''
    
    PLAYING = 'PLAY'
    '''
    The current file is playing
    '''
    
    
    def __init__(self):
        '''
        Constructor
        '''
        self.__info = {}
        for line in MOC.__interact('--info').stdout.read().decode('utf-8', 'replace').split('\n'):
            if ': ' in line:
                line = line.split(': ')
                self.__info[line[0]] = ': '.join(line[1:])
        self.state = self['State'] if 'State' in self else None
    
    
    def __contains__(self, key):
        '''
        Get whether or not a key is available
        
        @param   key:str  The key
        @return  :bool    The availability
        '''
        return key in self.__info
    
    
    def __getitem__(self, key):
        '''
        Get a details
        
        @param   key:str  The key
        @return  :str     The value associated with the key
        
        The following are defined (may be empty) if moc is
        playing or paused: File, Title, Artist, SongTitle,
        Album, TotalTime, TimeLet, TotalSec, CurrentTime,
        CurrentSec, Bitrate.
        '''
        return self.__info[key]
    
    
    def keys(self):
        '''
        Get available detail keys
        
        @return  :itr<str>  The keys
        '''
        return self.__info.keys()
    
    
    @staticmethod
    def __interact(*args):
        '''
        Run mocp
        
        @param   args:*str  The command line arguments, except the first
        @return  :Popen     The spawned process
        '''
        command = ['mocp'] + list(args)
        return subprocess.Popen(command, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
    
    
    @staticmethod
    def enqueue(files):
        '''
        Add files to the queue
        
        @param   files:itr<str>  The files
        @return  :Popen          The spawned process
        '''
        return MOC.__interact('--enqueue', '--', *files)
    
    
    @staticmethod
    def append(files):
        '''
        Append files, directories (recursively) and playlists to the playlist
        
        @param   files:itr<str>  The files
        @return  :Popen          The spawned process
        '''
        return MOC.__interact('--append', '--', *files)
    
    
    @staticmethod
    def clear():
        '''
        Clear the playlist
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--clear')
    
    
    @staticmethod
    def play():
        '''
        Start playing from the first item on the playlist
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--play')
    
    
    @staticmethod
    def next():
        '''
        Request playing the next song from the server's playlist
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--next')
    
    
    @staticmethod
    def previous():
        '''
        Request playing the previous song from the server's playlist
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--previous')
    
    
    @staticmethod
    def stop():
        '''
        Request the server to stop playing
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--stop')
    
    
    @staticmethod
    def exit():
        '''
        Bring down the server
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--exit')
    
    
    @staticmethod
    def pause():
        '''
        Request the server to pause playing
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--pause')
    
    
    @staticmethod
    def unpause():
        '''
        Request the server to resume playing when paused
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--unpause')
    
    
    @staticmethod
    def toggle_pause():
        '''
        Toggle between play and pause
        
        @return  :Popen  The spawned process
        '''
        return MOC.__interact('--toggle-pause')
    
    
    @staticmethod
    def seek(seconds):
        '''
        Seek forward (positive) or backward (negative) in the file currently being played
        
        @param   seconds:int  The number of seconds to seek forward, negative for backwards
        @return  :Popen?      The spawned process, `None` if nothing is spawned
        '''
        if not seconds == 0:
            return MOC.__interact('--seek', ('+%i' if seconds > 0 else '%i') % (seconds))
        return None
    
    
    @staticmethod
    def volume(volume, relative = True):
        '''
        Adjust the mixer volume
        
        @param   volume:int     The volume or volume increment
        @param   relative:bool  Whether to adjust, otherwise set
        @return  :Popen         The spawned process
        '''
        if relative and (not volume == 0):
            return MOC.__interact('--volume', ('+%i' if volume > 0 else '%i') % (volume))
        return MOC.__interact('--volume', '%i' % (max(0, volume)))
    
    
    @staticmethod
    def jump(position, precents = False):
        '''
        Jump to some position in the current file
        
        @param   position:int   The position either in seconds or precents
        @param   precents:bool  Whether to use precents, otherwise seconds
        @return  :Popen         The spawned process
        '''
        return MOC.__interact('--jump', '%i%s' % (position, '%' if precents else 's'))
    
    
    def toggle(shuffle = False, repeat = False, autonext = False):
        '''
        Toggle options
        
        @param   shuffle:bool   Whether to toggle shuffle
        @param   repeat:bool    Whether to toggle repeat
        @param   autonext:bool  Whether to toggle autonext
        @return  :Popen?        The spawned process, `None` if nothing is spawned
        '''
        if any([shuffle, repeat, autonext]):
            opts = [('shuffle', shuffle), ('repeat', repeat), ('autonext', autonext)]
            opts = map(lambda opt_val : opt_val[0] if opt_val[1] else None, opts)
            opts = filter(lambda opt : opt is not None, opts)
            return MOC.__interact('--toggle', ','.join(opts))
        return None
    
    
    def on(shuffle = False, repeat = False, autonext = False):
        '''
        Turn on options
        
        @param   shuffle:bool   Whether to turn on shuffle
        @param   repeat:bool    Whether to turn on repeat
        @param   autonext:bool  Whether to turn on autonext
        @return  :Popen?        The spawned process, `None` if nothing is spawned
        '''
        if any([shuffle, repeat, autonext]):
            opts = [('shuffle', shuffle), ('repeat', repeat), ('autonext', autonext)]
            opts = map(lambda opt_val : opt_val[0] if opt_val[1] else None, opts)
            opts = filter(lambda opt : opt is not None, opts)
            return MOC.__interact('--on', ','.join(opts))
        return None
    
    
    def off(shuffle = False, repeat = False, autonext = False):
        '''
        Turn off options
        
        @param   shuffle:bool   Whether to turn off shuffle
        @param   repeat:bool    Whether to turn off repeat
        @param   autonext:bool  Whether to turn off autonext
        @return  :Popen?        The spawned process, `None` if nothing is spawned
        '''
        if any([shuffle, repeat, autonext]):
            opts = [('shuffle', shuffle), ('repeat', repeat), ('autonext', autonext)]
            opts = map(lambda opt_val : opt_val[0] if opt_val[1] else None, opts)
            opts = filter(lambda opt : opt is not None, opts)
            return MOC.__interact('--off', ','.join(opts))
        return None