<commit_before>import logging
import logging.handlers
from django.conf import settings

from django_assets.filter import BaseFilter


__all__ = ('CSSUtilsFilter',)


class CSSUtilsFilter(BaseFilter):
    """Minifies CSS by removing whitespace, comments etc., using the Python
    `cssutils <http://cthedot.de/cssutils/>`_ library.

    Note that since this works as a parser on the syntax level, so invalid
    CSS input could potentially result in data loss.
    """

    name = 'cssutils'

    def setup(self):
        import cssutils
        self.cssutils = cssutils

        try:
            # cssutils logs to stdout by default, hide that in production
            if not settings.DEBUG:
                log = logging.getLogger('assets.cssutils')
                log.addHandler(logging.handlers.MemoryHandler(10))
                cssutils.log.setlog(log)
        except ImportError:
            # During doc generation, Django is not going to be setup and will
            # fail when the settings object is accessed. That's ok though.
            pass

    def apply(self, _in, out):
        sheet = self.cssutils.parseString(_in.read())
        self.cssutils.ser.prefs.useMinified()
        out.write(sheet.cssText)<commit_msg>Work around deprecation warning with new cssutils versions.<commit_after>import logging
import logging.handlers
from django.conf import settings

from django_assets.filter import BaseFilter


__all__ = ('CSSUtilsFilter',)


class CSSUtilsFilter(BaseFilter):
    """Minifies CSS by removing whitespace, comments etc., using the Python
    `cssutils <http://cthedot.de/cssutils/>`_ library.

    Note that since this works as a parser on the syntax level, so invalid
    CSS input could potentially result in data loss.
    """

    name = 'cssutils'

    def setup(self):
        import cssutils
        self.cssutils = cssutils

        try:
            # cssutils logs to stdout by default, hide that in production
            if not settings.DEBUG:
                log = logging.getLogger('assets.cssutils')
                log.addHandler(logging.handlers.MemoryHandler(10))

                # Newer versions of cssutils print a deprecation warning
                # for 'setlog'.
                if hasattr(cssutils.log, 'setLog'):
                    func = cssutils.log.setLog
                else:
                    func = cssutils.log.setlog
                func(log)
        except ImportError:
            # During doc generation, Django is not going to be setup and will
            # fail when the settings object is accessed. That's ok though.
            pass

    def apply(self, _in, out):
        sheet = self.cssutils.parseString(_in.read())
        self.cssutils.ser.prefs.useMinified()
        out.write(sheet.cssText)
{'fraction_non_alphanumeric': 0.07717273710782546, 'fraction_numerical': 0.00144248106743599, 'mean_word_length': 2.263529411764706, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6332755', 'n_tokens_mistral': 744, 'n_tokens_neox': 707, 'n_words': 244}