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
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