Merge branch 'oauth2'

This commit is contained in:
Théo Piboubès
2015-09-06 02:45:55 +02:00
2 changed files with 38 additions and 15 deletions

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, httpserver
from concurrent.futures import ThreadPoolExecutor
__all__ = ['token_validity', 'OAuthTool'] __all__ = ['token_validity', 'OAuthTool']
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -27,8 +28,10 @@ class HomeHandler(web.RequestHandler):
class AuthHandler(web.RequestHandler): class AuthHandler(web.RequestHandler):
def initialize(self):
self.compact = os.environ.get('BROWSER') in ['w3m', 'links', 'elinks', 'lynx']
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,7 +41,9 @@ 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:
# Stop IOLoop if using BackgroundBrowser (or GUI browser)
if not self.compact:
ioloop.IOLoop.current().stop() ioloop.IOLoop.current().stop()
class OAuthTool(object): class OAuthTool(object):
@@ -69,6 +74,8 @@ class OAuthTool(object):
(r'/auth', AuthHandler), (r'/auth', AuthHandler),
], template_path='rtv/templates') ], template_path='rtv/templates')
self.http_server = None
def get_config_fp(self): def get_config_fp(self):
HOME = os.path.expanduser('~') HOME = os.path.expanduser('~')
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME',
@@ -99,6 +106,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'
@@ -110,8 +124,9 @@ class OAuthTool(object):
self.open_config(update=True) self.open_config(update=True)
# If no previous OAuth data found, starting from scratch # If no previous OAuth data found, starting from scratch
if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'refresh_token'): if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'refresh_token'):
# Start HTTP server and listen on port 65000 if self.http_server is None:
self.callback_app.listen(65000) self.http_server = httpserver.HTTPServer(self.callback_app)
self.http_server.listen(65000)
# Generate a random UUID # Generate a random UUID
hex_uuid = uuid.uuid4().hex hex_uuid = uuid.uuid4().hex
@@ -122,7 +137,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:

View File

@@ -1,6 +1,14 @@
from setuptools import setup from setuptools import setup
from version import __version__ as version from version import __version__ as version
import sys
requirements = ['tornado', 'praw>=3.1.0', 'six', 'requests', 'kitchen']
# Python 2: add required concurrent.futures backport from Python 3.2
if sys.version_info.major <= 2:
requirements.append('futures')
setup( setup(
name='rtv', name='rtv',
version=version, version=version,
@@ -13,7 +21,7 @@ setup(
keywords='reddit terminal praw curses', keywords='reddit terminal praw curses',
packages=['rtv'], packages=['rtv'],
include_package_data=True, include_package_data=True,
install_requires=['tornado', 'praw>=3.1.0', 'six', 'requests', 'kitchen'], install_requires=requirements,
entry_points={'console_scripts': ['rtv=rtv.__main__:main']}, entry_points={'console_scripts': ['rtv=rtv.__main__:main']},
classifiers=[ classifiers=[
'Intended Audience :: End Users/Desktop', 'Intended Audience :: End Users/Desktop',