<commit_before>from datetime import datetime
from io import BytesIO
import logging
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL
from os.path import abspath, dirname, join
from PIL import Image
from PIL.ImageCms import getOpenProfile, getProfileName, profileToProfile
import sys


class Historian():

    def __init__(self, logging_threshold=INFO):
        self.history = []
        self.logger = logging.getLogger()
        self.logger.setLevel(logging_threshold)

    def get_history(self):
        return self.history

    def log(self, msg, log_level=DEBUG, data=None):
        self._append_history(msg, log_level, data)

    def _append_history(self, msg, log_level=DEBUG, data=None):
        self.history.append((datetime.now(), msg, data))
        if self.logger.isEnabledFor(log_level):
            self.logger.log(log_level, '{}: {}'.format(type(self), msg))


class MasterMaker(Historian):

    def __init__(self, src, logging_threshold=INFO, dest=None):
        Historian.__init__(self, logging_threshold)
        try:
            self.original = Image.open(src)
        except AttributeError:
            self.original = src
            self.original_filename = 'unknown ({})'.format(
                self.original.format)
            self.log(
                'initiated with image already in RAM ({})'
                ''.format(self.original.format))
        else:
            self.original_filename = src
            self.log(
                'initiated and opened image ({}) from file "{}"'
                ''.format(self.original.format, src))
        self.dest = dest
        self.profiles = {
            'srgb2': getOpenProfile(
                join(
                    dirname(abspath(__file__)),
                    'icc',
                    'sRGB_IEC61966-2-1_black_scaled.icc')),
            'srgb4': getOpenProfile(
                join(
                    dirname(abspath(__file__)),
                    'icc',
                    'sRGB_v4_ICC_preference.icc')),
        }
        self.profiles['original'] = self._original_profile()

    def make(self):
        self._make_mode_rgb()
        self._standardize_icc()
        # if dest is None:
        #     return self.master

    def _make_mode_rgb(self):
        if self.original.mode == 'RGB':
            self.rgb = self.original
        else:
            self.rgb = self.original.convert('RGB')

    def _standardize_icc(self):
        if self.profiles['original'] == self.profiles['srgb4']:
            self.srgb4 = self.rgb
        else:
            self.srgb4 = profileToProfile(
                self.rgb,
                self.profiles['original'],
                self.profiles['srgb4'])

    def _original_profile(self, force=False):
        if not force:
            try:
                return self.profiles['original']
            except AttributeError:
                pass
            except KeyError:<commit_msg>parameterize choice of target icc profile<commit_after>from datetime import datetime
from io import BytesIO
import logging
from logging import DEBUG, INFO
from os.path import abspath, dirname, join
from PIL import Image, TiffImagePlugin
from PIL.ImageCms import getOpenProfile, getProfileName, profileToProfile


class Historian():

    def __init__(self, logging_threshold=INFO):
        self.history = []
        self.logger = logging.getLogger()
        self.logger.setLevel(logging_threshold)

    def get_history(self):
        return self.history

    def log(self, msg, log_level=DEBUG, data=None):
        self._append_history(msg, log_level, data)

    def _append_history(self, msg, log_level=DEBUG, data=None):
        self.history.append((datetime.now(), msg, data))
        if self.logger.isEnabledFor(log_level):
            self.logger.log(log_level, '{}: {}'.format(type(self), msg))


class MasterMaker(Historian):

    def __init__(self, src, logging_threshold=INFO, dest=None):
        Historian.__init__(self, logging_threshold)
        try:
            self.original = Image.open(src)
        except AttributeError:
            self.original = src
            self.original_filename = 'unknown ({})'.format(
                self.original.format)
            self.log(
                'initiated with image already in RAM ({})'
                ''.format(self.original.format))
        else:
            self.original_filename = src
            self.log(
                'initiated and opened image ({}) from file "{}"'
                ''.format(self.original.format, src))
        self.dest = dest
        self.profiles = {
            'srgb2': getOpenProfile(
                join(
                    dirname(abspath(__file__)),
                    'icc',
                    'sRGB_IEC61966-2-1_black_scaled.icc')),
            'srgb4': getOpenProfile(
                join(
                    dirname(abspath(__file__)),
                    'icc',
                    'sRGB_v4_ICC_preference.icc')),
        }
        self.profiles['original'] = self._original_profile()
        self.master_info = TiffImagePlugin.ImageFileDirectory()

    def make(self):
        self._make_mode_rgb()
        self._standardize_icc()
        # if dest is None:
        #     return self.master

    def _make_mode_rgb(self):
        if self.original.mode == 'RGB':
            self.rgb = self.original
            self.log('Original color mode is RGB.')
        else:
            self.rgb = self.original.convert('RGB')
            self.log(
                'Original color mode was {} so converted to RGB.'
                ''.format(self.original.mode))

    def _standardize_icc(self, target='srgb4'):
        profile = self.profiles[target]
        profile_name = getProfileName(profile).strip()
        original = self.profiles['original']
        original_name = getProfileName(original).strip()
        if original == profile:
            self.master = self.rgb
            self.log(
                'Original ICC profile was already the specified target ({}).'
                ''.format(profile_name))
        else:
            self.master = profileToProfile(
                self.rgb,
                original,
                profile)
            self.log(
                'Original ICC profile ({}) was converted to the standard '
                'target ({}).'
                ''.format(
                    original_name,
                    profile_name))
        self.master_info[TiffImagePlugin.ICCPROFILE] = profile.tobytes()
        self.master_info.tagtype[TiffImagePlugin.ICCPROFILE] = 1

    def _original_profile(self, force=False):
        if not force:
            try:
                return self.profiles['original']
            except AttributeError:
                pass
            except KeyError:
{'fraction_non_alphanumeric': 0.10408646727864969, 'fraction_numerical': 0.0038495706248149247, 'mean_word_length': 1.864715860899067, 'pattern_counts': {'":': 0, '<': 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': '3592014', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1808, 'n_words': 401}