Onionr/onionr/onionrevents.py

73 lines
2.4 KiB
Python
Raw Permalink Normal View History

2018-03-03 04:19:01 +00:00
'''
2019-06-16 06:06:32 +00:00
Onionr - Private P2P Communication
2018-03-03 04:19:01 +00:00
This file deals with configuration management.
'''
'''
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/>.
'''
import config, logger, onionrplugins as plugins, onionrpluginapi as pluginapi
from threading import Thread
2019-08-04 04:52:57 +00:00
def get_pluginapi(data):
return pluginapi.SharedAPI(data)
2018-03-03 04:19:01 +00:00
2019-08-04 04:52:57 +00:00
def __event_caller(event_name, data = {}):
2018-03-03 04:19:01 +00:00
'''
DO NOT call this function, this is for threading code only.
Instead, call onionrevents.event
2018-03-03 04:19:01 +00:00
'''
for plugin in plugins.get_enabled_plugins():
try:
2019-08-05 00:34:57 +00:00
call(plugins.get_plugin(plugin), event_name, data, get_pluginapi(data))
2018-04-22 00:37:20 +00:00
except ModuleNotFoundError as e:
2019-07-20 15:52:03 +00:00
logger.warn('Disabling nonexistant plugin "%s"...' % plugin, terminal=True)
2019-08-04 04:52:57 +00:00
plugins.disable(plugin, stop_event = False)
2018-04-21 03:10:50 +00:00
except Exception as e:
2019-07-20 15:52:03 +00:00
logger.warn('Event "%s" failed for plugin "%s".' % (event_name, plugin), terminal=True)
logger.debug(str(e), terminal=True)
2018-03-03 04:19:01 +00:00
2019-08-04 04:52:57 +00:00
def event(event_name, data = {}, threaded = True):
'''
Calls an event on all plugins (if defined)
'''
if threaded:
2019-08-04 04:52:57 +00:00
thread = Thread(target = __event_caller, args = (event_name, data))
thread.start()
return thread
else:
2019-08-05 00:34:57 +00:00
__event_caller(event_name, data)
def call(plugin, event_name, data = None, pluginapi = None):
2018-03-03 04:19:01 +00:00
'''
Calls an event on a plugin if one is defined
'''
if not plugin is None:
try:
attribute = 'on_' + str(event_name).lower()
2019-08-05 00:34:57 +00:00
if pluginapi is None:
2019-08-05 04:08:56 +00:00
pluginapi = get_pluginapi(data)
2018-03-03 04:19:01 +00:00
if hasattr(plugin, attribute):
2019-08-04 04:52:57 +00:00
return getattr(plugin, attribute)(pluginapi, data)
2018-03-03 04:19:01 +00:00
return True
2018-04-21 03:10:50 +00:00
except Exception as e:
2019-08-06 03:10:21 +00:00
#logger.error(str(e), terminal=True)
2018-03-03 04:19:01 +00:00
return False
else:
return True