First round of lint fixes

This commit is contained in:
Daniel 2015-09-20 17:11:28 +02:00
parent 024ae52366
commit 49a32f0041
3 changed files with 21 additions and 21 deletions

View File

@ -1159,8 +1159,7 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
def download_whole(self, dest): def download_whole(self, dest):
"""Download the current page as a MHTML file, including all assets """Download the current page as a MHTML file, including all assets.
(e.g. images)
Args: Args:
dest: The file path to write the download to. dest: The file path to write the download to.

View File

@ -601,6 +601,7 @@ class DownloadItem(QObject):
@pyqtSlot() @pyqtSlot()
def on_meta_data_change(self): def on_meta_data_change(self):
"""Update the download's metadata."""
if self.reply is None: if self.reply is None:
return return
self.raw_headers = {} self.raw_headers = {}

View File

@ -20,7 +20,6 @@
"""Utils for writing a MHTML file.""" """Utils for writing a MHTML file."""
import functools import functools
import quopri
import io import io
from collections import namedtuple from collections import namedtuple
@ -29,7 +28,6 @@ from urllib.parse import urljoin
from uuid import uuid4 from uuid import uuid4
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from qutebrowser.utils import log, objreg from qutebrowser.utils import log, objreg
@ -48,32 +46,32 @@ def _chunked_base64(data, maxlen=76, linesep=b"\r\n"):
encoded = b64encode(data) encoded = b64encode(data)
result = [] result = []
for i in range(0, len(encoded), maxlen): for i in range(0, len(encoded), maxlen):
result.append(encoded[i:i+maxlen]) result.append(encoded[i:i + maxlen])
return linesep.join(result) return linesep.join(result)
def _rn_quopri(data): def _rn_quopri(data):
"""Return a quoted-printable representation of data.""" """Return a quoted-printable representation of data."""
# See RFC 2045 https://tools.ietf.org/html/rfc2045#section-6.7 # See RFC 2045 https://tools.ietf.org/html/rfc2045#section-6.7
# The stdlib version in the quopri module has inconsistencies with line # The stdlib version in the quopri module has inconsistencies with line
# endings and breaks up character escapes over multiple lines, which isn't # endings and breaks up character escapes over multiple lines, which isn't
# understood by qute and leads to jumbled text # understood by qute and leads to jumbled text
MAXLEN = 76 maxlen = 76
WHITESPACE = {ord(b"\t"), ord(b" ")} whitespace = {ord(b"\t"), ord(b" ")}
output = [] output = []
current_line = b"" current_line = b""
for byte in data: for byte in data:
# Literal representation according to (2) and (3) # Literal representation according to (2) and (3)
if (ord(b"!") <= byte <= ord(b"<") or if (ord(b"!") <= byte <= ord(b"<") or ord(b">") <= byte <= ord(b"~")
ord(b">") <= byte <= ord(b"~") or or byte in whitespace):
byte in WHITESPACE):
current_line += bytes([byte]) current_line += bytes([byte])
else: else:
current_line += b"=" + "{:02X}".format(byte).encode("ascii") current_line += b"=" + "{:02X}".format(byte).encode("ascii")
if len(current_line) >= MAXLEN: if len(current_line) >= maxlen:
# We need to account for the = character # We need to account for the = character
split = [current_line[:MAXLEN-1], current_line[MAXLEN-1:]] split = [current_line[:maxlen - 1], current_line[maxlen - 1:]]
quoted_pos = split[0].rfind(b"=") quoted_pos = split[0].rfind(b"=")
if quoted_pos + 2 >= MAXLEN - 1: if quoted_pos + 2 >= maxlen - 1:
split[0], token = split[0][:quoted_pos], split[0][quoted_pos:] split[0], token = split[0][:quoted_pos], split[0][quoted_pos:]
split[1] = token + split[1] split[1] = token + split[1]
current_line = split[1] current_line = split[1]
@ -93,8 +91,8 @@ E_QUOPRI = ("quoted-printable", _rn_quopri)
class MHTMLWriter(object): class MHTMLWriter(object):
"""A class for aggregating multiple files and outputting them to a MHTML
file.""" """A class for outputting multiple files to a MHTML document."""
BOUNDARY = b"---qute-mhtml-" + str(uuid4()).encode("ascii") BOUNDARY = b"---qute-mhtml-" + str(uuid4()).encode("ascii")
@ -102,7 +100,7 @@ class MHTMLWriter(object):
content_type=None): content_type=None):
self.root_content = root_content self.root_content = root_content
self.content_location = content_location self.content_location = content_location
self.content_type = None self.content_type = content_type
self._files = {} self._files = {}
@ -130,7 +128,7 @@ class MHTMLWriter(object):
del self._files[location] del self._files[location]
def write_to(self, fp): def write_to(self, fp):
"""Output the MHTML file to the given file-like object """Output the MHTML file to the given file-like object.
Args: Args:
fp: The file-object, openend in "wb" mode. fp: The file-object, openend in "wb" mode.
@ -144,6 +142,7 @@ class MHTMLWriter(object):
fp.write(b"--") fp.write(b"--")
def _output_header(self, fp): def _output_header(self, fp):
"""Output only the header to the given fileobject."""
if self.content_location is None: if self.content_location is None:
raise ValueError("content_location must be set") raise ValueError("content_location must be set")
if self.content_type is None: if self.content_type is None:
@ -158,6 +157,7 @@ class MHTMLWriter(object):
fp.write(b'"\r\n\r\n') fp.write(b'"\r\n\r\n')
def _output_root_file(self, fp): def _output_root_file(self, fp):
"""Output the root document to the fileobject."""
root_file = _File( root_file = _File(
content=self.root_content, content_type=self.content_type, content=self.root_content, content_type=self.content_type,
content_location=self.content_location, transfer_encoding=E_QUOPRI, content_location=self.content_location, transfer_encoding=E_QUOPRI,
@ -165,6 +165,7 @@ class MHTMLWriter(object):
self._output_file(fp, root_file) self._output_file(fp, root_file)
def _output_file(self, fp, file_struct): def _output_file(self, fp, file_struct):
"""Output the single given file to the fileobject."""
fp.write(b"--") fp.write(b"--")
fp.write(self.BOUNDARY) fp.write(self.BOUNDARY)
fp.write(b"\r\nContent-Location: ") fp.write(b"\r\nContent-Location: ")
@ -200,8 +201,8 @@ def start_download(dest):
# we're using .toHtml, it's probably safe to say that the content-type is # we're using .toHtml, it's probably safe to say that the content-type is
# HTML # HTML
writer.content_type = 'text/html; charset="UTF-8"' writer.content_type = 'text/html; charset="UTF-8"'
# Currently only downloading <link> (stylesheets), <script> (javascript) and # Currently only downloading <link> (stylesheets), <script> (javascript)
# <img> (image) elements. # and <img> (image) elements.
elements = (web_frame.findAllElements("link") + elements = (web_frame.findAllElements("link") +
web_frame.findAllElements("script") + web_frame.findAllElements("script") +
web_frame.findAllElements("img")) web_frame.findAllElements("img"))
@ -221,7 +222,7 @@ def start_download(dest):
return return
finish_file() finish_file()
def error(name, item, *args): def error(name, item, *_args):
pending_downloads.remove(item) pending_downloads.remove(item)
writer.add_file(name, b"") writer.add_file(name, b"")
if pending_downloads: if pending_downloads:
@ -235,7 +236,6 @@ def start_download(dest):
with open(dest, "wb") as file_output: with open(dest, "wb") as file_output:
writer.write_to(file_output) writer.write_to(file_output)
for element in elements: for element in elements:
element_url = element.attribute("src") element_url = element.attribute("src")
if not element_url: if not element_url: