diff --git a/bin/showgroup.py b/bin/showgroup.py index cba59723723599efb3c4b2689d08f89c3d688b78..b6daaaf0d27b96dc2f18f16d511e8c36ad55285d 100755 --- a/bin/showgroup.py +++ b/bin/showgroup.py @@ -16,12 +16,17 @@ OPTS['report'] = ['reportEnabled', 'reportUid'] OPTS['mail'] = 'mail' OPTS['telephone'] = 'telephoneNumber' +NOOPTS = collections.OrderedDict() +NOOPTS['no-member'] = 'memberUid' +NOOPTS['no-gid'] = 'gidNumber' +NOOPTS['no-owner'] = 'owner' + def main(): description = 'Show groups or group members' arg = 'group' arg_help = 'The group name (cn), or wildcards (*, cn* , *cn*)' - args = argparser.simple(description, arg=arg, arg_help=arg_help, opts=OPTS) + args = argparser.simple(description, arg=arg, arg_help=arg_help, opts=OPTS, noopts=NOOPTS) ldap = dphysldap.Ldap() @@ -38,6 +43,7 @@ def main(): if len(groups) != 1: #groups.sort('cn') + groups.remove_attrs([v for k, v in NOOPTS.items() if args[k]]) print(groups) else: diff --git a/lib/isg/argparser.py b/lib/isg/argparser.py index 923265c8d1839fa9e1d2938f6df54de88144e9c9..0eef12537a0a4432f997931de8ece46b49b8e960 100644 --- a/lib/isg/argparser.py +++ b/lib/isg/argparser.py @@ -2,7 +2,7 @@ import argparse -def simple(description, arg=None, arg_help='', opts=dict()): +def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()): """ Get cmd argument with simplified argparser """ @@ -19,6 +19,13 @@ def simple(description, arg=None, arg_help='', opts=dict()): action='store_const', const=True, help='Show ' + v) + for k, v in noopts.items(): + if isinstance(v, list): + v = ', '.join(v) + parser.add_argument('-' + k[3:4].upper(), '--' + k, dest=k, + action='store_const', const=True, + help='Hide ' + v) + parser.add_argument('--help', action='help', help='Show this help message and exit') diff --git a/lib/isg/dphysldap.py b/lib/isg/dphysldap.py index 8fa44b58b2ec21461b175d68e0fb32c8a5e9e8ab..aa44fa37a1a0c4903347f30481f7dbb976d66f62 100644 --- a/lib/isg/dphysldap.py +++ b/lib/isg/dphysldap.py @@ -150,16 +150,15 @@ class Groups(list): def __init__(self, ldap, attrs, *args): list.__init__(self, *args) self._ldap = ldap - self._attrs = attrs + self.attrs = attrs def __str__(self): """ Modifies the "informal" string value of str(x) or print(x) to return groups in tabulated form """ - headers = self._attrs - table = [[g[h] for h in headers] for g in self] - return tabulate.tabulate(table, tablefmt=FMT, headers=headers) + table = [[g[h] for h in self.attrs] for g in self] + return tabulate.tabulate(table, tablefmt=FMT, headers=self.attrs) #def sort(self, attr): # self = sorted(self, key=lambda k: k[attr].__str__()) @@ -170,7 +169,18 @@ class Groups(list): """ query = 'cn: {0}'.format(cn) self.clear() - self.extend(self._ldap.get_groups(query=query, attributes=self._attrs)) + self.extend(self._ldap.get_groups(query=query, attributes=self.attrs)) + + def remove_attr(self, attr): + if attr in self.attrs: + self.attrs.remove(attr) + + def remove_attrs(self, attrs): + if isinstance(attrs, list): + for attr in attrs: + self.remove_attr(attr) + else: + self.remove_attr(attrs) def members(self, index=None): """ @@ -192,16 +202,15 @@ class Users(list): def __init__(self, ldap, attrs, *args): list.__init__(self, *args) self._ldap = ldap - self._attrs = attrs + self.attrs = attrs def __str__(self): """ Modifies the "informal" string value of str(x) or print(x) to return users in tabulated form """ - headers = self._attrs - table = [[u[h] for h in headers] for u in self] - return tabulate.tabulate(table, tablefmt=FMT, headers=self._attrs) + table = [[u[h] for h in self.attrs] for u in self] + return tabulate.tabulate(table, tablefmt=FMT, headers=self.attrs) #def sort(self, attr): # self = sorted(self, key=lambda d: d[attr].__str__()) @@ -212,4 +221,15 @@ class Users(list): """ query = 'uid: {0}'.format(uid) self.clear() - self.extend(self._ldap.get_users(query=query, attributes=self._attrs)) + self.extend(self._ldap.get_users(query=query, attributes=self.attrs)) + + def remove_attr(self, attr): + if attr in self.attrs: + self.attrs.remove(attr) + + def remove_attrs(self, attrs): + if isinstance(attrs, list): + for attr in attrs: + self.remove_attr(attr) + else: + self.remove_attr(attrs)