From 2f9c1de06205489fd4705c71d0bfa45b90eca9dd Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 4 Aug 2019 19:34:57 -0500 Subject: [PATCH] progress removing onionr.py --- onionr/onionrcommands/parser/__init__.py | 16 ++++++++++++++-- onionr/onionrcommands/parser/arguments.py | 2 -- onionr/onionrevents.py | 7 ++++--- onionr/onionrpluginapi.py | 6 +----- onionr/static-data/default-plugins/flow/main.py | 5 +++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/onionr/onionrcommands/parser/__init__.py b/onionr/onionrcommands/parser/__init__.py index 2f14f080..6ae81132 100644 --- a/onionr/onionrcommands/parser/__init__.py +++ b/onionr/onionrcommands/parser/__init__.py @@ -20,7 +20,18 @@ import sys from etc import onionrvalues import logger, onionrexceptions +import onionrplugins +import onionrpluginapi from . import arguments, recommend + +def register_plugin_commands(cmd): + cmd = 'on_%s_cmd' % (cmd,) + for pl in onionrplugins.get_enabled_plugins(): + pl = onionrplugins.get_plugin(pl) + if hasattr(pl, cmd): + getattr(pl, cmd)(onionrpluginapi.PluginAPI) + return True + def register(): try: cmd = sys.argv[1] @@ -30,5 +41,6 @@ def register(): try: arguments.get_func(cmd)() except onionrexceptions.NotFound: - recommend.recommend() - sys.exit(3) \ No newline at end of file + if not register_plugin_commands(cmd): + recommend.recommend() + sys.exit(3) \ No newline at end of file diff --git a/onionr/onionrcommands/parser/arguments.py b/onionr/onionrcommands/parser/arguments.py index 4ec1bbb7..7479a476 100644 --- a/onionr/onionrcommands/parser/arguments.py +++ b/onionr/onionrcommands/parser/arguments.py @@ -10,8 +10,6 @@ def get_arguments(): ('stop', 'kill'): daemonlaunch.kill_daemon, ('add-address', 'addaddress', 'addadder'): keyadders.add_address } - args = events.event('init', data=args, threaded=False) - print(args) return args def get_help(): diff --git a/onionr/onionrevents.py b/onionr/onionrevents.py index cf1e6501..62e8dec7 100755 --- a/onionr/onionrevents.py +++ b/onionr/onionrevents.py @@ -31,7 +31,7 @@ def __event_caller(event_name, data = {}): ''' for plugin in plugins.get_enabled_plugins(): try: - return call(plugins.get_plugin(plugin), event_name, data, get_pluginapi(data)) + call(plugins.get_plugin(plugin), event_name, data, get_pluginapi(data)) except ModuleNotFoundError as e: logger.warn('Disabling nonexistant plugin "%s"...' % plugin, terminal=True) plugins.disable(plugin, stop_event = False) @@ -49,7 +49,7 @@ def event(event_name, data = {}, threaded = True): thread.start() return thread else: - return __event_caller(event_name, data) + __event_caller(event_name, data) def call(plugin, event_name, data = None, pluginapi = None): ''' @@ -59,7 +59,8 @@ def call(plugin, event_name, data = None, pluginapi = None): if not plugin is None: try: attribute = 'on_' + str(event_name).lower() - + if pluginapi is None: + pluginapi = get_pluginapi() if hasattr(plugin, attribute): return getattr(plugin, attribute)(pluginapi, data) diff --git a/onionr/onionrpluginapi.py b/onionr/onionrpluginapi.py index ba2ed817..c25e9984 100755 --- a/onionr/onionrpluginapi.py +++ b/onionr/onionrpluginapi.py @@ -144,8 +144,7 @@ class WebAPI: return self.pluginapi.get_onionr().api.getCallbacks(scope = scope) class SharedAPI: - def __init__(self, onionr, data): - self.onionr = onionr + def __init__(self, data): self.data = data self.daemon = DaemonAPI(self) @@ -153,9 +152,6 @@ class SharedAPI: self.commands = CommandAPI(self) self.web = WebAPI(self) - def get_onionr(self): - return self.onionr - def get_data(self): return self.data diff --git a/onionr/static-data/default-plugins/flow/main.py b/onionr/static-data/default-plugins/flow/main.py index 323a82c7..0aeb7d94 100755 --- a/onionr/static-data/default-plugins/flow/main.py +++ b/onionr/static-data/default-plugins/flow/main.py @@ -96,6 +96,9 @@ class OnionrFlow: except KeyboardInterrupt: self.flowRunning = False +def on_flow_cmd(api, data=None): + OnionrFlow().start() + def on_init(api, data = None): ''' This event is called after Onionr is initialized, but before the command @@ -106,8 +109,6 @@ def on_init(api, data = None): # by simply referencing the variable `pluginapi`. global pluginapi pluginapi = api - flow = OnionrFlow() - return data def on_processblocks(api, data=None): b_hash = reconstructhash.deconstruct_hash(data['block'].hash) # Get the 0-truncated block hash