]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Setting properties object_classes and attribute_types to a CaseInsensitiveDict
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 17 Nov 2020 08:25:55 +0000 (09:25 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 17 Nov 2020 08:25:55 +0000 (09:25 +0100)
lib/ldap_migration/__init__.py
lib/ldap_migration/idict.py

index 5c96d1071589bc77e817538c8e5fdfee6d990a71..702f4b4d587095ee0b6f5a66c1346c65950aaab2 100644 (file)
@@ -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())))
 
     # -------------------------------------------------------------------------
index 17679b2f92df9df6d4ac827e02629afde093da86..37b2ee5e4278ca8c39cca8008fc77c706bb8ad07 100644 (file)
@@ -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):