Simplify display_url to just use htmlview or webbrowser

svn: r23033
This commit is contained in:
John Ralls 2013-09-04 16:37:22 +00:00
parent b4a0ce3702
commit 2f7994e68a
2 changed files with 17 additions and 68 deletions

View File

@ -25,7 +25,7 @@ import constfunc
import config import config
import locale import locale
import os import os
import subprocess import webbrowser
#list of manuals on wiki, map locale code to wiki extension, add language codes #list of manuals on wiki, map locale code to wiki extension, add language codes
#completely, or first part, so pt_BR if Brazilian portugeze wiki manual, and #completely, or first part, so pt_BR if Brazilian portugeze wiki manual, and
@ -87,42 +87,5 @@ def url(link, uistate=None):
page = uistate.viewmanager.goto_page(cat_num, None) page = uistate.viewmanager.goto_page(cat_num, None)
page.open(link) page.open(link)
return return
if not open_file_with_default_application(link, display_error=False):
run_browser(link)
def run_browser(url):
"""
Attempt of find a browswer, and launch with the browser with the
specified URL
Use run_file first!
"""
try:
import webbrowser
webbrowser.open_new_tab(url)
except ImportError:
for browser in ['firefox', 'konqueror', 'epiphany',
'galeon', 'mozilla']:
prog = find_binary(browser)
if prog:
os.spawnvpe(os.P_NOWAIT, prog, [prog, url], os.environ)
return
# If we did not find a browser in the path, try this
try:
os.startfile(url)
except:
pass
def find_binary(file):
"""
Find the binary (executable) of a filename in the PATH, and return full
path if found, else return None.
"""
import os
search = os.environ['PATH'].split(':')
for path in search:
prog = os.path.join(path, file)
if os.path.isfile(prog):
return prog
return None
webbrowser.open_new_tab(link)

View File

@ -345,7 +345,7 @@ def display_error_dialog (index, errorstrings):
ErrorDialog(_("Error from external program"), error) ErrorDialog(_("Error from external program"), error)
def poll_external ((proc, errorstrings)): def poll_external (args):
""" """
Check the for completion of a task launched with Check the for completion of a task launched with
subprocess.Popen(). This function is intended to be passed to subprocess.Popen(). This function is intended to be passed to
@ -356,7 +356,7 @@ def poll_external ((proc, errorstrings)):
@errorstrings a dict of possible response values and the corresponding messages to display. @errorstrings a dict of possible response values and the corresponding messages to display.
@returns False when the function has completed. @returns False when the function has completed.
""" """
from QuestionDialog import ErrorDialog (proc, errorstrings)= args
resp = proc.poll() resp = proc.poll()
if resp is None: if resp is None:
return True return True
@ -365,37 +365,31 @@ def poll_external ((proc, errorstrings)):
display_error_dialog(resp, errorstrings) display_error_dialog(resp, errorstrings)
return False return False
def open_file_with_default_application(uri, display_error=True): def open_file_with_default_application(file_name):
""" """
Launch a program to open an arbitrary file. The file will be opened using Launch a program to open an arbitrary file. The file will be opened using
whatever program is configured on the host as the default program for that whatever program is configured on the host as the default program for that
type of file. type of file.
@param file_path: The path to the file to be opened. @param file_path: The path to the file to be opened.
Example: "c:\foo.txt" Example: "c:\\foo.txt"
@type file_path: string @type file_path: string
@return: nothing @return: nothing
""" """
from urlparse import urlparse
from time import sleep
errstrings = None errstrings = None
urlcomp = urlparse(uri)
if (not urlcomp.scheme or urlcomp.scheme == 'file'): norm_path = os.path.normpath(file_name)
norm_path = os.path.normpath(urlcomp.path)
if not os.path.exists(norm_path): if not os.path.exists(norm_path):
display_error_dialog(0, _("File does not exist")) display_error_dialog(0, _("File does not exist"))
return False return
else:
norm_path = uri
if constfunc.win(): if constfunc.win():
try: try:
os.startfile(norm_path) os.startfile(norm_path)
except WindowsError, msg: except WindowsError, msg:
display_error_dialog(0, str(msg)) display_error_dialog(0, str(msg))
return False
return True return
if constfunc.mac(): if constfunc.mac():
utility = '/usr/bin/open' utility = '/usr/bin/open'
@ -407,18 +401,10 @@ def open_file_with_default_application(uri, display_error=True):
4:_('The action failed.')} 4:_('The action failed.')}
proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT) proc = subprocess.Popen([utility, norm_path], stderr=subprocess.STDOUT)
sleep(.1)
resp = proc.poll()
if resp is None:
from gobject import timeout_add from gobject import timeout_add
timeout_add(1000, poll_external, (proc, errstrings)) timeout_add(1000, poll_external, (proc, errstrings))
return True return
if resp == 0:
return True
if display_error:
display_error_dialog(resp, errstrings)
return False
def process_pending_events(max_count=10): def process_pending_events(max_count=10):
""" """