Add comments.

master
Harshad Sharma 7 years ago
parent 9cb7ac4588
commit 0a7e464c82

@ -12,25 +12,38 @@ from .request import build_request
def userscript(func): def userscript(func):
def wrapper(): def wrapper():
userscript_name = os.path.basename(sys.argv[0]) """
Wraps a qutebrowser-userscript in a warm blanket
of easy to access parameters and a sprinkling
of debugging goodness.
"""
userscript_name = os.path.basename(sys.argv[0]) # example: my_script.py
try: try:
# Check if "--install" was passed, and handle it.
main_cli() main_cli()
except Exception as e: except Exception as e:
log_to_browser(traceback.format_exc(), 'Cannot execute cli handler', prefix=userscript_name) log_to_browser(traceback.format_exc(), 'Cannot execute cli handler', prefix=userscript_name)
sys.exit(1) sys.exit(1)
# main_cli() may call sys.exit() if --install was passed at command line.
# If sys.exit() was called by main_cli(), we would never reach here.
# Since ^that^ did not happen...
try: try:
request = build_request() request = build_request()
except Exception as e: except Exception as e:
log_to_browser(traceback.format_exc(), 'Cannot build request.', prefix=userscript_name) log_to_browser(traceback.format_exc(), 'Cannot build request.', prefix=userscript_name)
sys.exit(5) sys.exit(5)
# We now have a request to handle.
try: try:
func_ = userscript_cli(func) # Wrapper for future use; disabled for now.
command = func_(request) # func = userscript_cli(func)
command = func(request)
if not command: if not command:
# No command to execute, our work is done.
return return
except Exception as e: except Exception as e:
log_to_browser(traceback.format_exc(), 'Userscript error.', prefix=userscript_name) log_to_browser(traceback.format_exc(), 'Userscript error.', prefix=userscript_name)
sys.exit(10) sys.exit(10)
# We also have a command to execute.
if not request.fifo: if not request.fifo:
message = ('ERROR: userscript returned command: {}, ' message = ('ERROR: userscript returned command: {}, '
'but QUTE_FIFO was not found in passed environment.\n' 'but QUTE_FIFO was not found in passed environment.\n'
@ -38,6 +51,7 @@ def userscript(func):
log_to_browser(traceback.format_exc(), message, prefix=userscript_name) log_to_browser(traceback.format_exc(), message, prefix=userscript_name)
sys.exit(20) sys.exit(20)
try: try:
# Send the returned command to qutebrowser.
with open(request.fifo, 'w') as fifo: with open(request.fifo, 'w') as fifo:
fifo.write('{}\n'.format(command)) fifo.write('{}\n'.format(command))
except Exception as e: except Exception as e:

Loading…
Cancel
Save