From d591f9355189561f1817f6824ab2169f274e3359 Mon Sep 17 00:00:00 2001 From: Harshad Sharma Date: Fri, 16 Jun 2017 23:47:31 +0530 Subject: [PATCH] install to qutebrowser's userscripts directory for easy access. --- examples/debug.py | 2 +- setup.py | 1 + src/qutescript/cli.py | 6 +++++- src/qutescript/installer.py | 33 ++++++++++++++++++++++++++++----- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/examples/debug.py b/examples/debug.py index 6e142f4..341fb19 100755 --- a/examples/debug.py +++ b/examples/debug.py @@ -19,7 +19,7 @@ def dump_to_log(request): if args.kaboom: raise KaboomError('Oh noes!') with open('qutescript.debug.log', 'a') as logfile: - line = json.dumps(request.dump()) + line = json.dumps(request.as_dict()) logfile.writelines([line]) diff --git a/setup.py b/setup.py index cbef6af..4ef98e7 100644 --- a/setup.py +++ b/setup.py @@ -67,6 +67,7 @@ setup( ], install_requires=[ # eg: 'aspectlib==1.1.1', 'six>=1.7', + 'appdirs==1.4.3', ], extras_require={ # eg: diff --git a/src/qutescript/cli.py b/src/qutescript/cli.py index 9531576..a318ff7 100755 --- a/src/qutescript/cli.py +++ b/src/qutescript/cli.py @@ -24,6 +24,9 @@ import os parser = argparse.ArgumentParser(description='Qutebrowser userscript.') parser.add_argument('--install', action='store_true', help='Setup permissions and show install instructions.') +parser.add_argument('--bin', action='store', + help='Used with --install, sets up the command in ' + '[AppDir]/qutebrowser/userscripts for easy access.') class NoSubCommands(Exception): @@ -42,8 +45,9 @@ def main_cli(): def main_install(): from .installer import install + args = parser.parse_args() userscript_path = os.path.abspath(sys.argv[0]) - name = os.path.basename(userscript_path) + name = args.bin or os.path.basename(userscript_path) print(install(userscript_path, name=name)) sys.exit(0) diff --git a/src/qutescript/installer.py b/src/qutescript/installer.py index 7ed4179..3853e39 100644 --- a/src/qutescript/installer.py +++ b/src/qutescript/installer.py @@ -4,14 +4,20 @@ import sys import os import stat -TEMPLATE = """\ +REVIEW_TEMPLATE = """\ Qutebrowser userscript {name!r} was installed at: - {path} + {userscripts_path!r} You can try it out by running the command: - :spawn --userscript {interpreter}{path} + :spawn --userscript {name} + +""" + +LOADER_TEMPLATE = """\ +#!/usr/bin/env bash +{interpreter}"{path}" "$@" """ @@ -29,6 +35,23 @@ def get_interpreter(): return interpreter +def link_to_qutebrowser_userscripts_directory(path, name): + import appdirs + appdir = appdirs.user_data_dir('qutebrowser', 'qutebrowser') + userscripts_dir = os.path.join(appdir, 'userscripts') + userscripts_path = os.path.join(userscripts_dir, name) + interpreter = get_interpreter() + if not os.path.exists(userscripts_dir): + os.makedirs(userscripts_dir, exist_ok=False) + with open(userscripts_path, 'w') as bin_file: + bin_file.write(LOADER_TEMPLATE.format( + interpreter=interpreter, + path=path, + )) + setup_permissions(userscripts_path) + return userscripts_path + + def install(path, name=None): """ Sets permissions for qutescript at path and returns @@ -38,5 +61,5 @@ def install(path, name=None): name = name or os.path.basename(path) interpreter = get_interpreter() setup_permissions(path) - - return TEMPLATE.format(path=path, name=name, interpreter=interpreter) + userscripts_path = link_to_qutebrowser_userscripts_directory(path, name) + return REVIEW_TEMPLATE.format(userscripts_path=userscripts_path, name=name, interpreter=interpreter)