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')
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:
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
sys.exit(1)
@ -200,15 +200,21 @@ def main():
#print(_("Not (yet) implemented function"))
#return 1
except YunoHostError, error:
display_error(error)
display_error(error, json_print)
return error.code
else:
if json_print or not os.isatty(1):
if result is None:
pass
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)
else:
print(json.dumps(result))
pass
return 0

View file

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