Onionr/src/onionrplugins/onionrevents.py

74 lines
2.4 KiB
Python
Executable File

"""
Onionr - Private P2P Communication
Deals with configuration management.
"""
from threading import Thread
import traceback
import config, logging
import onionrplugins as plugins
from . import onionrpluginapi as pluginapi
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
def get_pluginapi(data):
return pluginapi.SharedAPI(data)
def __event_caller(event_name, data = {}):
"""DO NOT call this function, this is for threading code only.
Instead, call onionrevents.event
"""
disabled = config.get('plugins.disabled', [])
for plugin in plugins.get_enabled_plugins():
if plugin in disabled: continue
try:
call(plugins.get_plugin(plugin), event_name, data, get_pluginapi(data))
except ModuleNotFoundError as _:
logging.warn('Disabling nonexistant plugin "%s"...' % plugin)
plugins.disable(plugin, stop_event = False)
except Exception as _:
logging.error('Event "%s" failed for plugin "%s".' % (event_name, plugin))
logging.error('\n' + traceback.format_exc())
def event(event_name, data = {}, threaded = True):
"""Call an event on all plugins (if defined)"""
if threaded:
thread = Thread(target = __event_caller, args = (event_name, data), name=f'{event_name} event', daemon=True)
thread.start()
return thread
else:
__event_caller(event_name, data)
def call(plugin, event_name, data = None, pluginapi = None):
"""Call an event on a plugin if one is defined"""
if not plugin is None:
attribute = 'on_' + str(event_name).lower()
if pluginapi is None:
pluginapi = get_pluginapi(data)
if hasattr(plugin, attribute):
return getattr(plugin, attribute)(pluginapi, data)
return True
else:
return True