From bdefddc3f32dbc56118932c67e34e212d84d5a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20M=C3=A4der?= <maeder@phys.ethz.ch> Date: Tue, 27 Feb 2018 09:48:02 +0100 Subject: [PATCH] Add no-opts to exclude group attrs --- bin/showgroup.py | 8 +++++++- lib/isg/argparser.py | 9 ++++++++- lib/isg/dphysldap.py | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/bin/showgroup.py b/bin/showgroup.py index cba5972..b6daaaf 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 923265c..0eef125 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 8fa44b5..aa44fa3 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) -- GitLab