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-06 20:29:00 +02:00
|
|
|
__author__ = 'Kload'
|
2012-10-07 13:37:59 +02:00
|
|
|
__version__ = '2.0 beta1'
|
2012-10-06 20:29:00 +02:00
|
|
|
|
2012-10-06 16:22:15 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
2012-10-07 16:20:20 +02:00
|
|
|
import gettext
|
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-06 16:22:15 +02:00
|
|
|
|
2012-10-06 20:29:00 +02:00
|
|
|
|
2012-10-07 13:16:19 +02:00
|
|
|
def str_to_func(astr):
|
2012-10-07 16:20:20 +02:00
|
|
|
"""
|
|
|
|
Call a function from a string name
|
2012-10-07 13:16:19 +02:00
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
astr -- Name of function to call
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Function
|
|
|
|
|
2012-10-07 13:37:59 +02:00
|
|
|
"""
|
2012-10-06 20:29:00 +02:00
|
|
|
module, _, function = astr.rpartition('.')
|
|
|
|
if module:
|
|
|
|
__import__(module)
|
|
|
|
mod = sys.modules[module]
|
|
|
|
else:
|
2012-10-07 13:16:19 +02:00
|
|
|
mod = sys.modules['__main__'] # default module
|
2012-10-06 20:29:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
func = getattr(mod, function)
|
|
|
|
except NameError:
|
2012-10-07 16:20:20 +02:00
|
|
|
print(_('Error: Function is not defined'))
|
2012-10-06 20:29:00 +02:00
|
|
|
sys.exit(1)
|
2012-10-07 13:37:59 +02:00
|
|
|
else:
|
|
|
|
return func
|
2012-10-06 20:29:00 +02:00
|
|
|
|
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-06 20:29:00 +02:00
|
|
|
parsers = subparsers_category = subparsers_action = dict()
|
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 13:16:19 +02:00
|
|
|
# General
|
|
|
|
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 13:16:19 +02:00
|
|
|
# User
|
2012-10-06 20:29:00 +02:00
|
|
|
parsers['user_list'].add_argument(
|
|
|
|
'-a',
|
|
|
|
'--all',
|
2012-10-07 13:37:59 +02:00
|
|
|
action='store'
|
2012-10-06 20:29:00 +02:00
|
|
|
)
|
|
|
|
|
2012-10-07 13:16:19 +02:00
|
|
|
# Call arguments parsing
|
|
|
|
args = parsers['general'].parse_args()
|
2012-10-06 16:22:15 +02:00
|
|
|
|
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-07 16:20:20 +02:00
|
|
|
""" Main instructions """
|
2012-10-07 13:16:19 +02:00
|
|
|
|
|
|
|
action_dict = {
|
|
|
|
'user' : {
|
2012-10-07 16:20:20 +02:00
|
|
|
'help' : 'manage users',
|
2012-10-07 13:16:19 +02:00
|
|
|
'actions' : {
|
|
|
|
'list' : 'list users'
|
2012-10-07 16:20:20 +02:00
|
|
|
}
|
2012-10-07 13:16:19 +02:00
|
|
|
},
|
|
|
|
'domain' : {
|
2012-10-07 16:20:20 +02:00
|
|
|
'help' : 'manage domains',
|
|
|
|
'actions' : {}
|
2012-10-07 13:16:19 +02:00
|
|
|
},
|
|
|
|
'app' : {
|
2012-10-07 16:20:20 +02:00
|
|
|
'help' : 'manage apps',
|
|
|
|
'actions' : {}
|
2012-10-07 13:16:19 +02:00
|
|
|
},
|
|
|
|
'monitor' : {
|
2012-10-07 16:20:20 +02:00
|
|
|
'help' : 'monitoring functions',
|
|
|
|
'actions' : {}
|
2012-10-07 13:16:19 +02:00
|
|
|
},
|
|
|
|
'tools' : {
|
2012-10-07 16:20:20 +02:00
|
|
|
'help' : 'specific tools',
|
|
|
|
'actions' : {}
|
2012-10-07 13:16:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parsers = dict_to_parsers(action_dict)
|
|
|
|
args = parse_args(parsers)
|
2012-10-06 17:25:09 +02:00
|
|
|
args.func(vars(args))
|
2012-10-06 16:22:15 +02:00
|
|
|
|
2012-10-07 13:16:19 +02:00
|
|
|
|
2012-10-06 16:22:15 +02:00
|
|
|
if __name__ == '__main__':
|
2012-10-07 13:16:19 +02:00
|
|
|
main()
|