From e5e12703802f34ac292c3fcd261bca9141b7a1a2 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 24 Apr 2018 20:07:16 +0200 Subject: [PATCH] Exposed a global context.get_bus() method to allow plugins to send events to the main bus --- platypush/context/__init__.py | 11 +++++++++++ platypush/plugins/video/omxplayer.py | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index bfe243b36..4a463947d 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -9,6 +9,9 @@ backends = {} # Map: plugin_name -> plugin_instance plugins = {} +# Reference to the main application bus +main_bus = None + def register_backends(bus=None, global_scope=False, **kwargs): """ Initialize the backend objects based on the configuration and returns a name -> backend_instance map. @@ -19,6 +22,10 @@ def register_backends(bus=None, global_scope=False, **kwargs): kwargs -- Any additional key-value parameters required to initialize the backends """ + global main_bus + if bus: + main_bus = bus + if global_scope: global backends else: @@ -84,6 +91,10 @@ def get_plugin(plugin_name, reload=False): plugins[plugin_name] = plugin return plugin +def get_bus(): + global main_bus + return main_bus + def register_plugin(name, plugin, **kwargs): """ Registers a plugin instance by name """ global plugins diff --git a/platypush/plugins/video/omxplayer.py b/platypush/plugins/video/omxplayer.py index fbe5d4438..0f174376f 100644 --- a/platypush/plugins/video/omxplayer.py +++ b/platypush/plugins/video/omxplayer.py @@ -10,6 +10,7 @@ from bs4 import BeautifulSoup from dbus.exceptions import DBusException from omxplayer import OMXPlayer +from platypush.context import get_bus from platypush.plugins.media import PlayerState from platypush.message.response import Response from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \ @@ -158,17 +159,17 @@ class VideoOmxplayerPlugin(Plugin): def on_play(self): def _f(player): - self.bus.post(VideoPlayEvent(video=self.player.get_source())) + get_bus().post(VideoPlayEvent(video=self.player.get_source())) return _f def on_pause(self): def _f(player): - self.bus.post(VideoPauseEvent(video=self.player.get_source())) + get_bus().post(VideoPauseEvent(video=self.player.get_source())) return _f def on_stop(self): def _f(player): - self.bus.post(VideoStopEvent()) + get_bus().post(VideoStopEvent()) return _f