File: __init__.py

package info (click to toggle)
python-django-compressor 4.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,108 kB
  • sloc: python: 4,900; makefile: 123; javascript: 5
file content (35 lines) | stat: -rw-r--r-- 1,149 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import html
from importlib import import_module

from django.utils.functional import LazyObject

# support legacy parser module usage
from compressor.parser.base import ParserBase  # noqa
from compressor.parser.lxml import LxmlParser
from compressor.parser.default_htmlparser import DefaultHtmlParser as HtmlParser
from compressor.parser.beautifulsoup import BeautifulSoupParser  # noqa
from compressor.parser.html5lib import Html5LibParser  # noqa


class AutoSelectParser(LazyObject):
    options = (
        # TODO: make lxml.html parser first again
        (html.parser.__name__, HtmlParser),  # fast and part of the Python stdlib
        ("lxml.html", LxmlParser),  # lxml, extremely fast
    )

    def __init__(self, content):
        self._wrapped = None
        self._setup(content)

    def __getattr__(self, name):
        return getattr(self._wrapped, name)

    def _setup(self, content):
        for dependency, parser in self.options:
            try:
                import_module(dependency)
                self._wrapped = parser(content)
                break
            except (ImportError, TypeError):
                continue