Merge pull request #53 from Brobin/master
Fixed style to conform with PEP8
This commit is contained in:
@@ -18,6 +18,7 @@ from .docs import *
|
||||
|
||||
__all__ = []
|
||||
|
||||
|
||||
def load_config():
|
||||
"""
|
||||
Search for a configuration file at the location ~/.rtv and attempt to load
|
||||
@@ -34,6 +35,7 @@ def load_config():
|
||||
|
||||
return defaults
|
||||
|
||||
|
||||
def command_line():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -56,6 +58,7 @@ def command_line():
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
"Main entry point"
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ from .helpers import humanize_timestamp, wrap_text, strip_subreddit_url
|
||||
|
||||
__all__ = ['SubredditContent', 'SubmissionContent']
|
||||
|
||||
|
||||
class BaseContent(object):
|
||||
|
||||
def get(self, index, n_cols):
|
||||
@@ -40,7 +41,8 @@ class BaseContent(object):
|
||||
retval = []
|
||||
while stack:
|
||||
item = stack.pop(0)
|
||||
if isinstance(item, praw.objects.MoreComments) and (item.count==0):
|
||||
if isinstance(item, praw.objects.MoreComments) and (
|
||||
item.count == 0):
|
||||
continue
|
||||
nested = getattr(item, 'replies', None)
|
||||
if nested:
|
||||
@@ -70,9 +72,12 @@ class BaseContent(object):
|
||||
data['body'] = comment.body
|
||||
data['created'] = humanize_timestamp(comment.created_utc)
|
||||
data['score'] = '{} pts'.format(comment.score)
|
||||
data['author'] = (comment.author.name if getattr(comment, 'author') else '[deleted]')
|
||||
data['is_author'] = (data['author'] == getattr(comment.submission, 'author'))
|
||||
data['flair'] = (comment.author_flair_text if comment.author_flair_text else '')
|
||||
author = getattr(comment, 'author')
|
||||
data['author'] = (author.name if author else '[deleted]')
|
||||
sub_author = getattr(comment.submission, 'author')
|
||||
data['is_author'] = (data['author'] == sub_author)
|
||||
flair = comment.author_flair_text
|
||||
data['flair'] = (flair if flair else '')
|
||||
data['likes'] = comment.likes
|
||||
|
||||
return data
|
||||
@@ -94,7 +99,8 @@ class BaseContent(object):
|
||||
data['created'] = humanize_timestamp(sub.created_utc)
|
||||
data['comments'] = '{} comments'.format(sub.num_comments)
|
||||
data['score'] = '{} pts'.format(sub.score)
|
||||
data['author'] = (sub.author.name if getattr(sub, 'author') else '[deleted]')
|
||||
author = getattr(sub, 'author')
|
||||
data['author'] = (author.name if author else '[deleted]')
|
||||
data['permalink'] = sub.permalink
|
||||
data['subreddit'] = strip_subreddit_url(sub.permalink)
|
||||
data['flair'] = (sub.link_flair_text if sub.link_flair_text else '')
|
||||
@@ -104,7 +110,9 @@ class BaseContent(object):
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class SubmissionContent(BaseContent):
|
||||
|
||||
"""
|
||||
Grab a submission from PRAW and lazily store comments to an internal
|
||||
list for repeat access.
|
||||
@@ -155,7 +163,8 @@ class SubmissionContent(BaseContent):
|
||||
|
||||
elif index == -1:
|
||||
data = self._submission_data
|
||||
data['split_title'] = textwrap.wrap(data['title'], width=n_cols-2)
|
||||
data['split_title'] = textwrap.wrap(data['title'],
|
||||
width=n_cols -2)
|
||||
data['split_text'] = wrap_text(data['text'], width=n_cols - 2)
|
||||
data['n_rows'] = len(data['split_title']) + len(data['split_text']) + 5
|
||||
data['offset'] = 0
|
||||
@@ -222,6 +231,7 @@ class SubmissionContent(BaseContent):
|
||||
|
||||
|
||||
class SubredditContent(BaseContent):
|
||||
|
||||
"""
|
||||
Grabs a subreddit from PRAW and lazily stores submissions to an internal
|
||||
list for repeat access.
|
||||
|
||||
@@ -23,6 +23,7 @@ UARROW = u'\u25b2'.encode('utf-8')
|
||||
DARROW = u'\u25bc'.encode('utf-8')
|
||||
BULLET = u'\u2022'.encode('utf-8')
|
||||
|
||||
|
||||
def show_notification(stdscr, message):
|
||||
"""
|
||||
Overlay a message box on the center of the screen and wait for user input.
|
||||
@@ -57,13 +58,16 @@ def show_notification(stdscr, message):
|
||||
window = None
|
||||
stdscr.refresh()
|
||||
|
||||
|
||||
def show_help(stdscr):
|
||||
"""
|
||||
Overlay a message box with the help screen.
|
||||
"""
|
||||
show_notification(stdscr, HELP.split("\n"))
|
||||
|
||||
|
||||
class LoadScreen(object):
|
||||
|
||||
"""
|
||||
Display a loading dialog while waiting for a blocking action to complete.
|
||||
|
||||
@@ -145,7 +149,9 @@ class LoadScreen(object):
|
||||
window.refresh()
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
class Color(object):
|
||||
|
||||
"""
|
||||
Color attributes for curses.
|
||||
"""
|
||||
@@ -182,6 +188,7 @@ class Color(object):
|
||||
levels = [cls.MAGENTA, cls.CYAN, cls.GREEN, cls.YELLOW]
|
||||
return levels[level % len(levels)]
|
||||
|
||||
|
||||
def text_input(window, allow_resize=True):
|
||||
"""
|
||||
Transform a window into a text box that will accept user input and loop
|
||||
@@ -223,6 +230,7 @@ def text_input(window, allow_resize=True):
|
||||
curses.curs_set(0)
|
||||
return strip_textpad(out)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def curses_session():
|
||||
"""
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
class SubmissionError(Exception):
|
||||
"Submission could not be loaded"
|
||||
"""Submission could not be loaded"""
|
||||
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
|
||||
|
||||
class SubredditError(Exception):
|
||||
"Subreddit could not be reached"
|
||||
"""Subreddit could not be reached"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
class ProgramError(Exception):
|
||||
"Problem executing an external program"
|
||||
"""Problem executing an external program"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
class EscapeInterrupt(Exception):
|
||||
"Signal that the ESC key has been pressed"
|
||||
"""Signal that the ESC key has been pressed"""
|
||||
|
||||
@@ -11,6 +11,7 @@ from .exceptions import ProgramError
|
||||
__all__ = ['open_browser', 'clean', 'wrap_text', 'strip_textpad',
|
||||
'strip_subreddit_url', 'humanize_timestamp', 'open_editor']
|
||||
|
||||
|
||||
def open_editor(data=''):
|
||||
"""
|
||||
Open a temporary file using the system's default editor.
|
||||
@@ -39,6 +40,7 @@ def open_editor(data=''):
|
||||
|
||||
return text
|
||||
|
||||
|
||||
def open_browser(url):
|
||||
"""
|
||||
Call webbrowser.open_new_tab(url) and redirect stdout/stderr to devnull.
|
||||
@@ -52,6 +54,7 @@ def open_browser(url):
|
||||
with open(os.devnull, 'ab+', 0) as null:
|
||||
subprocess.check_call(args, stdout=null, stderr=null)
|
||||
|
||||
|
||||
def clean(string):
|
||||
"""
|
||||
Required reading!
|
||||
@@ -75,6 +78,7 @@ def clean(string):
|
||||
string = string.encode(encoding, 'replace')
|
||||
return string
|
||||
|
||||
|
||||
def wrap_text(text, width):
|
||||
"""
|
||||
Wrap text paragraphs to the given character width while preserving newlines.
|
||||
@@ -87,6 +91,7 @@ def wrap_text(text, width):
|
||||
out.extend(lines)
|
||||
return out
|
||||
|
||||
|
||||
def strip_textpad(text):
|
||||
"""
|
||||
Attempt to intelligently strip excess whitespace from the output of a
|
||||
@@ -121,6 +126,7 @@ def strip_textpad(text):
|
||||
out = '\n'.join(stack)
|
||||
return out
|
||||
|
||||
|
||||
def strip_subreddit_url(permalink):
|
||||
"""
|
||||
Strip a subreddit name from the subreddit's permalink.
|
||||
@@ -131,6 +137,7 @@ def strip_subreddit_url(permalink):
|
||||
subreddit = permalink.split('/')[4]
|
||||
return '/r/{}'.format(subreddit)
|
||||
|
||||
|
||||
def humanize_timestamp(utc_timestamp, verbose=False):
|
||||
"""
|
||||
Convert a utc timestamp into a human readable relative-time.
|
||||
|
||||
13
rtv/page.py
13
rtv/page.py
@@ -9,7 +9,9 @@ from .curses_helpers import Color, show_notification, show_help
|
||||
|
||||
__all__ = ['Navigator']
|
||||
|
||||
|
||||
class Navigator(object):
|
||||
|
||||
"""
|
||||
Handles math behind cursor movement and screen paging.
|
||||
"""
|
||||
@@ -73,7 +75,8 @@ class Navigator(object):
|
||||
else:
|
||||
self.page_index -= self.step
|
||||
if self._is_valid(self.absolute_index):
|
||||
# We have reached the beginning of the page - move the index
|
||||
# We have reached the beginning of the page - move the
|
||||
# index
|
||||
redraw = True
|
||||
else:
|
||||
self.page_index += self.step
|
||||
@@ -99,6 +102,7 @@ class Navigator(object):
|
||||
|
||||
|
||||
class BaseController(object):
|
||||
|
||||
"""
|
||||
Event handler for triggering functions with curses keypresses.
|
||||
|
||||
@@ -148,6 +152,7 @@ class BaseController(object):
|
||||
|
||||
|
||||
class BasePage(object):
|
||||
|
||||
"""
|
||||
Base terminal viewer incorperates a cursor to navigate content
|
||||
"""
|
||||
@@ -254,7 +259,8 @@ class BasePage(object):
|
||||
if self.reddit.user is not None:
|
||||
username = self.reddit.user.name
|
||||
s_col = (n_cols - len(username) - 1)
|
||||
# Only print the username if it fits in the empty space on the right
|
||||
# Only print the username if it fits in the empty space on the
|
||||
# right
|
||||
if (s_col - 1) >= len(sub_name):
|
||||
n = (n_cols - s_col - 1)
|
||||
self._header_window.addnstr(0, s_col, clean(username), n)
|
||||
@@ -313,7 +319,8 @@ class BasePage(object):
|
||||
self._remove_cursor()
|
||||
|
||||
valid, redraw = self.nav.move(direction, len(self._subwindows))
|
||||
if not valid: curses.flash()
|
||||
if not valid:
|
||||
curses.flash()
|
||||
|
||||
# Note: ACS_VLINE doesn't like changing the attribute, so always redraw.
|
||||
# if redraw: self._draw_content()
|
||||
|
||||
@@ -58,7 +58,10 @@ class SubmissionPage(BasePage):
|
||||
@SubmissionController.register(curses.KEY_F5, 'r')
|
||||
def refresh_content(self):
|
||||
url = self.content.name
|
||||
self.content = SubmissionContent.from_url(self.reddit, url, self.loader)
|
||||
self.content = SubmissionContent.from_url(
|
||||
self.reddit,
|
||||
url,
|
||||
self.loader)
|
||||
self.nav = Navigator(self.content.get, page_index=-1)
|
||||
|
||||
@SubmissionController.register(curses.KEY_ENTER, 10, 'o')
|
||||
|
||||
@@ -59,7 +59,8 @@ class SubredditPage(BasePage):
|
||||
n_rows, n_cols = self.stdscr.getmaxyx()
|
||||
self.stdscr.addstr(n_rows - 1, 0, prompt, attr)
|
||||
self.stdscr.refresh()
|
||||
window = self.stdscr.derwin(1, n_cols-len(prompt),n_rows-1, len(prompt))
|
||||
window = self.stdscr.derwin(1, n_cols - len(prompt),
|
||||
n_rows - 1, len(prompt))
|
||||
window.attrset(attr)
|
||||
|
||||
out = text_input(window)
|
||||
|
||||
Reference in New Issue
Block a user