From 9da771b5af65366e47e3caf57a87b027b3287bd6 Mon Sep 17 00:00:00 2001 From: Edvard Rejthar Date: Thu, 2 Nov 2023 15:01:41 +0100 Subject: [PATCH] Setting field type suppresses the auto-detection --- CHANGELOG.md | 3 +++ convey/field.py | 1 + convey/identifier.py | 9 ++++++--- convey/types.py | 4 ++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e727b5..000f44c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## 1.4.5 +* fix: Setting field type by `--type` suppresses the auto-detection. + ## 1.4.4 (2023-05-26) * fix: OTRS sending with no attachment forwarded diff --git a/convey/field.py b/convey/field.py index 03465c2..2eced5c 100644 --- a/convey/field.py +++ b/convey/field.py @@ -33,6 +33,7 @@ def __init__(self, name, is_chosen=True, source_field: Optional[Field] = None, s self.merge_operation = merge_operation "This field is merged from a remote file" self.possible_types = {} + "Possible types the field can be of. If self.type is specified, it is just self.type." if isinstance(name, Type): self.type = name else: diff --git a/convey/identifier.py b/convey/identifier.py index bce8eaf..a17d54c 100644 --- a/convey/identifier.py +++ b/convey/identifier.py @@ -17,13 +17,14 @@ if TYPE_CHECKING: from .field import Field + from .parser import Parser logger = logging.getLogger(__name__) class Identifier: - def __init__(self, parser): + def __init__(self, parser: Parser): self.parser = parser self.graph = None @@ -239,6 +240,10 @@ def identify_fields(self, quiet=False): return False for i, field in enumerate(self.parser.fields): + if field.type: + field.possible_types = {field.type : 100} + continue + possible_types = {} for type_ in Types.get_guessable_types(): score = type_.check_conformity(samples[i], self.parser.has_header, field) @@ -247,8 +252,6 @@ def identify_fields(self, quiet=False): # Use with Python3.8 # possible_types = {type_: score for type_ in Types.get_guessable_types() # if (score := type_.check_conformity(samples[i], self.parser.has_header, field))} - if field.type: - possible_types[field.type] = 100 if possible_types: # sort by biggest score - biggest probability the column is of this type field.possible_types = {k: v for k, v in sorted(possible_types.items(), key=lambda k: k[1], reverse=True)} diff --git a/convey/types.py b/convey/types.py index ea04469..9d6a4a3 100644 --- a/convey/types.py +++ b/convey/types.py @@ -138,7 +138,7 @@ def hostname_ips(val): try: Checker.hostname_ips_cache[val] = list( {addr[4][0] for addr in timeout(15, socket.getaddrinfo, val, None)}) - except (TimeoutError, OSError) as e: + except (TimeoutError, OSError, ValueError) as e: Checker.hostname_ips_cache[val] = [] return Checker.hostname_ips_cache[val] @@ -147,7 +147,7 @@ def hostname_ip(cls, val): if val not in cls.hostname_cache: try: cls.hostname_cache[val] = timeout(3, socket.gethostbyname, val) - except (TimeoutError, OSError) as e: + except (TimeoutError, OSError, ValueError) as e: logger.warning(f"Hostname {val}: {e}") cls.hostname_cache[val] = [] return cls.hostname_cache[val]