Format code

This commit is contained in:
Kloadut 2012-10-07 13:37:59 +02:00
parent df77aa9858
commit 8b781dc9f8

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__credits__ = '''Copyright (C) 2012 YunoHost __credits__ = """Copyright (C) 2012 YunoHost
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published
@ -15,9 +15,9 @@ __credits__ = '''Copyright (C) 2012 YunoHost
You should have received a copy of the GNU Affero General Public License 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 along with this program; if not, see http://www.gnu.org/licenses
''' """
__author__ = 'Kload' __author__ = 'Kload'
__version__ = '2.0_beta1' __version__ = '2.0 beta1'
import sys import sys
import argparse import argparse
@ -25,7 +25,7 @@ sys.path.append('lib') # Local temporary hack
def str_to_func(astr): def str_to_func(astr):
''' Call a function from a string name """Call a function from a string name
Keyword arguments: Keyword arguments:
astr -- Name of function to call astr -- Name of function to call
@ -33,7 +33,7 @@ def str_to_func(astr):
Returns: Returns:
Function Function
''' """
module, _, function = astr.rpartition('.') module, _, function = astr.rpartition('.')
if module: if module:
__import__(module) __import__(module)
@ -46,11 +46,12 @@ def str_to_func(astr):
except NameError: except NameError:
print 'Error: Function ' + category + '_' + action + '() is not defined' print 'Error: Function ' + category + '_' + action + '() is not defined'
sys.exit(1) sys.exit(1)
else:
return func return func
def dict_to_parsers(action_dict): def dict_to_parsers(action_dict):
''' Turn action dictionnary to parser and subparsers (2 level) """Turn action dictionnary to parser and subparsers (2 level)
Keyword arguments: Keyword arguments:
action_dict -- Multi-level dictionnary of categories/actions list action_dict -- Multi-level dictionnary of categories/actions list
@ -58,7 +59,7 @@ def dict_to_parsers(action_dict):
Returns: Returns:
Dictionnrary of parsers Dictionnrary of parsers
''' """
# Intialize parsers # Intialize parsers
parsers = subparsers_category = subparsers_action = dict() parsers = subparsers_category = subparsers_action = dict()
@ -71,13 +72,16 @@ def dict_to_parsers(action_dict):
subparsers_action[category] = subparsers_category[category].add_subparsers() subparsers_action[category] = subparsers_category[category].add_subparsers()
for action, helper in info['actions'].items(): for action, helper in info['actions'].items():
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper) parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper)
parsers[category + '_' + action].set_defaults(func = str_to_func('yunohost_' + category + '.' + category + '_' + action)) parsers[category + '_' + action].set_defaults(
func=str_to_func('yunohost_' + category
+ '.' + category
+ '_' + action))
return parsers return parsers
def parse_args(parsers): def parse_args(parsers):
''' Add and parse arguments """Add and parse arguments
Keyword arguments: Keyword arguments:
parsers -- parsers and subparsers as a dict parsers -- parsers and subparsers as a dict
@ -93,7 +97,7 @@ def parse_args(parsers):
http://docs.python.org/dev/library/argparse.html http://docs.python.org/dev/library/argparse.html
#argparse.ArgumentParser.add_argument #argparse.ArgumentParser.add_argument
''' """
# General # General
parsers['general'].add_argument( parsers['general'].add_argument(
@ -119,7 +123,8 @@ def parse_args(parsers):
def main(): def main():
''' Main instructions ''' """Main instructions
"""
action_dict = { action_dict = {
'user' : { 'user' : {