Skip to content

factory

appimage_updater.repositories.factory

Repository factory for creating appropriate repository clients.

This module provides factory functions to create the correct repository client based on URL patterns and repository types.

detect_repository_type(url)

Detect the repository type from a URL.

Parameters:

Name Type Description Default
url str

Repository URL

required

Returns:

Type Description
str

Repository type string (e.g., 'github', 'gitlab')

Raises:

Type Description
RepositoryError

If repository type cannot be determined

Source code in src/appimage_updater/repositories/factory.py
def detect_repository_type(url: str) -> str:
    """Detect the repository type from a URL.

    Args:
        url: Repository URL

    Returns:
        Repository type string (e.g., 'github', 'gitlab')

    Raises:
        RepositoryError: If repository type cannot be determined
    """
    repository_types = [
        GitHubRepository,
        DynamicDownloadRepository,  # Check dynamic before direct (more specific)
        DirectDownloadRepository,
    ]

    for repo_class in repository_types:
        # Create a temporary instance to test URL compatibility
        temp_client: RepositoryClient = repo_class()
        if temp_client.detect_repository_type(url):
            return temp_client.repository_type

    # Default to github for backward compatibility
    # This matches the existing behavior in pattern_generator.py
    return "github"

get_repository_client(url, timeout=30, user_agent=None, source_type=None, **kwargs)

Create appropriate repository client based on URL and optional source type.

Parameters:

Name Type Description Default
url str

Repository URL

required
timeout int

Request timeout in seconds

30
user_agent str | None

Custom user agent string

None
source_type str | None

Explicit source type (github, direct_download, dynamic_download, direct)

None
**kwargs Any

Repository-specific configuration options

{}

Returns:

Type Description
RepositoryClient

Appropriate repository client instance

Raises:

Type Description
RepositoryError

If no suitable repository client is found

Source code in src/appimage_updater/repositories/factory.py
def get_repository_client(
    url: str,
    timeout: int = 30,
    user_agent: str | None = None,
    source_type: str | None = None,
    **kwargs: Any,
) -> RepositoryClient:
    """Create appropriate repository client based on URL and optional source type.

    Args:
        url: Repository URL
        timeout: Request timeout in seconds
        user_agent: Custom user agent string
        source_type: Explicit source type (github, direct_download, dynamic_download, direct)
        **kwargs: Repository-specific configuration options

    Returns:
        Appropriate repository client instance

    Raises:
        RepositoryError: If no suitable repository client is found
    """
    # If explicit source type is provided, use it directly
    if source_type:
        # Map source types to repository classes
        type_mapping = {
            "github": GitHubRepository,
            "direct_download": DirectDownloadRepository,
            "dynamic_download": DynamicDownloadRepository,
            "direct": DirectDownloadRepository,  # "direct" maps to DirectDownloadRepository
        }

        if source_type in type_mapping:
            repo_class = type_mapping[source_type]
            client: RepositoryClient = repo_class(timeout=timeout, user_agent=user_agent, **kwargs)
            return client
        else:
            raise RepositoryError(f"Unsupported source type: {source_type}")

    # Fall back to URL-based detection if no explicit source type
    # Try each repository type in order of preference
    # Order matters: more specific types should come first
    repository_types = [
        GitHubRepository,
        DynamicDownloadRepository,  # Check dynamic before direct (more specific)
        DirectDownloadRepository,
    ]

    for repo_class in repository_types:
        # Create a temporary instance to test URL compatibility
        temp_client: RepositoryClient = repo_class(timeout=timeout, user_agent=user_agent, **kwargs)
        if temp_client.detect_repository_type(url):
            return temp_client

    # No suitable repository client found
    raise RepositoryError(f"No repository client available for URL: {url}")

get_supported_repository_types()

Get list of supported repository types.

Returns:

Type Description
list[str]

List of supported repository type strings

Source code in src/appimage_updater/repositories/factory.py
def get_supported_repository_types() -> list[str]:
    """Get list of supported repository types.

    Returns:
        List of supported repository type strings
    """
    return ["github", "dynamic_download", "direct_download"]