Python 3.4 compatability.

This commit is contained in:
Michael Lazar
2015-02-02 23:46:47 -08:00
parent c9c38dc264
commit cafc08dafa
6 changed files with 38 additions and 23 deletions

View File

@@ -3,6 +3,7 @@ from datetime import datetime
from contextlib import contextmanager from contextlib import contextmanager
import praw import praw
import six
from errors import SubmissionURLError, SubredditNameError from errors import SubmissionURLError, SubredditNameError
@@ -11,7 +12,13 @@ def clean(unicode_string):
Convert unicode string into ascii-safe characters. 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): def strip_subreddit_url(permalink):
@@ -34,19 +41,19 @@ def humanize_timestamp(utc_timestamp, verbose=False):
seconds = int(timedelta.total_seconds()) seconds = int(timedelta.total_seconds())
if seconds < 60: if seconds < 60:
return 'moments ago' if verbose else '0min' return 'moments ago' if verbose else '0min'
minutes = seconds / 60 minutes = seconds // 60
if minutes < 60: if minutes < 60:
return ('%d minutes ago' % minutes) if verbose else ('%dmin' % minutes) return ('%d minutes ago' % minutes) if verbose else ('%dmin' % minutes)
hours = minutes / 60 hours = minutes // 60
if hours < 24: if hours < 24:
return ('%d hours ago' % hours) if verbose else ('%dhr' % hours) return ('%d hours ago' % hours) if verbose else ('%dhr' % hours)
days = hours / 24 days = hours // 24
if days < 30: if days < 30:
return ('%d days ago' % days) if verbose else ('%dday' % days) return ('%d days ago' % days) if verbose else ('%dday' % days)
months = days / 30.4 months = days // 30.4
if months < 12: if months < 12:
return ('%d months ago' % months) if verbose else ('%dmonth' % months) 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) return ('%d years ago' % years) if verbose else ('%dyr' % years)
@contextmanager @contextmanager
@@ -312,7 +319,7 @@ class SubredditContent(BaseContent):
try: try:
with self._loader(): with self._loader():
submission = self._submissions.next() submission = next(self._submissions)
except StopIteration: except StopIteration:
raise IndexError raise IndexError
else: else:

View File

@@ -40,13 +40,13 @@ def main():
return return
except ConnectionError: except ConnectionError:
print 'Timeout: Could not connect to website' print('Timeout: Could not connect to website')
except SubmissionURLError as e: 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: except SubredditNameError as e:
print 'Could not reach subreddit: {}'.format(e.name) print('Could not reach subreddit: {}'.format(e.name))
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -204,7 +204,7 @@ class BasePage(object):
attribute |= attr attribute |= attr
n_rows, _ = window.getmaxyx() n_rows, _ = window.getmaxyx()
for row in xrange(n_rows): for row in range(n_rows):
window.chgat(row, 0, 1, attribute) window.chgat(row, 0, 1, attribute)
window.refresh() window.refresh()

View File

@@ -1,6 +1,8 @@
import curses import curses
import sys import sys
import six
from content import SubmissionContent from content import SubmissionContent
from page import BasePage from page import BasePage
from utils import LoadScreen, Color from utils import LoadScreen, Color
@@ -108,8 +110,14 @@ class SubmissionPage(BasePage):
# Vertical line, unfortunately vline() doesn't support custom color so # Vertical line, unfortunately vline() doesn't support custom color so
# we have to build it one chr at a time. # we have to build it one chr at a time.
attr = Color.get_level(data['level']) 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)
# 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 return attr | curses.ACS_VLINE
@@ -124,7 +132,7 @@ class SubmissionPage(BasePage):
win.addnstr(text, n_cols - win.getyx()[1], attr) win.addnstr(text, n_cols - win.getyx()[1], attr)
attr = Color.get_level(data['level']) 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) win.addch(y, 0, curses.ACS_VLINE, attr)
return attr | curses.ACS_VLINE return attr | curses.ACS_VLINE

View File

@@ -77,8 +77,8 @@ def display_message(stdscr, message):
message_len = len(message) message_len = len(message)
n_rows, n_cols = stdscr.getmaxyx() n_rows, n_cols = stdscr.getmaxyx()
s_row = (n_rows - 2) / 2 s_row = (n_rows - 2) // 2
s_col = (n_cols - message_len - 1) / 2 s_col = (n_cols - message_len - 1) // 2
window = stdscr.derwin(3, message_len+2, s_row, s_col) window = stdscr.derwin(3, message_len+2, s_row, s_col)
window.erase() window.erase()
@@ -137,12 +137,12 @@ class LoadScreen(object):
message_len = len(message) + len(trail) message_len = len(message) + len(trail)
n_rows, n_cols = self._stdscr.getmaxyx() n_rows, n_cols = self._stdscr.getmaxyx()
s_row = (n_rows - 2) / 2 s_row = (n_rows - 2) // 2
s_col = (n_cols - message_len - 1) / 2 s_col = (n_cols - message_len - 1) // 2
window = self._stdscr.derwin(3, message_len+2, s_row, s_col) window = self._stdscr.derwin(3, message_len+2, s_row, s_col)
while True: while True:
for i in xrange(len(trail)+1): for i in range(len(trail)+1):
if not self._is_running: if not self._is_running:
window.clear() window.clear()

View File

@@ -1,4 +1,6 @@
from setuptools import setup from setuptools import setup
#python setup.py develop --user
#python setup.py develop --user --uninstall
setup( setup(
name='rtv', name='rtv',
@@ -10,8 +12,6 @@ setup(
license='MIT', license='MIT',
keywords='reddit terminal praw', keywords='reddit terminal praw',
packages=['rtv'], packages=['rtv'],
install_requires=['praw'], install_requires=['praw', 'six'],
entry_points={'console_scripts': ['rtv=rtv:main']} entry_points={'console_scripts': ['rtv=rtv:main']}
) )
#python setup.py develop --user