Started updated user_* commands to use textui

This commit is contained in:
Jason Gerard DeRose 2008-11-17 15:27:08 -07:00
parent e7ec413158
commit 42bf555a3a
3 changed files with 59 additions and 12 deletions

View File

@ -209,9 +209,29 @@ class ldap(CrudBackend):
# The CRUD operations # The CRUD operations
def strip_none(self, kw):
"""
Remove any None values present in the LDAP attribute dict.
"""
for (key, value) in kw.iteritems():
if value is None:
continue
if type(value) in (list, tuple):
value = filter(
lambda v: type(v) in (str, unicode, bool, int, float),
value
)
if len(value) > 0:
yield (key, value)
else:
assert type(value) in (str, unicode, bool, int, float)
yield (key, value)
def create(self, **kw): def create(self, **kw):
if servercore.entry_exists(kw['dn']): if servercore.entry_exists(kw['dn']):
raise errors.DuplicateEntry("entry already exists") raise errors.DuplicateEntry("entry already exists")
kw = dict(self.strip_none(kw))
entry = ipaldap.Entry(kw['dn']) entry = ipaldap.Entry(kw['dn'])

View File

@ -198,6 +198,28 @@ class textui(backend.Backend):
for (key, value) in rows: for (key, value) in rows:
self.print_indented('%s = %r' % (key, value), indent) self.print_indented('%s = %r' % (key, value), indent)
def print_entry(self, entry, indent=1):
"""
Print an ldap entry dict.
For example:
>>> entry = dict(sn='Last', givenname='First', uid='flast')
>>> ui = textui()
>>> ui.print_entry(entry)
givenname: 'First'
sn: 'Last'
uid: 'flast'
"""
assert type(entry) is dict
for key in sorted(entry):
value = entry[key]
if type(value) in (list, tuple):
value = ', '.join(repr(v) for v in value)
else:
value = repr(value)
self.print_indented('%s: %s' % (key, value), indent)
def print_dashed(self, string, above=True, below=True): def print_dashed(self, string, above=True, below=True):
""" """
Print a string with a dashed line above and/or below. Print a string with a dashed line above and/or below.

View File

@ -191,18 +191,21 @@ class user_add(crud.Add):
kw['objectClass'] = config.get('ipauserobjectclasses') kw['objectClass'] = config.get('ipauserobjectclasses')
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret):
def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_name(self.name)
print "User added" textui.print_entry(result)
textui.print_dashed('Added user "%s"' % result['uid'])
api.register(user_add) api.register(user_add)
class user_del(crud.Del): class user_del(crud.Del):
'Delete an existing user.' 'Delete an existing user.'
def execute(self, uid, **kw): def execute(self, uid, **kw):
"""Delete a user. Not to be confused with inactivate_user. This """Delete a user. Not to be confused with inactivate_user. This
makes the entry go away completely. makes the entry go away completely.
@ -224,12 +227,12 @@ class user_del(crud.Del):
ldap = self.api.Backend.ldap ldap = self.api.Backend.ldap
dn = ldap.find_entry_dn("uid", uid) dn = ldap.find_entry_dn("uid", uid)
return ldap.delete(dn) return ldap.delete(dn)
def output_for_cli(self, ret):
def output_for_cli(self, textui, result, uid):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain('Deleted user "%s"' % uid)
print "User deleted"
api.register(user_del) api.register(user_del)
@ -254,12 +257,13 @@ class user_mod(crud.Mod):
dn = ldap.find_entry_dn("uid", uid) dn = ldap.find_entry_dn("uid", uid)
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, uid, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_name(self.name)
print "User updated" textui.print_entry(result)
textui.print_dashed('Updated user "%s"' % result['uid'])
api.register(user_mod) api.register(user_mod)
@ -330,9 +334,10 @@ class user_show(crud.Get):
return ldap.retrieve(dn) return ldap.retrieve(dn)
else: else:
return ldap.retrieve(dn, default_attributes) return ldap.retrieve(dn, default_attributes)
def output_for_cli(self, user):
if user: def output_for_cli(self, textui, result, uid, **options):
display_user(user) if result:
display_user(result)
api.register(user_show) api.register(user_show)