<commit_before>"""
gallerize.main
~~~~~~~~~~~~~~

:Copyright: 2007-2021 <NAME>
:License: MIT, see LICENSE for details.
"""

from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from itertools import islice
import os
from pathlib import Path
import shutil
import subprocess
import sys
from typing import (
    Any,
    Dict,
    Iterable,
    Iterator,
    List,
    Optional,
    Sequence,
    Tuple,
)

from jinja2 import Environment, PackageLoader


IMAGE_CAPTION_EXTENSION: str = '.txt'
TEMPLATE_EXTENSION: str = '.html'
OUTPUT_HTML_EXTENSION: str = '.html'

PATH: str = os.path.dirname(os.path.abspath(__file__))
PATH_STATIC: str = os.path.join(PATH, 'static')
TEMPLATE_ENVIRONMENT: Environment = Environment(
    autoescape=True,
    loader=PackageLoader(__package__, 'templates'),
    trim_blocks=False,
)


@dataclass(frozen=True)
class Dimension:
    width: int
    height: int


def debug(message: str, *args: Any) -> None:
    print(message.format(*args))


def resize_image(
    src_filename: str, dst_filename: str, max_dimension: Dimension
) -> None:
    """Create a resized (and antialiased) version of an image."""
    dimension_str = f'{max_dimension.width:d}x{max_dimension.height:d}'
    cmd = ['convert', '-resize', dimension_str, src_filename, dst_filename]
    subprocess.check_call(cmd)


def render_html_to_file(
    template_name: str, context: Dict[str, Any], path: str, page_name: str
) -> None:
    """Render the template and write the result to the given file."""
    context['url_for_page'] = lambda page: page + OUTPUT_HTML_EXTENSION
    html = render_template(template_name + TEMPLATE_EXTENSION, **context)

    filename = os.path.join(path, page_name + OUTPUT_HTML_EXTENSION)
    write_file(filename, html)


def render_template(template_filename: str, **context: Dict[str, Any]) -> str:
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)


def write_file(filename: str, content: str) -> None:
    debug('Writing "{}" ...', filename)
    Path(filename).write_text(content, encoding='utf-8')


def create_gallery(
    title: str,
    destination_path: str,
    resize: bool,
    max_image_size: Dimension,
    max_thumbnail_size: Dimension,
    full_image_filenames: List[str],
) -> Gallery:
    gallery = Gallery(
        title=title,
        destination_path=destination_path,
        resize=resize,
        max_image_size=max_image_size,
        max_thumbnail_size=max_thumbnail_size,
    )

    gallery.images = [
        Image(gallery, image) for image in sorted(full_image_filenames)
    ]
    gallery.link_images()

    return gallery


class Gallery:
    def __init__(
        self,
        title: str,
        destination_path: str,
        resize: bool,
        max_image_size: Dimension,
        max_thumbnail_size: Dimension,
    ) -> None:
        self.images: List[Image] = []
        self.title = title
        self.destination_path = destination_path
        self.resize = resize
        self.max_image_size = max_image_size
        self.max_thumbnail_size = max_thumbnail_size

    def link_images(self) -> None:
        """Assign the predecessor and successor for every image."""
        for previous_image, image, next_image in window(self.images):
            image.previous_image = previous_image
            image.next_image = next_image

    def generate(self) -> None:
        # Create destination path if it doesn't exist.
        if not os.path.exists(self.destination_path):
            debug(
                'Destination path "{}" does not exist, creating it.',
                self.destination_path,
            )
            os.mkdir(self.destination_path)

        self.generate_images()
        for image in self.images:
            image.load_caption()
        self.render_html_pages()
        self.copy_additional_static_files()
        debug('Done.')

    def generate_images(self) -> None:
        for image in self.images:
            image.generate_image()
            image.generate_thumbnail()
            image.load_caption()

    def render_html_pages(self) -> None:
        for image in self.images:
            image.render_html_page()
        self.render_html_index_page()

    def render_html_index_page(self) -> None:
        """Create an HTML document of thumbnails that link to single image
        documents.
        """
        context = {
            'gallery': self,
        }
        render_html_to_file('index', context, self.destination_path, 'index')

    def copy_additional_static_files(self) -> None:
        if not os.path.exists(PATH_STATIC):
            debug(
                'Path "{}", does not exist; not copying any static files.',
                PATH_STATIC,
            )
            return<commit_msg>Do not load captions twice<commit_after>"""
gallerize.main
~~~~~~~~~~~~~~

:Copyright: 2007-2021 <NAME>
:License: MIT, see LICENSE for details.
"""

