From cafc08dafa49de0b750680eef9dcd333ad1b65e8 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 2 Feb 2015 23:46:47 -0800 Subject: [PATCH] Python 3.4 compatability. --- rtv/content.py | 21 ++++++++++++++------- rtv/main.py | 6 +++--- rtv/page.py | 2 +- rtv/submission.py | 14 +++++++++++--- rtv/utils.py | 10 +++++----- setup.py | 8 ++++---- 6 files changed, 38 insertions(+), 23 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 8373bcf..2ed9656 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -3,6 +3,7 @@ from datetime import datetime from contextlib import contextmanager import praw +import six from errors import SubmissionURLError, SubredditNameError @@ -11,7 +12,13 @@ def clean(unicode_string): Convert unicode string into ascii-safe characters. """ - return unicode_string.encode('ascii', 'replace').replace('\\', '') + if six.PY2: + ascii_string = unicode_string.encode('ascii', 'replace') + else: + ascii_string = unicode_string.encode().decode('ascii', 'replace') + + ascii_string = ascii_string.replace('\\', '') + return ascii_string def strip_subreddit_url(permalink): @@ -34,19 +41,19 @@ def humanize_timestamp(utc_timestamp, verbose=False): seconds = int(timedelta.total_seconds()) if seconds < 60: return 'moments ago' if verbose else '0min' - minutes = seconds / 60 + minutes = seconds // 60 if minutes < 60: return ('%d minutes ago' % minutes) if verbose else ('%dmin' % minutes) - hours = minutes / 60 + hours = minutes // 60 if hours < 24: return ('%d hours ago' % hours) if verbose else ('%dhr' % hours) - days = hours / 24 + days = hours // 24 if days < 30: return ('%d days ago' % days) if verbose else ('%dday' % days) - months = days / 30.4 + months = days // 30.4 if months < 12: return ('%d months ago' % months) if verbose else ('%dmonth' % months) - years = months / 12 + years = months // 12 return ('%d years ago' % years) if verbose else ('%dyr' % years) @contextmanager @@ -312,7 +319,7 @@ class SubredditContent(BaseContent): try: with self._loader(): - submission = self._submissions.next() + submission = next(self._submissions) except StopIteration: raise IndexError else: diff --git a/rtv/main.py b/rtv/main.py index 76e4600..bb9283e 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -40,13 +40,13 @@ def main(): return except ConnectionError: - print 'Timeout: Could not connect to website' + print('Timeout: Could not connect to website') except SubmissionURLError as e: - print 'Could not reach submission URL: {}'.format(e.url) + print('Could not reach submission URL: {}'.format(e.url)) except SubredditNameError as e: - print 'Could not reach subreddit: {}'.format(e.name) + print('Could not reach subreddit: {}'.format(e.name)) if __name__ == '__main__': diff --git a/rtv/page.py b/rtv/page.py index 7b164c7..51fae31 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -204,7 +204,7 @@ class BasePage(object): attribute |= attr n_rows, _ = window.getmaxyx() - for row in xrange(n_rows): + for row in range(n_rows): window.chgat(row, 0, 1, attribute) window.refresh() \ No newline at end of file diff --git a/rtv/submission.py b/rtv/submission.py index 2ea6d34..fc5b463 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -1,6 +1,8 @@ import curses import sys +import six + from content import SubmissionContent from page import BasePage from utils import LoadScreen, Color @@ -108,8 +110,14 @@ class SubmissionPage(BasePage): # Vertical line, unfortunately vline() doesn't support custom color so # we have to build it one chr at a time. attr = Color.get_level(data['level']) - for y in xrange(n_rows): - win.addch(y, 0, curses.ACS_VLINE, attr) + for y in range(n_rows): + + # Nobody pays attention to curses ;( + # http://bugs.python.org/issue21088 + x = 0 + if (sys.version_info.major, sys.version_info.minor) == (3, 4): + x, y = y, x + win.addch(y, x, curses.ACS_VLINE, attr) return attr | curses.ACS_VLINE @@ -124,7 +132,7 @@ class SubmissionPage(BasePage): win.addnstr(text, n_cols - win.getyx()[1], attr) attr = Color.get_level(data['level']) - for y in xrange(n_rows): + for y in range(n_rows): win.addch(y, 0, curses.ACS_VLINE, attr) return attr | curses.ACS_VLINE diff --git a/rtv/utils.py b/rtv/utils.py index ce7349c..030bef6 100644 --- a/rtv/utils.py +++ b/rtv/utils.py @@ -77,8 +77,8 @@ def display_message(stdscr, message): message_len = len(message) n_rows, n_cols = stdscr.getmaxyx() - s_row = (n_rows - 2) / 2 - s_col = (n_cols - message_len - 1) / 2 + s_row = (n_rows - 2) // 2 + s_col = (n_cols - message_len - 1) // 2 window = stdscr.derwin(3, message_len+2, s_row, s_col) window.erase() @@ -137,12 +137,12 @@ class LoadScreen(object): message_len = len(message) + len(trail) n_rows, n_cols = self._stdscr.getmaxyx() - s_row = (n_rows - 2) / 2 - s_col = (n_cols - message_len - 1) / 2 + s_row = (n_rows - 2) // 2 + s_col = (n_cols - message_len - 1) // 2 window = self._stdscr.derwin(3, message_len+2, s_row, s_col) while True: - for i in xrange(len(trail)+1): + for i in range(len(trail)+1): if not self._is_running: window.clear() diff --git a/setup.py b/setup.py index 7897b50..0cdb7f3 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,6 @@ from setuptools import setup +#python setup.py develop --user +#python setup.py develop --user --uninstall setup( name='rtv', @@ -10,8 +12,6 @@ setup( license='MIT', keywords='reddit terminal praw', packages=['rtv'], - install_requires=['praw'], + install_requires=['praw', 'six'], entry_points={'console_scripts': ['rtv=rtv:main']} -) - -#python setup.py develop --user \ No newline at end of file +) \ No newline at end of file