From a6a030e92fbccaff30eaca895b403da7dab1df29 Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Sun, 10 Jul 2016 21:42:43 -0400 Subject: [PATCH 1/3] Add :print --pdf flag to skip printing dialog --- doc/help/commands.asciidoc | 3 ++- qutebrowser/browser/commands.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index bd85f2eac..1abcaffd5 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -487,12 +487,13 @@ If the pasted text contains newlines, each line gets opened in its own tab. [[print]] === print -Syntax: +:print [*--preview*]+ +Syntax: +:print [*--preview*] [*--pdf* 'file']+ Print the current/[count]th tab. ==== optional arguments * +*-p*+, +*--preview*+: Show preview instead of printing. +* +*-f*+, +*--pdf*+: The file path to write the PDF to. ==== count The tab index to print. diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 460b6ffe8..73dc84c90 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -31,7 +31,7 @@ from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWidgets import QApplication, QTabBar from PyQt5.QtCore import Qt, QUrl, QEvent from PyQt5.QtGui import QKeyEvent -from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog +from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog from PyQt5.QtWebKitWidgets import QWebPage import pygments import pygments.lexers @@ -301,12 +301,14 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', name='print', scope='window') @cmdutils.argument('count', count=True) - def printpage(self, preview=False, count=None): + @cmdutils.argument('pdf', flag='f') + def printpage(self, preview=False, count=None, *, pdf=None): """Print the current/[count]th tab. Args: preview: Show preview instead of printing. count: The tab index to print, or None. + pdf: The file path to write the PDF to. """ if not qtutils.check_print_compat(): # WORKAROUND (remove this when we bump the requirements to 5.3.0) @@ -322,6 +324,14 @@ class CommandDispatcher: Qt.WindowMinimizeButtonHint) diag.paintRequested.connect(tab.print) diag.exec_() + elif pdf: + pdf = os.path.expanduser(pdf) + directory = os.path.dirname(pdf) + if directory and not os.path.exists(directory): + os.mkdir(directory) + printer = QPrinter() + printer.setOutputFileName(pdf) + tab.print(printer) else: diag = QPrintDialog() diag.setAttribute(Qt.WA_DeleteOnClose) From 62ae793a24813db81e46cedaf49984cc8a821558 Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Mon, 11 Jul 2016 20:07:54 -0400 Subject: [PATCH 2/3] Generate docs --- README.asciidoc | 1 + qutebrowser/browser/commands.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.asciidoc b/README.asciidoc index 6c7c3a8c7..9bad4c53b 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -227,6 +227,7 @@ Contributors, sorted by the number of commits in descending order: * Matthias Lisin * Marcel Schilling * Johannes Martinsson +* Jeremy Kaplan * Jean-Christophe Petkovich * Jay Kamat * Helen Sherwood-Taylor diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 73dc84c90..45232282a 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -301,7 +301,7 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', name='print', scope='window') @cmdutils.argument('count', count=True) - @cmdutils.argument('pdf', flag='f') + @cmdutils.argument('pdf', flag='f', metavar='file') def printpage(self, preview=False, count=None, *, pdf=None): """Print the current/[count]th tab. From 6b2b096f3cfa6cfe861fc01ae1183d1fe5190fec Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Mon, 11 Jul 2016 20:08:24 -0400 Subject: [PATCH 3/3] Add test for :print --pdf --- qutebrowser/browser/commands.py | 1 + tests/end2end/features/conftest.py | 3 ++- tests/end2end/features/misc.feature | 7 +++++++ tests/end2end/features/test_misc_bdd.py | 11 +++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 45232282a..99ef1f088 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -332,6 +332,7 @@ class CommandDispatcher: printer = QPrinter() printer.setOutputFileName(pdf) tab.print(printer) + log.misc.debug("Print to file: {}".format(pdf)) else: diag = QPrintDialog() diag.setAttribute(Qt.WA_DeleteOnClose) diff --git a/tests/end2end/features/conftest.py b/tests/end2end/features/conftest.py index 9d1d5c074..8c9eea825 100644 --- a/tests/end2end/features/conftest.py +++ b/tests/end2end/features/conftest.py @@ -179,7 +179,7 @@ def set_setting(quteproc, httpbin, sect, opt, value): @bdd.when(bdd.parsers.parse("I run {command}")) -def run_command(quteproc, httpbin, command): +def run_command(quteproc, httpbin, tmpdir, command): """Run a qutebrowser command. The suffix "with count ..." can be used to pass a count to the command. @@ -199,6 +199,7 @@ def run_command(quteproc, httpbin, command): command = command.replace('(port)', str(httpbin.port)) command = command.replace('(testdata)', utils.abs_datapath()) + command = command.replace('(tmpdir)', str(tmpdir)) quteproc.send_cmd(command, count=count, invalid=invalid) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index ed415958d..5842a12a5 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -339,6 +339,13 @@ Feature: Various utility commands. And I run :debug-pyeval QApplication.instance().activeModalWidget().close() Then no crash should happen + Scenario: print pdf + Given a new tmpdir + When I open data/hello.txt + And I run :print --pdf (tmpdir)/hello.pdf + And I wait for "Print to file: *" in the log or skip the test + Then the file hello.pdf should exist in the tmpdir + # :pyeval Scenario: Running :pyeval diff --git a/tests/end2end/features/test_misc_bdd.py b/tests/end2end/features/test_misc_bdd.py index 443583166..7a28d059c 100644 --- a/tests/end2end/features/test_misc_bdd.py +++ b/tests/end2end/features/test_misc_bdd.py @@ -73,3 +73,14 @@ def check_cookie(quteproc, name, value): data = json.loads(content) print(data) assert data['cookies'][name] == value + + +@bdd.given(bdd.parsers.parse('a new tmpdir')) +def tmpdir(tmpdir_factory): + return tmpdir_factory.mktemp('tmpdir') + + +@bdd.then(bdd.parsers.parse('the file {filename} should exist in the tmpdir')) +def file_exists(quteproc, tmpdir, filename): + path = tmpdir / filename + assert os.path.exists(str(path))