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)
return pp
def pprint(self):
return self.__unicode__()
def __repr__(self):
return "<Journal with {} entries>".format(len(self.entries))
def write(self, filename=None):
"""Dumps the journal into the config file, overwriting it"""
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']:
journal = self._encrypt(journal)
with open(filename, 'wb') as journal_file:

View file

@ -7,6 +7,9 @@ try: from slugify import slugify
except ImportError: import slugify
try: import simplejson as json
except ImportError: import json
try: from .util import u
except (SystemError, ValueError): from util import u
def get_tags_count(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):
"""Returns the complete text of the Journal."""
return unicode(journal)
return journal.pprint()
def export(journal, format, output=None):
"""Exports the journal to various formats.
@ -93,7 +96,7 @@ def export(journal, format, output=None):
def write_files(journal, path, format):
"""Turns your journal into separate files for each entry.
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:
full_path = os.path.join(path, make_filename(e))
if format == 'json':
@ -101,7 +104,7 @@ def write_files(journal, path, format):
elif format == 'md':
content = e.to_md()
elif format == 'txt':
content = unicode(e)
content = u(e)
with open(full_path, 'w') as f:
f.write(content)
return "[Journal exported individual files in {}]".format(path)

View file

@ -169,7 +169,7 @@ def cli(manual_args=None):
# Writing mode
if mode_compose:
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())
entry = journal.new_entry(raw, args.date)
entry.starred = args.star
@ -183,7 +183,7 @@ def cli(manual_args=None):
strict=args.strict,
short=args.short)
journal.limit(args.limit)
print(unicode(journal))
print(journal.pprint())
# Various export modes
elif args.tags:

View file

@ -3,10 +3,15 @@
import sys
import os
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
STDOUT = sys.stdout
__cached_tz = None
def py23_input(msg):