From ebd73812bc95ae892f6f7fa75addf54f372c7774 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 3 May 2023 11:14:18 +0200 Subject: [PATCH] ldap.LdapServer: add format_modify_modlist() and factorize format_changes() & update_need() --- mylib/ldap.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/mylib/ldap.py b/mylib/ldap.py index fbdd0ca..cee9197 100644 --- a/mylib/ldap.py +++ b/mylib/ldap.py @@ -281,14 +281,10 @@ class LdapServer: ) return False - @staticmethod - def update_need(old, new, ignore_attrs=None, encode=False): + @classmethod + def update_need(cls, old, new, ignore_attrs=None, encode=False): """Check if an update is need on a LDAP object based on its old and new attributes values""" - ldif = modlist.modifyModlist( - encode_ldap_value(old) if encode else old, - encode_ldap_value(new) if encode else new, - ignore_attr_types=ignore_attrs if ignore_attrs else [], - ) + ldif = cls.get_changes(old, new, ignore_attrs=ignore_attrs, encode=encode) if not ldif: return False return True @@ -302,19 +298,23 @@ class LdapServer: ignore_attr_types=ignore_attrs if ignore_attrs else [], ) - @staticmethod - def format_changes(old, new, ignore_attrs=None, prefix=None, encode=False): + @classmethod + def format_changes(cls, old, new, ignore_attrs=None, prefix=None, encode=False): """ Format changes (modlist) on an object based on its old and new attributes values to display/log it """ + return cls.format_modify_modlist( + cls.get_changes(old, new, ignore_attrs=ignore_attrs, encode=encode), + prefix=prefix, + ) + + @staticmethod + def format_modify_modlist(ldif, prefix=None): + """Format modify modlist to display/log it""" msg = [] prefix = prefix if prefix else "" - for op, attr, val in modlist.modifyModlist( - encode_ldap_value(old) if encode else old, - encode_ldap_value(new) if encode else new, - ignore_attr_types=ignore_attrs if ignore_attrs else [], - ): + for op, attr, val in ldif: if op == ldap.MOD_ADD: # pylint: disable=no-member op = "ADD" elif op == ldap.MOD_DELETE: # pylint: disable=no-member