Added support for config file.
This commit is contained in:
18
rtv/main.py
18
rtv/main.py
@@ -4,7 +4,7 @@ import praw
|
|||||||
from requests.exceptions import ConnectionError, HTTPError
|
from requests.exceptions import ConnectionError, HTTPError
|
||||||
|
|
||||||
from rtv.errors import SubmissionURLError, SubredditNameError
|
from rtv.errors import SubmissionURLError, SubredditNameError
|
||||||
from rtv.utils import curses_session
|
from rtv.utils import curses_session, load_config
|
||||||
from rtv.subreddit import SubredditPage
|
from rtv.subreddit import SubredditPage
|
||||||
from rtv.submission import SubmissionPage
|
from rtv.submission import SubmissionPage
|
||||||
|
|
||||||
@@ -29,6 +29,8 @@ Global Commands
|
|||||||
on the page.
|
on the page.
|
||||||
`r` or `F5` : Refresh the current page.
|
`r` or `F5` : Refresh the current page.
|
||||||
`q` : Quit the program.
|
`q` : Quit the program.
|
||||||
|
`o` : Open the url of the selected item in the default web
|
||||||
|
browser.
|
||||||
|
|
||||||
Subreddit Mode
|
Subreddit Mode
|
||||||
Right or `Enter` : Open the currently selected submission in a new page.
|
Right or `Enter` : Open the currently selected submission in a new page.
|
||||||
@@ -49,7 +51,7 @@ def main():
|
|||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog='rtv', description=description, epilog=epilog,
|
prog='rtv', description=description, epilog=epilog,
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
parser.add_argument('-s', dest='subreddit', default='front', help='subreddit name')
|
parser.add_argument('-s', dest='subreddit', help='subreddit name')
|
||||||
parser.add_argument('-l', dest='link', help='full link to a submission')
|
parser.add_argument('-l', dest='link', help='full link to a submission')
|
||||||
|
|
||||||
group = parser.add_argument_group(
|
group = parser.add_argument_group(
|
||||||
@@ -62,8 +64,18 @@ def main():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Try to fill in empty arguments with values from the config.
|
||||||
|
# Command line flags should always take priority!
|
||||||
|
for key, val in load_config().items():
|
||||||
|
if getattr(args, key) is None:
|
||||||
|
setattr(args, key, val)
|
||||||
|
|
||||||
|
if args.subreddit is None:
|
||||||
|
args.subreddit = 'front'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
reddit = praw.Reddit(user_agent='reddit terminal viewer v0.0')
|
# TODO: Move version number to a centralized location
|
||||||
|
reddit = praw.Reddit(user_agent='reddit terminal viewer v1.05a')
|
||||||
reddit.config.decode_html_entities = True
|
reddit.config.decode_html_entities = True
|
||||||
|
|
||||||
if args.username:
|
if args.username:
|
||||||
|
|||||||
21
rtv/utils.py
21
rtv/utils.py
@@ -8,6 +8,9 @@ from contextlib import contextmanager
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
from types import MethodType
|
from types import MethodType
|
||||||
|
|
||||||
|
from six.moves import configparser
|
||||||
|
from six import create_bound_method
|
||||||
|
|
||||||
from .errors import EscapePressed
|
from .errors import EscapePressed
|
||||||
|
|
||||||
ESCAPE = 27
|
ESCAPE = 27
|
||||||
@@ -46,6 +49,22 @@ class Color(object):
|
|||||||
curses.init_pair(index, code[0], code[1])
|
curses.init_pair(index, code[0], code[1])
|
||||||
setattr(cls, attr, curses.color_pair(index))
|
setattr(cls, attr, curses.color_pair(index))
|
||||||
|
|
||||||
|
def load_config():
|
||||||
|
"""
|
||||||
|
Search for a configuration file at the location ~/.rtv and attempt to load
|
||||||
|
saved settings for things like the username and password.
|
||||||
|
"""
|
||||||
|
|
||||||
|
config_path = os.path.join(os.path.expanduser('~'), '.rtv')
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(config_path)
|
||||||
|
|
||||||
|
defaults = {}
|
||||||
|
if config.has_section('rtv'):
|
||||||
|
defaults = dict(config.items('rtv'))
|
||||||
|
|
||||||
|
return defaults
|
||||||
|
|
||||||
def patch_popen():
|
def patch_popen():
|
||||||
"""
|
"""
|
||||||
Patch subprocess.Popen default behavior to redirect stdout + stderr to null.
|
Patch subprocess.Popen default behavior to redirect stdout + stderr to null.
|
||||||
@@ -56,7 +75,7 @@ def patch_popen():
|
|||||||
stdout = open(os.devnull, 'w')
|
stdout = open(os.devnull, 'w')
|
||||||
func = partial(subprocess.Popen.__init__,
|
func = partial(subprocess.Popen.__init__,
|
||||||
stdout=stdout, stderr=stdout, close_fds=True)
|
stdout=stdout, stderr=stdout, close_fds=True)
|
||||||
subprocess.Popen.__init__ = MethodType(func, None, subprocess.Popen)
|
subprocess.Popen.__init__ = create_bound_method(func, subprocess.Popen)
|
||||||
|
|
||||||
def text_input(window):
|
def text_input(window):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user