# 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
from ..xlate import XLATOR
-__version__ = '0.1.0'
+__version__ = '0.2.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
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,
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
# =============================================================================