Skip to content
Snippets Groups Projects
Commit 9b0e4c6f authored by Sven Mäder's avatar Sven Mäder :speech_balloon:
Browse files

Merge branch 'local-rda' into 'master'

Local rda

See merge request core/python!11
parents ebe828dd b8325cd7
No related branches found
No related tags found
1 merge request!11Local rda
...@@ -16,12 +16,17 @@ OPTS['report'] = ['reportEnabled', 'reportUid'] ...@@ -16,12 +16,17 @@ OPTS['report'] = ['reportEnabled', 'reportUid']
OPTS['mail'] = 'mail' OPTS['mail'] = 'mail'
OPTS['telephone'] = 'telephoneNumber' OPTS['telephone'] = 'telephoneNumber'
NOOPTS = collections.OrderedDict()
NOOPTS['no-member'] = 'memberUid'
NOOPTS['no-gid'] = 'gidNumber'
NOOPTS['no-owner'] = 'owner'
def main(): def main():
description = 'Show groups or group members' description = 'Show groups or group members'
arg = 'group' arg = 'group'
arg_help = 'The group name (cn), or wildcards (*, cn* , *cn*)' 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() ldap = dphysldap.Ldap()
...@@ -38,6 +43,7 @@ def main(): ...@@ -38,6 +43,7 @@ def main():
if len(groups) != 1: if len(groups) != 1:
#groups.sort('cn') #groups.sort('cn')
groups.remove_attrs([v for k, v in NOOPTS.items() if args[k]])
print(groups) print(groups)
else: else:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import argparse 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 Get cmd argument with simplified argparser
""" """
...@@ -19,6 +19,13 @@ def simple(description, arg=None, arg_help='', opts=dict()): ...@@ -19,6 +19,13 @@ def simple(description, arg=None, arg_help='', opts=dict()):
action='store_const', const=True, action='store_const', const=True,
help='Show ' + v) 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', parser.add_argument('--help', action='help',
help='Show this help message and exit') help='Show this help message and exit')
......
...@@ -143,34 +143,57 @@ class Ldap(object): ...@@ -143,34 +143,57 @@ class Ldap(object):
return [Group(e) for e in entries] 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): def __init__(self, ldap, attrs, *args):
list.__init__(self, *args) list.__init__(self, *args)
self._ldap = ldap self._ldap = ldap
self._attrs = attrs self.attrs = attrs
def __str__(self): def __str__(self):
""" """
Modifies the "informal" string value of str(x) or print(x) 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 = [[e[a] for a in self.attrs] for e in self]
table = [[g[h] for h in headers] for g in self] return tabulate.tabulate(table, tablefmt=FMT, headers=self.attrs)
return tabulate.tabulate(table, tablefmt=FMT, headers=headers)
#def sort(self, attr): #def sort(self, attr):
# self = sorted(self, key=lambda k: k[attr].__str__()) # 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): def search(self, cn):
""" """
Search example: `cn`, `cn*`, `*cn*`, `cn1;cn2` Search example: `cn`, `cn*`, `*cn*`, `cn1;cn2`
""" """
query = 'cn: {0}'.format(cn) query = 'cn: {0}'.format(cn)
self.clear() 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): def members(self, index=None):
""" """
...@@ -185,31 +208,14 @@ class Groups(list): ...@@ -185,31 +208,14 @@ class Groups(list):
return self[index]['memberUid'] return self[index]['memberUid']
class Users(list): class Users(Entries):
""" """
Abstraction class for Users imitating list 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): def search(self, uid):
""" """
Search example: `uid`, `uid*`, `*uid*`, `uid1;uid2` Search example: `uid`, `uid*`, `*uid*`, `uid1;uid2`
""" """
query = 'uid: {0}'.format(uid) query = 'uid: {0}'.format(uid)
self.clear() self.clear()
self.extend(self._ldap.get_users(query=query, attributes=self._attrs)) self.extend(self._ldap.get_users(query=query, attributes=self.attrs))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment