moulinette/yunohost

168 lines
4.7 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-06 17:25:09 +02:00
sys.path.append('lib') # Local temporary hack
2012-10-07 16:20:20 +02:00
gettext.install('YunoHost')
2012-10-07 13:16:19 +02:00
2012-10-08 18:16:43 +02:00
from yunohost import YunoHostError, str_to_func, colorize
2012-10-06 20:29:00 +02:00
2012-10-08 18:16:43 +02:00
action_dict = {
'user' : {
'help' : 'Manage users',
'actions' : {
'list' : 'List users',
'add' : 'Add user'
}
},
'domain' : {
'help' : 'Manage domains',
'actions' : {}
},
'app' : {
'help' : 'Manage apps',
'actions' : {}
},
'monitor' : {
'help' : 'Monitoring functions',
'actions' : {}
},
'tools' : {
'help' : 'Specific tools',
'actions' : {}
}
}
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
def dict_to_parsers(action_dict):
2012-10-07 16:20:20 +02:00
"""
Turn action dictionnary to parser and subparsers (2 level)
2012-10-07 13:16:19 +02:00
Keyword arguments:
action_dict -- Multi-level dictionnary of categories/actions list
Returns:
Dictionnrary of parsers
2012-10-06 20:29:00 +02:00
2012-10-07 13:37:59 +02:00
"""
2012-10-07 13:16:19 +02:00
# Intialize parsers
2012-10-07 23:51:40 +02:00
parsers = subparsers_category = subparsers_action = {}
2012-10-07 13:16:19 +02:00
parsers['general'] = argparse.ArgumentParser()
subparsers = parsers['general'].add_subparsers()
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
# Compute dictionnary
for category, info in action_dict.items():
2012-10-07 13:37:59 +02:00
subparsers_category[category] = subparsers.add_parser(category, help=info['help'])
2012-10-06 20:29:00 +02:00
subparsers_action[category] = subparsers_category[category].add_subparsers()
for action, helper in info['actions'].items():
2012-10-07 13:37:59 +02:00
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper)
parsers[category + '_' + action].set_defaults(
2012-10-07 16:20:20 +02:00
func=str_to_func('yunohost_' + category + '.'
+ category + '_' + action))
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
return parsers
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
def parse_args(parsers):
2012-10-07 16:20:20 +02:00
"""
Add and parse arguments
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
Keyword arguments:
parsers -- parsers and subparsers as a dict
Returns:
Namespace of arguments
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
Usage:
Add general argument: parsers['general'].add_argument( options )
Add specific argument: parsers['user_delete'].add_argument( options )
Documentation:
http://docs.python.org/dev/library/argparse.html
#argparse.ArgumentParser.add_argument
2012-10-07 13:37:59 +02:00
"""
2012-10-07 18:20:06 +02:00
#####################
# General #
#####################
# Version
2012-10-07 13:16:19 +02:00
parsers['general'].add_argument(
2012-10-06 20:29:00 +02:00
'-v',
'--version',
2012-10-07 13:37:59 +02:00
action='version',
version='%(prog)s ' + __version__
2012-10-06 20:29:00 +02:00
)
2012-10-07 17:57:57 +02:00
#####################
# User #
#####################
2012-10-06 20:29:00 +02:00
2012-10-07 17:57:57 +02:00
# user list
2012-10-07 18:20:06 +02:00
parsers['user_list'].add_argument('-a', '--all', action='store_true')
2012-10-06 20:29:00 +02:00
2012-10-07 17:57:57 +02:00
# user add
parsers['user_add'].add_argument('-u', '--username')
parsers['user_add'].add_argument('-m', '--mail')
parsers['user_add'].add_argument('-f', '--firstname')
parsers['user_add'].add_argument('-l', '--lastname')
parsers['user_add'].add_argument('-p', '--password')
2012-10-07 18:20:06 +02:00
#########################
# End of arguments #
#########################
2012-10-07 13:16:19 +02:00
args = parsers['general'].parse_args()
2012-10-06 20:29:00 +02:00
return args
2012-10-06 16:22:15 +02:00
2012-10-07 13:16:19 +02:00
def main():
2012-10-08 18:16:43 +02:00
""" Main instructions """
try:
parsers = dict_to_parsers(action_dict)
args = parse_args(parsers)
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):
for attr, value in result.items():
if type(value) is str:
print(colorize(attr, 'purple') + ': ' + value)
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())