Advertising
- Fabien Devaux
- Monday, January 14th, 2008 at 2:42:20pm MST
- from select import select
- import subprocess
- mplayer = subprocess.Popen(['mplayer', '-input', 'cmdlist'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- class MPlayer(object):
- def __init__(self):
- self._mplayer = subprocess.Popen(['mplayer', '-slave', '-quiet', '-idle'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
- self._readlines()
- def _readlines(self):
- ret = []
- while any(select([self._mplayer.stdout.fileno()], [], [], 1.0)):
- ret.append( self._mplayer.stdout.readline() )
- return ret
- def _command(self, name, *args):
- cmd = '%s%s%s\n'%(name,
- ' ' if args else '',
- ' '.join(str(a) for a in args)
- )
- self._mplayer.stdin.write(cmd)
- return self._readlines()
- def __del__(self):
- self._command('quit')
- self._mplayer.wait()
- while True:
- line = mplayer.stdout.readline()
- if not line:
- break
- if line[0].isupper():
- continue
- args = line.split()
- cmd_name = args.pop(0)
- func_str = '''def _mp_func(self, *args):
- """%s"""
- if not (%d <= len(args) <= %d):
- raise TypeError('%s takes %d arguments (%%d given)'%%len(args))
- self._command('%s', *args)'''%(
- '%s(%s)'%(cmd_name, ', '.join(args)),
- len([a for a in args if a[0] != '[']),
- len(args),
- cmd_name,
- len(args),
- cmd_name,
- )
- exec(func_str)
- setattr(MPlayer, cmd_name, _mp_func)
- if __name__ == '__main__':
- mp = MPlayer()
- import readline
- readline.parse_and_bind('tab: complete')
- import rlcompleter
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.
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.