Advertisement
ijontichy

mpd_status.py

Jun 16th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import subprocess
  4. import sys
  5.  
  6. try:
  7.     import ansicodes   # local module
  8. except ImportError:
  9.     class ansicodes(object):
  10.         @staticmethod
  11.         def mapColors(strn, fgMap, bgMap=None, *, fCols=None, bCols=None, hardReset=False):
  12.             return "".join(strn)
  13.  
  14. def main():
  15.     mpcFormat = "%artist% - %track%:%title% - %album%"
  16.     mpcLines = subprocess.check_output(["mpc", "--format", mpcFormat])
  17.     mpcLines = mpcLines.decode().splitlines()
  18.  
  19.     mpcStatus = mpcLines[0]
  20.     if mpcStatus.startswith("volume:"):
  21.         return "Not playing"
  22.  
  23.     mpcTimeLine = mpcLines[1].split()
  24.     mpcTime = mpcTimeLine[2]
  25.     mpcMode = mpcTimeLine[0][1:-1]
  26.  
  27.     if mpcMode.lower() == "paused":
  28.         modeColor = "1"
  29.     else:
  30.         modeColor = "2"
  31.  
  32.     ret = ["<{}> ".format(mpcMode), mpcStatus, " [{}]".format(mpcTime)]
  33.  
  34.     return ansicodes.mapColors(ret, modeColor + " 3", hardReset=True)
  35.  
  36.  
  37. # specific to running directly
  38. def strExit(ret):
  39.     print(ret)
  40.  
  41. def intExit(ret):
  42.     return ret
  43.  
  44. mainAction = {str: strExit, int: intExit, None: int}
  45.  
  46. if __name__ == "__main__":
  47.     ret = main()
  48.  
  49.     if ret:
  50.         sys.exit(mainAction[ret.__class__](ret) or 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement