<commit_before>"""
A collection of log handlers that allow file permissions to be specified.
"""
import logging
import logging.handlers as handlers
import os


class FileHandler(logging.FileHandler):
    """
    A subclass of logging.FileHandler that accepts a permissions argument.

    The file used for logging will have the value of the permissions keyword
    value, e.g. for unix:  permissions=0o664
    """
    def __init__(self, filename, permissions=0o644, **kwargs):
        self.permissions = permissions
        logging.FileHandler.__init__(self, filename, **kwargs)

    def _open(self):
        stream = logging.FileHandler._open(self)
        try:
            os.chmod(self.baseFilename, self.permissions)
        except OSError as ex:
            # If we don't have access to change the permissions, we need to
            # rely on the initial file creator having done the chmod
            pass

        return stream


class TimedRotatingFileHandler(FileHandler, handlers.TimedRotatingFileHandler):
    """
    A subclass of logging.handlers.TimedRotatingFileHandler that accepts
    a permissions argument.

    The file used for logging will have the value of the permissions keyword
    value, e.g. for unix:  permissions=0o664
    """
    def __init__(self, filename, permissions=0o644, **kwargs):<commit_msg>Adding a new DateNameFileHandler that will apply strftime formatting to the filename<commit_after>"""
A collection of log handlers that allow file permissions to be specified.
"""
import logging
import logging.handlers as handlers
import os
import datetime

class FileHandler(logging.FileHandler):
    """
    A subclass of logging.FileHandler that accepts a permissions argument.

    The file used for logging will have the value of the permissions keyword
    value, e.g. for unix:  permissions=0o664
    """
    def __init__(self, filename, permissions=0o644, **kwargs):
        self.permissions = permissions
        logging.FileHandler.__init__(self, filename)

    def _open(self):
        stream = logging.FileHandler._open(self)
        try:
            os.chmod(self.baseFilename, self.permissions)
        except OSError as ex:
            # If we don't have access to change the permissions, we need to
            # rely on the initial file creator having done the chmod
            pass

        return stream


class DateNameFileHandler(FileHandler):
    """
    A subclass that allows for filenames w/ a dateformat.
    Will close and reopen filehandles as needed.
    For example, you could configure a handler to to have this filename:
    /tmp/logs/my_app-%Y-%M-%d.log
    """
    def __init__(self, *args, **kwargs):
        self._originalBaseFilename = None
        FileHandler.__init__(self, *args, **kwargs)

    def emit(self, *args, **kwargs):
        """
        If we our open file's name no longer matches the current moment's
        version of the filename, close it.  emit in FileHandler will open
        a new handle as needed.
        """
        if self.baseFilename != self._get_current_filename():
            self.close()

        FileHandler.emit(self, *args, **kwargs)

    def _open(self, *args, **kwargs):
        """
        Saves the original base filename, then overrides it with a date
        formatted filename.  Uses strftime arguments.
        """
        if not self._originalBaseFilename:
            self._originalBaseFilename = self.baseFilename

        self.baseFilename = self._get_current_filename()

        return FileHandler._open(self, *args, **kwargs)

    def _get_current_filename(self):
        return datetime.datetime.now().strftime(self._originalBaseFilename)


class TimedRotatingFileHandler(FileHandler, handlers.TimedRotatingFileHandler):
    """
    A subclass of logging.handlers.TimedRotatingFileHandler that accepts
    a permissions argument.

    The file used for logging will have the value of the permissions keyword
    value, e.g. for unix:  permissions=0o664
    """
    def __init__(self, filename, permissions=0o644, **kwargs):
{'fraction_non_alphanumeric': 0.07713226205191595, 'fraction_numerical': 0.007911001236093944, 'mean_word_length': 2.966666666666667, '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': '4915466', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1049, 'n_words': 387}