diff --git a/yunohost b/parse_args similarity index 95% rename from yunohost rename to parse_args index 7638977f..3f35abcf 100755 --- a/yunohost +++ b/parse_args @@ -31,7 +31,6 @@ if not __debug__: gettext.install('YunoHost') try: - sys.path.append('lib') from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict except ImportError: sys.stderr.write('Require YunoHost lib') @@ -342,6 +341,18 @@ def parse_dict(action_dict): args = parsers['general'].parse_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(): """ Main instructions @@ -355,25 +366,24 @@ def main(): """ action = parse_dict(action_dict) - # Connect to LDAP if the action is requiring it - if action['ldap']: - yldap = YunoHostLDAP() + try: + # Connect to LDAP if the action is requiring it + if action['ldap']: + yldap = YunoHostLDAP() + except YunoHostError, error: + display_error(error) + return error.code try: if action['ldap']: result = action['args'].func(vars(action['args']), yldap) else: result = action['args'].func(vars(action['args'])) - #except TypeError: - #print(_("Not (yet) implemented function")) - #return 1 + except TypeError: + print(_("Not (yet) implemented function")) + return 1 except YunoHostError, error: - if not __debug__ : - traceback.print_exc() - if os.isatty(1): - print('\n' + colorize(_("Error: "), 'red') + error.message) - else: - print(json.dumps({ 'error' : error.message })) + display_error(error) return error.code else: if os.isatty(1): diff --git a/lib/yunohost.py b/yunohost.py similarity index 98% rename from lib/yunohost.py rename to yunohost.py index 8b461061..84ab2e2d 100644 --- a/lib/yunohost.py +++ b/yunohost.py @@ -6,7 +6,6 @@ import ldap import ldap.modlist as modlist import re import getpass -sys.path.append('./') # Local temporary hack def colorize(astr, color): @@ -141,7 +140,7 @@ class YunoHostLDAP: """ self.conn = ldap.initialize('ldap://localhost:389') self.base = 'dc=yunohost,dc=org' - self.pwd = getpass.getpass(_('LDAP Admin Password: ')) + self.pwd = getpass.getpass(colorize(_('LDAP Admin Password: '), 'yellow')) try: self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd) except ldap.INVALID_CREDENTIALS: diff --git a/lib/yunohost_user.py b/yunohost_user.py similarity index 92% rename from lib/yunohost_user.py rename to yunohost_user.py index 6b7a9d35..d0d8711d 100644 --- a/lib/yunohost_user.py +++ b/yunohost_user.py @@ -32,14 +32,14 @@ def user_create(args, yldap): for arg in required_args: if not args[arg]: if os.isatty(1): - args[arg] = raw_input(colorize(arg.capitalize()+': ', 'yellow')) + args[arg] = raw_input(colorize(arg.capitalize()+': ', 'cyan')) else: raise Exception # Password if not args['password']: if os.isatty(1): - args['password'] = getpass.getpass(colorize('Password: ', 'yellow')) - pwd2 = getpass.getpass(colorize('Retype password:', 'yellow')) + args['password'] = getpass.getpass(colorize('Password: ', 'cyan')) + pwd2 = getpass.getpass(colorize('Retype password:', 'cyan')) if args['password'] != pwd2: raise YunoHostError(22, _("Passwords doesn't match")) else: @@ -78,8 +78,10 @@ def user_create(args, yldap): 'mailalias' : args['mail'] }) + #TODO: check if mail belongs to a domain + if yldap.add(rdn, attr_dict): win_msg(_("User successfully created")) - return attr_dict + return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] } else: raise YunoHostError(169, _('An error occured during user creation'))