Buffer journal file into string before reading or writing

This commit is contained in:
Stephan Gabler 2012-04-13 20:04:10 +02:00
parent 1569f9c59c
commit c6b86991c4

14
jrnl.py
View file

@ -17,6 +17,7 @@ import readline, glob
default_config = { default_config = {
'journal': os.path.expanduser("~/journal.txt"), 'journal': os.path.expanduser("~/journal.txt"),
'editor': "", 'editor': "",
'encrypt': True,
'default_hour': 9, 'default_hour': 9,
'default_minute': 0, 'default_minute': 0,
'timeformat': "%Y-%m-%d %H:%M", 'timeformat': "%Y-%m-%d %H:%M",
@ -86,8 +87,9 @@ class Journal:
entries = [] entries = []
current_entry = None current_entry = None
journal_file = open(filename) with open(filename) as f:
for line in journal_file.readlines(): journal_plain = f.read()
for line in journal_plain.split(os.linesep):
if line: if line:
try: try:
new_date = datetime.fromtimestamp(time.mktime(time.strptime(line[:date_length], config['timeformat']))) new_date = datetime.fromtimestamp(time.mktime(time.strptime(line[:date_length], config['timeformat'])))
@ -103,7 +105,6 @@ class Journal:
# Append last entry # Append last entry
if current_entry: if current_entry:
entries.append(current_entry) entries.append(current_entry)
journal_file.close()
for entry in entries: for entry in entries:
entry.parse_tags() entry.parse_tags()
return entries return entries
@ -123,10 +124,9 @@ class Journal:
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_file = open(filename, 'w') journal_plain = os.linesep.join([str(e) for e in self.entries])
for entry in self.entries: with open(filename, 'w') as journal_file:
journal_file.write(str(entry)+"\n") journal_file.write(journal_plain)
journal_file.close()
def sort(self): def sort(self):
"""Sorts the Journal's entries by date""" """Sorts the Journal's entries by date"""