Properly initialise journal files after creating them

This commit is contained in:
Manuel Ebert 2015-04-04 18:58:33 +11:00
parent cc85d905ca
commit df82ad1f4d
4 changed files with 36 additions and 16 deletions

View file

@ -50,3 +50,10 @@ class EncryptedJournal(Journal.Journal):
journal = Fernet(key).encrypt(text.encode('utf-8'))
with open(filename, 'w') as f:
f.write(journal)
@classmethod
def _create(cls, filename, password):
key = make_key(password)
dummy = Fernet(key).encrypt("")
with open(filename, 'w') as f:
f.write(dummy)

View file

@ -58,6 +58,10 @@ class Journal(object):
def _store(self, filename, text):
raise NotImplementedError
@classmethod
def _create(cls, filename):
raise NotImplementedError
def _parse(self, journal_txt):
"""Parses a journal that's stored in a string and returns a list of entries"""
@ -114,9 +118,11 @@ class Journal(object):
lambda match: util.colorize(match.group(0)),
pp, re.UNICODE)
else:
pp = re.sub( Entry.Entry.tag_regex(self.config['tagsymbols']),
pp = re.sub(
Entry.Entry.tag_regex(self.config['tagsymbols']),
lambda match: util.colorize(match.group(0)),
pp)
pp
)
return pp
def __repr__(self):
@ -223,6 +229,11 @@ class PlainJournal(Journal):
def __init__(self, name='default', **kwargs):
super(PlainJournal, self).__init__(name, **kwargs)
@classmethod
def _create(cls, filename):
with codecs.open(filename, "a", "utf-8"):
pass
def _load(self, filename):
with codecs.open(filename, "r", "utf-8") as f:
return f.read()

View file

@ -105,9 +105,8 @@ def decrypt(journal, filename=None):
def touch_journal(filename):
"""If filename does not exist, touch the file"""
if not os.path.exists(filename):
log.debug('Creating journal file %s', filename)
util.prompt("[Journal created at {0}]".format(filename))
open(filename, 'a').close()
Journal.PlainJournal._create(filename)
def list_journals(config):

View file

@ -10,6 +10,8 @@ import xdg.BaseDirectory
from . import util
from . import upgrade
from . import __version__
from .Journal import PlainJournal
from .EncryptedJournal import EncryptedJournal
import yaml
import logging
@ -104,6 +106,12 @@ def install():
journal_path = util.py23_input(path_query).strip() or JOURNAL_FILE_PATH
default_config['journals']['default'] = os.path.expanduser(os.path.expandvars(journal_path))
path = os.path.split(default_config['journals']['default'])[0] # If the folder doesn't exist, create it
try:
os.makedirs(path)
except OSError:
pass
# Encrypt it?
password = getpass.getpass("Enter password for journal (leave blank for no encryption): ")
if password:
@ -112,15 +120,10 @@ def install():
util.set_keychain("default", password)
else:
util.set_keychain("default", None)
EncryptedJournal._create(default_config['journals']['default'], password)
print("Journal will be encrypted.")
path = os.path.split(default_config['journals']['default'])[0] # If the folder doesn't exist, create it
try:
os.makedirs(path)
except OSError:
pass
open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there
else:
PlainJournal._create(default_config['journals']['default'])
config = default_config
save_config(config)