summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2017-05-10 10:19:48 +0200
committerjaseg <code@jaseg.net>2017-05-10 11:55:42 +0200
commitab773a593413b158857d19352983a3de5fd27bfd (patch)
tree2159df824e631e4d1052d6f5e1096f3b0169f7bf
parent56f3210494e714e18b392448757a1d565145f111 (diff)
downloadpython-mpv-ab773a593413b158857d19352983a3de5fd27bfd.tar.gz
python-mpv-ab773a593413b158857d19352983a3de5fd27bfd.tar.bz2
python-mpv-ab773a593413b158857d19352983a3de5fd27bfd.zip
Add callback/message handler decorators
-rw-r--r--mpv.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/mpv.py b/mpv.py
index 19124d1..7c89b86 100644
--- a/mpv.py
+++ b/mpv.py
@@ -23,7 +23,7 @@ import threading
import os
import sys
from warnings import warn
-from functools import partial
+from functools import partial, wraps
import collections
import re
import traceback
@@ -616,12 +616,31 @@ class MPV(object):
def unregister_message_handler(self, target):
del self._message_handlers[target]
+ def message_handler(self, target):
+ """ Decorator to register a message handler """
+ def register(handler):
+ self._message_handlers[target] = handler
+ handler.unregister_message_handler = partial(self.unregister_message_handler, target)
+ return handler
+ return register
+
def register_event_callback(self, callback):
self._event_callbacks.append(callback)
def unregister_event_callback(self, callback):
self._event_callbacks.remove(callback)
+ def event_callback(self, *event_types):
+ def register(callback):
+ @wraps(callback)
+ def wrapper(event, *args, **kwargs):
+ if event['event_id'] in (event_types or MpvEventID.ANY):
+ callback(event, *args, **kwargs)
+ self._event_callbacks.append(wrapper)
+ wrapper.unregister_event_callback = partial(self.unregister_event_callback, wrapper)
+ return wrapper
+ return register
+
@staticmethod
def _binding_name(callback_or_cmd):
return 'py_kb_{:016x}'.format(hash(callback_or_cmd)&0xffffffffffffffff)