From 884881546d337e2fd5a2f48c7eec24388880bd67 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Fri, 19 Apr 2013 17:32:32 +0200 Subject: [PATCH] Better utf8 support --- CHANGELOG.md | 3 ++- jrnl/Journal.py | 14 ++++++++++---- jrnl/jrnl.py | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6288f3fc..24dfbd6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,9 @@ Changelog ### 1.0.4 * [Improved] Python 2.6 compatibility +* [Improved] Better utf-8 support * [New] Python 3 compatibility -* [New] Respects the `XDG_CONFIG_HOME` environment variable for storing your configuration file +* [New] Respects the `XDG_CONFIG_HOME` environment variable for storing your configuration file (Thanks [evaryont](https://github.com/evaryont)) ### 1.0.3 (April 17, 2013) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index a95207f5..7b1d10dc 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -104,9 +104,9 @@ class Journal(object): Entries have the form (date, title, body).""" filename = filename or self.config['journal'] journal = None - with codecs.open(filename, "r", "utf-8") as f: - journal = f.read() if self.config['encrypt']: + with open(filename, "rb") as f: + journal = f.read() decrypted = None attempts = 0 while decrypted is None: @@ -121,6 +121,9 @@ class Journal(object): print("Extremely wrong password.") sys.exit(-1) journal = decrypted + else: + with codecs.open(filename, "r", "utf-8") as f: + journal = f.read() return journal def parse(self, journal): @@ -181,8 +184,11 @@ class Journal(object): journal = "\n".join([str(e) for e in self.entries]) if self.config['encrypt']: journal = self._encrypt(journal) - with codecs.open(filename, 'w', "utf-8") as journal_file: - journal_file.write(journal) + with open(filename, 'wb') as journal_file: + journal_file.write(journal) + else: + with codecs.open(filename, 'w', "utf-8") as journal_file: + journal_file.write(journal) def sort(self): """Sorts the Journal's entries by date""" diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 182eae00..20fa7e0f 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -32,7 +32,7 @@ __author__ = 'Manuel Ebert, Stephan Gabler' __license__ = 'MIT' xdg_config = os.environ.get('XDG_CONFIG_HOME') -CONFIG_PATH = os.path.join(xdg_config, "jrnl") if xdg_config else os.path.expanduser('~/.jrnl_config')) +CONFIG_PATH = os.path.join(xdg_config, "jrnl") if xdg_config else os.path.expanduser('~/.jrnl_config') PYCRYPTO = install.module_exists("Crypto") def parse_args():