readded help support including for plugins

This commit is contained in:
Kevin Froman 2019-08-29 18:26:43 -05:00
parent 604ce5f012
commit e02e7dba87
2 changed files with 42 additions and 19 deletions

View File

@ -24,17 +24,28 @@ import onionrplugins
import onionrpluginapi import onionrpluginapi
from . import arguments, recommend from . import arguments, recommend
plugin_command = lambda cmd: 'on_%s_cmd' % (cmd,)
def register_plugin_commands(cmd): def register_plugin_commands(cmd):
cmd = 'on_%s_cmd' % (cmd,) cmd = 'on_%s_cmd' % (cmd,)
plugin_cmd = plugin_command(cmd)
for pl in onionrplugins.get_enabled_plugins(): for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl) pl = onionrplugins.get_plugin(pl)
if hasattr(pl, cmd): if hasattr(pl, plugin_cmd):
getattr(pl, cmd)(onionrpluginapi.PluginAPI) getattr(pl, plugin_cmd)(onionrpluginapi.PluginAPI)
return True return True
def register(): def register():
PROGRAM_NAME = "onionr"
def get_help_message(cmd: str, default: str = 'No help available for this command'): 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 i in arguments.get_arguments():
for alias in i: for alias in i:
try: try:
@ -43,17 +54,40 @@ def register():
pass pass
return default return default
PROGRAM_NAME = "onionr"
try: try:
cmd = sys.argv[1] cmd = sys.argv[1]
except IndexError: except IndexError:
cmd = "" 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: try:
sys.argv[2] sys.argv[2]
except IndexError: except IndexError:
for i in arguments.get_arguments(): for i in arguments.get_arguments():
logger.info('%s <%s>: %s' % (PROGRAM_NAME, '/'.join(i), get_help_message(i[0])), terminal=True) 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: else:
try: try:
logger.info('%s %s: %s' % (PROGRAM_NAME, sys.argv[2], get_help_message(sys.argv[2])), terminal=True) 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) logger.error('%s: command does not exist.' % [sys.argv[2]], terminal=True)
sys.exit(3) sys.exit(3)
return return
try:
arguments.get_func(cmd)()
except onionrexceptions.NotFound:
if not register_plugin_commands(cmd):
recommend.recommend()
sys.exit(3)

View File

@ -113,11 +113,6 @@ def on_decrypt_cmd(api, data=None):
def on_encrypt_cmd(api, data=None): def on_encrypt_cmd(api, data=None):
PlainEncryption(api).encrypt() PlainEncryption(api).encrypt()
def on_init(api, data = None): on_encrypt_cmd.onionr_help = """encrypt <user_key>\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"""
This event is called after Onionr is initialized, but before the command ONIONR_COMMANDS = ['encrypt', 'decrypt']
inputted is executed. Could be called when daemon is starting or when
just the client is running.
'''
pluginapi = api
encrypt = PlainEncryption(pluginapi)