Created
August 25, 2014 21:30
-
-
Save peterbe/b68fe9bfe5e356973d62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import tempfile | |
import codecs | |
import StringIO | |
import gzip | |
import os | |
import re | |
import requests | |
from django.test.client import Client | |
from django.conf import settings | |
class ValidatorOperationalError(Exception): | |
pass | |
class ValidationError(AssertionError): | |
pass | |
class ValidatingClient(Client): | |
def get(self, *args, **kwargs): | |
response = super(ValidatingClient, self).get(*args, **kwargs) | |
disabled = getattr( | |
settings, | |
'VALIDATINGCLIENT_DISABLED', | |
True | |
) | |
if disabled: | |
return response | |
caller = inspect.stack()[1] | |
caller_file = caller[1] | |
caller_line = caller[2] | |
caller_name = caller[3] | |
if ( | |
response['Content-Type'].startswith('text/html') | |
and | |
response.status_code == 200 | |
): | |
try: | |
encoding = re.findall( | |
'charset=([\w-]+)', | |
response['Content-Type'] | |
)[0] | |
except IndexError: | |
encoding = 'utf-8' # sensible default | |
temp_dir = os.path.join( | |
tempfile.gettempdir(), 'outputtestingclient' | |
) | |
if not os.path.isdir(temp_dir): | |
os.mkdir(temp_dir) | |
temp_file = os.path.join( | |
temp_dir, | |
'%s-%s.html' % (caller_name, caller_line) | |
) | |
with codecs.open(temp_file, 'w', encoding) as f: | |
if not response.content: | |
raise ValueError('No response.content', args[0]) | |
f.write(response.content) | |
self._validate(temp_file, encoding, *args, **kwargs) | |
return response | |
def _validate(self, html_file, encoding, *args, **kwargs): | |
buf = StringIO.StringIO() | |
gzipper = gzip.GzipFile(fileobj=buf, mode='wb') | |
with codecs.open(html_file, 'r', encoding) as f: | |
gzipper.write(f.read()) | |
gzipper.close() | |
gzippeddata = buf.getvalue() | |
buf.close() | |
req = requests.post( | |
'http://html5.validator.nu/?out=text', | |
headers={ | |
'Content-Type': 'text/html', | |
'Accept-Encoding': 'gzip', | |
'Content-Encoding': 'gzip', | |
'Content-Length': len(gzippeddata), | |
}, | |
data=gzippeddata | |
) | |
if req.status_code != 200: | |
raise ValidatorOperationalError(req) | |
raise_exceptions = getattr( | |
settings, | |
'VALIDATINGCLIENT_HARDCORE', | |
False | |
) | |
messages = [] | |
for block in re.split('\n\n+', req.content): | |
messages.append(block) | |
if req.content: | |
print "VALIDATON TROUBLE" | |
print "To debug, see:" | |
print "\t", html_file | |
txt_file = re.sub('\.html$', '.txt', html_file) | |
print "\t", txt_file | |
with codecs.open(txt_file, 'w', encoding) as f: | |
f.write('Arguments to GET:\n') | |
for arg in args: | |
f.write('\t%s\n' % arg) | |
for k, w in kwargs.items(): | |
f.write('\t%s=%s\n' % (k, w)) | |
f.write('\n') | |
f.write(unicode(req.content, encoding)) | |
if raise_exceptions: | |
raise ValidationError(req.content) | |
else: | |
print "\n\n".join(messages) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output can look like this:
And the output something like this: