diff --git a/rtv/main.py b/rtv/main.py index 5770aa5..8f7ccd5 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -4,7 +4,7 @@ import praw from requests.exceptions import ConnectionError, HTTPError 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.submission import SubmissionPage @@ -29,6 +29,8 @@ Global Commands on the page. `r` or `F5` : Refresh the current page. `q` : Quit the program. + `o` : Open the url of the selected item in the default web + browser. Subreddit Mode Right or `Enter` : Open the currently selected submission in a new page. @@ -49,7 +51,7 @@ def main(): parser = argparse.ArgumentParser( prog='rtv', description=description, epilog=epilog, 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') group = parser.add_argument_group( @@ -62,8 +64,18 @@ def main(): 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: - 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 if args.username: diff --git a/rtv/utils.py b/rtv/utils.py index 1c44916..88813db 100644 --- a/rtv/utils.py +++ b/rtv/utils.py @@ -8,6 +8,9 @@ from contextlib import contextmanager from functools import partial from types import MethodType +from six.moves import configparser +from six import create_bound_method + from .errors import EscapePressed ESCAPE = 27 @@ -46,6 +49,22 @@ class Color(object): curses.init_pair(index, code[0], code[1]) 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(): """ Patch subprocess.Popen default behavior to redirect stdout + stderr to null. @@ -56,7 +75,7 @@ def patch_popen(): stdout = open(os.devnull, 'w') func = partial(subprocess.Popen.__init__, 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): """