moulinette/yunohost

216 lines
6.6 KiB
Text
Raw Normal View History

2012-10-06 17:57:08 +02:00
#!/usr/bin/env python
2012-10-06 16:22:15 +02:00
# -*- coding: utf-8 -*-
2012-10-07 16:20:20 +02:00
__credits__ = """
Copyright (C) 2012 YunoHost
2012-10-06 20:29:00 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, see http://www.gnu.org/licenses
2012-10-07 16:20:20 +02:00
"""
2012-10-07 18:20:06 +02:00
__author__ = 'Kload <kload@kload.fr>'
2012-10-07 13:37:59 +02:00
__version__ = '2.0 beta1'
2012-10-06 20:29:00 +02:00
2012-10-08 18:16:43 +02:00
import os
2012-10-06 16:22:15 +02:00
import sys
import argparse
2012-10-07 16:20:20 +02:00
import gettext
2012-10-08 18:16:43 +02:00
import json
if not __debug__:
import traceback
2012-10-10 12:20:28 +02:00
sys.path.append('lib')
2012-10-07 16:20:20 +02:00
gettext.install('YunoHost')
2012-10-07 13:16:19 +02:00
2012-10-10 14:19:17 +02:00
from yunohost import YunoHostError, str_to_func, colorize, pretty_print_dict
2012-10-06 20:29:00 +02:00
2012-10-08 20:46:52 +02:00
2012-10-08 23:02:26 +02:00
"""
2012-10-10 12:20:28 +02:00
Category/actions/arguments dictionnary
2012-10-08 20:46:52 +02:00
2012-10-10 12:20:28 +02:00
Except for general_arguments, this dictionary contains 3 levels
as in this sample command line :
yunohost monitor info --cpu --ram
^ ^ ^ ^
(script) | category | action | parameters
2012-10-08 20:46:52 +02:00
2012-10-10 12:20:28 +02:00
Above example will lead to the function 'monitor_info(args)'
in the file 'lib/yunohost_monitor.py' with 'cpu' and 'ram'
stored in args dictionnary.
Usage:
You can add a category at the first level, action at the second one,
and arguments at the third one.
Documentation:
You can see all arguments settings at the argparse documentation:
http://docs.python.org/dev/library/argparse.html
#argparse.ArgumentParser.add_argument
Don't forget to turn them as a dictionnary ('setting' : 'value')
2012-10-08 23:02:26 +02:00
"""
2012-10-10 12:20:28 +02:00
action_dict = {
#############################
# General args #
#############################
'general_arguments' : {
'-v' : {
'full' : '--version',
'help' : 'Display %(prog)s version',
'action' : 'version',
'version' : '%(prog)s ' + __version__,
},
}
2012-10-10 11:10:57 +02:00
#############################
# User #
#############################
'user' : {
2012-10-08 18:16:43 +02:00
'help' : 'Manage users',
'actions' : {
2012-10-10 11:10:57 +02:00
### user_list()
'list' : {
'help' : 'List users',
'arguments' : {
'-a' : {
'full' : '--all',
'help' : 'Display all users',
'action' : 'store_true'
},
}
},
### user_add()
'add' : {
'help' : 'Create user',
'arguments' : {
'-u' : {
'full' : '--username',
},
'-f' : {
'full' : '--firstname',
},
'-l' : {
'full' : '--lastname',
},
'-m' : {
'full' : '--mail',
},
'-p' : {
'full' : '--password',
},
}
},
2012-10-10 11:34:36 +02:00
}
2012-10-08 18:16:43 +02:00
},
'domain' : {
'help' : 'Manage domains',
2012-10-10 11:34:36 +02:00
'actions' : {}
2012-10-08 18:16:43 +02:00
},
'app' : {
'help' : 'Manage apps',
2012-10-10 11:34:36 +02:00
'actions' : {}
2012-10-08 18:16:43 +02:00
},
'monitor' : {
'help' : 'Monitoring functions',
2012-10-10 11:34:36 +02:00
'actions' : {}
2012-10-08 18:16:43 +02:00
},
'tools' : {
'help' : 'Specific tools',
2012-10-10 11:34:36 +02:00
'actions' : {}
2012-10-08 18:16:43 +02:00
}
}
2012-10-06 16:22:15 +02:00
2012-10-10 11:10:57 +02:00
def dict_to_args(action_dict):
2012-10-08 20:46:52 +02:00
"""
2012-10-10 11:10:57 +02:00
Turn action dictionnary to parser, subparsers and arguments
2012-10-08 20:46:52 +02:00
Keyword arguments:
2012-10-10 11:10:57 +02:00
action_dict -- Multi-level dictionnary of categories/actions/arguments list
2012-10-08 20:46:52 +02:00
Returns:
2012-10-10 11:10:57 +02:00
Namespace of args
2012-10-08 20:46:52 +02:00
"""
# Intialize parsers
parsers = subparsers_category = subparsers_action = {}
parsers['general'] = argparse.ArgumentParser()
subparsers = parsers['general'].add_subparsers()
2012-10-10 12:20:28 +02:00
# Add general arguments
for arg_name, arg_params in action_dict['general_arguments'].items():
if arg_params['full']:
arg_fullname = arg_params['full']
del arg_params['full']
parsers['general'].add_argument(arg_name, arg_fullname, **arg_params)
else:
parsers['general'].add_argument(arg_name, **arg_params)
del action_dict['general_arguments']
# Split categories into subparsers
2012-10-10 11:34:36 +02:00
for category, cat_params in action_dict.items():
subparsers_category[category] = subparsers.add_parser(category, help=cat_params['help'])
2012-10-08 20:46:52 +02:00
subparsers_action[category] = subparsers_category[category].add_subparsers()
2012-10-10 12:20:28 +02:00
# Split actions
2012-10-10 11:34:36 +02:00
for action, action_params in cat_params['actions'].items():
2012-10-10 11:10:57 +02:00
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['help'])
2012-10-10 12:20:28 +02:00
# Set the action's related function
2012-10-08 20:46:52 +02:00
parsers[category + '_' + action].set_defaults(
func=str_to_func('yunohost_' + category + '.'
+ category + '_' + action))
2012-10-10 12:20:28 +02:00
# Add arguments
2012-10-10 11:10:57 +02:00
for arg_name, arg_params in action_params['arguments'].items():
if arg_params['full']:
arg_fullname = arg_params['full']
del arg_params['full']
parsers[category + '_' + action].add_argument(arg_name, arg_fullname, **arg_params)
else:
parsers[category + '_' + action].add_argument(arg_name, **arg_params)
2012-10-08 20:46:52 +02:00
2012-10-10 11:10:57 +02:00
args = parsers['general'].parse_args()
return args
2012-10-08 20:46:52 +02:00
2012-10-07 13:16:19 +02:00
def main():
2012-10-10 12:20:28 +02:00
"""
Main instructions
2012-10-10 14:19:17 +02:00
Parse the action_dict and execute the action-specific function,
then print json or pretty result if executed in a tty :)
2012-10-10 12:20:28 +02:00
Returns:
int -- 0 or error code
"""
2012-10-08 18:16:43 +02:00
try:
2012-10-10 11:10:57 +02:00
args = dict_to_args(action_dict)
2012-10-08 18:16:43 +02:00
result = args.func(vars(args))
except YunoHostError, error:
if not __debug__ :
traceback.print_exc()
if os.isatty(1):
print('\n' + colorize(_('Error: '), 'red') + error.message)
else:
print(json.dumps({ 'error' : error.message }))
return error.code
else:
if os.isatty(1):
2012-10-10 14:19:17 +02:00
pretty_print_dict(result)
2012-10-08 18:16:43 +02:00
else:
print(json.dumps(result))
return 0
2012-10-07 13:16:19 +02:00
2012-10-06 16:22:15 +02:00
if __name__ == '__main__':
2012-10-08 18:16:43 +02:00
sys.exit(main())