<commit_before>"""Site targets."""
# Copyright (c) 2012-2022 <NAME> <<EMAIL>>
# SPDX-License-Identifier: BSD-3-Clause

import abc
import filecmp
from functools import cached_property
import os
import shutil
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional

from jinja2 import Template

from . import error as err


__all__ = [
    "collect_copy_targets",
    "CopyTarget",
    "Target",
    "TemplateTarget",
]


@dataclass(kw_only=True)
class Target(abc.ABC):

    """A single file created in the site output directory."""

    # Relative URL of the target.
    url: str

    @cached_property
    def url_parts(self) -> tuple[str, ...]:
        return tuple([part for part in self.url.split("/") if part])

    @abc.abstractmethod
    def write(self, parent_dir: Path) -> None:
        raise NotImplementedError()


@dataclass(kw_only=True)
class TemplateTarget(Target):

    """A target created by rendering from a template."""

    # Jinja2 template to use.
    template: Template

    # Render arguments.
    render_kwargs: dict

    # Source of the target.
    src: Optional[Path] = field(default=None)

    def write(self, parent_dir: Path) -> None:
        """Render the template and write it to the destination."""
        content = self.template.render(**self.render_kwargs)
        try:
            (parent_dir.joinpath(*self.url_parts)).write_text(content)
        except OSError as e:
            raise err.VoltResourceError(
                "could not write target" f" {'/'.join(self.url_parts)!r}: {e.strerror}"
            )


@dataclass(kw_only=True)
class CopyTarget(Target):

    """A target created by copying another file from the source directory."""

    # Source of the copy.
    src: Path

    # Path parts / tokens to the target.
    url_parts: tuple[str, ...]

    # Relative URL of the target.<commit_msg>chore: Streamline error message<commit_after>"""Site targets."""
# Copyright (c) 2012-2022 <NAME> <<EMAIL>>
# SPDX-License-Identifier: BSD-3-Clause

import abc
import filecmp
from functools import cached_property
import os
import shutil
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional

from jinja2 import Template

from . import error as err


__all__ = [
    "collect_copy_targets",
    "CopyTarget",
    "Target",
    "TemplateTarget",
]


@dataclass(kw_only=True)
class Target(abc.ABC):

    """A single file created in the site output directory."""

    # Relative URL of the target.
    url: str

    @cached_property
    def url_parts(self) -> tuple[str, ...]:
        return tuple([part for part in self.url.split("/") if part])

    @abc.abstractmethod
    def write(self, parent_dir: Path) -> None:
        raise NotImplementedError()


@dataclass(kw_only=True)
class TemplateTarget(Target):

    """A target created by rendering from a template."""

    # Jinja2 template to use.
    template: Template

    # Render arguments.
    render_kwargs: dict

    # Source of the target.
    src: Optional[Path] = field(default=None)

    def write(self, parent_dir: Path) -> None:
        """Render the template and write it to the destination."""
        content = self.template.render(**self.render_kwargs)
        try:
            (parent_dir.joinpath(*self.url_parts)).write_text(content)
        except OSError as e:
            raise err.VoltResourceError(
                f"could not write target {self.url!r}: {e.strerror}"
            )


@dataclass(kw_only=True)
class CopyTarget(Target):

    """A target created by copying another file from the source directory."""

    # Source of the copy.
    src: Path

    # Path parts / tokens to the target.
    url_parts: tuple[str, ...]

    # Relative URL of the target.
{'fraction_non_alphanumeric': 0.1089794830802025, 'fraction_numerical': 0.005861977084998668, 'mean_word_length': 3.0064034151547494, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1405895', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1093, 'n_words': 359}