1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

lxml: ensure lxml support is optional: if not available,

use_lxml_parser() will return false and doctests/pytest collection
won't test the lxml parser
This commit is contained in:
Ilya Kreymer 2014-03-26 14:05:02 -07:00
parent 4e53c2e9d8
commit da0623fbbb
3 changed files with 26 additions and 12 deletions

View File

@ -1,4 +1,10 @@
import lxml.etree
try:
import lxml.etree
LXML_SUPPORTED = True
except ImportError:
LXML_SUPPORTED = False
pass
import cgi
import re

View File

@ -13,14 +13,17 @@ HTML = HTMLRewriter
#=================================================================
def use_lxml_parser():
try:
import logging
from lxml_html_rewriter import LXMLHTMLRewriter
import logging
from lxml_html_rewriter import LXMLHTMLRewriter, LXML_SUPPORTED
if LXML_SUPPORTED:
global HTML
HTML = LXMLHTMLRewriter
logging.debug('Using LXML Parser')
except ImportError:
logging.debug('Error Loading LXML Parser')
return True
else:
logging.debug('LXML Parser not available')
return False
#=================================================================

View File

@ -114,10 +114,7 @@ ur"""
from pywb.rewrite.url_rewriter import UrlRewriter
try:
from pywb.rewrite.lxml_html_rewriter import LXMLHTMLRewriter
except ImportError:
pass
from pywb.rewrite.lxml_html_rewriter import LXMLHTMLRewriter, LXML_SUPPORTED
urlrewriter = UrlRewriter('20131226101010/http://example.com/some/path/index.html', '/web/')
@ -127,5 +124,13 @@ def parse(data, head_insert=None):
print parser.rewrite(data) + parser.close()
if __name__ == "__main__":
import doctest
doctest.testmod()
if LXML_SUPPORTED:
import doctest
doctest.testmod()
else:
# skip if not supported and lxml not available
if not LXML_SUPPORTED:
import pytest
lxml = pytest.importorskip('lxml.etree')