A modest string writer

moc.py 1.7KB

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