From d2822ccf8522a2dab9f451d801536a177d9fd38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Fri, 28 Aug 2015 20:28:58 +0200 Subject: [PATCH] Make OAuth compatible with Python 2 --- rtv/__main__.py | 5 +++-- rtv/oauth.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/rtv/__main__.py b/rtv/__main__.py index 881feff..9970e4a 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -79,7 +79,8 @@ def load_oauth_config(): defaults = dict(config.items('oauth')) else: # Populate OAuth section - config['oauth'] = {'auto_login': False} + config.add_section('oauth') + config.set('oauth', 'auto_login', 'false') with open(get_config_fp(), 'w') as cfg: config.write(cfg) defaults = dict(config.items('oauth')) @@ -154,7 +155,7 @@ def main(): reddit.config.decode_html_entities = False with curses_session() as stdscr: oauth = OAuthTool(reddit, stdscr, LoadScreen(stdscr)) - if args.auto_login == 'True': # Ew! + if args.auto_login == 'true': # Ew! oauth.authorize() if args.link: page = SubmissionPage(stdscr, reddit, oauth, url=args.link) diff --git a/rtv/oauth.py b/rtv/oauth.py index 0d3029c..f7d64cf 100644 --- a/rtv/oauth.py +++ b/rtv/oauth.py @@ -1,4 +1,3 @@ -from six.moves import configparser import curses import logging import os @@ -7,6 +6,7 @@ import uuid import webbrowser import praw +from six.moves import configparser from . import config from .curses_helpers import show_notification, prompt_input @@ -111,7 +111,7 @@ class OAuthTool(object): try: with self.loader(message='Refreshing token'): new_access_info = self.reddit.refresh_access_information( - self.config['oauth']['refresh_token']) + self.config.get('oauth', 'refresh_token')) self.access_info = new_access_info self.reddit.set_access_credentials(scope=set(self.access_info['scope']), access_token=self.access_info['access_token'], @@ -121,8 +121,8 @@ class OAuthTool(object): praw.errors.HTTPException) as e: show_notification(self.stdscr, ['Invalid OAuth data']) else: - self.config['oauth']['access_token'] = self.access_info['access_token'] - self.config['oauth']['refresh_token'] = self.access_info['refresh_token'] + self.config.set('oauth', 'access_token', self.access_info['access_token']) + self.config.set('oauth', 'refresh_token', self.access_info['refresh_token']) self.save_config() def authorize(self): @@ -132,7 +132,7 @@ class OAuthTool(object): self.open_config(update=True) # If no previous OAuth data found, starting from scratch - if 'oauth' not in self.config or 'access_token' not in self.config['oauth']: + if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'access_token'): # Generate a random UUID hex_uuid = uuid.uuid4().hex @@ -178,11 +178,11 @@ class OAuthTool(object): except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken) as e: show_notification(self.stdscr, ['Invalid OAuth data']) else: - if 'oauth' not in self.config: - self.config['oauth'] = {} + if not self.config.has_section('oauth'): + self.config.add_section('oauth') - self.config['oauth']['access_token'] = self.access_info['access_token'] - self.config['oauth']['refresh_token'] = self.access_info['refresh_token'] + self.config.set('oauth', 'access_token', self.access_info['access_token']) + self.config.set('oauth', 'refresh_token', self.access_info['refresh_token']) self.save_config() # Otherwise, fetch new access token else: