1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#!/usr/bin/python
# Copyright 2010 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import optparse
import os
import subprocess
import sys
import tempfile
import time
import traceback
template = '''\
[[!meta title="%(title)s"]]
[[!tag ]]
[[!meta date="%(date)s"]]
'''
class AppException(Exception):
pass
class App(object):
def __init__(self):
pass
def parse_args(self):
p = optparse.OptionParser()
p.add_option('--base', default=os.path.expanduser('~/Journal'))
return p.parse_args()
def tempfile(self, dirname, content):
fd, name = tempfile.mkstemp(dir=dirname)
os.write(fd, content)
os.close(fd)
return name
def main(self):
commands = {
'new': self.new_entry,
'list': self.list_entries,
'edit': self.edit_entry,
'finish': self.finish_entry,
}
opts, args = self.parse_args()
if args and args[0] in commands:
commands[args[0]](args[1:], opts)
else:
raise AppException('Usage: journal-note [options] cmd args...')
def drafts(self, opts):
return os.path.join(opts.base, 'drafts')
def new_entry(self, args, opts):
if not args:
raise AppException('Usage: journal-note new TITLE')
values = {
'title': args[0],
'date': time.strftime('%Y-%m-%d %H:%M')
}
name = self.tempfile(self.drafts(opts), template % values)
print name
def list_entries(self, args, opts):
for name in os.listdir(self.drafts(opts)):
print name
def edit_entry(self, args, opts):
if not args:
raise AppException('Usage: journal-note edit ID')
pathname = os.path.join(self.drafts(opts), args[0])
subprocess.check_call(['gedit', '--new-window', pathname])
def finish_entry(self, args, opts):
if not args:
raise AppException('Usage: journal-note finish ID')
draft = os.path.join(self.drafts(opts), args[0])
basename = time.strftime('%Y-%m-%d-%H:%M.mdwn')
i = 0
while True:
finished = os.path.join(opts.base, 'src', 'notes', basename)
if not os.path.exists(finished):
break
i += 1
basename = '%s-%d.mdwn' % (time.strftime('%Y-%m-%d-%H:%M:%S-'), i)
os.rename(draft, finished)
# FIXME: bzr add + commit, ikiwiki
if __name__ == '__main__':
try:
App().main()
except KeyboardInterrupt:
sys.exit(1)
except AppException, e:
sys.stderr.write('%s\n' % str(e))
sys.exit(1)
except SystemExit, e:
sys.exit(e.code)
except BaseException, e:
sys.stderr.write(traceback.format_exc(e))
sys.exit(1)
|