Style changes for mhtml and test_mhtml

This commit is contained in:
Daniel 2015-09-25 11:48:43 +02:00
parent b05a0d191d
commit 749b1c02cc
2 changed files with 32 additions and 28 deletions

View File

@ -23,11 +23,12 @@ import functools
import io import io
import os import os
import re import re
import collections import collections
import uuid import uuid
from email import policy, generator, encoders import email.policy
from email.mime import multipart import email.generator
import email.encoders
import email.mime.multipart
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
@ -66,13 +67,13 @@ def _get_css_imports(data):
return urls return urls
MHTMLPolicy = policy.default.clone(linesep='\r\n', max_line_length=0) MHTMLPolicy = email.policy.default.clone(linesep='\r\n', max_line_length=0)
E_BASE64 = encoders.encode_base64 E_BASE64 = email.encoders.encode_base64
"""Encode the file using base64 encoding""" """Encode the file using base64 encoding"""
E_QUOPRI = encoders.encode_quopri E_QUOPRI = email.encoders.encode_quopri
"""Encode the file using MIME quoted-printable encoding.""" """Encode the file using MIME quoted-printable encoding."""
@ -123,8 +124,8 @@ class MHTMLWriter():
Args: Args:
fp: The file-object, openend in "wb" mode. fp: The file-object, openend in "wb" mode.
""" """
msg = multipart.MIMEMultipart('related', msg = email.mime.multipart.MIMEMultipart(
'---=_qute-{}'.format(uuid.uuid4())) 'related', '---=_qute-{}'.format(uuid.uuid4()))
root = self._create_root_file() root = self._create_root_file()
msg.attach(root) msg.attach(root)
@ -132,7 +133,7 @@ class MHTMLWriter():
for _, file_data in sorted(self._files.items()): for _, file_data in sorted(self._files.items()):
msg.attach(self._create_file(file_data)) msg.attach(self._create_file(file_data))
gen = generator.BytesGenerator(fp, policy=MHTMLPolicy) gen = email.generator.BytesGenerator(fp, policy=MHTMLPolicy)
gen.flatten(msg) gen.flatten(msg)
def _create_root_file(self): def _create_root_file(self):
@ -145,7 +146,7 @@ class MHTMLWriter():
def _create_file(self, f): def _create_file(self, f):
"""Return the single given file as MIMEMultipart.""" """Return the single given file as MIMEMultipart."""
msg = multipart.MIMEMultipart() msg = email.mime.multipart.MIMEMultipart()
msg['Content-Location'] = f.content_location msg['Content-Location'] = f.content_location
# Get rid of the default type multipart/mixed # Get rid of the default type multipart/mixed
del msg['Content-Type'] del msg['Content-Type']

View File

@ -78,7 +78,7 @@ def test_refuses_non_ascii_header_value(checker, header, value):
writer = mhtml.MHTMLWriter(**defaults) writer = mhtml.MHTMLWriter(**defaults)
with pytest.raises(UnicodeEncodeError) as excinfo: with pytest.raises(UnicodeEncodeError) as excinfo:
writer.write_to(checker.fp) writer.write_to(checker.fp)
assert "'ascii' codec can't encode" in str(excinfo.value) assert "'ascii' codec can't encode" in str(excinfo.value)
def test_file_encoded_as_base64(checker): def test_file_encoded_as_base64(checker):
@ -268,22 +268,25 @@ def test_css_url_scanner(style, expected_urls):
assert urls == expected_urls assert urls == expected_urls
def test_noclose_bytesio_fake_close(): class TestNoCloseBytesIO:
fp = mhtml._NoCloseBytesIO() # WORKAROUND for https://bitbucket.org/logilab/pylint/issues/540/
fp.write(b'Value') # pylint: disable=no-member
fp.close()
assert fp.getvalue() == b'Value'
fp.write(b'Eulav')
assert fp.getvalue() == b'ValueEulav'
def test_fake_close(self):
fp = mhtml._NoCloseBytesIO()
fp.write(b'Value')
fp.close()
assert fp.getvalue() == b'Value'
fp.write(b'Eulav')
assert fp.getvalue() == b'ValueEulav'
def test_noclose_bytesio_actual_close(): def test_actual_close(self):
fp = mhtml._NoCloseBytesIO() fp = mhtml._NoCloseBytesIO()
fp.write(b'Value') fp.write(b'Value')
fp.actual_close() fp.actual_close()
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
fp.getvalue() fp.getvalue()
assert 'I/O operation on closed file.' == str(excinfo.value) assert str(excinfo.value) == 'I/O operation on closed file.'
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
fp.write(b'Closed') fp.write(b'Closed')
assert 'I/O operation on closed file.' == str(excinfo.value) assert str(excinfo.value) == 'I/O operation on closed file.'