From d24c81bce6ef2353e7a408377c4a5362d8799d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Piboub=C3=A8s?= Date: Fri, 28 Aug 2015 21:13:57 +0200 Subject: [PATCH] External OAuth configuration file --- README.rst | 19 +++++++++++++------ rtv/__main__.py | 50 ++++++++++++++++++++++++------------------------- rtv/oauth.py | 4 ++-- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/README.rst b/README.rst index fd14260..a32e6b1 100644 --- a/README.rst +++ b/README.rst @@ -155,20 +155,21 @@ If you prefer to stay in the terminal, use ``$BROWSER`` to specify a console-bas Config File ----------- -RTV will read a configuration placed at ``~/.config/rtv/rtv.cfg`` (or ``$XDG_CONFIG_HOME``). -Each line in the file will replace the corresponding default argument in the launch script. +RTV will read two configuration files: +* ``~/.config/rtv/rtv.cfg`` (or ``$XDG_CONFIG_HOME/.rtv``) +* ``~/.config/rtv/oauth.cfg`` (or ``$XDG_CONFIG_HOME/.rtv-oauth``) +Each line in the files will replace the corresponding default argument in the launch script. This can be used to avoid having to re-enter login credentials every time the program is launched. -The OAuth section contains a boolean to trigger auto-login (defaults to False). +The OAuth section contains a boolean to trigger auto-login (defaults to false). When authenticated, two additional fields are written : **access_token** and **refresh_token**. Those are basically like username and password : they are used to authenticate you on Reddit servers. Example initial config: -.. code-block:: ini +**rtv.cfg** - [oauth] - auto_login=False +.. code-block:: ini [rtv] # Log file location @@ -184,6 +185,12 @@ Example initial config: # This may be necessary for compatibility with some terminal browsers # ascii=True +**oauth.cfg** + +.. code-block:: ini + + [oauth] + auto_login=false ========= Changelog diff --git a/rtv/__main__.py b/rtv/__main__.py index 9970e4a..8d3c1f8 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -22,7 +22,13 @@ from tornado import ioloop __all__ = [] -def get_config_fp(): +def load_rtv_config(): + """ + Attempt to load saved settings for things like the username and password. + """ + + config = configparser.ConfigParser() + HOME = os.path.expanduser('~') XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.join(HOME, '.config')) @@ -35,30 +41,9 @@ def get_config_fp(): # get the first existing config file for config_path in config_paths: if os.path.exists(config_path): + config.read(config_path) break - return config_path - -def open_config(): - """ - Search for a configuration file at the location ~/.rtv and attempt to load - saved settings for things like the username and password. - """ - - config = configparser.ConfigParser() - - config_path = get_config_fp() - config.read(config_path) - - return config - -def load_rtv_config(): - """ - Attempt to load saved settings for things like the username and password. - """ - - config = open_config() - defaults = {} if config.has_section('rtv'): defaults = dict(config.items('rtv')) @@ -73,7 +58,22 @@ def load_oauth_config(): Attempt to load saved OAuth settings """ - config = open_config() + config = configparser.ConfigParser() + + HOME = os.path.expanduser('~') + XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', + os.path.join(HOME, '.config')) + + config_paths = [ + os.path.join(XDG_CONFIG_HOME, 'rtv', 'oauth.cfg'), + os.path.join(HOME, '.rtv-oauth') + ] + + # get the first existing config file + for config_path in config_paths: + if os.path.exists(config_path): + config.read(config_path) + break if config.has_section('oauth'): defaults = dict(config.items('oauth')) @@ -81,7 +81,7 @@ def load_oauth_config(): # Populate OAuth section config.add_section('oauth') config.set('oauth', 'auto_login', 'false') - with open(get_config_fp(), 'w') as cfg: + with open(config_path, 'w') as cfg: config.write(cfg) defaults = dict(config.items('oauth')) diff --git a/rtv/oauth.py b/rtv/oauth.py index f7d64cf..8fcd118 100644 --- a/rtv/oauth.py +++ b/rtv/oauth.py @@ -77,8 +77,8 @@ class OAuthTool(object): os.path.join(HOME, '.config')) config_paths = [ - os.path.join(XDG_CONFIG_HOME, 'rtv', 'rtv.cfg'), - os.path.join(HOME, '.rtv') + os.path.join(XDG_CONFIG_HOME, 'rtv', 'oauth.cfg'), + os.path.join(HOME, '.rtv-oauth') ] # get the first existing config file