From a6300b494ae680811c976c0bf0ade11b8f2ffc9a Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 9 Mar 2015 20:47:07 -0700 Subject: [PATCH] Open webbrowser in subprocess to redirect stdout. --- rtv/submission.py | 4 ++-- rtv/subreddit.py | 10 +++++----- rtv/utils.py | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/rtv/submission.py b/rtv/submission.py index 25c197e..d078577 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -6,7 +6,7 @@ import six from .content import SubmissionContent from .page import BasePage -from .utils import LoadScreen, Color, ESCAPE, display_help +from .utils import LoadScreen, Color, ESCAPE, display_help, open_new_tab class SubmissionPage(BasePage): @@ -81,7 +81,7 @@ class SubmissionPage(BasePage): # Always open the page for the submission # May want to expand at some point to open comment permalinks url = self.content.get(-1)['permalink'] - webbrowser.open_new_tab(url) + open_new_tab(url) def draw_item(self, win, data, inverted=False): diff --git a/rtv/subreddit.py b/rtv/subreddit.py index aae59db..bab2fe3 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -1,6 +1,5 @@ import curses import sys -import webbrowser from requests.exceptions import HTTPError @@ -8,7 +7,8 @@ from .errors import SubredditNameError from .page import BasePage from .submission import SubmissionPage from .content import SubredditContent -from .utils import LoadScreen, Color, text_input, display_message, display_help +from .utils import (LoadScreen, Color, text_input, display_message, + display_help, open_new_tab) # Used to keep track of browsing history across the current session @@ -109,11 +109,11 @@ class SubredditPage(BasePage): def open_link(self): - url_full = self.content.get(self.nav.absolute_index)['url_full'] - webbrowser.open_new_tab(url_full) + url = self.content.get(self.nav.absolute_index)['url_full'] + open_new_tab(url) global _opened_links - _opened_links.add(url_full) + _opened_links.add(url) @staticmethod def draw_item(win, data, inverted=False): diff --git a/rtv/utils.py b/rtv/utils.py index 6a50ca1..3ea74f4 100644 --- a/rtv/utils.py +++ b/rtv/utils.py @@ -1,5 +1,6 @@ import os import sys +import subprocess import curses import time import threading @@ -116,7 +117,6 @@ def text_input(window): curses.curs_set(0) return out - def display_message(stdscr, message): "Display a message box at the center of the screen and wait for a keypress" @@ -148,7 +148,6 @@ def display_message(stdscr, message): window = None stdscr.refresh() - def display_help(stdscr): """Display a help message box at the center of the screen and wait for a keypress""" @@ -156,7 +155,6 @@ def display_help(stdscr): help_msgs = HELP.split("\n") display_message(stdscr, help_msgs) - class LoadScreen(object): def __init__(self, stdscr): @@ -220,6 +218,18 @@ class LoadScreen(object): window.refresh() time.sleep(interval) +def open_new_tab(url): + """ + Call webbrowser.open_new_tab(url) and redirect stdout/stderr to devnull. + + This is a workaround to stop firefox from spewing warning messages to the + console. See http://bugs.python.org/issue22277 for a better description + of the problem. + """ + command = "import webbrowser; webbrowser.open_new_tab('%s')" % url + args = [sys.executable, '-c', command] + with open(os.devnull, 'ab+', 0) as null: + subprocess.check_call(args, stdout=null, stderr=null) @contextmanager def curses_session():