External editor can be used for composing by specifying the editor config option

This commit is contained in:
Stephan Gabler 2012-04-13 18:37:28 +02:00
parent 5b5570f41e
commit 04cc68a29a

34
jrnl.py
View file

@ -1,20 +1,24 @@
#!/usr/bin/python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import os
import tempfile
import parsedatetime.parsedatetime as pdt import parsedatetime.parsedatetime as pdt
import parsedatetime.parsedatetime_consts as pdc import parsedatetime.parsedatetime_consts as pdc
import subprocess
import re import re
import argparse import argparse
from datetime import datetime from datetime import datetime
import time import time
import json import json
import sys
config = { config = {
'journal': "/home/manuel/Dropbox/Notes/journal.txt", 'journal': "/Users/dedan/Dropbox/Notes/journal_neu.txt",
'editor': "subl -w",
'default_hour': 9, 'default_hour': 9,
'default_minute': 0, 'default_minute': 0,
'timeformat': "%Y-%m-%d %H:%M", 'timeformat': "%Y-%m-%d %H:%M",
'tagsymbols': '#@' 'tagsymbols': '@'
} }
class Entry: class Entry:
@ -37,7 +41,7 @@ class Entry:
space = "\n" space = "\n"
return "%(date)s %(title)s %(body)s %(space)s" % { return "%(date)s %(title)s %(body)s %(space)s" % {
'date': date_str, 'date': date_str,
'title': self.title, 'title': self.title,
'body': body, 'body': body,
'space': space 'space': space
@ -133,13 +137,13 @@ class Journal:
def filter(self, tags=[], start_date=None, end_date=None, strict=False): def filter(self, tags=[], start_date=None, end_date=None, strict=False):
"""Removes all entries from the journal that don't match the filter. """Removes all entries from the journal that don't match the filter.
tags is a list of tags, each being a string that starts with one of the tags is a list of tags, each being a string that starts with one of the
tag symbols defined in the config, e.g. ["@John", "#WorldDomination"]. tag symbols defined in the config, e.g. ["@John", "#WorldDomination"].
start_date and end_date define a timespan by which to filter. start_date and end_date define a timespan by which to filter.
If strict is True, all tags must be present in an entry. If false, the If strict is True, all tags must be present in an entry. If false, the
entry is kept if any tag is present.""" entry is kept if any tag is present."""
search_tags = set(tags) search_tags = set(tags)
end_date = self.parse_date(end_date) end_date = self.parse_date(end_date)
@ -162,7 +166,7 @@ class Journal:
return date return date
date, flag = self.dateparse.parse(date) date, flag = self.dateparse.parse(date)
if not flag: # Oops, unparsable. if not flag: # Oops, unparsable.
return None return None
@ -198,6 +202,7 @@ class Journal:
self.sort() self.sort()
if __name__ == "__main__": if __name__ == "__main__":
print sys.argv
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
composing = parser.add_argument_group('Composing', 'Will make an entry out of whatever follows as arguments') composing = parser.add_argument_group('Composing', 'Will make an entry out of whatever follows as arguments')
composing.add_argument('-date', dest='date', help='Date, e.g. "yesterday at 5pm"') composing.add_argument('-date', dest='date', help='Date, e.g. "yesterday at 5pm"')
@ -210,6 +215,7 @@ if __name__ == "__main__":
reading.add_argument('-n', dest='limit', default=None, metavar="N", help='Shows the last n entries matching the filter', nargs="?", type=int) reading.add_argument('-n', dest='limit', default=None, metavar="N", help='Shows the last n entries matching the filter', nargs="?", type=int)
reading.add_argument('-json', dest='json', action="store_true", help='Returns a JSON-encoded version of the Journal') reading.add_argument('-json', dest='json', action="store_true", help='Returns a JSON-encoded version of the Journal')
args = parser.parse_args() args = parser.parse_args()
print args
# open journal # open journal
journal = Journal(config=config) journal = Journal(config=config)
@ -225,7 +231,15 @@ if __name__ == "__main__":
# No text? Query # No text? Query
if compose and not args.text: if compose and not args.text:
raw = raw_input("Compose Entry: ") if config['editor']:
tmpfile = os.path.join(tempfile.gettempdir(), "jrnl")
subprocess.call(config['editor'].split() + [tmpfile])
with open(tmpfile) as f:
raw = f.read()
os.remove(tmpfile)
else:
raw = raw_input("Compose Entry: ")
if raw: if raw:
args.text = [raw] args.text = [raw]
else: else:
@ -233,7 +247,7 @@ if __name__ == "__main__":
# Writing mode # Writing mode
if compose: if compose:
raw = " ".join(args.text).strip() raw = " ".join(args.text).strip()
journal.new_entry(raw, args.date) journal.new_entry(raw, args.date)
print journal print journal
journal.write() journal.write()