12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- # -*- coding: utf-8 -*-
-
- import sardine
-
- import os
- import subprocess
-
-
- class TrackData(object):
-
- _field_names = ['state', 'artist', 'album', 'song', 'ct', 'tt']
-
- def __init__(self):
- self.state = ""
- self.artist = ""
- self.album = ""
- self.song = ""
- self.ct = "" # current time
- self.tt = "" # total time
-
- def load(self):
- FNULL = open(os.devnull, 'w+')
- for fieldname in self.__class__._field_names:
- value = subprocess.check_output(['mocp', '-Q', "%" + fieldname],
- stderr=FNULL)
- setattr(self, fieldname, value.strip())
-
-
- class MocTitler(object):
-
- def __init__(self):
- self.title = "(unknown)"
- self.td = TrackData()
-
- def assemble_title(self):
- """Assemble the data to human-readable format"""
- d = self.td
- title = ""
- title += "(%s) " % d.state
- title += d.artist if d.artist else ""
- title += " " if d.artist and d.album else ""
- title += "[%s]" % d.album if d.album else ""
- if (d.artist or d.album) and (d.song or d.tt):
- title += ": "
- title += d.song if d.song else ""
- title += " (%s/%s)" % (d.ct, d.tt) if d.tt else ""
-
- return title
-
- def get_title(self):
- """Get title or explaining message"""
- try:
- self.td.load()
- return self.assemble_title()
- except subprocess.CalledProcessError:
- return "(not running)"
-
-
- class Plugin(sardine.BasePlugin):
-
- name = "moc"
-
- def render_plain(self):
- return MocTitler().get_title()
|