moulinette/yunohost

140 lines
3.5 KiB
Text
Raw Normal View History

2012-10-06 16:22:15 +02:00
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import sys
import argparse
2012-10-06 17:25:09 +02:00
sys.path.append('lib') # Local temporary hack
2012-10-06 16:22:15 +02:00
# Version of YunoHost
version = '2.0'
"""""""""""""""""""""""""""""""""""""""""""""""""""""
CATEGORIES / ACTIONS
array = {
'category1' : {
'actions' : {
'action1' : 'text of action1 help'
'action2' : 'text of action2 help'
'action3' : 'text of action3 help'
},
'help' : 'text of category1 help'
},
'category2' : {
'actions' : {
'action4' : 'text of action4 help'
'action5' : 'text of action5 help'
'action6' : 'text of action6 help'
},
'help' : 'text of category2 help'
},
}
Leads to functions:
category1_action1()
category1_action2()
category1_action3()
category2_action4()
category2_action5()
category6_action6()
"""""""""""""""""""""""""""""""""""""""""""""""""""""
parser_array = {
'user' : {
'actions' : {
'list' : 'list users'
},
'help' : 'manage users'
},
'domain' : {
'actions' : {},
'help' : 'manage domains'
},
'app' : {
'actions' : {},
'help' : 'manage apps'
},
'monitor' : {
'actions' : {},
'help' : 'monitoring functions'
},
'tools' : {
'actions' : {},
'help' : 'specific tools'
}
}
""" 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"
return getattr(mod, function)
""" Intialize parsers """
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
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())
"""
2012-10-06 17:10:19 +02:00
2012-10-06 16:22:15 +02:00
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)
2012-10-06 16:34:11 +02:00
try:
2012-10-06 17:10:19 +02:00
parsers[category + '_' + action].set_defaults(func = str2fun('yunohost_' + category + '.' + category + '_' + action))
2012-10-06 16:34:11 +02:00
except AttributeError:
print 'Error: Function ' + category + '_' + action + '() has not been defined'
sys.exit(1)
2012-10-06 16:22:15 +02:00
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
ARGUMENT PARSING
Add general argument:
parser.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 """
parser.add_argument(
'-v',
'--version',
action = 'version',
version = '%(prog)s '+ version
)
""" User """
parsers['user_list'].add_argument(
'-a',
'--all',
action = 'store'
)
""" Call arguments parsing """
args = parser.parse_args()
def main(args):
2012-10-06 17:25:09 +02:00
args.func(vars(args))
2012-10-06 16:22:15 +02:00
if __name__ == '__main__':
main(args)