Style changes for mhtml and test_mhtml
This commit is contained in:
parent
b05a0d191d
commit
749b1c02cc
@ -23,11 +23,12 @@ import functools
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
|
||||
import collections
|
||||
import uuid
|
||||
from email import policy, generator, encoders
|
||||
from email.mime import multipart
|
||||
import email.policy
|
||||
import email.generator
|
||||
import email.encoders
|
||||
import email.mime.multipart
|
||||
|
||||
from PyQt5.QtCore import QUrl
|
||||
|
||||
@ -66,13 +67,13 @@ def _get_css_imports(data):
|
||||
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"""
|
||||
|
||||
E_QUOPRI = encoders.encode_quopri
|
||||
E_QUOPRI = email.encoders.encode_quopri
|
||||
"""Encode the file using MIME quoted-printable encoding."""
|
||||
|
||||
|
||||
@ -123,8 +124,8 @@ class MHTMLWriter():
|
||||
Args:
|
||||
fp: The file-object, openend in "wb" mode.
|
||||
"""
|
||||
msg = multipart.MIMEMultipart('related',
|
||||
'---=_qute-{}'.format(uuid.uuid4()))
|
||||
msg = email.mime.multipart.MIMEMultipart(
|
||||
'related', '---=_qute-{}'.format(uuid.uuid4()))
|
||||
|
||||
root = self._create_root_file()
|
||||
msg.attach(root)
|
||||
@ -132,7 +133,7 @@ class MHTMLWriter():
|
||||
for _, file_data in sorted(self._files.items()):
|
||||
msg.attach(self._create_file(file_data))
|
||||
|
||||
gen = generator.BytesGenerator(fp, policy=MHTMLPolicy)
|
||||
gen = email.generator.BytesGenerator(fp, policy=MHTMLPolicy)
|
||||
gen.flatten(msg)
|
||||
|
||||
def _create_root_file(self):
|
||||
@ -145,7 +146,7 @@ class MHTMLWriter():
|
||||
|
||||
def _create_file(self, f):
|
||||
"""Return the single given file as MIMEMultipart."""
|
||||
msg = multipart.MIMEMultipart()
|
||||
msg = email.mime.multipart.MIMEMultipart()
|
||||
msg['Content-Location'] = f.content_location
|
||||
# Get rid of the default type multipart/mixed
|
||||
del msg['Content-Type']
|
||||
|
@ -78,7 +78,7 @@ def test_refuses_non_ascii_header_value(checker, header, value):
|
||||
writer = mhtml.MHTMLWriter(**defaults)
|
||||
with pytest.raises(UnicodeEncodeError) as excinfo:
|
||||
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):
|
||||
@ -268,22 +268,25 @@ def test_css_url_scanner(style, expected_urls):
|
||||
assert urls == expected_urls
|
||||
|
||||
|
||||
def test_noclose_bytesio_fake_close():
|
||||
fp = mhtml._NoCloseBytesIO()
|
||||
fp.write(b'Value')
|
||||
fp.close()
|
||||
assert fp.getvalue() == b'Value'
|
||||
fp.write(b'Eulav')
|
||||
assert fp.getvalue() == b'ValueEulav'
|
||||
class TestNoCloseBytesIO:
|
||||
# WORKAROUND for https://bitbucket.org/logilab/pylint/issues/540/
|
||||
# pylint: disable=no-member
|
||||
|
||||
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():
|
||||
fp = mhtml._NoCloseBytesIO()
|
||||
fp.write(b'Value')
|
||||
fp.actual_close()
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
fp.getvalue()
|
||||
assert 'I/O operation on closed file.' == str(excinfo.value)
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
fp.write(b'Closed')
|
||||
assert 'I/O operation on closed file.' == str(excinfo.value)
|
||||
def test_actual_close(self):
|
||||
fp = mhtml._NoCloseBytesIO()
|
||||
fp.write(b'Value')
|
||||
fp.actual_close()
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
fp.getvalue()
|
||||
assert str(excinfo.value) == 'I/O operation on closed file.'
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
fp.write(b'Closed')
|
||||
assert str(excinfo.value) == 'I/O operation on closed file.'
|
||||
|
Loading…
Reference in New Issue
Block a user