from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from itertools import islice
import os
from pathlib import Path
import shutil
import subprocess
import sys
from typing import (
    Any,
    Dict,
    Iterable,
    Iterator,
    List,
    Optional,
    Sequence,
    Tuple,
)

from jinja2 import Environment, PackageLoader


IMAGE_CAPTION_EXTENSION: str = '.txt'
TEMPLATE_EXTENSION: str = '.html'
OUTPUT_HTML_EXTENSION: str = '.html'

PATH: str = os.path.dirname(os.path.abspath(__file__))
PATH_STATIC: str = os.path.join(PATH, 'static')
TEMPLATE_ENVIRONMENT: Environment = Environment(
    autoescape=True,
    loader=PackageLoader(__package__, 'templates'),
    trim_blocks=False,
)


@dataclass(frozen=True)
class Dimension:
    width: int
    height: int


def debug(message: str, *args: Any) -> None:
    print(message.format(*args))


def resize_image(
    src_filename: str, dst_filename: str, max_dimension: Dimension
) -> None:
    """Create a resized (and antialiased) version of an image."""
    dimension_str = f'{max_dimension.width:d}x{max_dimension.height:d}'
    cmd = ['convert', '-resize', dimension_str, src_filename, dst_filename]
    subprocess.check_call(cmd)


def render_html_to_file(
    template_name: str, context: Dict[str, Any], path: str, page_name: str
) -> None:
    """Render the template and write the result to the given file."""
    context['url_for_page'] = lambda page: page + OUTPUT_HTML_EXTENSION
    html = render_template(template_name + TEMPLATE_EXTENSION, **context)

    filename = os.path.join(path, page_name + OUTPUT_HTML_EXTENSION)
    write_file(filename, html)


def render_template(template_filename: str, **context: Dict[str, Any]) -> str:
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)


def write_file(filename: str, content: str) -> None:
    debug('Writing "{}" ...', filename)
    Path(filename).write_text(content, encoding='utf-8')


def create_gallery(
    title: str,
    destination_path: str,
    resize: bool,
    max_image_size: Dimension,
    max_thumbnail_size: Dimension,
    full_image_filenames: List[str],
) -> Gallery:
    gallery = Gallery(
        title=title,
        destination_path=destination_path,
        resize=resize,
        max_image_size=max_image_size,
        max_thumbnail_size=max_thumbnail_size,
    )

    gallery.images = [
        Image(gallery, image) for image in sorted(full_image_filenames)
    ]
    gallery.link_images()

    return gallery


class Gallery:
    def __init__(
        self,
        title: str,
        destination_path: str,
        resize: bool,
        max_image_size: Dimension,
        max_thumbnail_size: Dimension,
    ) -> None:
        self.images: List[Image] = []
        self.title = title
        self.destination_path = destination_path
        self.resize = resize
        self.max_image_size = max_image_size
        self.max_thumbnail_size = max_thumbnail_size

    def link_images(self) -> None:
        """Assign the predecessor and successor for every image."""
        for previous_image, image, next_image in window(self.images):
            image.previous_image = previous_image
            image.next_image = next_image

    def generate(self) -> None:
        # Create destination path if it doesn't exist.
        if not os.path.exists(self.destination_path):
            debug(
                'Destination path "{}" does not exist, creating it.',
                self.destination_path,
            )
            os.mkdir(self.destination_path)

        self.generate_images()
        self.render_html_pages()
        self.copy_additional_static_files()
        debug('Done.')

    def generate_images(self) -> None:
        for image in self.images:
            image.generate_image()
            image.generate_thumbnail()
            image.load_caption()

    def render_html_pages(self) -> None:
        for image in self.images:
            image.render_html_page()
        self.render_html_index_page()

    def render_html_index_page(self) -> None:
        """Create an HTML document of thumbnails that link to single image
        documents.
        """
        context = {
            'gallery': self,
        }
        render_html_to_file('index', context, self.destination_path, 'index')

    def copy_additional_static_files(self) -> None:
        if not os.path.exists(PATH_STATIC):
            debug(
                'Path "{}", does not exist; not copying any static files.',
                PATH_STATIC,
            )
            return
{'fraction_non_alphanumeric': 0.10159697415423408, 'fraction_numerical': 0.002101281781886951, 'mean_word_length': 2.875814332247557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6921287', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2818, 'n_words': 694}