Add: request.send_command, .send_text and

rename .send_to_browser => .send_html
master
Harshad Sharma 7 years ago
parent d45b0712bc
commit d4c6796d5e

@ -6,7 +6,7 @@ from qutescript import userscript
@userscript
def hello_world(request):
request.send_to_browser('Hello, world!')
request.send_text('Hello, world!')
if __name__ == '__main__':

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

@ -1,5 +1,6 @@
# coding=utf-8
from qutescript.utils import send_to_browser, log_to_browser
from qutescript.utils import log_to_browser, send_html
class Request(object):
@ -17,7 +18,7 @@ class Request(object):
self.title = None
self.selected_text = None
self.selected_html = None
self.send_to_browser = send_to_browser
self.send_html = send_html
self.log_to_browser = log_to_browser
def as_dict(self):
@ -37,6 +38,16 @@ class Request(object):
'selected_html': self.selected_html,
}
def send_command(self, command):
if not self.fifo:
raise FileNotFoundError('QUTE_FIFO not defined in environment.')
with open(self.fifo, 'w') as out_file:
out_file.write('{}\n'.format(command))
def send_text(self, text, prefix=None, script_path=None):
html = '<pre>{}</pre>'.format(text)
self.send_html(html, prefix=prefix, script_path=script_path)
def build_request():
import os

@ -5,8 +5,12 @@ import tempfile
HTML_BODY = """\
<html>
<head>
<title>Qutescript: {prefix}</title>
</head>
<body>
{}
<pre>{script_path}</pre>
{content}
</body>
"""
@ -36,24 +40,29 @@ def normalize_prefix(prefix):
return prefix
def log_to_browser(*messages, prefix: str = None, console=True):
def log_to_browser(*messages, prefix: str = None, console=True, script_path=None):
"""
Write messages to logs and a temporary file,
Attempt to open the file through FIFO in the browser.
"""
[write_log(msg, console=console) for msg in messages]
html_messages = '\n'.join([HTML_MESSAGE_TEMPLATE.format(m) for m in messages])
html_body = HTML_BODY.format(html_messages)
send_to_browser(html_body, prefix=prefix)
html_body = HTML_BODY.format(
content=html_messages,
prefix=prefix,
script_path=script_path,
)
send_html(html_body, prefix=prefix, script_path=script_path)
def send_to_browser(text, prefix: str = None):
def send_html(text, prefix: str = None, script_path=None):
fifo = os.getenv('QUTE_FIFO')
if not fifo:
return
prefix = normalize_prefix(prefix)
prefix = 'qutescript_{}'.format((prefix or ''))
script_path = script_path or ''
with tempfile.NamedTemporaryFile(mode='w', prefix=prefix, suffix='.html', delete=False) as out_file:
out_file.writelines(text)
with open(fifo, 'w') as fifo_file:

Loading…
Cancel
Save