#!/usr/bin/env python # -*- coding: utf-8 -*- __credits__ = """ Copyright (C) 2012 YunoHost 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 """ __author__ = 'Kload ' __version__ = '2.0 beta1' import os import sys import argparse import gettext import json if not __debug__: import traceback sys.path.append('lib') # Local temporary hack gettext.install('YunoHost') from yunohost import YunoHostError, str_to_func, colorize """ Category/actions dictionnary Usage: Add your "'action' : 'help'" to a category of this dictionnary, and make sure the file yunohost_my-category.py exists in ./lib Then create the function called category_action(args). You can add some arguments with the function parse_args() below. Example: 'add' : 'Add user' refers to : user_add(args) function in lib/yunohost_user.py """ action_dict = { ############################# # User # ############################# 'user' : { 'help' : 'Manage users', 'actions' : { ### 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', }, } }, } }, 'domain' : { 'help' : 'Manage domains', 'actions' : {} }, 'app' : { 'help' : 'Manage apps', 'actions' : {} }, 'monitor' : { 'help' : 'Monitoring functions', 'actions' : {} }, 'tools' : { 'help' : 'Specific tools', 'actions' : {} } } def parse_args(parsers): """ Add and parse arguments Keyword arguments: parsers -- parsers and subparsers as a dict Returns: Namespace of arguments 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 """ ##################### # General # ##################### # Version parsers['general'].add_argument( '-v', '--version', action='version', version='%(prog)s ' + __version__ ) ##################### # User # ##################### # user list parsers['user_list'].add_argument('-a', '--all', action='store_true') # 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') ######################### # End of arguments # ######################### args = parsers['general'].parse_args() return args def dict_to_args(action_dict): """ Turn action dictionnary to parser, subparsers and arguments Keyword arguments: action_dict -- Multi-level dictionnary of categories/actions/arguments list Returns: Namespace of args """ # Intialize parsers parsers = subparsers_category = subparsers_action = {} parsers['general'] = argparse.ArgumentParser() subparsers = parsers['general'].add_subparsers() # Compute dictionnary for category, cat_params in action_dict.items(): subparsers_category[category] = subparsers.add_parser(category, help=cat_params['help']) subparsers_action[category] = subparsers_category[category].add_subparsers() for action, action_params in cat_params['actions'].items(): parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['help']) parsers[category + '_' + action].set_defaults( func=str_to_func('yunohost_' + category + '.' + category + '_' + action)) # Argument parsing 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) args = parsers['general'].parse_args() return args def main(): """ Main instructions """ try: args = dict_to_args(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 if __name__ == '__main__': sys.exit(main())