|
|
|
@ -4,9 +4,9 @@ import sys
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
from qutescript.utils import log_to_browser
|
|
|
|
|
|
|
|
|
|
from .cli import main_cli, userscript_cli
|
|
|
|
|
from qutescript.utils import log_to_browser
|
|
|
|
|
from .cli import main_cli
|
|
|
|
|
from .request import build_request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -17,12 +17,16 @@ def userscript(func):
|
|
|
|
|
of easy to access parameters and a sprinkling
|
|
|
|
|
of debugging goodness.
|
|
|
|
|
"""
|
|
|
|
|
userscript_name = os.path.basename(sys.argv[0]) # example: my_script.py
|
|
|
|
|
script_path = sys.argv[0]
|
|
|
|
|
prefix = os.path.basename(script_path) # example: my_script.py
|
|
|
|
|
try:
|
|
|
|
|
# Check if "--install" was passed, and handle it.
|
|
|
|
|
main_cli()
|
|
|
|
|
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=prefix,
|
|
|
|
|
script_path=script_path)
|
|
|
|
|
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.
|
|
|
|
@ -30,7 +34,10 @@ def userscript(func):
|
|
|
|
|
try:
|
|
|
|
|
request = build_request()
|
|
|
|
|
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=prefix,
|
|
|
|
|
script_path=script_path)
|
|
|
|
|
sys.exit(5)
|
|
|
|
|
# We now have a request to handle.
|
|
|
|
|
try:
|
|
|
|
@ -41,14 +48,20 @@ def userscript(func):
|
|
|
|
|
# No command to execute, our work is done.
|
|
|
|
|
return
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log_to_browser(traceback.format_exc(), 'Userscript error.', prefix=userscript_name)
|
|
|
|
|
log_to_browser(traceback.format_exc(),
|
|
|
|
|
'Userscript error.',
|
|
|
|
|
prefix=prefix,
|
|
|
|
|
script_path=script_path)
|
|
|
|
|
sys.exit(10)
|
|
|
|
|
# We also have a command to execute.
|
|
|
|
|
if not request.fifo:
|
|
|
|
|
message = ('ERROR: userscript returned command: {}, '
|
|
|
|
|
'but QUTE_FIFO was not found in passed environment.\n'
|
|
|
|
|
'Try: :spawn --userscript /path/to/script ?')
|
|
|
|
|
log_to_browser(traceback.format_exc(), message, prefix=userscript_name)
|
|
|
|
|
log_to_browser(traceback.format_exc(),
|
|
|
|
|
message,
|
|
|
|
|
prefix=prefix,
|
|
|
|
|
script_path=script_path)
|
|
|
|
|
sys.exit(20)
|
|
|
|
|
try:
|
|
|
|
|
# Send the returned command to qutebrowser.
|
|
|
|
@ -57,7 +70,9 @@ def userscript(func):
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log_to_browser(
|
|
|
|
|
traceback.format_exc(),
|
|
|
|
|
'Cannot write to FIFO: {!r}'.format(request.fifo), prefix=userscript_name)
|
|
|
|
|
'Cannot write to FIFO: {!r}'.format(request.fifo),
|
|
|
|
|
prefix=prefix,
|
|
|
|
|
script_path=script_path)
|
|
|
|
|
sys.exit(30)
|
|
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|