Add --install option to userscripts through decorator.

master
Harshad Sharma 7 years ago
parent 692da8db27
commit e82f3a2eba

@ -11,5 +11,6 @@ def dump_to_log(request):
line = json.dumps(request.dump())
logfile.writelines([line])
if __name__ == '__main__':
dump_to_log()

@ -1,6 +1,6 @@
__version__ = "0.1.0"
from .decorators import qutescript
from .decorator import qutescript
from .request import Request, build_request
__all__ = [

@ -16,6 +16,8 @@ Why does this file exist, and why not put this in __main__?
"""
import argparse
import os
import sys
parser = argparse.ArgumentParser(description='Command description.')
parser.add_argument('names', metavar='NAME', nargs=argparse.ZERO_OR_MORE,
@ -25,3 +27,21 @@ parser.add_argument('names', metavar='NAME', nargs=argparse.ZERO_OR_MORE,
def main(args=None):
args = parser.parse_args(args=args)
print(args.names)
# ---
script_cli_parser = argparse.ArgumentParser(description='Qutebrowser userscript.')
script_cli_parser.add_argument('--install', action='store_true', help='Setup permissions and show install instructions.')
def script_cli(args=None):
args = script_cli_parser.parse_args(args=args)
if not args.install:
return
from .installer import install
userscript_path = os.path.abspath(sys.argv[0])
path = os.path.abspath(userscript_path)
name = os.path.basename(userscript_path)
print(install(path, name=name))
sys.exit(0)

@ -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)

@ -51,4 +51,6 @@ def build_request():
request.title = os.getenv('QUTE_TITLE')
request.selected_text = os.getenv('QUTE_SELECTED_TEXT')
request.selected_html = os.getenv('QUTE_SELECTED_HTML')
if not request.mode:
raise AssertionError('Unable to read environment variables, did you pass `:spawn --userscript` ?')
return request

Loading…
Cancel
Save