From b8325cd7f7168a5a6fcdbcbaedae90e2401ca597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20M=C3=A4der?= <maeder@phys.ethz.ch> Date: Tue, 27 Feb 2018 15:28:42 +0100 Subject: [PATCH] Add Entries class --- lib/isg/dphysldap.py | 66 +++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/lib/isg/dphysldap.py b/lib/isg/dphysldap.py index aa44fa3..72b26f8 100644 --- a/lib/isg/dphysldap.py +++ b/lib/isg/dphysldap.py @@ -143,9 +143,9 @@ 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) @@ -155,22 +155,14 @@ class Groups(list): 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 """ - table = [[g[h] for h in self.attrs] for g in self] + 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 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)) - def remove_attr(self, attr): if attr in self.attrs: self.attrs.remove(attr) @@ -182,6 +174,27 @@ class Groups(list): 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)) + def members(self, index=None): """ Returns list of members @@ -195,26 +208,10 @@ 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 - """ - 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__()) - def search(self, uid): """ Search example: `uid`, `uid*`, `*uid*`, `uid1;uid2` @@ -222,14 +219,3 @@ class Users(list): query = 'uid: {0}'.format(uid) self.clear() 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