]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Better handling of integer attribute values
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 7 Dec 2020 17:44:11 +0000 (18:44 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 7 Dec 2020 17:44:11 +0000 (18:44 +0100)
lib/ldap_migration/__init__.py

index 6507871a736728ed8b2b2234374d5c9f564d10a2..08a516b4c376ed8220a7a4ee41e0df52ad6a75e5 100644 (file)
@@ -45,7 +45,7 @@ from .config import LDAPMigrationConfiguration
 from .idict import CaseInsensitiveDict
 from .istringset import CaseInsensitiveStringSet
 
-__version__ = '0.8.8'
+__version__ = '0.8.9'
 
 LOG = logging.getLogger(__name__)
 CFG_BASENAME = 'ldap-migration.ini'
@@ -638,30 +638,7 @@ class LDAPMigrationApplication(BaseApplication):
             val = 0
 
             if attribute in self.integer_attribute_types:
-
-                if is_sequence(src_val):
-                    val = []
-                    for value in src_val:
-                        if isinstance(value, Number):
-                            val.append(value)
-                            continue
-                        try:
-                            mbytes = human2mbytes(value, as_float=True)
-                            val.append(int(mbytes * 1024 * 1024))
-                        except ValueError as e:
-                            msg = "Invalid value in attribute {attr!r} of item {dn!r}: {e}"
-                            msg = msg.format(attr=attribute, dn=src_dn, e=e)
-                            raise ReadLDAPItemError(msg)
-                else:
-                    if isinstance(src_val, Number):
-                        val = src_val
-                    try:
-                        mbytes = human2mbytes(src_val, as_float=True)
-                        val = int(mbytes * 1024 * 1024)
-                    except ValueError as e:
-                        msg = "Invalid value in attribute {attr!r} of item {dn!r}: {e}"
-                        msg = msg.format(attr=attribute, dn=src_dn, e=e)
-                        raise ReadLDAPItemError(msg)
+                val = self._get_integer_value(src_val)
 
                 if self.verbose > 1:
                     msg = "Migrated integer value: {old!r} => {new!r}.".format(
@@ -675,6 +652,63 @@ class LDAPMigrationApplication(BaseApplication):
 
         return entry
 
+    # -------------------------------------------------------------------------
+    def _get_integer_value(self, src_val):
+
+        val = 0
+
+        if is_sequence(src_val):
+            val = []
+            for value in src_val:
+                if isinstance(value, Number):
+                    val.append(value)
+                    continue
+                try:
+                    v_bytes = int(value)
+                    val.append(v_bytes)
+                    continue
+                except ValueError as e:
+                    if self.verbose > 3:
+                        msg = "Could not interprete {v!r} a an integer value: {e}".format(
+                                v=value, e=e)
+                        LOG.debug(msg)
+                try:
+                    mbytes = human2mbytes(value, as_float=True)
+                    val.append(int(mbytes * 1024 * 1024))
+                except ValueError as e:
+                    msg = "Invalid value in attribute {attr!r} of item {dn!r}: {e}"
+                    msg = msg.format(attr=attribute, dn=src_dn, e=e)
+                    raise ReadLDAPItemError(msg)
+
+            return val
+
+        if isinstance(src_val, Number):
+            return src_val
+
+        try:
+            v_bytes = int(src_val)
+            return v_bytes
+
+        except ValueError as e:
+            pass
+
+        if self.verbose > 3:
+            msg = "Could not interprete {v!r} a an integer value: {e}".format(
+                    v=src_val, e=e)
+            LOG.debug(msg)
+
+        try:
+            mbytes = human2mbytes(src_val, as_float=True)
+            val = int(mbytes * 1024 * 1024)
+            return val
+        except ValueError as e:
+            msg = "Invalid value in attribute {attr!r} of item {dn!r}: {e}"
+            msg = msg.format(attr=attribute, dn=src_dn, e=e)
+            raise ReadLDAPItemError(msg)
+
+        return 0
+
+
     # -------------------------------------------------------------------------
     def get_target_item(self, tgt_dn, with_acl=False):