Skip to content

utils

utils

Seeder service utility functions.

Pure helpers that do not require service instance state.

Classes

Functions

parse_seed_file

parse_seed_file(path: Path) -> list[Relay]

Parse a seed file and validate relay URLs.

Each line is passed to parse_relay_url for URL validation and network detection. Lines starting with # are treated as comments and skipped.

Parameters:

  • path (Path) –

    Path to the seed file (one URL per line).

Returns:

  • list[Relay]

    List of validated Relay objects.

Source code in src/bigbrotr/services/seeder/utils.py
def parse_seed_file(path: Path) -> list[Relay]:
    """Parse a seed file and validate relay URLs.

    Each line is passed to
    [parse_relay_url][bigbrotr.services.common.utils.parse_relay_url]
    for URL validation and network detection. Lines starting with ``#`` are
    treated as comments and skipped.

    Args:
        path: Path to the seed file (one URL per line).

    Returns:
        List of validated [Relay][bigbrotr.models.relay.Relay] objects.
    """
    relays: list[Relay] = []

    try:
        with path.open(encoding="utf-8") as f:
            for line in f:
                url = line.strip()
                if not url or url.startswith("#"):
                    continue
                relay = parse_relay_url(url)
                if relay:
                    relays.append(relay)
                else:
                    _logger.warning("relay_parse_failed: %s", url)
    except (OSError, UnicodeDecodeError) as exc:
        _logger.warning("seed_file_read_error: %s (%s)", path, exc)

    return relays