Merge pull request #256 from zbelial/master

Call monolith asynchronously
master
Andy Stewart 6 years ago committed by GitHub
commit de3d0c66ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      core/browser.py
  2. 16
      core/utils.py

@ -25,13 +25,14 @@ from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtNetwork import QNetworkCookie from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineContextMenuData, QWebEngineProfile, QWebEngineSettings from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineContextMenuData, QWebEngineProfile, QWebEngineSettings
from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtWidgets import QApplication, QWidget
from core.utils import touch, is_port_in_use, string_to_base64, popen_and_call from core.utils import touch, is_port_in_use, string_to_base64, popen_and_call, call_and_check_code
from core.buffer import Buffer from core.buffer import Buffer
from urllib.parse import urlparse, parse_qs, urlunparse, urlencode from urllib.parse import urlparse, parse_qs, urlunparse, urlencode
import os import os
import subprocess import subprocess
import re import re
import base64 import base64
from functools import partial
MOUSE_BACK_BUTTON = 8 MOUSE_BACK_BUTTON = 8
@ -471,6 +472,17 @@ class BrowserBuffer(Buffer):
else: else:
self.message_to_emacs.emit("Failed to save current webpage as '{}'.".format(file_path)) self.message_to_emacs.emit("Failed to save current webpage as '{}'.".format(file_path))
def notify_monolith_message(self, download_path, file_path, title, retcode):
if retcode == 0:
title_path = os.path.join(os.path.expanduser(download_path), "{}.html".format(title))
try:
os.rename(file_path, title_path)
self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(title_path))
except Exception:
self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(file_path))
else:
self.message_to_emacs.emit("Failed to save current page as single file.")
def record_url(self, url): def record_url(self, url):
self.request_url = url.toString() self.request_url = url.toString()
@ -643,16 +655,9 @@ class BrowserBuffer(Buffer):
qd = parse_qs(parsed.query, keep_blank_values=True) qd = parse_qs(parsed.query, keep_blank_values=True)
file_path = os.path.join(os.path.expanduser(self.emacs_var_dict["eaf-browser-download-path"]), "{}.html".format(parsed.netloc)) file_path = os.path.join(os.path.expanduser(self.emacs_var_dict["eaf-browser-download-path"]), "{}.html".format(parsed.netloc))
self.message_to_emacs.emit("Saving as single file...") self.message_to_emacs.emit("Saving as single file...")
ret = subprocess.call("monolith " + self.url + " -o " + file_path, shell=True) args = ["monolith", self.url, "-o", file_path]
if ret == 0: handler = partial(self.notify_monolith_message, self.emacs_var_dict["eaf-browser-download-path"], file_path, self.title)
title_path = os.path.join(os.path.expanduser(self.emacs_var_dict["eaf-browser-download-path"]), "{}.html".format(self.title)) call_and_check_code(args, handler)
try:
os.rename(file_path, title_path)
self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(title_path))
except Exception:
self.message_to_emacs.emit("Successfully saved current webpage as '{}'.".format(file_path))
else:
self.message_to_emacs.emit("Failed to save current page as single file.")
def cancel_input_message(self, result_tag): def cancel_input_message(self, result_tag):
if result_tag == "jump_link" or result_tag == "jump_link_new_buffer" or result_tag == "jump_link_background_buffer": if result_tag == "jump_link" or result_tag == "jump_link_new_buffer" or result_tag == "jump_link_background_buffer":

@ -110,3 +110,19 @@ def popen_and_call(popen_args, on_exit, stdout_file=None):
thread.start() thread.start()
# returns immediately after the thread starts # returns immediately after the thread starts
return thread return thread
def call_and_check_code(popen_args, on_exit, stdout_file=None):
"""
Runs the given args in a subprocess.Popen, and then calls the function
on_exit when the subprocess completes.
on_exit is a callable object, and popen_args is a list/tuple of args that
would give to subprocess.Popen.
"""
def run_in_thread(on_exit, popen_args):
retcode = subprocess.call(popen_args, stdout=stdout_file)
on_exit(retcode)
return
thread = threading.Thread(target=run_in_thread, args=(on_exit, popen_args))
thread.start()
# returns immediately after the thread starts
return thread

Loading…
Cancel
Save