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/5] 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/5] 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/5] 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/5] 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/5] 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)