replace global constant with class variable and decorators
This commit is contained in:
parent
2c7b0d2fb4
commit
a923572341
@ -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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user