Refactor and fix beautifulsoup version selection in the parser
it cared for six.PY3 which has little to do with the beautifulsoup version
This commit is contained in:
@@ -1,36 +1,34 @@
|
||||
from __future__ import absolute_import
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils import six
|
||||
from django.utils.encoding import smart_text
|
||||
|
||||
from compressor.exceptions import ParserError
|
||||
from compressor.parser import ParserBase
|
||||
from compressor.utils.decorators import cached_property
|
||||
|
||||
|
||||
class BeautifulSoupParser(ParserBase):
|
||||
|
||||
@cached_property
|
||||
def soup(self):
|
||||
def __init__(self, content):
|
||||
super(BeautifulSoupParser, self).__init__(content)
|
||||
try:
|
||||
if six.PY3:
|
||||
from bs4 import BeautifulSoup
|
||||
else:
|
||||
from bs4 import BeautifulSoup
|
||||
self.use_bs4 = True
|
||||
self.soup = BeautifulSoup(self.content, "html.parser")
|
||||
except ImportError:
|
||||
try:
|
||||
from BeautifulSoup import BeautifulSoup
|
||||
return BeautifulSoup(self.content)
|
||||
except ImportError as err:
|
||||
raise ImproperlyConfigured("Error while importing BeautifulSoup: %s" % err)
|
||||
except Exception as err:
|
||||
raise ParserError("Error while initializing Parser: %s" % err)
|
||||
self.use_bs4 = False
|
||||
self.soup = BeautifulSoup(self.content)
|
||||
except ImportError as err:
|
||||
raise ImproperlyConfigured("Error while importing BeautifulSoup: %s" % err)
|
||||
|
||||
def css_elems(self):
|
||||
if six.PY3:
|
||||
if self.use_bs4:
|
||||
return self.soup.find_all({'link': True, 'style': True})
|
||||
else:
|
||||
return self.soup.findAll({'link': True, 'style': True})
|
||||
|
||||
def js_elems(self):
|
||||
if six.PY3:
|
||||
if self.use_bs4:
|
||||
return self.soup.find_all('script')
|
||||
else:
|
||||
return self.soup.findAll('script')
|
||||
|
||||
Reference in New Issue
Block a user