Open terminal web browser asynchronously

This commit is contained in:
Théo Piboubès
2015-09-05 16:25:34 +02:00
parent 0134e157d0
commit 8e6758a389

View File

@@ -11,7 +11,8 @@ from six.moves import configparser
from . import config from . import config
from .curses_helpers import show_notification, prompt_input from .curses_helpers import show_notification, prompt_input
from tornado import ioloop, web from tornado import gen, ioloop, web
from concurrent.futures import ThreadPoolExecutor
__all__ = ['token_validity', 'OAuthTool'] __all__ = ['token_validity', 'OAuthTool']
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -28,7 +29,6 @@ class HomeHandler(web.RequestHandler):
class AuthHandler(web.RequestHandler): class AuthHandler(web.RequestHandler):
def get(self): def get(self):
try:
global oauth_state global oauth_state
global oauth_code global oauth_code
global oauth_error global oauth_error
@@ -38,8 +38,6 @@ class AuthHandler(web.RequestHandler):
oauth_error = self.get_argument('error', default='error_placeholder') oauth_error = self.get_argument('error', default='error_placeholder')
self.render('auth.html', state=oauth_state, code=oauth_code, error=oauth_error) self.render('auth.html', state=oauth_state, code=oauth_code, error=oauth_error)
finally:
ioloop.IOLoop.current().stop()
class OAuthTool(object): class OAuthTool(object):
@@ -99,6 +97,13 @@ class OAuthTool(object):
self.config.remove_option('oauth', 'refresh_token') self.config.remove_option('oauth', 'refresh_token')
self.save_config() self.save_config()
@gen.coroutine
def open_terminal_browser(self, url):
with ThreadPoolExecutor(max_workers=1) as executor:
yield executor.submit(webbrowser.open_new_tab, url)
ioloop.IOLoop.current().stop()
def authorize(self): def authorize(self):
if self.compact and not '.compact' in self.reddit.config.API_PATHS['authorize']: if self.compact and not '.compact' in self.reddit.config.API_PATHS['authorize']:
self.reddit.config.API_PATHS['authorize'] += '.compact' self.reddit.config.API_PATHS['authorize'] += '.compact'
@@ -122,7 +127,7 @@ class OAuthTool(object):
if self.compact: if self.compact:
show_notification(self.stdscr, ['Opening ' + os.environ.get('BROWSER')]) show_notification(self.stdscr, ['Opening ' + os.environ.get('BROWSER')])
curses.endwin() curses.endwin()
webbrowser.open_new_tab(permission_ask_page_link) ioloop.IOLoop.current().add_callback(self.open_terminal_browser, permission_ask_page_link)
ioloop.IOLoop.current().start() ioloop.IOLoop.current().start()
curses.doupdate() curses.doupdate()
else: else: