Win messages in json

This commit is contained in:
Kload 2013-03-15 15:59:00 +00:00
parent fee3c7c47c
commit 4b12b7a4c4
2 changed files with 20 additions and 9 deletions

View file

@ -38,7 +38,7 @@ if not __debug__:
gettext.install('YunoHost') gettext.install('YunoHost')
try: try:
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate, win
except ImportError: except ImportError:
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n') sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
sys.exit(1) sys.exit(1)
@ -200,15 +200,21 @@ def main():
#print(_("Not (yet) implemented function")) #print(_("Not (yet) implemented function"))
#return 1 #return 1
except YunoHostError, error: except YunoHostError, error:
display_error(error) display_error(error, json_print)
return error.code return error.code
else: else:
if result is None: if json_print or not os.isatty(1):
pass if result is None:
elif os.isatty(1) and not json_print: result = {}
if len(win) > 0:
result['success'] = []
for msg in win:
result['success'].append(msg)
print(json.dumps(result))
elif result is not None:
pretty_print_dict(result) pretty_print_dict(result)
else: else:
print(json.dumps(result)) pass
return 0 return 0

View file

@ -17,6 +17,7 @@ import string
if not __debug__: if not __debug__:
import traceback import traceback
win = []
lemon_tmp_conf = '/tmp/tmplemonconf' lemon_tmp_conf = '/tmp/tmplemonconf'
def random_password(length=8): def random_password(length=8):
@ -87,8 +88,12 @@ def win_msg(astr):
astr -- Win message to display astr -- Win message to display
""" """
global win
if os.isatty(1): if os.isatty(1):
print('\n' + colorize(_("Success: "), 'green') + astr + '\n') print('\n' + colorize(_("Success: "), 'green') + astr + '\n')
else:
win.append(astr)
def str_to_func(astr): def str_to_func(astr):
@ -175,17 +180,17 @@ def get_required_args(args, required_args, password=False):
return args return args
def display_error(error): def display_error(error, json_print=False):
""" """
Nice error displaying Nice error displaying
""" """
if not __debug__ : if not __debug__ :
traceback.print_exc() traceback.print_exc()
if os.isatty(1): if os.isatty(1) and not json_print:
print('\n' + colorize(_("Error: "), 'red') + error.message) print('\n' + colorize(_("Error: "), 'red') + error.message)
else: else:
print(json.dumps({ 'error' : error.message })) print(json.dumps({ error.code : error.message }))
def lemon_configuration(conf_dict): def lemon_configuration(conf_dict):