Move cli abstraction

This commit is contained in:
uid 2020-10-11 11:55:36 +02:00
parent d762251476
commit ae81ec3885
2 changed files with 53 additions and 46 deletions

47
src/phi/cli.py Normal file
View File

@ -0,0 +1,47 @@
import sys
import argparse
import inspect
from phi.logging import setup_logging, get_logger
log = get_logger(__name__)
parser = argparse.ArgumentParser()
subparses = parser.add_subparsers(title='actions', dest='action')
cli_callbacks = {}
def register(action_info='', param_infos=[]):
def decorator(action):
# Get function name and arguments
action_name = action.__name__
param_names = inspect.getfullargspec(action)[0]
# Create subparser for specific action
subparser = subparses.add_parser(action_name, help=action_info)
for i, name in enumerate(param_names):
info = param_infos[i] if i<len(param_infos) else ''
subparser.add_argument(dest=name, help=info)
# Register action
cli_callbacks[action_name] = action, param_names
return action
return decorator
def run(args):
for action_name, (action, param_names) in cli_callbacks.items():
if args['action'] == action_name:
action(**{pname: args[pname] for pname in param_names})
def add_arg(name, example, info):
parser.add_argument(name, metavar=example, help=info)
def get_args():
namespace = parser.parse_args(sys.argv[1:])
args = namespace.__dict__
return args

View File

@ -1,64 +1,24 @@
#!/usr/bin/env python3
from pprint import pformat as pp
import sys
import argparse
import inspect
import phi.ldap.client
from phi.config import get_config
from phi.logging import setup_logging, get_logger
from phi import cli
import phi.ldap.client
from phi.ldap.user import get_user_by_uid
log = get_logger(__name__)
parser = argparse.ArgumentParser()
parser.add_argument(
'--config', metavar='config.yml', help='custom configuration file')
subparses = parser.add_subparsers(title='actions', dest='action')
cli_callbacks = {}
def cli_register(action_info='', param_infos=[]):
def decorator(action):
# Get function name and arguments
action_name = action.__name__
param_names = inspect.getfullargspec(action)[0]
# Create subparser for specific action
subparser = subparses.add_parser(action_name, help=action_info)
for name, info in zip(param_names, param_infos):
subparser.add_argument(dest=name, help=info)
# Register action
cli_callbacks[action_name] = action, param_names
return action
return decorator
def cli_run(args):
for action_name, (action, param_names) in cli_callbacks.items():
if args['action'] == action_name:
action(**{pname: args[pname] for pname in param_names})
def cli_get_args():
namespace = parser.parse_args(sys.argv[1:])
args = namespace.__dict__
return args
@cli_register('dispaly user field', ['user identifier'])
@cli.register('dispaly user field', ['user identifier'])
def showuser(uid):
user = get_user_by_uid(client, uid)
print('\nuid: {}\n{}\n\n'.format(uid, pp(user)))
if __name__ == '__main__':
args = cli_get_args()
cli.add_arg('--config', 'config.yml', 'custom configuration file')
args = cli.get_args()
config_file = args['config']
@ -75,7 +35,7 @@ if __name__ == '__main__':
log.info('Arguments: {}'.format(pp(args)))
cli_run(args)
cli.run(args)
log.info('Closing LDAP client')
client.close()