mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
160 lines
4.1 KiB
Python
Executable file
160 lines
4.1 KiB
Python
Executable file
#!/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 sys
|
|
import argparse
|
|
sys.path.append('lib') # Local temporary hack
|
|
|
|
|
|
def str_to_func(astr):
|
|
"""Call a function from a string name
|
|
|
|
Keyword arguments:
|
|
astr -- Name of function to call
|
|
|
|
Returns:
|
|
Function
|
|
|
|
"""
|
|
module, _, function = astr.rpartition('.')
|
|
if module:
|
|
__import__(module)
|
|
mod = sys.modules[module]
|
|
else:
|
|
mod = sys.modules['__main__'] # default module
|
|
|
|
try:
|
|
func = getattr(mod, function)
|
|
except NameError:
|
|
print 'Error: Function ' + category + '_' + action + '() is not defined'
|
|
sys.exit(1)
|
|
else:
|
|
return func
|
|
|
|
|
|
def dict_to_parsers(action_dict):
|
|
"""Turn action dictionnary to parser and subparsers (2 level)
|
|
|
|
Keyword arguments:
|
|
action_dict -- Multi-level dictionnary of categories/actions list
|
|
|
|
Returns:
|
|
Dictionnrary of parsers
|
|
|
|
"""
|
|
|
|
# Intialize parsers
|
|
parsers = subparsers_category = subparsers_action = dict()
|
|
parsers['general'] = argparse.ArgumentParser()
|
|
subparsers = parsers['general'].add_subparsers()
|
|
|
|
# Compute dictionnary
|
|
for category, info in action_dict.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=str_to_func('yunohost_' + category
|
|
+ '.' + category
|
|
+ '_' + action))
|
|
|
|
return parsers
|
|
|
|
|
|
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
|
|
parsers['general'].add_argument(
|
|
'-v',
|
|
'--version',
|
|
action='version',
|
|
version='%(prog)s ' + __version__
|
|
)
|
|
|
|
|
|
# User
|
|
parsers['user_list'].add_argument(
|
|
'-a',
|
|
'--all',
|
|
action='store'
|
|
)
|
|
|
|
# Call arguments parsing
|
|
args = parsers['general'].parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
def main():
|
|
"""Main instructions
|
|
"""
|
|
|
|
action_dict = {
|
|
'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'
|
|
}
|
|
}
|
|
|
|
parsers = dict_to_parsers(action_dict)
|
|
args = parse_args(parsers)
|
|
args.func(vars(args))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|