From 4f7aebd6fb25834b11759e0ab5bb996ca1cf22b7 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 16 Feb 2018 23:18:19 +0100 Subject: [PATCH] Add cleanup in MPV class and shorthand command method in MPVControl --- grzegorz/__init__.py | 2 +- grzegorz/mpv.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/grzegorz/__init__.py b/grzegorz/__init__.py index 8274c54..fbed340 100644 --- a/grzegorz/__init__.py +++ b/grzegorz/__init__.py @@ -6,7 +6,7 @@ from . import nyasync mpv_control = mpv.MPVControl() 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(): await asyncio.gather( diff --git a/grzegorz/mpv.py b/grzegorz/mpv.py index ab02399..c271d2e 100644 --- a/grzegorz/mpv.py +++ b/grzegorz/mpv.py @@ -1,3 +1,4 @@ +import os import asyncio import json @@ -32,18 +33,25 @@ class MPV: await asyncio.sleep(0.1) else: raise Exception("MPV died before socket connected") - - await asyncio.gather( + + self._future = asyncio.gather( self.ensure_running(), self.process_outgoing(), 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): return self.proc.returncode is None async def ensure_running(self): await self.proc.wait() + self._cleanup() raise Exception("MPV died unexpectedly") async def process_outgoing(self): @@ -81,3 +89,8 @@ class MPVControl: # is the safest option. self.mpv.requests.put_nowait(msg) return await self.mpv.responses.get() + + #Shorthand command requests: + async def loadfile(self, file): + return await self.send_request({"command":["loadfile", file]}) +