Streamlining

This commit is contained in:
Michael Lazar
2016-07-19 01:58:03 -07:00
parent 6094b45029
commit 1174514632
4 changed files with 58 additions and 49 deletions

View File

@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import re import re
from datetime import datetime from datetime import datetime
from itertools import groupby
import six import six
import praw import praw
@@ -182,22 +181,23 @@ class Content(object):
return data return data
@staticmethod @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. displayed through the terminal.
""" """
data = {} data = {}
data['object'] = reddit data['object'] = subscription
if isinstance(reddit, praw.objects.Subreddit): if isinstance(subscription, praw.objects.Multireddit):
data['type'] = 'Subreddit'
data['name'] = "/r/" + reddit.display_name
data['title'] = reddit.title
elif isinstance(reddit, praw.objects.Multireddit):
data['type'] = 'Multireddit' data['type'] = 'Multireddit'
data['name'] = reddit.path data['name'] = subscription.path
data['title'] = reddit.description_md data['title'] = subscription.description_md
else:
data['type'] = 'Subscription'
data['name'] = "/r/" + subscription.display_name
data['title'] = subscription.title
return data return data
@staticmethod @staticmethod
@@ -526,46 +526,57 @@ class SubredditContent(Content):
class SubscriptionContent(Content): class SubscriptionContent(Content):
def __init__(self, name, reddits, loader): def __init__(self, name, subscriptions, loader):
self.name = name self.name = name
self.order = None self.order = None
self._loader = loader self._loader = loader
self._reddits = reddits self._subscriptions = subscriptions
self._reddit_data = [] self._subscription_data = []
try: try:
self.get(0) self.get(0)
except IndexError: except IndexError:
raise exceptions.SubscriptionError('No {}'.format(self.name)) raise exceptions.SubscriptionError('No content')
@classmethod @classmethod
def from_func(cls, name, func, loader): def from_user(cls, reddit, loader, content_type='subreddit'):
reddits = (r for r in func()) if content_type == 'subreddit':
return cls(name, reddits, loader) 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): 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` inside of a window of width `n_cols`
""" """
if index < 0: if index < 0:
raise IndexError raise IndexError
while index >= len(self._reddit_data): while index >= len(self._subscription_data):
try: try:
with self._loader('Loading {}'.format(self.name)): with self._loader('Loading content'):
reddit = next(self._reddits) subscription = next(self._subscriptions)
if self._loader.exception: if self._loader.exception:
raise IndexError raise IndexError
except StopIteration: except StopIteration:
raise IndexError raise IndexError
else: else:
data = self.strip_praw_reddit(reddit) data = self.strip_praw_subscription(subscription)
self._reddit_data.append(data) 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['split_title'] = self.wrap_text(data['title'], width=n_cols)
data['n_rows'] = len(data['split_title']) + 1 data['n_rows'] = len(data['split_title']) + 1
data['offset'] = 0 data['offset'] = 0

View File

@@ -33,8 +33,8 @@ HELP = """
`e` : Edit an existing post or comment `e` : Edit an existing post or comment
`d` : Delete an existing post or comment `d` : Delete an existing post or comment
`i` : Display new messages prompt `i` : Display new messages prompt
`s` : Open/close subscribed subreddits list `s` : Open subscribed subreddits
`S` : Open/close subscribed multireddits list `S` : Open subscribed multireddits
[Subreddit Mode] [Subreddit Mode]
`l` or `RIGHT` : Enter the selected submission `l` or `RIGHT` : Enter the selected submission

View File

@@ -69,7 +69,7 @@ class SubredditPage(Page):
def prompt_subreddit(self): def prompt_subreddit(self):
"Open a prompt to navigate to a different subreddit" "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: if name is not None:
self.refresh_content(order='ignore', name=name) self.refresh_content(order='ignore', name=name)
@@ -156,10 +156,9 @@ class SubredditPage(Page):
def open_subscriptions(self): def open_subscriptions(self):
"Open user subscriptions page" "Open user subscriptions page"
func = lambda : self.reddit.get_my_subreddits(limit=None)
with self.term.loader('Loading subscriptions'): with self.term.loader('Loading subscriptions'):
page = SubscriptionPage(self.reddit, 'My Subscriptions', func, page = SubscriptionPage(self.reddit, self.term, self.config,
self.term, self.config, self.oauth) self.oauth)
if self.term.loader.exception: if self.term.loader.exception:
return return
@@ -167,19 +166,18 @@ class SubredditPage(Page):
# When the user has chosen a subreddit in the subscriptions list, # When the user has chosen a subreddit in the subscriptions list,
# refresh content with the selected subreddit # 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', self.refresh_content(order='ignore',
name=page.reddit_data['name']) name=page.subreddit_data['name'])
@SubredditController.register(Command('MULTIREDDIT_OPEN_SUBSCRIPTIONS')) @SubredditController.register(Command('MULTIREDDIT_OPEN_SUBSCRIPTIONS'))
@logged_in @logged_in
def open_multireddit_subscriptions(self): def open_multireddit_subscriptions(self):
"Open user multireddit subscriptions page" "Open user multireddit subscriptions page"
func = lambda : self.reddit.get_my_multireddits()
with self.term.loader('Loading multireddits'): with self.term.loader('Loading multireddits'):
page = SubscriptionPage(self.reddit, page = SubscriptionPage(self.reddit, self.term, self.config,
'My Multireddits', func, self.term, self.config, self.oauth) self.oauth, content_type='multireddit')
if self.term.loader.exception: if self.term.loader.exception:
return return
@@ -189,7 +187,7 @@ class SubredditPage(Page):
# refresh content with the selected subreddit # refresh content with the selected subreddit
if page.reddit_data is not None: if page.reddit_data is not None:
self.refresh_content(order='ignore', self.refresh_content(order='ignore',
name=page.reddit_data['name']) name=page.subreddit_data['name'])
def _draw_item(self, win, data, inverted): def _draw_item(self, win, data, inverted):

View File

@@ -14,36 +14,36 @@ class SubscriptionController(PageController):
class SubscriptionPage(Page): 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) super(SubscriptionPage, self).__init__(reddit, term, config, oauth)
self.controller = SubscriptionController(self, keymap=config.keymap) self.controller = SubscriptionController(self, keymap=config.keymap)
self.name = name self.content = SubscriptionContent.from_user(reddit, term.loader,
self.func = func content_type)
self.content = SubscriptionContent.from_func(name, func, term.loader)
self.nav = Navigator(self.content.get) self.nav = Navigator(self.content.get)
self.reddit_data = None self.content_type = content_type
self.subreddit_data = None
@SubscriptionController.register(Command('REFRESH')) @SubscriptionController.register(Command('REFRESH'))
def refresh_content(self, order=None, name=None): 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: if order:
self.term.flash() self.term.flash()
return return
with self.term.loader(): with self.term.loader():
self.content = SubscriptionContent.from_func( self.content = SubscriptionContent.from_user(
self.name, self.func, self.term.loader) self.reddit, self.term.loader, self.content_type)
if not self.term.loader.exception: if not self.term.loader.exception:
self.nav = Navigator(self.content.get) self.nav = Navigator(self.content.get)
@SubscriptionController.register(Command('SUBSCRIPTION_SELECT')) @SubscriptionController.register(Command('SUBSCRIPTION_SELECT'))
def select_reddit(self): def select_subreddit(self):
"Store the selected reddit and return to the subreddit page" "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 self.active = False
@SubscriptionController.register(Command('SUBSCRIPTION_EXIT')) @SubscriptionController.register(Command('SUBSCRIPTION_EXIT'))