<commit_before>import enum
import shutil
import sys
from typing import Mapping

from .enums import Color
from .misc import isatty


class ColorMap:
    def __init__(self, color_map: Mapping[str, str]):
        self.add_colors(color_map)

    def __getitem__(self, name: str):
        return getattr(self, name)

    def __setitem__(self, name: str, color: str):
        setattr(self, name, color)

    def add_colors(self, color_map: Mapping[str, str]):
        for name, color in color_map.items():
            setattr(self, name, color)


class Printer:

    # Symbolic name => color
    color_map = {
        "header": Color.magenta,
        "info": Color.blue,
        "success": Color.green,
        "echo": Color.cyan,
        "warning": Color.yellow,
        "error": Color.red,
        "danger": Color.red,
        "debug": Color.cyan,
    }

    def __init__(
        self,
        colors: enum.Enum = Color,
        color_map: Mapping = None,
        default_color=None,
    ):
        self.colors = colors
        self.color_map = ColorMap({color.name: color for color in colors})
        self.color_map.add_colors(self.__class__.color_map)
        if color_map:
            self.color_map.add_colors(color_map)
        self.default_color = self.get_color(default_color)

    def __call__(self, *args, **kwargs):
        self.print(*args, **kwargs)
<commit_msg>Improve color setup in util.Printer<commit_after>import enum
import shutil
import sys
from typing import Mapping

from .enums import Color
from .misc import isatty


class ColorMap:
    def __init__(self, *color_maps):
        for color_map in color_maps:
            self.add_colors(color_map)

    def __getitem__(self, name: str):
        return getattr(self, name)

    def __setitem__(self, name: str, color: str):
        setattr(self, name, color)

    def add_colors(self, color_map: Mapping[str, str]):
        if isinstance(color_map, type) and issubclass(color_map, enum.Enum):
            items = ((color.name, color) for color in color_map)
        else:
            items = color_map.items()
        for name, color in items:
            setattr(self, name, color)


class Printer:

    # Symbolic name => color
    color_map = {
        "header": Color.magenta,
        "info": Color.blue,
        "success": Color.green,
        "echo": Color.cyan,
        "warning": Color.yellow,
        "error": Color.red,
        "danger": Color.red,
        "debug": Color.cyan,
    }

    def __init__(
        self,
        colors: enum.Enum = Color,
        color_map: Mapping = None,
        default_color=None,
    ):
        self.colors = colors
        self.color_map = ColorMap()
        if colors:
            self.color_map.add_colors(colors)
        if self.__class__.color_map:
            self.color_map.add_colors(self.__class__.color_map)
        if color_map:
            self.color_map.add_colors(color_map)
        self.default_color = self.get_color(default_color)

    def __call__(self, *args, **kwargs):
        self.print(*args, **kwargs)

{'fraction_non_alphanumeric': 0.1079302402105956, 'fraction_numerical': 0.0, 'mean_word_length': 2.3591160220994474, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 5, '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': '2113281', 'n_tokens_mistral': 966, 'n_tokens_neox': 943, 'n_words': 238}