A modest string writer

moc.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. import sardine
  3. import os
  4. import subprocess
  5. class TrackData(object):
  6. _field_names = ['state', 'artist', 'album', 'song', 'ct', 'tt']
  7. def __init__(self):
  8. self.state = ""
  9. self.artist = ""
  10. self.album = ""
  11. self.song = ""
  12. self.ct = "" # current time
  13. self.tt = "" # total time
  14. def load(self):
  15. FNULL = open(os.devnull, 'w+')
  16. for fieldname in self.__class__._field_names:
  17. value = subprocess.check_output(['mocp', '-Q', "%" + fieldname],
  18. stderr=FNULL)
  19. setattr(self, fieldname, value.strip())
  20. class MocTitler(object):
  21. def __init__(self):
  22. self.title = "(unknown)"
  23. self.td = TrackData()
  24. def assemble_title(self):
  25. """Assemble the data to human-readable format"""
  26. d = self.td
  27. title = ""
  28. title += "(%s) " % d.state
  29. title += d.artist if d.artist else ""
  30. title += " " if d.artist and d.album else ""
  31. title += "[%s]" % d.album if d.album else ""
  32. if (d.artist or d.album) and (d.song or d.tt):
  33. title += ": "
  34. title += d.song if d.song else ""
  35. title += " (%s/%s)" % (d.ct, d.tt) if d.tt else ""
  36. return title
  37. def get_title(self):
  38. """Get title or explaining message"""
  39. try:
  40. self.td.load()
  41. return self.assemble_title()
  42. except subprocess.CalledProcessError:
  43. return "(not running)"
  44. class Plugin(sardine.BasePlugin):
  45. name = "moc"
  46. def render_plain(self):
  47. return MocTitler().get_title()