Fixes for Python 3 Support

This commit is contained in:
Manuel Ebert 2013-04-19 15:19:21 +02:00
parent 8200ebb6fe
commit 9f33c9c9c4
4 changed files with 28 additions and 13 deletions

View file

@ -1,7 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import Entry try: from . import Entry
except (SystemError, ValueError): import Entry
import codecs
import os import os
import parsedatetime.parsedatetime as pdt import parsedatetime.parsedatetime as pdt
import re import re
@ -14,8 +16,9 @@ import readline, glob
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
from Crypto.Random import random, atfork from Crypto.Random import random, atfork
crypto_installed = True
except ImportError: except ImportError:
pass crypto_installed = False
try: try:
import pyreadline as readline import pyreadline as readline
except ImportError: except ImportError:
@ -63,6 +66,8 @@ class Journal(object):
def _decrypt(self, cipher): def _decrypt(self, cipher):
"""Decrypts a cipher string using self.key as the key and the first 16 byte of the cipher as the IV""" """Decrypts a cipher string using self.key as the key and the first 16 byte of the cipher as the IV"""
if not crypto_installed:
sys.exit("Error: PyCrypto is not installed.")
if not cipher: if not cipher:
return "" return ""
crypto = AES.new(self.key, AES.MODE_CBC, cipher[:16]) crypto = AES.new(self.key, AES.MODE_CBC, cipher[:16])
@ -78,6 +83,8 @@ class Journal(object):
def _encrypt(self, plain): def _encrypt(self, plain):
"""Encrypt a plaintext string using self.key as the key""" """Encrypt a plaintext string using self.key as the key"""
if not crypto_installed:
sys.exit("Error: PyCrypto is not installed.")
atfork() # A seed for PyCrypto atfork() # A seed for PyCrypto
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
crypto = AES.new(self.key, AES.MODE_CBC, iv) crypto = AES.new(self.key, AES.MODE_CBC, iv)
@ -90,14 +97,14 @@ class Journal(object):
def make_key(self, prompt="Password: "): def make_key(self, prompt="Password: "):
"""Creates an encryption key from the default password or prompts for a new password.""" """Creates an encryption key from the default password or prompts for a new password."""
password = self.config['password'] or getpass.getpass(prompt) password = self.config['password'] or getpass.getpass(prompt)
self.key = hashlib.sha256(password).digest() self.key = hashlib.sha256(password.encode('utf-8')).digest()
def open(self, filename=None): def open(self, filename=None):
"""Opens the journal file defined in the config and parses it into a list of Entries. """Opens the journal file defined in the config and parses it into a list of Entries.
Entries have the form (date, title, body).""" Entries have the form (date, title, body)."""
filename = filename or self.config['journal'] filename = filename or self.config['journal']
journal = None journal = None
with open(filename) as f: with codecs.open(filename, "r", "utf-8") as f:
journal = f.read() journal = f.read()
if self.config['encrypt']: if self.config['encrypt']:
decrypted = None decrypted = None
@ -174,7 +181,7 @@ class Journal(object):
journal = "\n".join([str(e) for e in self.entries]) journal = "\n".join([str(e) for e in self.entries])
if self.config['encrypt']: if self.config['encrypt']:
journal = self._encrypt(journal) journal = self._encrypt(journal)
with open(filename, 'w') as journal_file: with codecs.open(filename, 'w', "utf-8") as journal_file:
journal_file.write(journal) journal_file.write(journal)
def sort(self): def sort(self):

View file

@ -12,5 +12,5 @@ __author__ = 'Manuel Ebert'
__license__ = 'MIT License' __license__ = 'MIT License'
__copyright__ = 'Copyright 2013 Manuel Ebert' __copyright__ = 'Copyright 2013 Manuel Ebert'
from jrnl.Journal import Journal from . import Journal
from jrnl.jrnl import cli from . import jrnl

View file

@ -6,7 +6,9 @@ import getpass
try: import simplejson as json try: import simplejson as json
except ImportError: import json except ImportError: import json
import os import os
import util try: from . import util
except (SystemError, ValueError): import util
def module_exists(module_name): def module_exists(module_name):
"""Checks if a module exists and can be imported""" """Checks if a module exists and can be imported"""

View file

@ -7,6 +7,12 @@
license: MIT, see LICENSE for more details. license: MIT, see LICENSE for more details.
""" """
try:
from . import Journal
from . import util
from . import exporters
from . import install
except (SystemError, ValueError):
import Journal import Journal
import util import util
import exporters import exporters
@ -186,7 +192,7 @@ def cli():
raw = " ".join(args.text).strip() raw = " ".join(args.text).strip()
entry = journal.new_entry(raw, args.date) entry = journal.new_entry(raw, args.date)
entry.starred = args.star entry.starred = args.star
print("[Entry added to {0} journal]").format(journal_name) print("[Entry added to {0} journal]".format(journal_name))
journal.write() journal.write()
# Reading mode # Reading mode