Open webbrowser in subprocess to redirect stdout.

This commit is contained in:
Michael Lazar
2015-03-09 20:47:07 -07:00
parent 04f3a5b7b6
commit a6300b494a
3 changed files with 20 additions and 10 deletions

View File

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

View File

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

View File

@@ -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():