Exposed a global context.get_bus() method to allow plugins to send events to the main bus

This commit is contained in:
Fabio Manganiello 2018-04-24 20:07:16 +02:00
parent 9052e18f9c
commit e5e1270380
2 changed files with 15 additions and 3 deletions

View file

@ -9,6 +9,9 @@ backends = {}
# Map: plugin_name -> plugin_instance # Map: plugin_name -> plugin_instance
plugins = {} plugins = {}
# Reference to the main application bus
main_bus = None
def register_backends(bus=None, global_scope=False, **kwargs): def register_backends(bus=None, global_scope=False, **kwargs):
""" Initialize the backend objects based on the configuration and returns """ Initialize the backend objects based on the configuration and returns
a name -> backend_instance map. 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 kwargs -- Any additional key-value parameters required to initialize the backends
""" """
global main_bus
if bus:
main_bus = bus
if global_scope: if global_scope:
global backends global backends
else: else:
@ -84,6 +91,10 @@ def get_plugin(plugin_name, reload=False):
plugins[plugin_name] = plugin plugins[plugin_name] = plugin
return plugin return plugin
def get_bus():
global main_bus
return main_bus
def register_plugin(name, plugin, **kwargs): def register_plugin(name, plugin, **kwargs):
""" Registers a plugin instance by name """ """ Registers a plugin instance by name """
global plugins global plugins

View file

@ -10,6 +10,7 @@ from bs4 import BeautifulSoup
from dbus.exceptions import DBusException from dbus.exceptions import DBusException
from omxplayer import OMXPlayer from omxplayer import OMXPlayer
from platypush.context import get_bus
from platypush.plugins.media import PlayerState from platypush.plugins.media import PlayerState
from platypush.message.response import Response from platypush.message.response import Response
from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \ from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \
@ -158,17 +159,17 @@ class VideoOmxplayerPlugin(Plugin):
def on_play(self): def on_play(self):
def _f(player): def _f(player):
self.bus.post(VideoPlayEvent(video=self.player.get_source())) get_bus().post(VideoPlayEvent(video=self.player.get_source()))
return _f return _f
def on_pause(self): def on_pause(self):
def _f(player): def _f(player):
self.bus.post(VideoPauseEvent(video=self.player.get_source())) get_bus().post(VideoPauseEvent(video=self.player.get_source()))
return _f return _f
def on_stop(self): def on_stop(self):
def _f(player): def _f(player):
self.bus.post(VideoStopEvent()) get_bus().post(VideoStopEvent())
return _f return _f