From a923572341c476ec6e670a7effa7f521ba7b90f1 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 3 Nov 2015 22:45:59 +0100 Subject: [PATCH 1/9] replace global constant with class variable and decorators --- qutebrowser/browser/network/qutescheme.py | 29 +++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index 8c064fb9a..a0a20ec0e 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -49,6 +49,15 @@ class QuteSchemeHandler(schemehandler.SchemeHandler): """Scheme handler for qute: URLs.""" + handlers = dict() + + @staticmethod + def addHandler(name): + """Add a handler to the qute: sheme.""" + def namedecorator(function): + QuteSchemeHandler.handlers[name] = function + return namedecorator + def createRequest(self, _op, request, _outgoing_data): """Create a new request. @@ -66,10 +75,10 @@ class QuteSchemeHandler(schemehandler.SchemeHandler): log.misc.debug("url: {}, path: {}, host {}".format( request.url().toDisplayString(), path, host)) try: - handler = HANDLERS[path] + handler = QuteSchemeHandler.handlers[path] except KeyError: try: - handler = HANDLERS[host] + handler = QuteSchemeHandler.handlers[host] except KeyError: errorstr = "No handler found for {}!".format( request.url().toDisplayString()) @@ -108,6 +117,7 @@ class JSBridge(QObject): message.error(win_id, e) +@QuteSchemeHandler.addHandler('pyeval') def qute_pyeval(_win_id, _request): """Handler for qute:pyeval. Return HTML content as bytes.""" html = jinja.env.get_template('pre.html').render( @@ -123,6 +133,7 @@ def qute_version(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') +@QuteSchemeHandler.addHandler('plainlog') def qute_plainlog(_win_id, _request): """Handler for qute:plainlog. Return HTML content as bytes.""" if log.ram_handler is None: @@ -133,6 +144,7 @@ def qute_plainlog(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') +@QuteSchemeHandler.addHandler('log') def qute_log(_win_id, _request): """Handler for qute:log. Return HTML content as bytes.""" if log.ram_handler is None: @@ -144,11 +156,13 @@ def qute_log(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') +@QuteSchemeHandler.addHandler('gpl') def qute_gpl(_win_id, _request): """Handler for qute:gpl. Return HTML content as bytes.""" return utils.read_file('html/COPYING.html').encode('ASCII') +@QuteSchemeHandler.addHandler('help') def qute_help(win_id, request): """Handler for qute:help. Return HTML content as bytes.""" try: @@ -176,6 +190,7 @@ def qute_help(win_id, request): return utils.read_file(path).encode('UTF-8', errors='xmlcharrefreplace') +@QuteSchemeHandler.addHandler('settings') def qute_settings(win_id, _request): """Handler for qute:settings. View/change qute configuration.""" config_getter = functools.partial(objreg.get('config').get, raw=True) @@ -184,13 +199,3 @@ def qute_settings(win_id, _request): confget=config_getter) return html.encode('UTF-8', errors='xmlcharrefreplace') - -HANDLERS = { - 'pyeval': qute_pyeval, - 'version': qute_version, - 'plainlog': qute_plainlog, - 'log': qute_log, - 'gpl': qute_gpl, - 'help': qute_help, - 'settings': qute_settings, -} From 0233423d9aeaafd69588fb5e82a58a3ff03da444 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 3 Nov 2015 22:52:32 +0100 Subject: [PATCH 2/9] using a class method is cleaner --- qutebrowser/browser/network/qutescheme.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index a0a20ec0e..6144eeaea 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -51,11 +51,11 @@ class QuteSchemeHandler(schemehandler.SchemeHandler): handlers = dict() - @staticmethod - def addHandler(name): + @classmethod + def addHandler(cls, name): """Add a handler to the qute: sheme.""" def namedecorator(function): - QuteSchemeHandler.handlers[name] = function + cls.handlers[name] = function return namedecorator def createRequest(self, _op, request, _outgoing_data): From 84c498b638d55611b5ee612beeae79247cf6bc2a Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 3 Nov 2015 23:12:38 +0100 Subject: [PATCH 3/9] use global variable in place of class variable --- qutebrowser/browser/network/qutescheme.py | 33 +++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index 6144eeaea..c07858290 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -45,19 +45,18 @@ from qutebrowser.config import configexc, configdata pyeval_output = ":pyeval was never called" +HANDLERS = dict() + +def addHandler(name): + """Add a handler to the qute: sheme.""" + def namedecorator(function): + HANDLERS[name] = function + return namedecorator + class QuteSchemeHandler(schemehandler.SchemeHandler): """Scheme handler for qute: URLs.""" - handlers = dict() - - @classmethod - def addHandler(cls, name): - """Add a handler to the qute: sheme.""" - def namedecorator(function): - cls.handlers[name] = function - return namedecorator - def createRequest(self, _op, request, _outgoing_data): """Create a new request. @@ -75,10 +74,10 @@ class QuteSchemeHandler(schemehandler.SchemeHandler): log.misc.debug("url: {}, path: {}, host {}".format( request.url().toDisplayString(), path, host)) try: - handler = QuteSchemeHandler.handlers[path] + handler = HANDLERS[path] except KeyError: try: - handler = QuteSchemeHandler.handlers[host] + handler = HANDLERS[host] except KeyError: errorstr = "No handler found for {}!".format( request.url().toDisplayString()) @@ -117,7 +116,7 @@ class JSBridge(QObject): message.error(win_id, e) -@QuteSchemeHandler.addHandler('pyeval') +@addHandler('pyeval') def qute_pyeval(_win_id, _request): """Handler for qute:pyeval. Return HTML content as bytes.""" html = jinja.env.get_template('pre.html').render( @@ -133,7 +132,7 @@ def qute_version(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@QuteSchemeHandler.addHandler('plainlog') +@addHandler('plainlog') def qute_plainlog(_win_id, _request): """Handler for qute:plainlog. Return HTML content as bytes.""" if log.ram_handler is None: @@ -144,7 +143,7 @@ def qute_plainlog(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@QuteSchemeHandler.addHandler('log') +@addHandler('log') def qute_log(_win_id, _request): """Handler for qute:log. Return HTML content as bytes.""" if log.ram_handler is None: @@ -156,13 +155,13 @@ def qute_log(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@QuteSchemeHandler.addHandler('gpl') +@addHandler('gpl') def qute_gpl(_win_id, _request): """Handler for qute:gpl. Return HTML content as bytes.""" return utils.read_file('html/COPYING.html').encode('ASCII') -@QuteSchemeHandler.addHandler('help') +@addHandler('help') def qute_help(win_id, request): """Handler for qute:help. Return HTML content as bytes.""" try: @@ -190,7 +189,7 @@ def qute_help(win_id, request): return utils.read_file(path).encode('UTF-8', errors='xmlcharrefreplace') -@QuteSchemeHandler.addHandler('settings') +@addHandler('settings') def qute_settings(win_id, _request): """Handler for qute:settings. View/change qute configuration.""" config_getter = functools.partial(objreg.get('config').get, raw=True) From 896ac0a7e95128ea84219982f2a82d7fbec68f4d Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 3 Nov 2015 23:15:07 +0100 Subject: [PATCH 4/9] fix the comments on style/typos --- qutebrowser/browser/network/qutescheme.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index c07858290..b73e61484 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -45,10 +45,10 @@ from qutebrowser.config import configexc, configdata pyeval_output = ":pyeval was never called" -HANDLERS = dict() +HANDLERS = {} -def addHandler(name): - """Add a handler to the qute: sheme.""" +def add_handler(name): + """Add a handler to the qute: scheme.""" def namedecorator(function): HANDLERS[name] = function return namedecorator From 835489483853270040fe77a77e0b771efe577b70 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Wed, 4 Nov 2015 09:49:46 +0100 Subject: [PATCH 5/9] rename the add_handler function everywhere --- qutebrowser/browser/network/qutescheme.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index b73e61484..5fd82aa4b 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -51,6 +51,7 @@ def add_handler(name): """Add a handler to the qute: scheme.""" def namedecorator(function): HANDLERS[name] = function + return function return namedecorator class QuteSchemeHandler(schemehandler.SchemeHandler): @@ -116,7 +117,7 @@ class JSBridge(QObject): message.error(win_id, e) -@addHandler('pyeval') +@add_handler('pyeval') def qute_pyeval(_win_id, _request): """Handler for qute:pyeval. Return HTML content as bytes.""" html = jinja.env.get_template('pre.html').render( @@ -132,7 +133,7 @@ def qute_version(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@addHandler('plainlog') +@add_handler('plainlog') def qute_plainlog(_win_id, _request): """Handler for qute:plainlog. Return HTML content as bytes.""" if log.ram_handler is None: @@ -143,7 +144,7 @@ def qute_plainlog(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@addHandler('log') +@add_handler('log') def qute_log(_win_id, _request): """Handler for qute:log. Return HTML content as bytes.""" if log.ram_handler is None: @@ -155,13 +156,13 @@ def qute_log(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') -@addHandler('gpl') +@add_handler('gpl') def qute_gpl(_win_id, _request): """Handler for qute:gpl. Return HTML content as bytes.""" return utils.read_file('html/COPYING.html').encode('ASCII') -@addHandler('help') +@add_handler('help') def qute_help(win_id, request): """Handler for qute:help. Return HTML content as bytes.""" try: @@ -189,7 +190,7 @@ def qute_help(win_id, request): return utils.read_file(path).encode('UTF-8', errors='xmlcharrefreplace') -@addHandler('settings') +@add_handler('settings') def qute_settings(win_id, _request): """Handler for qute:settings. View/change qute configuration.""" config_getter = functools.partial(objreg.get('config').get, raw=True) From 3525659b9068d0372a7748c1843557f39cc735eb Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 4 Nov 2015 17:41:33 +0100 Subject: [PATCH 6/9] Fix pep8 issues. --- qutebrowser/browser/network/qutescheme.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index 5fd82aa4b..f9f56e687 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -47,6 +47,7 @@ pyeval_output = ":pyeval was never called" HANDLERS = {} + def add_handler(name): """Add a handler to the qute: scheme.""" def namedecorator(function): @@ -54,6 +55,7 @@ def add_handler(name): return function return namedecorator + class QuteSchemeHandler(schemehandler.SchemeHandler): """Scheme handler for qute: URLs.""" @@ -198,4 +200,3 @@ def qute_settings(win_id, _request): win_id=win_id, title='settings', config=configdata, confget=config_getter) return html.encode('UTF-8', errors='xmlcharrefreplace') - From 939e95b34424853666ae7494950577bc81d654e5 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 4 Nov 2015 17:45:12 +0100 Subject: [PATCH 7/9] Add missing decorator for qute:version. --- qutebrowser/browser/network/qutescheme.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qutebrowser/browser/network/qutescheme.py b/qutebrowser/browser/network/qutescheme.py index f9f56e687..c98a33c15 100644 --- a/qutebrowser/browser/network/qutescheme.py +++ b/qutebrowser/browser/network/qutescheme.py @@ -127,6 +127,7 @@ def qute_pyeval(_win_id, _request): return html.encode('UTF-8', errors='xmlcharrefreplace') +@add_handler('version') def qute_version(_win_id, _request): """Handler for qute:version. Return HTML content as bytes.""" html = jinja.env.get_template('version.html').render( From ac10fbc095a8b1a01ca51bad8e3b53f5b533f420 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 4 Nov 2015 17:45:16 +0100 Subject: [PATCH 8/9] Handle qute:... handlers in run_vulture. --- scripts/dev/run_vulture.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/dev/run_vulture.py b/scripts/dev/run_vulture.py index 81c672aea..4d9805332 100644 --- a/scripts/dev/run_vulture.py +++ b/scripts/dev/run_vulture.py @@ -33,6 +33,7 @@ import qutebrowser.app # pylint: disable=unused-import from qutebrowser.commands import cmdutils from qutebrowser.utils import utils from qutebrowser.browser import rfc6266 +from qutebrowser.browser.network import qutescheme def whitelist_generator(): @@ -71,6 +72,10 @@ def whitelist_generator(): yield 'PyQt5.QtGui.QAbstractTextDocumentLayout.PaintContext().clip' yield 'PyQt5.QtWidgets.QStyleOptionViewItem.backgroundColor' + # qute:... handlers + for func in qutescheme.HANDLERS.values(): + yield 'qutebrowser.browser.network.qutescheme.' + func.__name__ + # Globals # https://bitbucket.org/jendrikseipp/vulture/issues/10/ yield 'qutebrowser.misc.utilcmds.pyeval_output' From 58c5b52ff393efa50a3d8d8ea873b1cb155d33bc Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 4 Nov 2015 17:45:33 +0100 Subject: [PATCH 9/9] Update docs. --- README.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/README.asciidoc b/README.asciidoc index 859856d40..43240d97c 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -157,6 +157,7 @@ Contributors, sorted by the number of commits in descending order: * Jimmy * skinnay * Zach-Button +* Felix Van der Jeugt * rikn00 * Patric Schmitz * Martin Zimmermann