From e02e7dba87cdf71aef2026f49f79f0efcad16f3c Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 29 Aug 2019 18:26:43 -0500 Subject: [PATCH] readded help support including for plugins --- onionr/onionrcommands/parser/__init__.py | 50 +++++++++++++++---- .../default-plugins/encrypt/main.py | 11 ++-- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/onionr/onionrcommands/parser/__init__.py b/onionr/onionrcommands/parser/__init__.py index 212a3017..786e76e3 100644 --- a/onionr/onionrcommands/parser/__init__.py +++ b/onionr/onionrcommands/parser/__init__.py @@ -24,17 +24,28 @@ import onionrplugins import onionrpluginapi from . import arguments, recommend +plugin_command = lambda cmd: 'on_%s_cmd' % (cmd,) + def register_plugin_commands(cmd): cmd = 'on_%s_cmd' % (cmd,) + plugin_cmd = plugin_command(cmd) for pl in onionrplugins.get_enabled_plugins(): pl = onionrplugins.get_plugin(pl) - if hasattr(pl, cmd): - getattr(pl, cmd)(onionrpluginapi.PluginAPI) + if hasattr(pl, plugin_cmd): + getattr(pl, plugin_cmd)(onionrpluginapi.PluginAPI) return True def register(): - PROGRAM_NAME = "onionr" def get_help_message(cmd: str, default: str = 'No help available for this command'): + pl_cmd = plugin_command(cmd) + for pl in onionrplugins.get_enabled_plugins(): + pl = onionrplugins.get_plugin(pl) + if hasattr(pl, pl_cmd): + try: + return getattr(pl, pl_cmd).onionr_help + except AttributeError: + pass + for i in arguments.get_arguments(): for alias in i: try: @@ -43,17 +54,40 @@ def register(): pass return default + PROGRAM_NAME = "onionr" + try: cmd = sys.argv[1] except IndexError: cmd = "" + + is_help_cmd = False + if cmd.replace('--', '').lower() == 'help': is_help_cmd = True + + try: + arguments.get_func(cmd)() + except onionrexceptions.NotFound: + if not register_plugin_commands(cmd) and not is_help_cmd: + recommend.recommend() + sys.exit(3) - if cmd.replace('--', '').lower() == 'help': + if is_help_cmd: try: sys.argv[2] except IndexError: for i in arguments.get_arguments(): logger.info('%s <%s>: %s' % (PROGRAM_NAME, '/'.join(i), get_help_message(i[0])), terminal=True) + for pl in onionrplugins.get_enabled_plugins(): + pl = onionrplugins.get_plugin(pl) + if hasattr(pl, 'ONIONR_COMMANDS'): + print('') + try: + logger.info('%s commands:' % (pl.plugin_name,), terminal=True) + except AttributeError: + logger.info('%s commands:' % (pl.__name__,), terminal=True) + for plugin_cmd in pl.ONIONR_COMMANDS: + logger.info('%s %s: %s' % (PROGRAM_NAME, plugin_cmd, get_help_message(plugin_cmd)), terminal=True) + print('') else: try: logger.info('%s %s: %s' % (PROGRAM_NAME, sys.argv[2], get_help_message(sys.argv[2])), terminal=True) @@ -61,10 +95,4 @@ def register(): logger.error('%s: command does not exist.' % [sys.argv[2]], terminal=True) sys.exit(3) return - - try: - arguments.get_func(cmd)() - except onionrexceptions.NotFound: - if not register_plugin_commands(cmd): - recommend.recommend() - sys.exit(3) \ No newline at end of file + \ No newline at end of file diff --git a/onionr/static-data/default-plugins/encrypt/main.py b/onionr/static-data/default-plugins/encrypt/main.py index 5cd52682..05ce88ea 100755 --- a/onionr/static-data/default-plugins/encrypt/main.py +++ b/onionr/static-data/default-plugins/encrypt/main.py @@ -113,11 +113,6 @@ def on_decrypt_cmd(api, data=None): def on_encrypt_cmd(api, data=None): PlainEncryption(api).encrypt() -def on_init(api, data = None): - ''' - This event is called after Onionr is initialized, but before the command - inputted is executed. Could be called when daemon is starting or when - just the client is running. - ''' - pluginapi = api - encrypt = PlainEncryption(pluginapi) \ No newline at end of file +on_encrypt_cmd.onionr_help = """encrypt \nEncrypt text data to an Onionr user key. Similar to PGP""" +on_decrypt_cmd.onionr_help = """decrypt\nDecrypt text data with your Onionr key. Similar to PGP""" +ONIONR_COMMANDS = ['encrypt', 'decrypt'] \ No newline at end of file