Streamlining
This commit is contained in:
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
from datetime import datetime
|
||||
from itertools import groupby
|
||||
|
||||
import six
|
||||
import praw
|
||||
@@ -182,22 +181,23 @@ class Content(object):
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def strip_praw_reddit(reddit):
|
||||
def strip_praw_subscription(subscription):
|
||||
"""
|
||||
Parse through a reddit object and return a dict with data ready to be
|
||||
Parse through a subscription and return a dict with data ready to be
|
||||
displayed through the terminal.
|
||||
"""
|
||||
|
||||
data = {}
|
||||
data['object'] = reddit
|
||||
if isinstance(reddit, praw.objects.Subreddit):
|
||||
data['type'] = 'Subreddit'
|
||||
data['name'] = "/r/" + reddit.display_name
|
||||
data['title'] = reddit.title
|
||||
elif isinstance(reddit, praw.objects.Multireddit):
|
||||
data['object'] = subscription
|
||||
if isinstance(subscription, praw.objects.Multireddit):
|
||||
data['type'] = 'Multireddit'
|
||||
data['name'] = reddit.path
|
||||
data['title'] = reddit.description_md
|
||||
data['name'] = subscription.path
|
||||
data['title'] = subscription.description_md
|
||||
else:
|
||||
data['type'] = 'Subscription'
|
||||
data['name'] = "/r/" + subscription.display_name
|
||||
data['title'] = subscription.title
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
@@ -526,46 +526,57 @@ class SubredditContent(Content):
|
||||
|
||||
class SubscriptionContent(Content):
|
||||
|
||||
def __init__(self, name, reddits, loader):
|
||||
def __init__(self, name, subscriptions, loader):
|
||||
|
||||
self.name = name
|
||||
self.order = None
|
||||
self._loader = loader
|
||||
self._reddits = reddits
|
||||
self._reddit_data = []
|
||||
self._subscriptions = subscriptions
|
||||
self._subscription_data = []
|
||||
|
||||
try:
|
||||
self.get(0)
|
||||
except IndexError:
|
||||
raise exceptions.SubscriptionError('No {}'.format(self.name))
|
||||
raise exceptions.SubscriptionError('No content')
|
||||
|
||||
@classmethod
|
||||
def from_func(cls, name, func, loader):
|
||||
reddits = (r for r in func())
|
||||
return cls(name, reddits, loader)
|
||||
def from_user(cls, reddit, loader, content_type='subreddit'):
|
||||
if content_type == 'subreddit':
|
||||
name = 'My Subreddits'
|
||||
items = reddit.get_my_subreddits(limit=None)
|
||||
elif content_type == 'multireddit':
|
||||
name = 'My Multireddits'
|
||||
items = reddit.get_my_multireddits(limit=None)
|
||||
elif content_type == 'popular':
|
||||
name = 'Popular Subreddits'
|
||||
items = reddit.get_popular_subreddits(limit=None)
|
||||
else:
|
||||
raise exceptions.SubscriptionError('Invalid type %s', content_type)
|
||||
|
||||
return cls(name, items, loader)
|
||||
|
||||
def get(self, index, n_cols=70):
|
||||
"""
|
||||
Grab the `i`th reddit, with the title field formatted to fit
|
||||
Grab the `i`th object, with the title field formatted to fit
|
||||
inside of a window of width `n_cols`
|
||||
"""
|
||||
|
||||
if index < 0:
|
||||
raise IndexError
|
||||
|
||||
while index >= len(self._reddit_data):
|
||||
while index >= len(self._subscription_data):
|
||||
try:
|
||||
with self._loader('Loading {}'.format(self.name)):
|
||||
reddit = next(self._reddits)
|
||||
with self._loader('Loading content'):
|
||||
subscription = next(self._subscriptions)
|
||||
if self._loader.exception:
|
||||
raise IndexError
|
||||
except StopIteration:
|
||||
raise IndexError
|
||||
else:
|
||||
data = self.strip_praw_reddit(reddit)
|
||||
self._reddit_data.append(data)
|
||||
data = self.strip_praw_subscription(subscription)
|
||||
self._subscription_data.append(data)
|
||||
|
||||
data = self._reddit_data[index]
|
||||
data = self._subscription_data[index]
|
||||
data['split_title'] = self.wrap_text(data['title'], width=n_cols)
|
||||
data['n_rows'] = len(data['split_title']) + 1
|
||||
data['offset'] = 0
|
||||
|
||||
@@ -33,8 +33,8 @@ HELP = """
|
||||
`e` : Edit an existing post or comment
|
||||
`d` : Delete an existing post or comment
|
||||
`i` : Display new messages prompt
|
||||
`s` : Open/close subscribed subreddits list
|
||||
`S` : Open/close subscribed multireddits list
|
||||
`s` : Open subscribed subreddits
|
||||
`S` : Open subscribed multireddits
|
||||
|
||||
[Subreddit Mode]
|
||||
`l` or `RIGHT` : Enter the selected submission
|
||||
|
||||
@@ -69,7 +69,7 @@ class SubredditPage(Page):
|
||||
def prompt_subreddit(self):
|
||||
"Open a prompt to navigate to a different subreddit"
|
||||
|
||||
name = self.term.prompt_input('Enter reddit page: ')
|
||||
name = self.term.prompt_input('Enter page: /')
|
||||
if name is not None:
|
||||
self.refresh_content(order='ignore', name=name)
|
||||
|
||||
@@ -156,10 +156,9 @@ class SubredditPage(Page):
|
||||
def open_subscriptions(self):
|
||||
"Open user subscriptions page"
|
||||
|
||||
func = lambda : self.reddit.get_my_subreddits(limit=None)
|
||||
with self.term.loader('Loading subscriptions'):
|
||||
page = SubscriptionPage(self.reddit, 'My Subscriptions', func,
|
||||
self.term, self.config, self.oauth)
|
||||
page = SubscriptionPage(self.reddit, self.term, self.config,
|
||||
self.oauth)
|
||||
if self.term.loader.exception:
|
||||
return
|
||||
|
||||
@@ -167,19 +166,18 @@ class SubredditPage(Page):
|
||||
|
||||
# When the user has chosen a subreddit in the subscriptions list,
|
||||
# refresh content with the selected subreddit
|
||||
if page.reddit_data is not None:
|
||||
if page.subreddit_data is not None:
|
||||
self.refresh_content(order='ignore',
|
||||
name=page.reddit_data['name'])
|
||||
name=page.subreddit_data['name'])
|
||||
|
||||
@SubredditController.register(Command('MULTIREDDIT_OPEN_SUBSCRIPTIONS'))
|
||||
@logged_in
|
||||
def open_multireddit_subscriptions(self):
|
||||
"Open user multireddit subscriptions page"
|
||||
|
||||
func = lambda : self.reddit.get_my_multireddits()
|
||||
with self.term.loader('Loading multireddits'):
|
||||
page = SubscriptionPage(self.reddit,
|
||||
'My Multireddits', func, self.term, self.config, self.oauth)
|
||||
page = SubscriptionPage(self.reddit, self.term, self.config,
|
||||
self.oauth, content_type='multireddit')
|
||||
if self.term.loader.exception:
|
||||
return
|
||||
|
||||
@@ -189,7 +187,7 @@ class SubredditPage(Page):
|
||||
# refresh content with the selected subreddit
|
||||
if page.reddit_data is not None:
|
||||
self.refresh_content(order='ignore',
|
||||
name=page.reddit_data['name'])
|
||||
name=page.subreddit_data['name'])
|
||||
|
||||
def _draw_item(self, win, data, inverted):
|
||||
|
||||
|
||||
@@ -14,36 +14,36 @@ class SubscriptionController(PageController):
|
||||
|
||||
class SubscriptionPage(Page):
|
||||
|
||||
def __init__(self, reddit, name, func, term, config, oauth):
|
||||
def __init__(self, reddit, term, config, oauth, content_type='subreddit'):
|
||||
super(SubscriptionPage, self).__init__(reddit, term, config, oauth)
|
||||
|
||||
self.controller = SubscriptionController(self, keymap=config.keymap)
|
||||
self.name = name
|
||||
self.func = func
|
||||
self.content = SubscriptionContent.from_func(name, func, term.loader)
|
||||
self.content = SubscriptionContent.from_user(reddit, term.loader,
|
||||
content_type)
|
||||
self.nav = Navigator(self.content.get)
|
||||
self.reddit_data = None
|
||||
self.content_type = content_type
|
||||
self.subreddit_data = None
|
||||
|
||||
@SubscriptionController.register(Command('REFRESH'))
|
||||
def refresh_content(self, order=None, name=None):
|
||||
"Re-download all reddits and reset the page index"
|
||||
"Re-download all subscriptions and reset the page index"
|
||||
|
||||
# reddit listings does not support sorting by order
|
||||
# reddit.get_my_subreddits() does not support sorting by order
|
||||
if order:
|
||||
self.term.flash()
|
||||
return
|
||||
|
||||
with self.term.loader():
|
||||
self.content = SubscriptionContent.from_func(
|
||||
self.name, self.func, self.term.loader)
|
||||
self.content = SubscriptionContent.from_user(
|
||||
self.reddit, self.term.loader, self.content_type)
|
||||
if not self.term.loader.exception:
|
||||
self.nav = Navigator(self.content.get)
|
||||
|
||||
@SubscriptionController.register(Command('SUBSCRIPTION_SELECT'))
|
||||
def select_reddit(self):
|
||||
"Store the selected reddit and return to the subreddit page"
|
||||
def select_subreddit(self):
|
||||
"Store the selected subreddit and return to the subreddit page"
|
||||
|
||||
self.reddit_data = self.content.get(self.nav.absolute_index)
|
||||
self.subreddit_data = self.content.get(self.nav.absolute_index)
|
||||
self.active = False
|
||||
|
||||
@SubscriptionController.register(Command('SUBSCRIPTION_EXIT'))
|
||||
|
||||
Reference in New Issue
Block a user