From 637a1917ff552bb5a54166a5c406aa870ba4a085 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 17 Nov 2020 09:25:55 +0100 Subject: [PATCH] Setting properties object_classes and attribute_types to a CaseInsensitiveDict --- lib/ldap_migration/__init__.py | 54 ++++++++++++++-------------------- lib/ldap_migration/idict.py | 18 +++++++++++- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index 5c96d10..702f4b4 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -35,7 +35,7 @@ from fb_tools.errors import FbAppError from .config import LDAPMigrationConfiguration from .idict import CaseInsensitiveDict -__version__ = '0.5.3' +__version__ = '0.5.4' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -81,8 +81,8 @@ class LDAPMigrationApplication(BaseApplication): self.all_dns_file = None self.structural_dns_file = None - self.object_classes = {} - self.attribute_types = {} + self.object_classes = CaseInsensitiveDict() + self.attribute_types = CaseInsensitiveDict() self.dns = CaseInsensitiveDict() self.struct_dns = CaseInsensitiveDict() @@ -121,6 +121,8 @@ class LDAPMigrationApplication(BaseApplication): """ res = super(LDAPMigrationApplication, self).as_dict(short=short) + res['object_classes'] = self.object_classes.as_dict(short=short) + res['attribute_types'] = self.attribute_types.as_dict(short=short) res['cfg_dir'] = self.cfg_dir res['cfg_file'] = self.cfg_file res['dns'] = self.dns.as_dict(short=short) @@ -378,9 +380,7 @@ class LDAPMigrationApplication(BaseApplication): first = False if is_sequence(object_class.name): for oc_name in object_class.name: - name_lc = oc_name.lower() oc = { - 'single_name': oc_name, 'name': object_class.name, 'oid': object_class.oid, 'description': object_class.description, @@ -393,11 +393,10 @@ class LDAPMigrationApplication(BaseApplication): 'experimental': object_class.experimental, 'raw_definition': object_class.raw_definition, } - self.object_classes[name_lc] = oc + self.object_classes[oc_name] = oc else: - name_lc = object_class.name.lower() + oc_name = object_class.name oc = { - 'single_name': object_class.name, 'name': object_class.name, 'oid': object_class.oid, 'description': object_class.description, @@ -410,17 +409,14 @@ class LDAPMigrationApplication(BaseApplication): 'experimental': object_class.experimental, 'raw_definition': object_class.raw_definition, } - self.object_classes[name_lc] = oc + self.object_classes[oc_name] = oc - LOG.debug("Found {} ObjectClasses.".format(len(self.object_classes.keys()))) + LOG.debug("Found {} ObjectClasses.".format(len(self.object_classes))) if self.verbose > 2: if self.verbose > 3: - LOG.debug("Discovered ObjectClasses:\n" + pp(self.object_classes)) + LOG.debug("Discovered ObjectClasses:\n" + pp(self.object_classes.as_dict())) else: - tmp_dict = {} - for oc_name in self.object_classes.keys(): - tmp_dict[oc_name] = self.object_classes[oc_name]['single_name'] - LOG.debug("Discovered ObjectClasses:\n" + pp(tmp_dict)) + LOG.debug("Discovered ObjectClasses:\n" + pp(list(self.object_classes.keys()))) # ------------------------------------------------------------------------- def discover_target_attribute_types(self): @@ -440,9 +436,7 @@ class LDAPMigrationApplication(BaseApplication): if is_sequence(atype.name): for at_name in atype.name: - name_lc = at_name.lower() at = { - 'single_name': at_name, 'name': atype.name, 'oid': atype.oid, 'description': atype.description, @@ -463,11 +457,10 @@ class LDAPMigrationApplication(BaseApplication): 'syntax': atype.syntax, 'usage': atype.usage, } - self.attribute_types[name_lc] = at + self.attribute_types[at_name] = at else: - name_lc = atype.name.lower() + at_name = atype.name at = { - 'single_name': atype.name, 'name': atype.name, 'oid': atype.oid, 'description': atype.description, @@ -488,17 +481,14 @@ class LDAPMigrationApplication(BaseApplication): 'syntax': atype.syntax, 'usage': atype.usage, } - self.attribute_types[name_lc] = at + self.attribute_types[at_name] = at - LOG.debug("Found {} AttributeTypes.".format(len(self.attribute_types.keys()))) - if self.verbose > 2: + LOG.debug("Found {} AttributeTypes.".format(len(self.attribute_types))) + if self.verbose > 1: if self.verbose > 3: - LOG.debug("Discovered AttributeTypes:\n" + pp(self.attribute_types)) + LOG.debug("Discovered AttributeTypes:\n" + pp(self.attribute_types.as_dict())) else: - tmp_dict = {} - for name_lc in self.attribute_types.keys(): - tmp_dict[name_lc] = self.attribute_types[name_lc]['single_name'] - LOG.debug("Discovered AttributeTypes:\n" + pp(tmp_dict)) + LOG.debug("Discovered AttributeTypes:\n" + pp(list(self.attribute_types.keys()))) # ------------------------------------------------------------------------- def check_tmp_dir(self): @@ -534,7 +524,8 @@ class LDAPMigrationApplication(BaseApplication): def lookup_for_attrtype(self, attrtype, silent=True): at_lc = attrtype.lower() - if at_lc not in self.attribute_types: + canon_attrtype = self.attribute_types.get_key(attrtype, strict=False) + if canon_attrtype is None: msg = "AttributeType {!r} not found.".format(attrtype) if silent: if self.verbose > 2: @@ -543,8 +534,7 @@ class LDAPMigrationApplication(BaseApplication): LOG.error(msg) return None - new_at = self.attribute_types[at_lc]['single_name'] - return new_at + return canon_attrtype # ------------------------------------------------------------------------- def mangle_dn_token(self, old_token): @@ -656,7 +646,7 @@ class LDAPMigrationApplication(BaseApplication): LOG.info("Found {nr} structural items in subtree of {sfx!r}.".format( nr=count_dns, sfx=self.config.suffix)) - if self.verbose > 2: + if self.verbose > 3: LOG.debug("Registred structural DN tokens:\n{}".format(pp(self.struct_dns.as_dict()))) # ------------------------------------------------------------------------- diff --git a/lib/ldap_migration/idict.py b/lib/ldap_migration/idict.py index 17679b2..37b2ee5 100644 --- a/lib/ldap_migration/idict.py +++ b/lib/ldap_migration/idict.py @@ -24,7 +24,7 @@ from fb_tools.common import pp from fb_tools.errors import FbError from fb_tools.obj import FbBaseObject -__version__ = '0.1.3' +__version__ = '0.1.4' LOG = logging.getLogger(__name__) @@ -328,6 +328,22 @@ class CaseInsensitiveDict(MutableMapping): return new_dict + # ------------------------------------------------------------------------- + def get_key(self, key, strict=True): + """Getting the saved value of the key in correct form.""" + + if not isinstance(key, str): + raise WrongKeyTypeError(key) + + for okey in self._map.keys(): + if okey.lower() == key.lower(): + return okey + + if strict: + raise CaseInsensitiveKeyError(key) + + return None + # ------------------------------------------------------------------------- def set_key(self, key, *args): -- 2.39.5