Rename files

This commit is contained in:
Kloadut 2012-10-10 20:53:42 +02:00
parent 45fcc5bed8
commit c5edde0053
3 changed files with 30 additions and 19 deletions

View file

@ -31,7 +31,6 @@ if not __debug__:
gettext.install('YunoHost') gettext.install('YunoHost')
try: try:
sys.path.append('lib')
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict
except ImportError: except ImportError:
sys.stderr.write('Require YunoHost lib') sys.stderr.write('Require YunoHost lib')
@ -342,6 +341,18 @@ def parse_dict(action_dict):
args = parsers['general'].parse_args() args = parsers['general'].parse_args()
return { 'ldap' : ldap, 'args' : args } return { 'ldap' : ldap, 'args' : args }
def display_error(error):
"""
Nice error displaying
"""
if not __debug__ :
traceback.print_exc()
if os.isatty(1):
print('\n' + colorize(_("Error: "), 'red') + error.message)
else:
print(json.dumps({ 'error' : error.message }))
def main(): def main():
""" """
Main instructions Main instructions
@ -355,25 +366,24 @@ def main():
""" """
action = parse_dict(action_dict) action = parse_dict(action_dict)
# Connect to LDAP if the action is requiring it try:
if action['ldap']: # Connect to LDAP if the action is requiring it
yldap = YunoHostLDAP() if action['ldap']:
yldap = YunoHostLDAP()
except YunoHostError, error:
display_error(error)
return error.code
try: try:
if action['ldap']: if action['ldap']:
result = action['args'].func(vars(action['args']), yldap) result = action['args'].func(vars(action['args']), yldap)
else: else:
result = action['args'].func(vars(action['args'])) result = action['args'].func(vars(action['args']))
#except TypeError: except TypeError:
#print(_("Not (yet) implemented function")) print(_("Not (yet) implemented function"))
#return 1 return 1
except YunoHostError, error: except YunoHostError, error:
if not __debug__ : display_error(error)
traceback.print_exc()
if os.isatty(1):
print('\n' + colorize(_("Error: "), 'red') + error.message)
else:
print(json.dumps({ 'error' : error.message }))
return error.code return error.code
else: else:
if os.isatty(1): if os.isatty(1):

View file

@ -6,7 +6,6 @@ import ldap
import ldap.modlist as modlist import ldap.modlist as modlist
import re import re
import getpass import getpass
sys.path.append('./') # Local temporary hack
def colorize(astr, color): def colorize(astr, color):
@ -141,7 +140,7 @@ class YunoHostLDAP:
""" """
self.conn = ldap.initialize('ldap://localhost:389') self.conn = ldap.initialize('ldap://localhost:389')
self.base = 'dc=yunohost,dc=org' self.base = 'dc=yunohost,dc=org'
self.pwd = getpass.getpass(_('LDAP Admin Password: ')) self.pwd = getpass.getpass(colorize(_('LDAP Admin Password: '), 'yellow'))
try: try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd) self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
except ldap.INVALID_CREDENTIALS: except ldap.INVALID_CREDENTIALS:

View file

@ -32,14 +32,14 @@ def user_create(args, yldap):
for arg in required_args: for arg in required_args:
if not args[arg]: if not args[arg]:
if os.isatty(1): if os.isatty(1):
args[arg] = raw_input(colorize(arg.capitalize()+': ', 'yellow')) args[arg] = raw_input(colorize(arg.capitalize()+': ', 'cyan'))
else: else:
raise Exception raise Exception
# Password # Password
if not args['password']: if not args['password']:
if os.isatty(1): if os.isatty(1):
args['password'] = getpass.getpass(colorize('Password: ', 'yellow')) args['password'] = getpass.getpass(colorize('Password: ', 'cyan'))
pwd2 = getpass.getpass(colorize('Retype password:', 'yellow')) pwd2 = getpass.getpass(colorize('Retype password:', 'cyan'))
if args['password'] != pwd2: if args['password'] != pwd2:
raise YunoHostError(22, _("Passwords doesn't match")) raise YunoHostError(22, _("Passwords doesn't match"))
else: else:
@ -78,8 +78,10 @@ def user_create(args, yldap):
'mailalias' : args['mail'] 'mailalias' : args['mail']
}) })
#TODO: check if mail belongs to a domain
if yldap.add(rdn, attr_dict): if yldap.add(rdn, attr_dict):
win_msg(_("User successfully created")) win_msg(_("User successfully created"))
return attr_dict return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] }
else: else:
raise YunoHostError(169, _('An error occured during user creation')) raise YunoHostError(169, _('An error occured during user creation'))