Add cleanup in MPV class and shorthand command method in MPVControl

This commit is contained in:
Peder Bergebakken Sundt 2018-02-16 23:18:19 +01:00
parent 694f7aab11
commit 4f7aebd6fb
2 changed files with 16 additions and 3 deletions

View File

@ -6,7 +6,7 @@ from . import nyasync
mpv_control = mpv.MPVControl() mpv_control = mpv.MPVControl()
async def test(): async def test():
await mpv_control.send_request({"command":["loadfile",'grzegorz/res/logo.jpg']}) await mpv_control.loadfile('grzegorz/res/logo.jpg')
async def entry(): async def entry():
await asyncio.gather( await asyncio.gather(

View File

@ -1,3 +1,4 @@
import os
import asyncio import asyncio
import json import json
@ -32,18 +33,25 @@ class MPV:
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
else: else:
raise Exception("MPV died before socket connected") raise Exception("MPV died before socket connected")
await asyncio.gather( self._future = asyncio.gather(
self.ensure_running(), self.ensure_running(),
self.process_outgoing(), self.process_outgoing(),
self.process_incomming(), self.process_incomming(),
) )
await self._future
def _cleanup(self):
if os.path.exists(self._ipc_endpoint):
os.remove(self._ipc_endpoint)
self._future.cancel()#reduces a lot of errors on exit
def is_running(self): def is_running(self):
return self.proc.returncode is None return self.proc.returncode is None
async def ensure_running(self): async def ensure_running(self):
await self.proc.wait() await self.proc.wait()
self._cleanup()
raise Exception("MPV died unexpectedly") raise Exception("MPV died unexpectedly")
async def process_outgoing(self): async def process_outgoing(self):
@ -81,3 +89,8 @@ class MPVControl:
# is the safest option. # is the safest option.
self.mpv.requests.put_nowait(msg) self.mpv.requests.put_nowait(msg)
return await self.mpv.responses.get() return await self.mpv.responses.get()
#Shorthand command requests:
async def loadfile(self, file):
return await self.send_request({"command":["loadfile", file]})