From cb8d65d8c038334648df498f580098951a9610d7 Mon Sep 17 00:00:00 2001 From: Rakan Alhneiti Date: Wed, 17 Feb 2016 20:53:16 +0100 Subject: [PATCH] Handle different encodings, use six --- pocket_cli/storage.py | 16 +++++++++++++--- requirements.txt | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pocket_cli/storage.py b/pocket_cli/storage.py index 377efb3..a897441 100644 --- a/pocket_cli/storage.py +++ b/pocket_cli/storage.py @@ -1,5 +1,8 @@ +from __future__ import unicode_literals + import csv import os +import six class Storage: @@ -24,12 +27,12 @@ class Storage: if self.is_empty(): write_header = True - with open(self._filename, 'a+') as csv_file: + with open(self._filename, 'a+b') 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 = [] @@ -38,7 +41,7 @@ class Storage: return index row_counter = 0 - with open(self._filename, 'r') as csv_file: + with open(self._filename, 'rb') as csv_file: reader = csv.DictReader(csv_file) for row in reader: index.append(row) @@ -57,3 +60,10 @@ class Storage: def clear(self): if os.path.exists(self._filename): os.remove(self._filename) + + def _encode_data(self, 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 diff --git a/requirements.txt b/requirements.txt index da80a73..64d1937 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ click==6.2 pocket-api requests==2.9.1 progress==1.2 +future==0.15.2 +six==1.10.0