Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Cache destination path computation in FileConfig #56

@plutopulp

Description

@plutopulp

Cache destination path computation in FileConfig

Problem

FileConfig computes the destination path (filename + subdirectory) multiple times:

  • Once for download_id generation
  • Once for actual download operation
  • Potentially multiple times during progress tracking/logging

Each computation calls get_destination_filename() which may involve URL parsing and sanitisation.

Proposed Solution

Add destination_relative_path as a @computed_field property that caches the relative path:

@computed_field
@property
def destination_relative_path(self) -> Path:
    """Relative path from base directory to where file will be saved.

    Cached computed field combining filename and optional subdirectory.
    This is the path component that uniquely identifies where the file
    will be saved, relative to the base download directory.

    Returns:
        Relative path (Path object)

    Examples:
        >>> config = FileConfig(url="https://example.com/file.txt")
        >>> config.destination_relative_path
        PosixPath('example.com-file.txt')

        >>> config = FileConfig(
        ...     url="https://example.com/file.txt",
        ...     destination_subdir="docs"
        ... )
        >>> config.destination_relative_path
        PosixPath('docs/example.com-file.txt')
    """
    filename = self.get_destination_filename()
    if self.destination_subdir:
        return Path(self.destination_subdir) / filename
    return Path(filename)

def get_destination_path(self, base_dir: Path) -> Path:
    """Get full absolute destination path.

    Combines base_dir with cached relative path.

    Args:
        base_dir: Base download directory

    Returns:
        Full absolute path where file should be saved
    """
    return base_dir / self.destination_relative_path

@computed_field
@property
def download_id(self) -> str:
# update implementation to use self.destination_relative_path

Benefits

  • Performance: Path computed once, cached for reuse
  • Consistency: download_id and get_destination_path() use same cached value

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions