Add --install option to userscripts through decorator.
parent
692da8db27
commit
e82f3a2eba
@ -0,0 +1,52 @@
|
||||
# coding=utf-8
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
import os
|
||||
from .cli import script_cli
|
||||
|
||||
from .request import build_request
|
||||
|
||||
log_file_path = './qutescript.log'
|
||||
|
||||
|
||||
def write_log(message, file_path=None):
|
||||
print('***', message)
|
||||
file_path = file_path or log_file_path
|
||||
file_path = os.path.abspath(os.path.expanduser(file_path))
|
||||
record = [message, '\n', '\n']
|
||||
with open(file_path, 'a') as logfile:
|
||||
logfile.writelines(record)
|
||||
|
||||
|
||||
def qutescript(func):
|
||||
def wrapper():
|
||||
script_cli(args=sys.argv[1:])
|
||||
try:
|
||||
request = build_request()
|
||||
except Exception as e:
|
||||
write_log(traceback.format_exc())
|
||||
write_log('Cannot build request.')
|
||||
sys.exit(1)
|
||||
try:
|
||||
command = func(request)
|
||||
if not command:
|
||||
return
|
||||
except Exception as e:
|
||||
write_log(traceback.format_exc())
|
||||
write_log('Userscript error.')
|
||||
sys.exit(2)
|
||||
if not request.fifo:
|
||||
write_log('ERROR: userscript returned command: {}, '
|
||||
'but QUTE_FIFO was not found in passed environment.\n'
|
||||
'Try: :spawn --userscript /path/to/script ?')
|
||||
sys.exit(3)
|
||||
try:
|
||||
with open(request.fifo, 'w') as fifo:
|
||||
fifo.write('{}\n'.format(command))
|
||||
except Exception as e:
|
||||
write_log(traceback.format_exc())
|
||||
write_log('Cannot write to FIFO: {!r}'.format(request.fifo))
|
||||
sys.exit(4)
|
||||
return wrapper
|
@ -1,33 +0,0 @@
|
||||
# coding=utf-8
|
||||
|
||||
import sys
|
||||
|
||||
import os
|
||||
|
||||
from .request import build_request
|
||||
|
||||
log_file_path = './qutescript.log'
|
||||
|
||||
|
||||
def write_log(message, file_path=None):
|
||||
file_path = file_path or log_file_path
|
||||
file_path = os.path.abspath(os.path.expanduser(file_path))
|
||||
with open(file_path, 'a') as logfile:
|
||||
logfile.writelines([message])
|
||||
|
||||
|
||||
def qutescript(func):
|
||||
def wrapper():
|
||||
request = build_request()
|
||||
command = func(request)
|
||||
if not command:
|
||||
return
|
||||
if not request.fifo:
|
||||
write_log('ERROR: userscript returned command: {}, '
|
||||
'but QUTE_FIFO was not found in passed environment.\n'
|
||||
'Try: :spawn --userscript /path/to/script ?')
|
||||
sys.exit(1)
|
||||
with open(request.fifo, 'w') as fifo:
|
||||
fifo.write('{}\n'.format(command))
|
||||
|
||||
return wrapper
|
@ -0,0 +1,33 @@
|
||||
# coding=utf-8
|
||||
import os
|
||||
import stat
|
||||
|
||||
TEMPLATE = """\
|
||||
Qutebrowser userscript {name!r} was installed at:
|
||||
|
||||
{path}
|
||||
|
||||
You can try it out by running the command:
|
||||
|
||||
:spawn --userscript {path}
|
||||
"""
|
||||
|
||||
|
||||
def setup_permissions(path):
|
||||
file_stat = os.stat(path)
|
||||
os.chmod(path, file_stat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
||||
|
||||
|
||||
def format_commands(path, name):
|
||||
return TEMPLATE.format(path=path, name=name)
|
||||
|
||||
|
||||
def install(path, name=None):
|
||||
"""
|
||||
Sets permissions for qutescript at path and returns
|
||||
instructons and commands to integrate with qutebrowser.
|
||||
"""
|
||||
path = os.path.abspath(os.path.expanduser(path))
|
||||
name = name or os.path.basename(path)
|
||||
setup_permissions(path)
|
||||
return format_commands(path=path, name=name)
|
Loading…
Reference in New Issue