Normalize hostnames by removing control chars, collapsing duplicates, and limiting length.
(self, hostname: Optional[str])
| 546 | """Safely convert value to integer.""" |
| 547 | try: |
| 548 | return int(value) if value else default |
| 549 | except (ValueError, TypeError): |
| 550 | return default |
| 551 | |
| 552 | def _is_pseudo_mac(self, mac: str) -> bool: |
| 553 | """Check if MAC is a pseudo-MAC (format: 00:00:c0:a8:xx:xx or similar).""" |
| 554 | if not mac: |
| 555 | return False |
| 556 | return mac.lower().startswith('00:00:') |
| 557 | |
| 558 | def sanitize_hostname(self, hostname: Optional[str]) -> str: |
| 559 | """Normalize hostnames by removing control chars, collapsing duplicates, and limiting length.""" |
| 560 | if hostname is None: |
| 561 | return '' |
| 562 | |
| 563 | # Normalize unicode and drop non-printable characters |
| 564 | normalized = unicodedata.normalize('NFKC', str(hostname)) |
| 565 | normalized = ''.join(ch for ch in normalized if ch.isprintable()) |
| 566 | normalized = normalized.replace('\r', ' ').replace('\n', ' ').replace('\t', ' ') |
| 567 | |
| 568 | # Split on known separators and collapse whitespace |
| 569 | raw_tokens = re.split(r'[;,\|]+', normalized) |
| 570 | cleaned_tokens = [] |
| 571 | seen = set() |
| 572 | |
| 573 | for token in raw_tokens: |
| 574 | token = re.sub(r'\s+', ' ', token).strip(" _-.") |
| 575 | if not token: |
| 576 | continue |
| 577 | token_key = token.lower() |
| 578 | if token_key in seen: |
| 579 | continue |
| 580 | seen.add(token_key) |
| 581 | cleaned_tokens.append(token) |
| 582 | if len(cleaned_tokens) >= 4: |
| 583 | break # Prevent very long alias lists |
no test coverage detected