Improvements

This commit is contained in:
Kload 2012-10-06 20:29:00 +02:00
parent 5b262362af
commit 3f30dee108

163
yunohost
View file

@ -1,12 +1,45 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- 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 sys import sys
import argparse import argparse
sys.path.append('lib') # Local temporary hack sys.path.append('lib') # Local temporary hack
# Version of YunoHost
version = '2.0' """ Useful string-to-function definition """
def str2fun(astr):
module, _, function = astr.rpartition('.')
if module:
__import__(module)
mod = sys.modules[module]
else:
mod = sys.modules['__main__'] # or whatever's the "default module"
try:
func = getattr(mod, function)
except NameError:
print 'Error: Function ' + category + '_' + action + '() is not defined'
sys.exit(1)
return func
""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""
CATEGORIES / ACTIONS CATEGORIES / ACTIONS
@ -39,62 +72,48 @@ Leads to functions:
category6_action6() category6_action6()
""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""
parser_array = {
'user' : { def parse_args(parser):
'actions' : {
'list' : 'list users' parser_array = {
'user' : {
'actions' : {
'list' : 'list users'
},
'help' : 'manage users'
}, },
'help' : 'manage users' 'domain' : {
}, 'actions' : {},
'domain' : { 'help' : 'manage domains'
'actions' : {}, },
'help' : 'manage domains' 'app' : {
}, 'actions' : {},
'app' : { 'help' : 'manage apps'
'actions' : {}, },
'help' : 'manage apps' 'monitor' : {
}, 'actions' : {},
'monitor' : { 'help' : 'monitoring functions'
'actions' : {}, },
'help' : 'monitoring functions' 'tools' : {
}, 'actions' : {},
'tools' : { 'help' : 'specific tools'
'actions' : {}, }
'help' : 'specific tools'
} }
}
""" Useful string-to-function definition """ """ Intialize parsers """
def str2fun(astr): subparsers = parser.add_subparsers()
module, _, function = astr.rpartition('.') parsers = subparsers_category = subparsers_action = dict()
if module:
__import__(module)
mod = sys.modules[module]
else:
mod = sys.modules['__main__'] # or whatever's the "default module"
try: """
func = getattr(mod, function) Turn above array into subparsers (e.g. parsers['user_list'])
except NameError: and assign functions related to (e.g. user_list())
print 'Error: Function ' + category + '_' + action + '() is not defined' """
sys.exit(1) for category, info in parser_array.items():
return func subparsers_category[category] = subparsers.add_parser(category, help = info['help'])
subparsers_action[category] = subparsers_category[category].add_subparsers()
""" Intialize parsers """ for action, helper in info['actions'].items():
parser = argparse.ArgumentParser() parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help = helper)
subparsers = parser.add_subparsers() parsers[category + '_' + action].set_defaults(func = str2fun('yunohost_' + category + '.' + category + '_' + action))
parsers = subparsers_category = subparsers_action = dict()
"""
Turn above array into subparsers (e.g. parsers['user_list'])
and assign functions related to (e.g. user_list())
"""
for category, info in parser_array.items():
subparsers_category[category] = subparsers.add_parser(category, help = info['help'])
subparsers_action[category] = subparsers_category[category].add_subparsers()
for action, helper in info['actions'].items():
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help = helper)
parsers[category + '_' + action].set_defaults(func = str2fun('yunohost_' + category + '.' + category + '_' + action))
""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""""""
@ -114,26 +133,32 @@ Documentation:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" General """ """ General """
parser.add_argument( parser.add_argument(
'-v', '-v',
'--version', '--version',
action = 'version', action = 'version',
version = '%(prog)s '+ version version = '%(prog)s '+ version
) )
""" User """ """ User """
parsers['user_list'].add_argument( parsers['user_list'].add_argument(
'-a', '-a',
'--all', '--all',
action = 'store' action = 'store'
) )
""" Call arguments parsing """
args = parser.parse_args()
return args
""" Call arguments parsing """
args = parser.parse_args()
def main(args): def main(args):
parser = argparse.ArgumentParser()
args = parse_args(parser)
args.func(vars(args)) args.func(vars(args))
if __name__ == '__main__': if __name__ == '__main__':