External OAuth configuration file

This commit is contained in:
Théo Piboubès
2015-08-28 21:13:57 +02:00
parent c579aa928d
commit d24c81bce6
3 changed files with 40 additions and 33 deletions

View File

@@ -155,20 +155,21 @@ If you prefer to stay in the terminal, use ``$BROWSER`` to specify a console-bas
Config File Config File
----------- -----------
RTV will read a configuration placed at ``~/.config/rtv/rtv.cfg`` (or ``$XDG_CONFIG_HOME``). RTV will read two configuration files:
Each line in the file will replace the corresponding default argument in the launch script. * ``~/.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. 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**. 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. Those are basically like username and password : they are used to authenticate you on Reddit servers.
Example initial config: Example initial config:
.. code-block:: ini **rtv.cfg**
[oauth] .. code-block:: ini
auto_login=False
[rtv] [rtv]
# Log file location # Log file location
@@ -184,6 +185,12 @@ Example initial config:
# This may be necessary for compatibility with some terminal browsers # This may be necessary for compatibility with some terminal browsers
# ascii=True # ascii=True
**oauth.cfg**
.. code-block:: ini
[oauth]
auto_login=false
========= =========
Changelog Changelog

View File

@@ -22,7 +22,13 @@ from tornado import ioloop
__all__ = [] __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('~') HOME = os.path.expanduser('~')
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME',
os.path.join(HOME, '.config')) os.path.join(HOME, '.config'))
@@ -35,30 +41,9 @@ def get_config_fp():
# get the first existing config file # get the first existing config file
for config_path in config_paths: for config_path in config_paths:
if os.path.exists(config_path): if os.path.exists(config_path):
config.read(config_path)
break 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 = {} defaults = {}
if config.has_section('rtv'): if config.has_section('rtv'):
defaults = dict(config.items('rtv')) defaults = dict(config.items('rtv'))
@@ -73,7 +58,22 @@ def load_oauth_config():
Attempt to load saved OAuth settings 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'): if config.has_section('oauth'):
defaults = dict(config.items('oauth')) defaults = dict(config.items('oauth'))
@@ -81,7 +81,7 @@ def load_oauth_config():
# Populate OAuth section # Populate OAuth section
config.add_section('oauth') config.add_section('oauth')
config.set('oauth', 'auto_login', 'false') config.set('oauth', 'auto_login', 'false')
with open(get_config_fp(), 'w') as cfg: with open(config_path, 'w') as cfg:
config.write(cfg) config.write(cfg)
defaults = dict(config.items('oauth')) defaults = dict(config.items('oauth'))

View File

@@ -77,8 +77,8 @@ class OAuthTool(object):
os.path.join(HOME, '.config')) os.path.join(HOME, '.config'))
config_paths = [ config_paths = [
os.path.join(XDG_CONFIG_HOME, 'rtv', 'rtv.cfg'), os.path.join(XDG_CONFIG_HOME, 'rtv', 'oauth.cfg'),
os.path.join(HOME, '.rtv') os.path.join(HOME, '.rtv-oauth')
] ]
# get the first existing config file # get the first existing config file