Merge pull request #1 from rakanalh/python2-backport

Python2 backport
This commit is contained in:
Rakan Alhneiti 2016-02-17 23:15:38 +01:00
commit 5902af13bd
6 changed files with 77 additions and 13 deletions

View file

@ -15,7 +15,7 @@ Features
* Multiple `fetch` command calls will retrieve articles since last fetch.
Note: This is a Python 3 application tested on 3.5.0. Work in progress to make it 2.7 compatible.
Note: This application has been tested on Python 2.7.10 and 3.5.0.
Installation
------------

View file

@ -1,3 +1,5 @@
from future.utils import raise_from
import math
import time
from datetime import datetime
@ -57,7 +59,7 @@ class PocketApp:
try:
return self._pocket.add(url, title, tags)
except PocketException as e:
raise self._check_exception(e) from e
raise_from(self._check_exception(e), e)
def get_articles(self, limit=None, order=None):
if self._storage.is_empty():
@ -80,13 +82,13 @@ class PocketApp:
sort=sort)
return self._get_articles_index(articles)
except PocketException as e:
raise self._check_exception(e) from e
raise_from(self._check_exception(e), e)
def archive_article(self, item_id):
try:
self._pocket.archive(int(item_id)).commit()
except PocketException as e:
raise self._check_exception(e) from e
raise_from(self._check_exception(e), e)
def find_article(self, item_id):
index = self._storage.read()
@ -118,7 +120,7 @@ class PocketApp:
)
except PocketException as e:
spinner.finish()
raise self._check_exception(e) from e
raise_from(self._check_exception(e), e)
if not articles['list']:
break

View file

@ -1,6 +1,12 @@
from __future__ import absolute_import
from __future__ import print_function
from builtins import input
import random
import subprocess
import sys
import six
import webbrowser
import click
@ -195,8 +201,12 @@ def output_articles(articles):
for article in articles:
if int(article['reading_time']) <= 0:
article['reading_time'] = 'Unknown'
pager.stdin.write(
bytearray(format_article(article, line=True), 'utf-8'))
content = format_article(article, line=True)
if six.PY3:
content = bytearray(content, 'utf-8')
pager.stdin.write(content)
pager.stdin.close()
pager.wait()

View file

@ -1,5 +1,8 @@
from __future__ import unicode_literals
import csv
import os
import six
class Storage:
@ -24,12 +27,16 @@ class Storage:
if self.is_empty():
write_header = True
with open(self._filename, 'a+') as csv_file:
mode = 'a+b'
if six.PY3:
mode = 'a+t'
with open(self._filename, mode) as csv_file:
dict_writer = csv.DictWriter(csv_file, data[0].keys())
if write_header:
dict_writer.writeheader()
dict_writer.writerows(data)
dict_writer.writerows(self._encode_data(data))
def read(self, limit=10, order='asc'):
index = []
@ -37,8 +44,12 @@ class Storage:
if not os.path.exists(self._filename):
return index
mode = 'rb'
if six.PY3:
mode = 'r'
row_counter = 0
with open(self._filename, 'r') as csv_file:
with open(self._filename, mode) as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
index.append(row)
@ -57,3 +68,13 @@ class Storage:
def clear(self):
if os.path.exists(self._filename):
os.remove(self._filename)
def _encode_data(self, data):
if six.PY3:
return data
for index, item in enumerate(data):
for key, value in item.items():
if isinstance(value, six.string_types):
data[index][key] = value.encode('utf-8')
return data

View file

@ -1,12 +1,41 @@
import shutil
import os
try:
from shutil import get_terminal_size
except ImportError:
def get_terminal_size():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.env['LINES'], os.env['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1]), int(cr[0])
def format_article(article, header=None, footer=None, line=False):
content = ''
if header:
content = header
content = '{}\n'.format(header)
if line:
content += '{}\n'.format('=' * (shutil.get_terminal_size()[0]-1))
content += '{}\n'.format('=' * (get_terminal_size()[0]-1))
content += '{} - {}\nReading Time: {} Mins\nURL: {}\n'.format(
article['id'],

View file

@ -2,3 +2,5 @@ click==6.2
pocket-api
requests==2.9.1
progress==1.2
future==0.15.2
six==1.10.0