From: Frank Brehm Date: Mon, 7 Dec 2020 17:44:11 +0000 (+0100) Subject: Better handling of integer attribute values X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=b10f35274f8d65329e9c713412a6f67f93293331;p=pixelpark%2Fldap-migration.git Better handling of integer attribute values --- diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index 6507871..08a516b 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -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):