Python 3 improvements

This commit is contained in:
Manuel Ebert 2013-07-19 13:24:18 +02:00
parent 03c1395c01
commit f9bdc13210
4 changed files with 18 additions and 7 deletions

View file

@ -179,13 +179,16 @@ class Journal(object):
pp) pp)
return pp return pp
def pprint(self):
return self.__unicode__()
def __repr__(self): def __repr__(self):
return "<Journal with {} entries>".format(len(self.entries)) return "<Journal with {} entries>".format(len(self.entries))
def write(self, filename=None): def write(self, filename=None):
"""Dumps the journal into the config file, overwriting it""" """Dumps the journal into the config file, overwriting it"""
filename = filename or self.config['journal'] filename = filename or self.config['journal']
journal = "\n".join([unicode(e) for e in self.entries]) journal = "\n".join([e.__unicode__() for e in self.entries])
if self.config['encrypt']: if self.config['encrypt']:
journal = self._encrypt(journal) journal = self._encrypt(journal)
with open(filename, 'wb') as journal_file: with open(filename, 'wb') as journal_file:

View file

@ -7,6 +7,9 @@ try: from slugify import slugify
except ImportError: import slugify except ImportError: import slugify
try: import simplejson as json try: import simplejson as json
except ImportError: import json except ImportError: import json
try: from .util import u
except (SystemError, ValueError): from util import u
def get_tags_count(journal): def get_tags_count(journal):
"""Returns a set of tuples (count, tag) for all tags present in the journal.""" """Returns a set of tuples (count, tag) for all tags present in the journal."""
@ -60,7 +63,7 @@ def to_md(journal):
def to_txt(journal): def to_txt(journal):
"""Returns the complete text of the Journal.""" """Returns the complete text of the Journal."""
return unicode(journal) return journal.pprint()
def export(journal, format, output=None): def export(journal, format, output=None):
"""Exports the journal to various formats. """Exports the journal to various formats.
@ -93,7 +96,7 @@ def export(journal, format, output=None):
def write_files(journal, path, format): def write_files(journal, path, format):
"""Turns your journal into separate files for each entry. """Turns your journal into separate files for each entry.
Format should be either json, md or txt.""" Format should be either json, md or txt."""
make_filename = lambda entry: e.date.strftime("%C-%m-%d_{}.{}".format(slugify(unicode(e.title)), format)) make_filename = lambda entry: e.date.strftime("%C-%m-%d_{}.{}".format(slugify(u(e.title)), format))
for e in journal.entries: for e in journal.entries:
full_path = os.path.join(path, make_filename(e)) full_path = os.path.join(path, make_filename(e))
if format == 'json': if format == 'json':
@ -101,7 +104,7 @@ def write_files(journal, path, format):
elif format == 'md': elif format == 'md':
content = e.to_md() content = e.to_md()
elif format == 'txt': elif format == 'txt':
content = unicode(e) content = u(e)
with open(full_path, 'w') as f: with open(full_path, 'w') as f:
f.write(content) f.write(content)
return "[Journal exported individual files in {}]".format(path) return "[Journal exported individual files in {}]".format(path)

View file

@ -169,7 +169,7 @@ def cli(manual_args=None):
# Writing mode # Writing mode
if mode_compose: if mode_compose:
raw = " ".join(args.text).strip() raw = " ".join(args.text).strip()
if type(raw) is not unicode: if util.PY2 and type(raw) is not unicode:
raw = raw.decode(sys.getfilesystemencoding()) raw = raw.decode(sys.getfilesystemencoding())
entry = journal.new_entry(raw, args.date) entry = journal.new_entry(raw, args.date)
entry.starred = args.star entry.starred = args.star
@ -183,7 +183,7 @@ def cli(manual_args=None):
strict=args.strict, strict=args.strict,
short=args.short) short=args.short)
journal.limit(args.limit) journal.limit(args.limit)
print(unicode(journal)) print(journal.pprint())
# Various export modes # Various export modes
elif args.tags: elif args.tags:

View file

@ -3,10 +3,15 @@
import sys import sys
import os import os
from tzlocal import get_localzone from tzlocal import get_localzone
PY3 = sys.version_info[0] == 3
PY2 = sys.version_info[0] == 2
def u(s):
"""Mock unicode function for python 2 and 3 compatibility."""
return s if PY3 else unicode(s, "unicode_escape")
STDIN = sys.stdin STDIN = sys.stdin
STDOUT = sys.stdout STDOUT = sys.stdout
__cached_tz = None __cached_tz = None
def py23_input(msg): def py23_input(msg):