Simplify; stick to argparse for because with click's framework, this

use case seems convoluted :-S
master
Harshad Sharma 7 years ago
parent c294b608c6
commit 26ffc87735

@ -3,10 +3,21 @@
import json
from qutescript import qutescript
from qutescript.cli import parser
class KaboomError(Exception):
pass
parser.add_argument('--kaboom', action='store_true', help='Make things explode.')
@qutescript
def dump_to_log(request):
args = parser.parse_args()
if args.kaboom:
raise KaboomError('Oh noes!')
with open('qutescript.debug.log', 'a') as logfile:
line = json.dumps(request.dump())
logfile.writelines([line])

@ -15,36 +15,42 @@ Why does this file exist, and why not put this in __main__?
Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
"""
import sys
import argparse
import click
import os
# ---
# script_cli_parser = argparse.ArgumentParser(description='Qutebrowser userscript.')
# script_cli_parser.add_argument('--install', action='store_true', help='Setup permissions and show install
# instructions.')
parser = argparse.ArgumentParser(description='Qutebrowser userscript.')
parser.add_argument('--install', action='store_true',
help='Setup permissions and show install instructions.')
class NoSubCommands(Exception):
pass
@click.group(invoke_without_command=True)
@click.pass_context
def userscript(ctx):
def main_cli():
"""
Qutebrowser Userscript
"""
if ctx.invoked_subcommand is None:
raise NoSubCommands()
args = parser.parse_args()
if not args.install:
return
main_install()
@userscript.command(name='install')
def userscript_install():
def main_install():
from .installer import install
userscript_path = os.path.abspath(sys.argv[0])
path = os.path.abspath(userscript_path)
name = os.path.basename(userscript_path)
click.echo_via_pager(install(path, name=name))
print(install(path, name=name))
sys.exit(0)
def userscript_cli(func):
def wrapper(request):
return func(request)
return wrapper

@ -3,75 +3,43 @@
import sys
import traceback
import tempfile
import os
from qutescript.utils import send_messages_to_browser
from .cli import NoSubCommands, userscript
from .cli import main_cli, userscript_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 send_traceback_to_browser(trace, *messages):
"""
Write trace and messages to a temporary file,
Attempt to open the file through FIFO in the browser.
"""
write_log(trace)
[write_log(msg) for msg in messages]
fifo = os.getenv('QUTE_FIFO')
out_lines = ['<html><body><pre>', trace] + ['<p>{}</p>'.format(m or '&nbsp;') for m in messages]
if not fifo:
return
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as trace_file:
trace_file.writelines(out_lines)
print('***', trace_file.name)
with open(fifo, 'w') as fifo_file:
fifo_file.write('open -t file://{}'.format(
os.path.abspath(trace_file.name)))
def qutescript(func):
def wrapper():
try:
userscript()
except NoSubCommands as e:
pass
main_cli()
except Exception as e:
send_traceback_to_browser(traceback.format_exc(), 'Cannot execute cli handler')
send_messages_to_browser(traceback.format_exc(), 'Cannot execute cli handler')
sys.exit(1)
try:
request = build_request()
except Exception as e:
send_traceback_to_browser(traceback.format_exc(), 'Cannot build request.')
send_messages_to_browser(traceback.format_exc(), 'Cannot build request.')
sys.exit(5)
try:
command = func(request)
func_ = userscript_cli(func)
command = func_(request)
if not command:
return
except Exception as e:
send_traceback_to_browser(traceback.format_exc(), 'Userscript error.')
send_messages_to_browser(traceback.format_exc(), 'Userscript error.')
sys.exit(10)
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 ?')
send_traceback_to_browser(traceback.format_exc(), message)
send_messages_to_browser(traceback.format_exc(), message)
sys.exit(20)
try:
with open(request.fifo, 'w') as fifo:
fifo.write('{}\n'.format(command))
except Exception as e:
send_traceback_to_browser(
send_messages_to_browser(
traceback.format_exc(),
'Cannot write to FIFO: {!r}'.format(request.fifo))
sys.exit(30)

@ -0,0 +1,33 @@
#!/usr/bin/env python
# coding=utf-8
import os
import tempfile
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 send_messages_to_browser(*messages):
"""
Write messages to a temporary file,
Attempt to open the file through FIFO in the browser.
"""
[write_log(msg) for msg in messages]
fifo = os.getenv('QUTE_FIFO')
if not fifo:
return
out_lines = ['<html><body><pre>'] + ['<p>{}</p>'.format(m or '&nbsp;') for m in messages]
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as trace_file:
trace_file.writelines(out_lines)
print('***', trace_file.name)
with open(fifo, 'w') as fifo_file:
fifo_file.write('open -t file://{}'.format(
os.path.abspath(trace_file.name)))
Loading…
Cancel
Save