<commit_before>"""Defines a set of functions that handle naming of the downloaded files."""

from collections import defaultdict
from typing import Any, Callable, Dict, Optional

from flickr_api.objects import Photo, Photoset

from flickr_download.utils import get_filename

DEFAULT_HANDLER = "title_increment"
"""The default handler if none is specified"""

FilenameHandler = Callable[[Optional[Photoset], Photo, Optional[str]], str]


def _get_short_docstring(docstring: Optional[str]) -> Optional[str]:
    """Given a docstring return the first sentence of it.

    @param: docstring: str, the docstring to parse
    @return: str, the short docstring
    """
    return docstring.split(".")[0].strip() if docstring else None


def title(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after title (falls back to photo id).

    @param pset: the photoset
    @param photo: the photo
    @param suffice: optional suffix
    @return: the filename
    """
    if not photo.title:
        return idd(pset, photo, suffix)

    return get_filename(f"{photo.title}{suffix}")


def idd(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after photo id.

    @param pset: the photoset
    @param photo: the photo
    @param suffice: optional suffix
    @return: the filename
    """
    return f"{photo.id}{suffix}"


def title_and_id(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after title and photo id.

    @param pset: Flickr.Photoset, the photoset
    @param photo: Flickr.Photo, the photo
    @param suffice: str, optional suffix
    @return: str, the filename
    """
    if not photo.title:
        return idd(pset, photo, suffix)

    return get_filename(f"{photo.title}-{photo.id}{suffix}")


INCREMENT_INDEX: Dict[Any, Any] = defaultdict(lambda: defaultdict(int))
"""Photoset -> filename index for title_increment function duplicate tracking"""


def title_increment(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after photo title, but add an incrementing counter on<commit_msg>use _ for unused variable to silence linter<commit_after>"""Defines a set of functions that handle naming of the downloaded files."""

from collections import defaultdict
from typing import Any, Callable, Dict, Optional

from flickr_api.objects import Photo, Photoset

from flickr_download.utils import get_filename

DEFAULT_HANDLER = "title_increment"
"""The default handler if none is specified"""

FilenameHandler = Callable[[Optional[Photoset], Photo, Optional[str]], str]


def _get_short_docstring(docstring: Optional[str]) -> Optional[str]:
    """Given a docstring return the first sentence of it.

    @param: docstring: str, the docstring to parse
    @return: str, the short docstring
    """
    return docstring.split(".")[0].strip() if docstring else None


def title(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after title (falls back to photo id).

    @param pset: the photoset
    @param photo: the photo
    @param suffice: optional suffix
    @return: the filename
    """
    if not photo.title:
        return idd(pset, photo, suffix)

    return get_filename(f"{photo.title}{suffix}")


def idd(_: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after photo id.

    @param pset: the photoset
    @param photo: the photo
    @param suffice: optional suffix
    @return: the filename
    """
    return f"{photo.id}{suffix}"


def title_and_id(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after title and photo id.

    @param pset: Flickr.Photoset, the photoset
    @param photo: Flickr.Photo, the photo
    @param suffice: str, optional suffix
    @return: str, the filename
    """
    if not photo.title:
        return idd(pset, photo, suffix)

    return get_filename(f"{photo.title}-{photo.id}{suffix}")


INCREMENT_INDEX: Dict[Any, Any] = defaultdict(lambda: defaultdict(int))
"""Photoset -> filename index for title_increment function duplicate tracking"""


def title_increment(pset: Optional[Photoset], photo: Photo, suffix: Optional[str]) -> str:
    """Name file after photo title, but add an incrementing counter on
{'fraction_non_alphanumeric': 0.11304146397961547, 'fraction_numerical': 0.000463284688441047, 'mean_word_length': 4.152744630071599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '231657', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1246, 'n_words': 451}