Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Fabien Devaux
Monday, January 14th, 2008 at 2:42:20pm MST 

  1. from select import select
  2. import subprocess
  3.  
  4. mplayer = subprocess.Popen(['mplayer', '-input', 'cmdlist'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  5.  
  6. class MPlayer(object):
  7.     def __init__(self):
  8.         self._mplayer = subprocess.Popen(['mplayer', '-slave', '-quiet', '-idle'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
  9.         self._readlines()
  10.  
  11.     def _readlines(self):
  12.         ret = []
  13.         while any(select([self._mplayer.stdout.fileno()], [], [], 1.0)):
  14.             ret.append( self._mplayer.stdout.readline() )
  15.         return ret
  16.  
  17.     def _command(self, name, *args):
  18.         cmd = '%s%s%s\n'%(name,
  19.                 ' ' if args else '',
  20.                 ' '.join(str(a) for a in args)
  21.                 )
  22.         self._mplayer.stdin.write(cmd)
  23.         return self._readlines()
  24.  
  25.     def __del__(self):
  26.         self._command('quit')
  27.         self._mplayer.wait()
  28.  
  29. while True:
  30.     line = mplayer.stdout.readline()
  31.     if not line:
  32.         break
  33.     if line[0].isupper():
  34.         continue
  35.     args = line.split()
  36.     cmd_name = args.pop(0)
  37.     func_str = '''def _mp_func(self, *args):
  38.     """%s"""
  39.     if not (%d <= len(args) <= %d):
  40.         raise TypeError('%s takes %d arguments (%%d given)'%%len(args))
  41.     self._command('%s', *args)'''%(
  42.             '%s(%s)'%(cmd_name, ', '.join(args)),
  43.             len([a for a in args if a[0] != '[']),
  44.             len(args),
  45.             cmd_name,
  46.             len(args),
  47.             cmd_name,
  48.             )
  49.     exec(func_str)
  50.  
  51.     setattr(MPlayer, cmd_name, _mp_func)
  52.  
  53. if __name__ == '__main__':
  54.     mp = MPlayer()
  55.     import readline
  56.     readline.parse_and_bind('tab: complete')
  57.     import rlcompleter

Paste Details

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right