]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Evaluating entries to keep in lib/pp_admintools/config/mirror_ldap.py
authorFrank Brehm <frank@brehm-online.com>
Mon, 24 Oct 2022 15:49:37 +0000 (17:49 +0200)
committerFrank Brehm <frank@brehm-online.com>
Mon, 24 Oct 2022 15:49:37 +0000 (17:49 +0200)
lib/pp_admintools/config/mirror_ldap.py

index 7fc16ae9c9527de4963dbffbf9e9f57256cf6a95..6adf088ba5eb6cbcc06e0d3ade11c981fa0123c4 100644 (file)
@@ -11,8 +11,10 @@ from __future__ import absolute_import
 
 # Standard module
 import logging
+import re
 # import copy
-# import re
+
+from collections.abc import Mapping
 
 # Third party modules
 from fb_tools.common import is_sequence, pp
@@ -25,7 +27,7 @@ from .ldap import LdapConfigError, LdapConfiguration
 
 from ..xlate import XLATOR
 
-__version__ = '0.1.0'
+__version__ = '0.2.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -42,6 +44,12 @@ class MirrorLdapConfigError(LdapConfigError):
 class MirrorLdapConfiguration(LdapConfiguration):
     """A class for configuring the mirror-ldap application."""
 
+    pat_mirror_ldap_key = r'^\s*mirror[_-]?ldap\s*$'
+    re_mirror_ldap_key = re.compile(pat_mirror_ldap_key, re.IGNORECASE)
+
+    pat_keep_entries_key = r'^\s*keep[_-]?entries\s*$'
+    re_keep_entries_key = re.compile(pat_keep_entries_key, re.IGNORECASE)
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -77,15 +85,66 @@ class MirrorLdapConfiguration(LdapConfiguration):
     def eval_section(self, section_name):
 
         sn = section_name.lower()
-        section = self.cfg[section_name]
 
         if self.verbose > 1:
             LOG.debug(_("Evaluating configuration section {sn!r} ...").format(sn=sn))
 
-        if self.verbose > 3:
+        super(MirrorLdapConfiguration, self).eval_section(section_name)
+
+        if self.re_mirror_ldap_key.match(section_name):
+            self._eval_mirror_ldap_section_bare(section_name)
+            return
+
+    # -------------------------------------------------------------------------
+    def _eval_mirror_ldap_section_bare(self, section_name):
+
+        section = self.cfg[section_name]
+        if self.verbose > 4:
             LOG.debug("Section:\n" + pp(section))
 
-        super(MirrorLdapConfiguration, self).eval_section(section_name)
+        if not isinstance(section, Mapping):
+            LOG.warning(_("Section {sn!r} is not a {what}.").format(
+                sn=section_name, what='Mapping (dict)'))
+            return
+
+        for sub_section_name in section.keys():
+
+            sub_section = section[sub_section_name]
+
+            if self.re_keep_entries_key.match(sub_section_name):
+                self._eval_entries_keep(section_name, sub_section_name, sub_section)
+                continue
+
+    # -------------------------------------------------------------------------
+    def _eval_entries_keep(self, section_name, sub_section_name, sub_section):
+
+        section = self.cfg[section_name]
+        sub_section = section[sub_section_name]
+        sn = str(section_name) + '/' + str(sub_section_name)
+
+        if self.verbose > 2:
+            LOG.debug(_("Evaluating configuration section {sn!r} ...").format(sn=sn))
+
+        if self.verbose > 2:
+            LOG.debug("Section:\n" + pp(sub_section))
+
+        if not isinstance(sub_section, Mapping):
+            LOG.warning(_("Section {sn!r} is not a {what}.").format(
+                sn=sn, what='Mapping (dict)'))
+            return
+
+        for inst_name in sub_section.keys():
+            if inst_name not in self.entries_keep:
+                self.entries_keep[inst_name] = []
+            inst_entries = sub_section[inst_name]
+            if is_sequence(inst_entries):
+                for entry in inst_entries:
+                    if entry not in self.entries_keep[inst_name]:
+                        self.entries_keep[inst_name].append(entry)
+            else:
+                if inst_name not in self.entries_keep[inst_name]:
+                    self.entries_keep[inst_name].append(inst_entries)
+        return
 
 
 # =============================================================================