From 5c68a008c931546ef371015e8ef1d277cc084d28 Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 13:06:17 -0500 Subject: [PATCH 1/9] Basic framework to post submission, doesn't actually submit it yet --- rtv/docs.py | 10 ++++++++++ rtv/subreddit.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index b47a31b..81f4ec9 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -52,3 +52,13 @@ COMMENT_FILE = """ # Replying to {author}'s {type} {content} """ + +SUBMISSION_FILE = """ +# Please enter your submission. Lines starting with '#' will be ignored, +# and an empty field aborts the submission. +# +# The first line will be interpreted as the title +# Following titles will be interpreted as the content +# +# Posting to /r/{name} +""" diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 5dfe990..f57a989 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -1,12 +1,13 @@ import curses - +import time import requests from .exceptions import SubredditError from .page import BasePage, Navigator, BaseController from .submission import SubmissionPage from .content import SubredditContent -from .helpers import clean, open_browser +from .helpers import clean, open_browser, open_editor +from .docs import SUBMISSION_FILE from .curses_helpers import (BULLET, UARROW, DARROW, Color, LoadScreen, text_input, show_notification) @@ -89,6 +90,37 @@ class SubredditPage(BasePage): global opened_links opened_links.add(url) + @SubredditController.register('p') + def post_submission(self): + # Abort if user isn't logged in + if not self.reddit.is_logged_in(): + show_notification(self.stdscr, ["Login to reply"]) + return + + name = self.content.name + if '+' in name: + show_notification(self.stdscr, ['Can\'t post to a multireddit']) + return + + # Open the submission window + submission_info = SUBMISSION_FILE.format(name=name) + curses.endwin() + submission_text = open_editor(submission_info) + curses.doupdate() + + # Abort if there is no content + if not submission_text: + curses.flash() + return + try: + title, content = submission_text.split('\n', 1) + show_notification(self.stdscr, [title]) + except praw.errors.APIException as e: + show_notification(self.stdscr, [e.message]) + else: + time.sleep(0.5) + self.refresh_content() + @staticmethod def draw_item(win, data, inverted=False): From 19aea61d4e6ba86d1ff79c54b88cac61fb7b1244 Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 13:07:14 -0500 Subject: [PATCH 2/9] typo --- rtv/docs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtv/docs.py b/rtv/docs.py index 81f4ec9..13ab5a4 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -58,7 +58,7 @@ SUBMISSION_FILE = """ # and an empty field aborts the submission. # # The first line will be interpreted as the title -# Following titles will be interpreted as the content +# Following lines will be interpreted as the content # # Posting to /r/{name} """ From e3db8d4ee4eaadfbfb7f2d1c8bdfb8b752df5e8c Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 13:20:18 -0500 Subject: [PATCH 3/9] properly strip subreddit name and post submission --- rtv/subreddit.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rtv/subreddit.py b/rtv/subreddit.py index f57a989..4559476 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -1,6 +1,7 @@ import curses import time import requests +import praw from .exceptions import SubredditError from .page import BasePage, Navigator, BaseController @@ -102,6 +103,10 @@ class SubredditPage(BasePage): show_notification(self.stdscr, ['Can\'t post to a multireddit']) return + name = name.strip(' /') # Strip leading and trailing backslashes + if name.startswith('r/'): + name = name[2:] + # Open the submission window submission_info = SUBMISSION_FILE.format(name=name) curses.endwin() @@ -114,7 +119,7 @@ class SubredditPage(BasePage): return try: title, content = submission_text.split('\n', 1) - show_notification(self.stdscr, [title]) + self.reddit.submit(name, title, text=content) except praw.errors.APIException as e: show_notification(self.stdscr, [e.message]) else: From feffe83e060491818d134bb0f01f6c403efa3d93 Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 15:07:57 -0500 Subject: [PATCH 4/9] Disallow posting to /r/all, /r/front. Use subreddit instead of name --- rtv/docs.py | 2 +- rtv/subreddit.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index 13ab5a4..fe691e3 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -60,5 +60,5 @@ SUBMISSION_FILE = """ # The first line will be interpreted as the title # Following lines will be interpreted as the content # -# Posting to /r/{name} +# Posting to {name} """ diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 4559476..7f0a745 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -95,20 +95,20 @@ class SubredditPage(BasePage): def post_submission(self): # Abort if user isn't logged in if not self.reddit.is_logged_in(): - show_notification(self.stdscr, ["Login to reply"]) + show_notification(self.stdscr, ['Login to reply']) return - name = self.content.name - if '+' in name: - show_notification(self.stdscr, ['Can\'t post to a multireddit']) - return + subreddit = self.reddit.get_subreddit(self.content.name) - name = name.strip(' /') # Strip leading and trailing backslashes - if name.startswith('r/'): - name = name[2:] + # Make sure it is a valid subreddit for submission + sub = str(subreddit) + if '+' in sub or sub == '/r/all' or sub == '/r/front': + message = 'Can\'t post to {0}'.format(sub) + show_notification(self.stdscr, [message]) + return # Open the submission window - submission_info = SUBMISSION_FILE.format(name=name) + submission_info = SUBMISSION_FILE.format(name=sub) curses.endwin() submission_text = open_editor(submission_info) curses.doupdate() @@ -119,7 +119,7 @@ class SubredditPage(BasePage): return try: title, content = submission_text.split('\n', 1) - self.reddit.submit(name, title, text=content) + self.reddit.submit(subreddit, title, text=content) except praw.errors.APIException as e: show_notification(self.stdscr, [e.message]) else: From 26d4d247b0acc54accb25de06c94f63a40ded264 Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 16:27:42 -0500 Subject: [PATCH 5/9] fix subreddit name when user tries to post to /new etc. --- rtv/docs.py | 2 +- rtv/subreddit.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index fe691e3..13ab5a4 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -60,5 +60,5 @@ SUBMISSION_FILE = """ # The first line will be interpreted as the title # Following lines will be interpreted as the content # -# Posting to {name} +# Posting to /r/{name} """ diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 7f0a745..5003b6a 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -101,9 +101,10 @@ class SubredditPage(BasePage): subreddit = self.reddit.get_subreddit(self.content.name) # Make sure it is a valid subreddit for submission - sub = str(subreddit) - if '+' in sub or sub == '/r/all' or sub == '/r/front': - message = 'Can\'t post to {0}'.format(sub) + # Strips the subreddit to just the name + sub = str(subreddit).split('/')[2] + if '+' in sub or sub == 'all' or sub == 'front': + message = 'Can\'t post to /r/{0}'.format(sub) show_notification(self.stdscr, [message]) return @@ -119,7 +120,7 @@ class SubredditPage(BasePage): return try: title, content = submission_text.split('\n', 1) - self.reddit.submit(subreddit, title, text=content) + self.reddit.submit(sub, title, text=content) except praw.errors.APIException as e: show_notification(self.stdscr, [e.message]) else: From 49977151d1f0e3ca6b14eb1e63bf7380021c76ce Mon Sep 17 00:00:00 2001 From: Tobin Date: Wed, 1 Apr 2015 21:26:12 -0500 Subject: [PATCH 6/9] Make sure the post has content, show dialog if not --- rtv/subreddit.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 5003b6a..beacc0d 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -123,6 +123,8 @@ class SubredditPage(BasePage): self.reddit.submit(sub, title, text=content) except praw.errors.APIException as e: show_notification(self.stdscr, [e.message]) + except ValueError: + show_notification(self.stdscr, ['No post content! Post aborted.']) else: time.sleep(0.5) self.refresh_content() From 66bc9f56672328fcd91bd7aec0d87ef57d0485e5 Mon Sep 17 00:00:00 2001 From: Tobin Date: Thu, 2 Apr 2015 22:58:27 -0500 Subject: [PATCH 7/9] toggle hidden comment with arrow left or h --- rtv/content.py | 4 ++++ rtv/submission.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rtv/content.py b/rtv/content.py index 5a1f872..302ebea 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -229,6 +229,10 @@ class SubmissionContent(BaseContent): else: raise ValueError('% type not recognized' % data['type']) + def is_hidden_comment(self, index): + data = self.get(index) + return data['type'] == 'HiddenComment' + class SubredditContent(BaseContent): diff --git a/rtv/submission.py b/rtv/submission.py index 43899d6..f8bfa95 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -53,7 +53,11 @@ class SubmissionPage(BasePage): @SubmissionController.register(curses.KEY_LEFT, 'h') def exit_submission(self): - self.active = False + index = self.nav.absolute_index + if self.content.is_hidden_comment(index): + self.content.toggle(index) + else: + self.active = False @SubmissionController.register(curses.KEY_F5, 'r') def refresh_content(self): From a294010e27568460f65d556709a1cfb1c5df5dc5 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Fri, 3 Apr 2015 12:46:05 +0100 Subject: [PATCH 8/9] Read config from XDG_CONFIG_HOME (Fix #65) Config file is read from the first of these two locations: * $XDG_CONFIG_HOME/rtv/rtv.cfg * ~/.rtv --- README.rst | 4 +--- rtv/__main__.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index c6e5a5f..e5d8c02 100644 --- a/README.rst +++ b/README.rst @@ -91,14 +91,12 @@ In submission mode you can view the self text for a submission and browse commen Configuration ------------- -RTV will read a configuration file located at **~/.rtv**. +RTV will read a configuration file located at ``$XDG_CONFIG_HOME/rtv/rtv.cfg`` or ``~/.config/rtv/rtv.cfg`` if ``$XDG_CONFIG_HOME`` is not set. This can be used to avoid having to re-enter login credentials every time the program is launched. Each line in the file will replace the corresponding default argument in the launch script. Example config: -**~/.rtv** - .. code-block:: ini [rtv] diff --git a/rtv/__main__.py b/rtv/__main__.py index 44b5bd8..c571874 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -26,9 +26,20 @@ def load_config(): 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) + + 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', 'rtv.cfg'), + os.path.join(HOME, '.rtv') + ] + + # read only the first existing config file + for config_path in config_paths: + if os.path.exists(config_path): + config.read(config_path) + break defaults = {} if config.has_section('rtv'): From db7f2f9fd7d33a175e9f864f2c37f213c5d94faf Mon Sep 17 00:00:00 2001 From: Tobin Date: Fri, 3 Apr 2015 23:31:47 -0500 Subject: [PATCH 9/9] Revert "toggle hidden comment with arrow left or h" This reverts commit 66bc9f56672328fcd91bd7aec0d87ef57d0485e5. --- rtv/content.py | 4 ---- rtv/submission.py | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index b1ef638..8d3a501 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -229,10 +229,6 @@ class SubmissionContent(BaseContent): else: raise ValueError('% type not recognized' % data['type']) - def is_hidden_comment(self, index): - data = self.get(index) - return data['type'] == 'HiddenComment' - class SubredditContent(BaseContent): diff --git a/rtv/submission.py b/rtv/submission.py index ee7aa63..461fc67 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -53,11 +53,7 @@ class SubmissionPage(BasePage): @SubmissionController.register(curses.KEY_LEFT, 'h') def exit_submission(self): - index = self.nav.absolute_index - if self.content.is_hidden_comment(index): - self.content.toggle(index) - else: - self.active = False + self.active = False @SubmissionController.register(curses.KEY_F5, 'r') def refresh_content(self):