2016-11-12 08:34:21 +01:00
import string
2016-11-07 22:14:46 +01:00
import datetime
import subprocess
2016-11-12 08:34:21 +01:00
from collections import defaultdict
import bumblebee . util
import bumblebee . module
2016-11-07 22:14:46 +01:00
def description ( ) :
return " Displays the current song and artist playing in cmus "
2016-11-12 08:34:21 +01:00
def parameters ( ) :
return [
" cmus.format: Format of the displayed song information, arbitrary tags (as available from cmus-remote -Q) can be used (defaults to {artist} - {title} {position} / {duration} ) "
]
2016-11-07 22:14:46 +01:00
class Module ( bumblebee . module . Module ) :
def __init__ ( self , output , config , alias ) :
super ( Module , self ) . __init__ ( output , config , alias )
2016-11-12 08:34:21 +01:00
self . _status = " default "
self . _fmt = self . _config . parameter ( " format " , " {artist} - {title} {position} / {duration} " )
2016-11-07 22:14:46 +01:00
2016-11-12 12:11:42 +01:00
output . add_callback ( module = " cmus.prev " , button = 1 , cmd = " cmus-remote -r " )
output . add_callback ( module = " cmus.next " , button = 1 , cmd = " cmus-remote -n " )
output . add_callback ( module = " cmus.shuffle " , button = 1 , cmd = " cmus-remote -S " )
output . add_callback ( module = " cmus.repeat " , button = 1 , cmd = " cmus-remote -R " )
output . add_callback ( module = self . instance ( ) , button = 1 , cmd = " cmus-remote -u " )
2016-11-12 08:34:21 +01:00
def _loadsong ( self ) :
process = subprocess . Popen ( [ " cmus-remote " , " -Q " ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
self . _query , self . _error = process . communicate ( )
2016-11-07 22:14:46 +01:00
self . _query = self . _query . decode ( " utf-8 " ) . split ( " \n " )
2016-11-12 08:34:21 +01:00
self . _status = " default "
def _tags ( self ) :
tags = defaultdict ( lambda : ' ' )
2016-11-07 22:14:46 +01:00
for line in self . _query :
2016-11-12 08:34:21 +01:00
if line . startswith ( " status " ) :
2016-11-12 12:11:42 +01:00
status = line . split ( " " , 2 ) [ 1 ]
2016-11-12 08:34:21 +01:00
self . _status = status
if line . startswith ( " tag " ) :
2016-11-12 12:11:42 +01:00
key , value = line . split ( " " , 2 ) [ 1 : ]
2016-11-12 08:34:21 +01:00
tags . update ( { key : value } )
if line . startswith ( " duration " ) :
2016-11-12 12:11:42 +01:00
sec = line . split ( " " ) [ 1 ]
2016-11-12 08:34:21 +01:00
tags . update ( { " duration " : bumblebee . util . durationfmt ( int ( sec ) ) } )
if line . startswith ( " position " ) :
2016-11-12 12:11:42 +01:00
sec = line . split ( " " ) [ 1 ]
2016-11-12 08:34:21 +01:00
tags . update ( { " position " : bumblebee . util . durationfmt ( int ( sec ) ) } )
2016-11-12 12:11:42 +01:00
if line . startswith ( " set repeat " ) :
self . _repeat = False if line . split ( " " ) [ 2 ] == " false " else True
if line . startswith ( " set shuffle " ) :
self . _shuffle = False if line . split ( " " ) [ 2 ] == " false " else True
2016-11-12 08:34:21 +01:00
return tags
def widgets ( self ) :
self . _loadsong ( )
tags = self . _tags ( )
2016-11-12 12:11:42 +01:00
return [
bumblebee . output . Widget ( self , " " , instance = " cmus.prev " ) ,
bumblebee . output . Widget ( self , string . Formatter ( ) . vformat ( self . _fmt , ( ) , tags ) ) ,
bumblebee . output . Widget ( self , " " , instance = " cmus.next " ) ,
bumblebee . output . Widget ( self , " " , instance = " cmus.shuffle " ) ,
bumblebee . output . Widget ( self , " " , instance = " cmus.repeat " ) ,
]
2016-11-12 08:34:21 +01:00
2016-11-07 22:14:46 +01:00
def state ( self , widget ) :
2016-11-12 12:11:42 +01:00
if widget . instance ( ) == " cmus.shuffle " :
return " on " if self . _shuffle else " off "
if widget . instance ( ) == " cmus.repeat " :
return " on " if self . _repeat else " off "
2016-11-12 08:34:21 +01:00
return self . _status
2016-11-07 22:14:46 +01:00
2016-11-09 19:38:32 +01:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4