<commit_before>"""Class for Searching Cards resource generator."""

from random import sample, shuffle
from math import ceil
from PIL import Image, ImageDraw, ImageFont
from yattag import Doc
from utils.BaseResourceGenerator import BaseResourceGenerator

IMAGE_PATH = "static/img/resources/searching-cards/{}-cards-{}.png"
X_BASE_COORD = 1803
X_COORD_DECREMENT = 516
Y_COORD = 240
FONT_PATH = "static/fonts/PatrickHand-Regular.ttf"
FONT = ImageFont.truetype(FONT_PATH, 200)


class SearchingCardsResourceGenerator(BaseResourceGenerator):
    """Class for Searching Cards resource generator."""

    additional_valid_options = {
        "number_cards": ["15", "31"],
        "max_number": ["cards", "99", "999", "blank"],
        "help_sheet": [True, False],
    }

    def data(self):
        """Create a image for Searching Cards resource.

        Returns:
            A list of dictionaries for each resource page.
        """
        pages = []
        number_cards = int(self.requested_options["number_cards"])
        max_number = self.requested_options["max_number"]
        help_sheet = self.requested_options["help_sheet"]

        if max_number == "cards":
            numbers = list(range(1, number_cards + 1))
            shuffle(numbers)
            range_text = "1 to {}".format(number_cards)
        elif max_number != "blank":
            numbers = sample(range(1, int(max_number) + 1), number_cards)
            range_text = "1 to {}".format(max_number)
        else:
            numbers = []
            range_text = "Add list of numbers below:"

        if help_sheet:
            pages.append({"type": "html", "data": self.create_help_sheet(numbers, range_text)})

        number_of_pages = range(ceil(number_cards / 4))
        for page in number_of_pages:
            if page == number_of_pages[-1]:
                image_path = IMAGE_PATH.format(3, 1)
            else:
                image_path = IMAGE_PATH.format(4, page + 1)

            image = Image.open(image_path)

            if max_number != "blank":
                draw = ImageDraw.Draw(image)
                page_numbers = numbers[:4]
                numbers = numbers[4:]
                coord_x = X_BASE_COORD
                for number in page_numbers:
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=FONT)
                    draw.text(
                        (coord_x - (text_width / 2), Y_COORD - (text_height / 2)),
                        text,
                        font=FONT,
                        fill="#000"
                    )
                    coord_x -= X_COORD_DECREMENT

            image = image.rotate(90, expand=True)
            pages.append({"type": "image", "data": image})
        pages[0]["thumbnail"] = True<commit_msg>Fix bug where searching cards resource thumbnails showed the wrong page<commit_after>"""Class for Searching Cards resource generator."""

from random import sample, shuffle
from math import ceil
from PIL import Image, ImageDraw, ImageFont
from yattag import Doc
from utils.BaseResourceGenerator import BaseResourceGenerator

IMAGE_PATH = "static/img/resources/searching-cards/{}-cards-{}.png"
X_BASE_COORD = 1803
X_COORD_DECREMENT = 516
Y_COORD = 240
FONT_PATH = "static/fonts/PatrickHand-Regular.ttf"
FONT = ImageFont.truetype(FONT_PATH, 200)


class SearchingCardsResourceGenerator(BaseResourceGenerator):
    """Class for Searching Cards resource generator."""

    additional_valid_options = {
        "number_cards": ["15", "31"],
        "max_number": ["cards", "99", "999", "blank"],
        "help_sheet": [True, False],
    }

    def data(self):
        """Create a image for Searching Cards resource.

        Returns:
            A list of dictionaries for each resource page.
        """
        pages = []
        number_cards = int(self.requested_options["number_cards"])
        max_number = self.requested_options["max_number"]
        help_sheet = self.requested_options["help_sheet"]

        if max_number == "cards":
            numbers = list(range(1, number_cards + 1))
            shuffle(numbers)
            range_text = "1 to {}".format(number_cards)
        elif max_number != "blank":
            numbers = sample(range(1, int(max_number) + 1), number_cards)
            range_text = "1 to {}".format(max_number)
        else:
            numbers = []
            range_text = "Add list of numbers below:"

        if help_sheet:
            pages.append({"type": "html", "data": self.create_help_sheet(numbers, range_text)})

        number_of_pages = range(ceil(number_cards / 4))
        for (page_number, page) in enumerate(number_of_pages):
            if page == number_of_pages[-1]:
                image_path = IMAGE_PATH.format(3, 1)
            else:
                image_path = IMAGE_PATH.format(4, page + 1)

            image = Image.open(image_path)

            if max_number != "blank":
                draw = ImageDraw.Draw(image)
                page_numbers = numbers[:4]
                numbers = numbers[4:]
                coord_x = X_BASE_COORD
                for number in page_numbers:
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=FONT)
                    draw.text(
                        (coord_x - (text_width / 2), Y_COORD - (text_height / 2)),
                        text,
                        font=FONT,
                        fill="#000"
                    )
                    coord_x -= X_COORD_DECREMENT

            image = image.rotate(90, expand=True)
            page_data = {"type": "image", "data": image}
            if page_number == 0:
                page_data["thumbnail"] = True
            pages.append(page_data)
{'fraction_non_alphanumeric': 0.10797227036395148, 'fraction_numerical': 0.015251299826689775, 'mean_word_length': 2.0262191924488726, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, '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': '2891000', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1584, 'n_words': 391}