From 173b306c5bb24d3434e4b3358be3fb0d51e2eefc Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 15 Jan 2020 19:30:03 -0800 Subject: [PATCH] Refactor cli command loading --- amanuensis/__main__.py | 12 ++------- amanuensis/cli/__init__.py | 52 ++++++++++++++++++++++++++++++++++++-- amanuensis/cli/lexicon.py | 5 ++++ amanuensis/cli/user.py | 8 +++--- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index 444cca6..300fe21 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -46,14 +46,9 @@ def repl(args): traceback.print_exc() def get_parser(valid_commands): - # Pull out the command functions' docstrings to describe them. - command_descs = "\n".join([ - "- {0}: {1}".format(name, func.__doc__) - for name, func in valid_commands.items()]) - # Set up the top-level parser. parser = argparse.ArgumentParser( - description="Available commands:\n{}\n".format(command_descs), + description=cli.describe_commands(), formatter_class=argparse.RawDescriptionHelpFormatter) # The config directory. parser.add_argument("--config-dir", @@ -105,10 +100,7 @@ def get_parser(valid_commands): def main(argv): # Enumerate valid commands from the CLI module. - commands = { - name[8:].replace("_", "-") : func - for name, func in vars(cli).items() - if name.startswith("command_")} + commands = cli.get_commands() args = get_parser(commands).parse_args(argv) diff --git a/amanuensis/cli/__init__.py b/amanuensis/cli/__init__.py index e310560..8505028 100644 --- a/amanuensis/cli/__init__.py +++ b/amanuensis/cli/__init__.py @@ -8,5 +8,53 @@ # run after commandline parsing has already occurred. # -from cli.server import * -from cli.user import * +def server_commands(commands={}): + if commands: return commands + import cli.server + for name, func in vars(cli.server).items(): + if name.startswith("command_"): + name = name[8:].replace("_", "-") + commands[name] = func + return commands + +def lexicon_commands(commands={}): + if commands: return commands + import cli.lexicon + for name, func in vars(cli.lexicon).items(): + if name.startswith("command_"): + name = name[8:].replace("_", "-") + commands["lexicon-" + name] = func + return commands + +def user_commands(commands={}): + if commands: return commands + import cli.user + for name, func in vars(cli.user).items(): + if name.startswith("command_"): + name = name[8:].replace("_", "-") + commands["user-" + name] = func + return commands + +def get_commands(): + return {**server_commands(), **lexicon_commands(), **user_commands()} + +def describe_commands(): + longest = max(map(len, server_commands().keys())) + server_desc = "General commands:\n{}\n".format("\n".join([ + " {1:<{0}} : {2}".format(longest, name, func.__doc__ or "") + for name, func in server_commands().items() + ])) + + longest = max(map(len, lexicon_commands().keys())) + lexicon_desc = "Lexicon commands:\n{}\n".format("\n".join([ + " {1:<{0}} : {2}".format(longest, name, func.__doc__ or "") + for name, func in lexicon_commands().items() + ])) + + longest = max(map(len, user_commands().keys())) + user_desc = "User commands:\n{}\n".format("\n".join([ + " {1:<{0}} : {2}".format(longest, name, func.__doc__ or "") + for name, func in user_commands().items() + ])) + + return "\n".join([server_desc, lexicon_desc, user_desc]) diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index 11539f4..2df1316 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -1 +1,6 @@ from cli.helpers import add_argument, no_argument + +@no_argument +def command_noop(args): + """noop""" + pass \ No newline at end of file diff --git a/amanuensis/cli/user.py b/amanuensis/cli/user.py index dacb46b..d95dfc4 100644 --- a/amanuensis/cli/user.py +++ b/amanuensis/cli/user.py @@ -3,7 +3,7 @@ from cli.helpers import add_argument, no_argument @add_argument("--username", help="User's login handle") @add_argument("--displayname", help="User's publicly displayed name") @add_argument("--email", help="User's email") -def command_user_add(args): +def command_add(args): """Creates a user""" import json @@ -33,7 +33,7 @@ def command_user_add(args): print("Username: {}\nUser ID: {}\nPassword: {}".format(args.username, new_user.uid, tmp_pw)) @add_argument("--id", help="id of user to delete") -def command_user_delete(args): +def command_delete(args): import os import config @@ -49,7 +49,7 @@ def command_user_delete(args): del j[uid] @no_argument -def command_user_list(args): +def command_list(args): """Lists users""" import os @@ -66,7 +66,7 @@ def command_user_list(args): print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username'])) @add_argument("--username", help="The user to change password for") -def command_user_passwd(args): +def command_passwd(args): """Set a user's password""" import getpass import os