PageRenderTime 96ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/pythm/mplayer/mplayer.py

https://github.com/yarikoptic/pythm
Python | 123 lines | 105 code | 3 blank | 15 comment | 0 complexity | e0de52156f069abc57a219b36c60b25b MD5 | raw file
  1. # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
  2. # vi: set ft=python sts=4 ts=4 sw=4 et:
  3. ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
  4. #
  5. # See COPYING file distributed along with the pythm for the
  6. # copyright and license terms.
  7. #
  8. ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
  9. """ TODO: Module description """
  10. import os
  11. import select
  12. import subprocess
  13. import time
  14. import sys
  15. from threading import Lock
  16. #http://code.activestate.com/recipes/542195/
  17. class MPlayer(object):
  18. """ A class to access a slave mplayer process
  19. you may want to use command(name, args*) directly
  20. or call populate() to access functions (and minimal doc).
  21. Exemples:
  22. mp.command('loadfile', '/desktop/funny.mp3')
  23. mp.command('pause')
  24. mp.command('quit')
  25. Note:
  26. After a .populate() call, you can access an higher level interface:
  27. mp.loadfile('/desktop/funny.mp3')
  28. mp.pause()
  29. mp.quit()
  30. Beyond syntax, advantages are:
  31. - completion
  32. - minimal documentation
  33. - minimal return type parsing
  34. """
  35. exe_name = 'mplayer' if os.sep == '/' else 'mplayer.exe'
  36. def __init__(self,niceval=None):
  37. args = [self.exe_name, '-slave', '-quiet', '-idle','-nolirc']
  38. self._mplayer = subprocess.Popen(args,
  39. stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
  40. if self._mplayer and niceval!=None:
  41. pid = self._mplayer.pid
  42. os.system("renice "+ str(niceval) +" -p "+str(pid))
  43. self.lock = Lock()
  44. def command(self,cmd,*args):
  45. """
  46. a command that does not read anything
  47. """
  48. return self._cmd(cmd,None,False,*args)
  49. def arraycmd(self,name,key,*args):
  50. """
  51. array reading of mplayers output
  52. """
  53. return self._cmd(name,key,True,*args)
  54. def cmd(self,name,key,*args):
  55. """
  56. single line reading with key
  57. """
  58. return self._cmd(name,key,False,*args)
  59. def quit(self):
  60. self.command("quit",0)
  61. self._mplayer.wait()
  62. def _cmd(self,name,key,readall,*args):
  63. """
  64. key is the beginning of a line that the command waits for.
  65. if it is None, the read from mplayers stdout will be omitted.
  66. if readall is true, all is read until a line begins with key.
  67. """
  68. cmd = '%s%s%s\n'%(name,
  69. ' ' if args else '',
  70. ' '.join(repr(a) for a in args)
  71. )
  72. if readall:
  73. ret = []
  74. else:
  75. ret = None
  76. self.lock.acquire()
  77. i = 0
  78. try:
  79. #print "CMD: " + str(cmd)
  80. self._mplayer.stdin.write(cmd)
  81. if key != None:
  82. while any(select.select([self._mplayer.stdout.fileno()], [], [], 20)):
  83. tmp = self._mplayer.stdout.readline()
  84. # print "MPLAYER:" + tmp.strip()
  85. if readall:
  86. ret.append(tmp)
  87. if tmp.startswith(key):
  88. break
  89. else:
  90. if tmp.startswith(key):
  91. val = tmp.split('=', 1)[1].rstrip()
  92. try:
  93. ret = eval(val)
  94. except:
  95. ret = val
  96. break
  97. except Exception,e:
  98. print "error in mplayer.cmd: ", e
  99. pass
  100. self.lock.release()
  101. return ret