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..72b26f8060f6ae08859bf16a5c35be181e02776a 100644 --- a/lib/isg/dphysldap.py +++ b/lib/isg/dphysldap.py @@ -143,34 +143,57 @@ class Ldap(object): return [Group(e) for e in entries] -class Groups(list): +class Entries(list): """ - Abstraction class for Groups imitating list + Abstraction class for Entries imitating 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 + to return entries 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 = [[e[a] for a in self.attrs] for e in self] + return tabulate.tabulate(table, tablefmt=FMT, headers=self.attrs) #def sort(self, attr): # self = sorted(self, key=lambda k: k[attr].__str__()) + 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 search(self, query): + """ + Query syntax: `<attributeName>: <attributeValue(s)>` + AttributeValue: `a`, `a*`, `*a*`, `a;b` + """ + self.clear() + self.extend(self._ldap.get_entries(query=query, attributes=self.attrs)) + + +class Groups(Entries): + """ + Abstraction class for Groups imitating list + """ def search(self, cn): """ Search example: `cn`, `cn*`, `*cn*`, `cn1;cn2` """ 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 members(self, index=None): """ @@ -185,31 +208,14 @@ class Groups(list): return self[index]['memberUid'] -class Users(list): +class Users(Entries): """ Abstraction class for Users imitating list """ - def __init__(self, ldap, attrs, *args): - list.__init__(self, *args) - self._ldap = ldap - 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) - - #def sort(self, attr): - # self = sorted(self, key=lambda d: d[attr].__str__()) - def search(self, uid): """ Search example: `uid`, `uid*`, `*uid*`, `uid1;uid2` """ 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))